feat(messages): sync saved message tags
This commit is contained in:
parent
a785ae7491
commit
6b3eba6c5d
42 changed files with 1581 additions and 551 deletions
|
|
@ -121,70 +121,3 @@ func (s *ChannelStore) ClearRecentMessageReactions(ctx context.Context, userID i
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ChannelStore) ListSavedReactionTags(ctx context.Context, userID int64, limit int) ([]domain.SavedReactionTag, error) {
|
||||
if userID == 0 {
|
||||
return nil, domain.ErrChannelInvalid
|
||||
}
|
||||
if limit <= 0 {
|
||||
return []domain.SavedReactionTag{}, nil
|
||||
}
|
||||
if limit > domain.MaxSavedReactionTags {
|
||||
limit = domain.MaxSavedReactionTags
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT reaction_type, reaction_value, title, reaction_count
|
||||
FROM user_saved_reaction_tags
|
||||
WHERE user_id = $1
|
||||
ORDER BY reaction_count DESC, updated_at DESC, reaction_type ASC, reaction_value ASC
|
||||
LIMIT $2`, userID, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list saved reaction tags: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]domain.SavedReactionTag, 0, limit)
|
||||
for rows.Next() {
|
||||
var reactionType, reactionValue, title string
|
||||
var count int
|
||||
if err := rows.Scan(&reactionType, &reactionValue, &title, &count); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reaction, ok := domain.MessageReactionFromValue(domain.MessageReactionType(reactionType), reactionValue)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
out = append(out, domain.SavedReactionTag{
|
||||
UserID: userID,
|
||||
Reaction: reaction,
|
||||
Title: title,
|
||||
Count: count,
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *ChannelStore) UpsertSavedReactionTag(ctx context.Context, tag domain.SavedReactionTag) error {
|
||||
if tag.UserID == 0 || tag.Reaction.Type != domain.MessageReactionEmoji {
|
||||
return domain.ErrChannelInvalid
|
||||
}
|
||||
reactionValue := strings.TrimSpace(tag.Reaction.Emoticon)
|
||||
if reactionValue == "" {
|
||||
return domain.ErrChannelInvalid
|
||||
}
|
||||
if tag.Count < 0 {
|
||||
tag.Count = 0
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
INSERT INTO user_saved_reaction_tags (user_id, reaction_type, reaction_value, title, reaction_count)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (user_id, reaction_type, reaction_value) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
reaction_count = GREATEST(user_saved_reaction_tags.reaction_count, EXCLUDED.reaction_count),
|
||||
updated_at = now()`, tag.UserID, string(tag.Reaction.Type), reactionValue, tag.Title, tag.Count); err != nil {
|
||||
return fmt.Errorf("upsert saved reaction tag: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue