Skip to content

Commit

Permalink
feat: store brainery post in fortress (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
namnhce authored Jun 21, 2023
1 parent 4f7723b commit b8fa879
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 17 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/bwmarrin/discordgo v0.26.1
github.com/gin-gonic/gin v1.8.2
github.com/joho/godotenv v1.4.0
github.com/k0kubun/pp/v3 v3.2.0
github.com/sashabaranov/go-openai v1.10.1
github.com/shopspring/decimal v1.3.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.14.0
)
Expand All @@ -26,7 +26,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -170,8 +168,6 @@ github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand All @@ -197,6 +193,8 @@ github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUA
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/sashabaranov/go-openai v1.10.1 h1:6WyHJaNzF266VaEEuW6R4YW+Ei0wpMnqRYPGK7fhuhQ=
github.com/sashabaranov/go-openai v1.10.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
Expand Down
25 changes: 25 additions & 0 deletions pkg/adapter/fortress/fortress.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,28 @@ func (f *Fortress) UpsertRollupRecord(record *model.EngagementsRollupRecord) err

return nil
}

func (f *Fortress) CreateBraineryPost(post *model.CreateBraineryLogRequest) error {
jsonValue, err := json.Marshal(post)
req, err := f.makeReq("/api/v1/brainery-logs", http.MethodPost, bytes.NewBuffer(jsonValue))
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var errMsg ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errMsg); err != nil {
return errors.New("invalid decoded, error " + err.Error())
}
return errors.New("invalid call, code " + strconv.Itoa(resp.StatusCode) + " " + errMsg.Message)
}

return nil
}
2 changes: 2 additions & 0 deletions pkg/adapter/fortress/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ type FortressAdapter interface {
SendChangelog(changelog *model.Changelog) error

UpsertRollupRecord(record *model.EngagementsRollupRecord) error

CreateBraineryPost(post *model.CreateBraineryLogRequest) error
}
6 changes: 3 additions & 3 deletions pkg/adapter/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (o *OpenAI) SummarizeBraineryPost(content string) (string, error) {
},
{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf(`Article: %s.
Write a descriptive paragraph for the article that mentioned above.
Write this paragraph under 50 words, do not mention the source. `, content),
Content: fmt.Sprintf(`Memo: %s.
Write a excerpt for the Memo that mentioned above.
Write this excerpt under 40 words, do not mention the source. `, content),
},
},
},
Expand Down
6 changes: 5 additions & 1 deletion pkg/discord/command/brainery/brainery.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (e *Brainery) Post(message *model.DiscordMessage) error {
return e.view.Error().Raise(message, "There is no URL or more than one URL in your message.")
}

if !strings.Contains(extractURL[0], "https://brain.d.foundation") {
return e.view.Error().Raise(message, "The article should be get https://brain.d.foundation.")
}

if len(extractDiscordID) == 0 || len(extractDiscordID) > 1 {
return e.view.Error().Raise(message, "There is no valid user or more than one user tagged in your message.")
}
Expand All @@ -76,7 +80,7 @@ func (e *Brainery) Post(message *model.DiscordMessage) error {
URL: extractURL[0],
DiscordID: extractDiscordID[0],
Reward: reward,
PublishDate: &now,
PublishedAt: &now,
Tags: extractTags,
Github: gh,
}
Expand Down
32 changes: 28 additions & 4 deletions pkg/discord/service/brainery/brainery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/PuerkitoBio/goquery"
"github.com/shopspring/decimal"

"github.com/dwarvesf/fortress-discord/pkg/adapter"
"github.com/dwarvesf/fortress-discord/pkg/logger"
Expand All @@ -31,7 +32,7 @@ type PostInput struct {
URL string
Author string
Reward string
PublishDate *time.Time
PublishedAt *time.Time
Tags []string
Github string
DiscordID string
Expand Down Expand Up @@ -82,17 +83,37 @@ func (e *Brainery) Post(in *PostInput) (*model.Brainery, error) {
return nil, err
}

return &model.Brainery{
mBrainery := &model.Brainery{
Title: title,
URL: in.URL,
Author: in.Author,
Description: description + "..",
Reward: in.Reward,
PublishDate: in.PublishDate,
PublishedAt: in.PublishedAt,
Tags: tags,
Github: in.Github,
DiscordID: in.DiscordID,
}, nil
}

rw, err := decimal.NewFromString(mBrainery.Reward)
if err != nil {
return nil, err
}

err = e.adapter.Fortress().CreateBraineryPost(&model.CreateBraineryLogRequest{
Title: mBrainery.Title,
URL: mBrainery.URL,
GithubID: mBrainery.Github,
DiscordID: mBrainery.DiscordID,
Tags: separateTags(mBrainery.Tags),
PublishedAt: mBrainery.PublishedAt.Format(time.RFC3339),
Reward: rw,
})
if err != nil {
return nil, err
}

return mBrainery, nil
}

func convertURL(originalURL string) (string, error) {
Expand Down Expand Up @@ -167,3 +188,6 @@ func buildTags(tags []string) string {

return strings.Join(result, "\n")
}
func separateTags(tags string) []string {
return strings.Split(strings.ReplaceAll(tags, "#", ""), "\n")
}
3 changes: 2 additions & 1 deletion pkg/discord/view/brainery/brainery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func New(ses *discordgo.Session) Viewer {
func (h *Brainery) Help(message *model.DiscordMessage) error {
content := []string{
"**?brainery post**・publish new brainery article.",
"*Example:* `?brainery post <url> @n #tag1 #tag2 gh:namnhce`",
}

msg := &discordgo.MessageEmbed{
Expand Down Expand Up @@ -73,7 +74,7 @@ func (h *Brainery) Post(original *model.DiscordMessage, content *model.Brainery,
Fields: messageEmbed,

Footer: &discordgo.MessageEmbedFooter{
Text: "Added at " + content.PublishDate.Format("January 2, 2006 3:04 PM") + " 🎉🎉🎉",
Text: "Added at " + content.PublishedAt.Format("January 2, 2006 3:04 PM") + " 🎉🎉🎉",
},
Timestamp: "custom",
Thumbnail: &discordgo.MessageEmbedThumbnail{
Expand Down
18 changes: 16 additions & 2 deletions pkg/model/brainery.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package model

import "time"
import (
"time"

"github.com/shopspring/decimal"
)

// Brainery is in-app model, after process from adapters
type Brainery struct {
Expand All @@ -9,8 +13,18 @@ type Brainery struct {
Author string
Description string
Reward string
PublishDate *time.Time
PublishedAt *time.Time
Tags string
Github string
DiscordID string
}

type CreateBraineryLogRequest struct {
Title string `json:"title"`
URL string `json:"url"`
GithubID string `json:"githubID"`
DiscordID string `json:"discordID"`
Tags []string `json:"tags"`
PublishedAt string `json:"publishedAt"`
Reward decimal.Decimal `json:"reward"`
}

0 comments on commit b8fa879

Please sign in to comment.