From dbac362bc716df8444e6d9c142739798c6762fb4 Mon Sep 17 00:00:00 2001 From: saledjenic <86303051+saledjenic@users.noreply.github.com> Date: Fri, 5 Nov 2021 14:48:20 +0100 Subject: [PATCH] Added api points for fetching message details and message reactions (#2423) by message id --- protocol/message_persistence.go | 50 +++++++++++++++++++++++++++++++++ protocol/messenger.go | 9 ++++++ services/ext/api.go | 8 ++++++ 3 files changed, 67 insertions(+) diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index c666d84ef..2763f2826 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -1008,6 +1008,56 @@ func (db sqlitePersistence) EmojiReactionsByChatID(chatID string, currCursor str return result, nil } +// EmojiReactionsByChatIDMessageID returns the emoji reactions for the queried message. +func (db sqlitePersistence) EmojiReactionsByChatIDMessageID(chatID string, messageID string) ([]*EmojiReaction, error) { + + args := []interface{}{chatID, messageID} + query := `SELECT + e.clock_value, + e.source, + e.emoji_id, + e.message_id, + e.chat_id, + e.local_chat_id, + e.retracted + FROM + emoji_reactions e + WHERE NOT(e.retracted) + AND + e.local_chat_id = ? + AND + e.message_id = ? + LIMIT 1000` + + rows, err := db.db.Query( + query, + args..., + ) + if err != nil { + return nil, err + } + defer rows.Close() + + var result []*EmojiReaction + for rows.Next() { + var emojiReaction EmojiReaction + err := rows.Scan(&emojiReaction.Clock, + &emojiReaction.From, + &emojiReaction.Type, + &emojiReaction.MessageId, + &emojiReaction.ChatId, + &emojiReaction.LocalChatID, + &emojiReaction.Retracted) + if err != nil { + return nil, err + } + + result = append(result, &emojiReaction) + } + + return result, nil +} + // EmojiReactionsByChatIDs returns the emoji reactions for the queried messages, up to a maximum of 100, as it's a potentially unbound number. // NOTE: This is not completely accurate, as the messages in the database might have change since the last call to `MessageByChatID`. func (db sqlitePersistence) EmojiReactionsByChatIDs(chatIDs []string, currCursor string, limit int) ([]*EmojiReaction, error) { diff --git a/protocol/messenger.go b/protocol/messenger.go index d4206a673..e4d3a1a0b 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -4617,6 +4617,15 @@ func (m *Messenger) EmojiReactionsByChatID(chatID string, cursor string, limit i return m.persistence.EmojiReactionsByChatID(chatID, cursor, limit) } +func (m *Messenger) EmojiReactionsByChatIDMessageID(chatID string, messageID string) ([]*EmojiReaction, error) { + _, err := m.persistence.Chat(chatID) + if err != nil { + return nil, err + } + + return m.persistence.EmojiReactionsByChatIDMessageID(chatID, messageID) +} + func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReactionID string) (*MessengerResponse, error) { emojiR, err := m.persistence.EmojiReactionByID(emojiReactionID) if err != nil { diff --git a/services/ext/api.go b/services/ext/api.go index e474767b5..6c9309831 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -505,6 +505,10 @@ func (api *PublicAPI) ChatMessages(chatID, cursor string, limit int) (*Applicati }, nil } +func (api *PublicAPI) MessageByMessageID(messageID string) (*common.Message, error) { + return api.service.messenger.MessageByID(messageID) +} + func (api *PublicAPI) AllMessagesFromChatWhichMatchTerm(chatID, searchTerm string, caseSensitive bool) (*ApplicationMessagesResponse, error) { messages, err := api.service.messenger.AllMessageByChatIDWhichMatchTerm(chatID, searchTerm, caseSensitive) if err != nil { @@ -833,6 +837,10 @@ func (api *PublicAPI) EmojiReactionsByChatID(chatID string, cursor string, limit return api.service.messenger.EmojiReactionsByChatID(chatID, cursor, limit) } +func (api *PublicAPI) EmojiReactionsByChatIDMessageID(chatID string, messageID string) ([]*protocol.EmojiReaction, error) { + return api.service.messenger.EmojiReactionsByChatIDMessageID(chatID, messageID) +} + // Urls func (api *PublicAPI) GetLinkPreviewWhitelist() []urls.Site {