Skip to content

Commit

Permalink
feat: migrate table memo log
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Jan 21, 2025
1 parent 8dcb517 commit 9acfe79
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- +migrate Up
-- Add JSONB column to store discord account IDs
ALTER TABLE memo_logs
ADD COLUMN discord_account_ids JSONB DEFAULT '[]'::JSONB;

-- Migrate data from memo_authors to memo_logs
WITH author_data AS (
SELECT
memo_log_id,
json_agg(discord_account_id) AS discord_account_ids
FROM memo_authors
GROUP BY memo_log_id
)
UPDATE memo_logs ml
SET discord_account_ids = ad.discord_account_ids
FROM author_data ad
WHERE ml.id = ad.memo_log_id;

-- Drop the memo_authors table
DROP TABLE memo_authors;
DROP TABLE brainery_logs;

-- +migrate Down
CREATE TABLE "brainery_logs" (
"id" uuid NOT NULL DEFAULT uuid(),
"deleted_at" timestamp,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now(),
"title" text NOT NULL,
"url" text NOT NULL,
"github_id" text,
"discord_id" text NOT NULL,
"employee_id" uuid,
"tags" jsonb,
"published_at" timestamp NOT NULL,
"reward" numeric,
CONSTRAINT "brainery_logs_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id"),
PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS memo_authors (
memo_log_id UUID NOT NULL REFERENCES memo_logs(id),
discord_account_id UUID NOT NULL REFERENCES discord_accounts(id),
created_at TIMESTAMP(6) DEFAULT (now()),
PRIMARY KEY (memo_log_id, discord_account_id)
);

INSERT INTO memo_authors (memo_log_id, discord_account_id)
SELECT
id AS memo_log_id,
jsonb_array_elements(discord_account_ids)::UUID AS discord_account_id
FROM memo_logs
WHERE jsonb_array_length(discord_account_ids) > 0;

-- Remove the JSONB column
ALTER TABLE memo_logs
DROP COLUMN discord_account_ids;
7 changes: 6 additions & 1 deletion pkg/controller/memologs/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,17 @@ func (c *controller) Sync() ([]model.MemoLog, error) {
continue
}

discordAccountIDs := make([]string, 0, len(authors))
for _, author := range authors {
discordAccountIDs = append(discordAccountIDs, author.ID.String())
}

newMemos = append(newMemos, model.MemoLog{
Title: item.Title,
URL: item.Link,
Description: item.Description,
PublishedAt: &pubDate,
Authors: authors,
DiscordAccountIDs: discordAccountIDs,
AuthorMemoUsernames: authorUsernames,
Category: extractMemoCategory(item.Link),
})
Expand Down
19 changes: 17 additions & 2 deletions pkg/handler/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,14 @@ func (h *handler) SyncMemo(c *gin.Context) {
return
}

_, err = h.service.Discord.SendNewMemoMessage(h.config.Discord.IDs.DwarvesGuild, memos, targetChannelID)
_, err = h.service.Discord.SendNewMemoMessage(
h.config.Discord.IDs.DwarvesGuild,
memos,
targetChannelID,
func(discordAccountID string) (*model.DiscordAccount, error) {
return h.store.DiscordAccount.One(h.repo.DB(), discordAccountID)
},
)
if err != nil {
h.logger.Error(err, "failed to send new memo message")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, ""))
Expand Down Expand Up @@ -430,7 +437,15 @@ func (h *handler) NotifyWeeklyMemos(c *gin.Context) {
targetChannelID = discordRandomChannel
}

_, err = h.service.Discord.SendWeeklyMemosMessage(h.config.Discord.IDs.DwarvesGuild, memos, weekRangeStr, targetChannelID)
_, err = h.service.Discord.SendWeeklyMemosMessage(
h.config.Discord.IDs.DwarvesGuild,
memos,
weekRangeStr,
targetChannelID,
func(discordAccountID string) (*model.DiscordAccount, error) {
return h.store.DiscordAccount.One(h.repo.DB(), discordAccountID)
},
)
if err != nil {
h.logger.Error(err, "failed to send weekly memos report")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, ""))
Expand Down
22 changes: 14 additions & 8 deletions pkg/handler/memologs/memo_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/bwmarrin/discordgo"
"github.com/gin-gonic/gin"

"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/controller"
"github.com/dwarvesf/fortress-api/pkg/handler/memologs/request"
Expand All @@ -18,7 +20,6 @@ import (
"github.com/dwarvesf/fortress-api/pkg/store"
"github.com/dwarvesf/fortress-api/pkg/store/memolog"
"github.com/dwarvesf/fortress-api/pkg/view"
"github.com/gin-gonic/gin"
)

type handler struct {
Expand Down Expand Up @@ -132,14 +133,19 @@ func (h *handler) Create(c *gin.Context) {
authors = append(authors, newAuthor)
}

discordAccountIDs := make([]string, 0, len(authors))
for _, author := range authors {
discordAccountIDs = append(discordAccountIDs, author.ID.String())
}

b := model.MemoLog{
Title: b.Title,
URL: b.URL,
Authors: authors,
Tags: b.Tags,
PublishedAt: &publishedAt,
Description: b.Description,
Reward: b.Reward,
Title: b.Title,
URL: b.URL,
DiscordAccountIDs: discordAccountIDs,
Tags: b.Tags,
PublishedAt: &publishedAt,
Description: b.Description,
Reward: b.Reward,
}
memologs = append(memologs, b)
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/model/memo_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/lib/pq"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)

type MemoLog struct {
Expand All @@ -19,7 +18,7 @@ type MemoLog struct {
Reward decimal.Decimal
Category pq.StringArray `json:"value" gorm:"type:text[]"`

Authors []DiscordAccount `json:"authors" gorm:"many2many:memo_authors;"`
DiscordAccountIDs JSONArrayString `json:"discord_account_ids" gorm:"type:jsonb;column:discord_account_ids"`

// This field is used to make sure response always contains authors
AuthorMemoUsernames []string `json:"-" gorm:"-"`
Expand All @@ -33,6 +32,4 @@ type DiscordAccountMemoRank struct {
Rank int
}

func (MemoLog) BeforeCreate(db *gorm.DB) error {
return db.SetupJoinTable(&MemoLog{}, "Authors", &MemoAuthor{})
}
// Remove BeforeCreate method as we no longer use many-to-many join table
53 changes: 40 additions & 13 deletions pkg/service/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,17 +678,30 @@ func (d *discordClient) SendEmbeddedMessageWithChannel(original *model.OriginalD
return msg, err
}

func (d *discordClient) SendNewMemoMessage(guildID string, memos []model.MemoLog, channelID string) (*discordgo.Message, error) {
func (d *discordClient) SendNewMemoMessage(
guildID string,
memos []model.MemoLog,
channelID string,
getDiscordAccountByID func(discordAccountID string) (*model.DiscordAccount, error),
) (*discordgo.Message, error) {
for i, content := range memos {
if i <= 10 {
var textMessage string

authorField := ""
for _, author := range content.Authors {
if author.DiscordID != "" {
authorField += fmt.Sprintf(" <@%s> ", author.DiscordID)
} else if author.DiscordUsername != "" {
authorField += fmt.Sprintf(" @%s ", author.DiscordUsername)
for _, discordAccountID := range content.DiscordAccountIDs {
// Fetch discord account details for the ID
discordAccount, err := getDiscordAccountByID(discordAccountID)
if err != nil {
// If fetching fails, use the ID as a fallback
authorField += fmt.Sprintf(" <@%s> ", discordAccountID)
continue
}

if discordAccount.DiscordID != "" {
authorField += fmt.Sprintf(" <@%s> ", discordAccount.DiscordID)
} else if discordAccount.DiscordUsername != "" {
authorField += fmt.Sprintf(" @%s ", discordAccount.DiscordUsername)
} else {
authorField += " **@unknown-user**"
}
Expand Down Expand Up @@ -722,7 +735,13 @@ func (d *discordClient) SendNewMemoMessage(guildID string, memos []model.MemoLog
return nil, nil
}

func (d *discordClient) SendWeeklyMemosMessage(guildID string, memos []model.MemoLog, weekRangeStr, channelID string) (*discordgo.Message, error) {
func (d *discordClient) SendWeeklyMemosMessage(
guildID string,
memos []model.MemoLog,
weekRangeStr,
channelID string,
getDiscordAccountByID func(discordAccountID string) (*model.DiscordAccount, error),
) (*discordgo.Message, error) {
bagde1Emoji := getEmoji("BADGE1")
bagde5Emoji := getEmoji("BADGE5")
pepeNoteEmoji := getEmoji("PEPE_NOTE")
Expand Down Expand Up @@ -769,13 +788,21 @@ func (d *discordClient) SendWeeklyMemosMessage(guildID string, memos []model.Mem

for idx, mem := range memos {
authorField := ""
for _, author := range mem.Authors {
authorMap[author.DiscordID] += 1
for _, discordAccountID := range mem.DiscordAccountIDs {
// Fetch discord account details for the ID
discordAccount, err := getDiscordAccountByID(discordAccountID)
if err != nil {
// If fetching fails, use the ID as a fallback
authorField += fmt.Sprintf(" <@%s> ", discordAccountID)
continue
}

authorMap[discordAccount.DiscordID] += 1

if author.DiscordID != "" {
authorField += fmt.Sprintf(" <@%s> ", author.DiscordID)
} else if author.DiscordUsername != "" {
authorField += fmt.Sprintf(" @%s ", author.DiscordUsername)
if discordAccount.DiscordID != "" {
authorField += fmt.Sprintf(" <@%s> ", discordAccount.DiscordID)
} else if discordAccount.DiscordUsername != "" {
authorField += fmt.Sprintf(" @%s ", discordAccount.DiscordUsername)
} else {
authorField += " **@unknown-user**"
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/service/discord/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ type IService interface {
ReportBraineryMetrics(queryView string, braineryMetric *view.BraineryMetric, channelID string) (*discordgo.Message, error)
DeliveryMetricWeeklyReport(deliveryMetrics *view.DeliveryMetricWeeklyReport, leaderBoard *view.WeeklyLeaderBoard, channelID string) (*discordgo.Message, error)
DeliveryMetricMonthlyReport(deliveryMetrics *view.DeliveryMetricMonthlyReport, leaderBoard *view.WeeklyLeaderBoard, channelID string) (*discordgo.Message, error)
SendNewMemoMessage(guildID string, memos []model.MemoLog, channelID string) (*discordgo.Message, error)
SendWeeklyMemosMessage(guildID string, memos []model.MemoLog, weekRangeStr, channelID string) (*discordgo.Message, error)
SendNewMemoMessage(
guildID string,
memos []model.MemoLog,
channelID string,
getDiscordAccountByID func(discordAccountID string) (*model.DiscordAccount, error),
) (*discordgo.Message, error)
SendWeeklyMemosMessage(
guildID string,
memos []model.MemoLog,
weekRangeStr,
channelID string,
getDiscordAccountByID func(discordAccountID string) (*model.DiscordAccount, error),
) (*discordgo.Message, error)
/*
WEBHOOK
*/
Expand Down
Loading

0 comments on commit 9acfe79

Please sign in to comment.