Skip to content

Commit

Permalink
fix(changelog): update changelog preview ui
Browse files Browse the repository at this point in the history
  • Loading branch information
quanghuynguyen1902 committed Jan 25, 2024
1 parent f461c0f commit ddf20e2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 69 deletions.
3 changes: 3 additions & 0 deletions pkg/consts/changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package consts

const NewChangelogDiscordFooter = "⎯⎯⎯⎯⎯\nView changelog detail on [Mochi web](https://beta.mochi.gg/changelog)"
146 changes: 77 additions & 69 deletions pkg/entities/product_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/yuin/goldmark/text"
"gorm.io/gorm"

"github.com/defipod/mochi/pkg/consts"
"github.com/defipod/mochi/pkg/kafka/message"
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/model"
Expand Down Expand Up @@ -158,7 +159,7 @@ func (e *Entity) CrawlChangelogs() {

for _, pc := range changelogNotConfirmed {
content, images := e.ParseChangelogContent(pc.Title, pc.Content)
err := e.SendChangelogToChannel(pc.FileName, content, images)
err := e.SendChangelogToChannel(pc.FileName, pc.Version, content, images)

if err != nil {
e.log.Error(err, "[entity.CrawlChangelogs()] - cannot send changelog to channel")
Expand Down Expand Up @@ -234,79 +235,92 @@ func (e *Entity) GetProductTheme(req request.GetProductThemeRequest) ([]model.Pr
}

func (e *Entity) PublishChangeLog(req request.ProductChangelogSnapshotRequest) error {
err := e.repo.ProductChangelogs.UpdateProductChangelogSnapshot(productchangelogs.ProductChangelogSnapshotQuery{
Filename: req.ChangelogName,
IsPublic: req.IsPublic,
})

changelog, err := e.repo.ProductChangelogs.GetChangelogByFilename(req.ChangelogName)
if err != nil {
e.log.Errorf(err, "[entity.PublishChangeLog] - e.repo.ProductChangelogs.UpdateProductChangelogSnapshot failed")
e.log.Errorf(err, "[entity.PublishChangeLog] - e.repo.ProductChangelogs.GetChangelogByFilename failed")
return err
}

changelog, err := e.repo.ProductChangelogs.GetChangelogByFilename(req.ChangelogName)

changelogSnapshot, err := e.repo.ProductChangelogs.GetChangelogSnapshotByFilename(req.ChangelogName)
if err != nil {
e.log.Errorf(err, "[entity.PublishChangeLog] - e.repo.ProductChangelogs.GetChangelogByFilename failed")
e.log.Errorf(err, "[entity.PublishChangeLog] - e.repo.ProductChangelogs.GetChangelogSnapshotByFilename failed")
return err
}

msg := message.NewChangelog{
Type: typeset.NOTIFICATION_NEW_CHANGELOG,
NewChangelogMetadata: message.NewChangelogMetadata{
Product: changelog.Product,
Title: changelog.Title,
Content: changelog.Content,
FileName: changelog.FileName,
GithubUrl: changelog.GithubUrl,
Version: changelog.Version,
ThumbnailUrl: changelog.ThumbnailUrl,
IsExpired: changelog.IsExpired,
CreatedAt: changelog.CreatedAt,
UpdatedAt: changelog.UpdatedAt,
},
if !changelogSnapshot.IsPublic {
msg := message.NewChangelog{
Type: typeset.NOTIFICATION_NEW_CHANGELOG,
NewChangelogMetadata: message.NewChangelogMetadata{
Product: changelog.Product,
Title: changelog.Title,
Content: changelog.Content,
FileName: changelog.FileName,
GithubUrl: changelog.GithubUrl,
Version: changelog.Version,
ThumbnailUrl: changelog.ThumbnailUrl,
IsExpired: changelog.IsExpired,
CreatedAt: changelog.CreatedAt,
UpdatedAt: changelog.UpdatedAt,
},
}

// 3. push notification for new changelogs
// TODO. implement push notification
byteNotification, _ := json.Marshal(msg)

err = e.kafka.ProduceNotification(e.cfg.Kafka.NotificationTopic, byteNotification)
if err != nil {
e.log.Errorf(err, "[entity.PublishChangeLog] - e.kafka.Produce failed")
return err
}

}

// 3. push notification for new changelogs
// TODO. implement push notification
byteNotification, _ := json.Marshal(msg)
err = e.repo.ProductChangelogs.UpdateProductChangelogSnapshot(productchangelogs.ProductChangelogSnapshotQuery{
Filename: req.ChangelogName,
IsPublic: req.IsPublic,
})

err = e.kafka.ProduceNotification(e.cfg.Kafka.NotificationTopic, byteNotification)
if err != nil {
e.log.Errorf(err, "[entity.PublishChangeLog] - e.kafka.Produce failed")
e.log.Errorf(err, "[entity.PublishChangeLog] - e.repo.ProductChangelogs.UpdateProductChangelogSnapshot failed")
return err
}

return nil
}

func (e *Entity) SendChangelogToChannel(filename string, content string, images []string) error {
content += "\n\n**Want to publish this changelog?**"
sections := strings.Split(content, `\<br\>`)
func (e *Entity) SendChangelogToChannel(filename string, version string, content string, images []string) error {
var image string
var mochiAvatar string

for index, section := range sections {
var image string
if index < len(images) {
image = images[index]
}
msg := &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Description: section,
Color: 0x62A1FE,
Image: &discordgo.MessageEmbedImage{
URL: image,
},
},
}
_, err := e.discord.ChannelMessageSendComplex(e.cfg.MochiChangelogChannelID, msg)
if err != nil {
e.log.Error(err, "[entity.SendChangelogToChannel()] - cannot send messsage to changelog channel")
return err
}
if len(images) > 0 {
image = images[0]
}

confirmButtonMsg := &discordgo.MessageSend{
emojis, err := e.GetEmojiByCode("MOCHI_CIRCLE")
if err != nil {
e.log.Error(err, "[entity.GetEmojiByCode()] - cannot get emoji")
return err
}

if emojis != nil {
mochiAvatar = emojis.EmojiUrl
}

msg := &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Description: content,
Color: 0x62A1FE,
Image: &discordgo.MessageEmbedImage{
URL: image,
},
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("v%s", version),
IconURL: mochiAvatar,
},
Timestamp: time.Now().UTC().Format("2006-01-02 15:04:05"),
},
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
Expand All @@ -319,21 +333,12 @@ func (e *Entity) SendChangelogToChannel(filename string, content string, images
Style: discordgo.SuccessButton,
CustomID: fmt.Sprintf(`mochi_changelog_confirm_%s`, filename),
},
discordgo.Button{
Label: "Cancel",
Emoji: discordgo.ComponentEmoji{
Name: "revoke",
ID: "1077631119073230970",
},
Style: discordgo.DangerButton,
CustomID: fmt.Sprintf(`mochi_changelog_cancel_%s`, filename),
},
},
},
},
}

_, err := e.discord.ChannelMessageSendComplex(e.cfg.MochiChangelogChannelID, confirmButtonMsg)
_, err = e.discord.ChannelMessageSendComplex(e.cfg.MochiChangelogChannelID, msg)
if err != nil {
e.log.Error(err, "[entity.SendChangelogToChannel()] - cannot send confirm button to changelog channel")
return err
Expand All @@ -344,7 +349,8 @@ func (e *Entity) SendChangelogToChannel(filename string, content string, images

func (e *Entity) ParseChangelogContent(title string, content string) (string, []string) {
replaceContent := regexp.MustCompile(`\*\*(.*?)\*\*`).ReplaceAllString(content, `\<b>$1\</b>`)
input := []byte(replaceContent)
replaceContent = strings.ReplaceAll(replaceContent, "[//]: new_line", `\newline`)
input := []byte(strings.Split(replaceContent, "[//]: break")[0])
reader := text.NewReader(input)
markdownAST := goldmark.DefaultParser().Parse(reader)

Expand All @@ -364,8 +370,10 @@ func (e *Entity) ParseChangelogContent(title string, content string) (string, []
}

// parse strong text
text = fmt.Sprintf("**%s**\n%s", title, text)
text = fmt.Sprintf("<a:gem:1095990259877158964> **%s**\n%s", title, text)
text = regexp.MustCompile(`\\<b>(.*?)\\</b>`).ReplaceAllString(text, "**$1**")
text = strings.ReplaceAll(text, `\newline`, "")
text += fmt.Sprintf("\n%s", consts.NewChangelogDiscordFooter)

return text, filteredImages
}
Expand All @@ -376,11 +384,11 @@ func (e *Entity) parseMarkDown(content ast.Node, ctx *model.MarkdownContext, sou
if entering {
switch n.Kind() {
case ast.KindHeading:
text += "\n"
if ctx.FirstHeading {
astHeading := n.(*ast.Heading)
if astHeading.Level != 3 {
text += "\n"
text += fmt.Sprintf(`**%s**`, string(n.FirstChild().Text(source)))
}
text += "**" + string(n.FirstChild().Text(source)) + "**"
ctx.FirstHeading = true
break
case ast.KindParagraph:
Expand All @@ -389,7 +397,7 @@ func (e *Entity) parseMarkDown(content ast.Node, ctx *model.MarkdownContext, sou
break
case ast.KindImage:
astImage := n.(*ast.Image)
text += `\<br\>`
//text += `\<br\>`
ctx.Images = append(ctx.Images, string(astImage.Destination))
break
case ast.KindListItem:
Expand Down
5 changes: 5 additions & 0 deletions pkg/repo/product_changelogs/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func (pg pg) GetChangelogByFilename(filename string) (p model.ProductChangelogs,
return p, db.Where("file_name = ?", filename).First(&p).Error
}

func (pg pg) GetChangelogSnapshotByFilename(filename string) (p model.ProductChangelogSnapshot, err error) {
db := pg.db
return p, db.Where("filename = ?", filename).First(&p).Error
}

func (pg *pg) List(q ListQuery) (changeLogs []model.ProductChangelogs, total int64, err error) {
db := pg.db.Model(&model.ProductChangelogs{})
if q.Product != "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/repo/product_changelogs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/defipod/mochi/pkg/model"
type Store interface {
List(q ListQuery) (changeLogs []model.ProductChangelogs, total int64, err error)
GetChangelogByFilename(title string) (p model.ProductChangelogs, err error)
GetChangelogSnapshotByFilename(filename string) (p model.ProductChangelogSnapshot, err error)
Create(changelog *model.ProductChangelogs) error
GetNewChangelog() ([]model.ProductChangelogs, error)
GetChangelogNotConfirmed() ([]model.ProductChangelogs, error)
Expand Down

0 comments on commit ddf20e2

Please sign in to comment.