-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
14044 handle edit messages in bridge #4983
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.177.0 | ||
0.177.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1551,6 +1551,18 @@ func (db sqlitePersistence) SaveMessages(messages []*common.Message) (err error) | |
} | ||
|
||
if msg.ContentType == protobuf.ChatMessage_BRIDGE_MESSAGE { | ||
// check updates first | ||
var hasMessage bool | ||
hasMessage, err = db.bridgeMessageExists(tx, msg.GetBridgeMessage().MessageID) | ||
if err != nil { | ||
return | ||
} | ||
if hasMessage { | ||
// bridge message exists, this is edit | ||
err = db.updateBridgeMessageContent(tx, msg.GetBridgeMessage().MessageID, msg.GetBridgeMessage().Content) | ||
return | ||
} | ||
|
||
err = db.saveBridgeMessage(tx, msg.GetBridgeMessage(), msg.ID) | ||
if err != nil { | ||
return | ||
|
@@ -2967,9 +2979,26 @@ func (db sqlitePersistence) findStatusMessageIdsReplies(tx *sql.Tx, bridgeMessag | |
return statusMessageIDs, nil | ||
} | ||
|
||
// Finds status messages id which are replies for bridgeMessageID | ||
func (db sqlitePersistence) findStatusMessageIdsRepliedTo(tx *sql.Tx, parentMessageID string) (string, error) { | ||
rows, err := tx.Query(`SELECT user_messages_id FROM bridge_messages WHERE message_id = ?`, parentMessageID) | ||
func (db sqlitePersistence) FindStatusMessageIdForBridgeMessageId(messageID string) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean it could get slower over time? Excuse my ignorance, are the messages split per chats or something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, we query from bridge messages. |
||
rows, err := db.db.Query(`SELECT user_messages_id FROM bridge_messages WHERE message_id = ?`, messageID) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer rows.Close() | ||
|
||
if rows.Next() { | ||
var statusMessageID string | ||
err = rows.Scan(&statusMessageID) | ||
if err != nil { | ||
return "", err | ||
} | ||
return statusMessageID, nil | ||
} | ||
return "", nil | ||
} | ||
|
||
func (db sqlitePersistence) findStatusMessageIdForBridgeMessageId(tx *sql.Tx, messageID string) (string, error) { | ||
rows, err := tx.Query(`SELECT user_messages_id FROM bridge_messages WHERE message_id = ?`, messageID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
@@ -3003,6 +3032,23 @@ func (db sqlitePersistence) updateStatusMessagesWithResponse(tx *sql.Tx, statusM | |
return err | ||
} | ||
|
||
func (db sqlitePersistence) bridgeMessageExists(tx *sql.Tx, bridgeMessageID string) (exists bool, err error) { | ||
err = tx.QueryRow(`SELECT EXISTS(SELECT 1 FROM bridge_messages WHERE message_id = ?)`, bridgeMessageID).Scan(&exists) | ||
return exists, err | ||
} | ||
|
||
func (db sqlitePersistence) updateBridgeMessageContent(tx *sql.Tx, bridgeMessageID string, content string) error { | ||
sql := "UPDATE bridge_messages SET content = ? WHERE message_id = ?" | ||
stmt, err := tx.Prepare(sql) | ||
if err != nil { | ||
return err | ||
} | ||
defer stmt.Close() | ||
|
||
_, err = stmt.Exec(content, bridgeMessageID) | ||
return err | ||
} | ||
|
||
// Finds if there are any messages that are replies to that message (in case replies were received earlier) | ||
func (db sqlitePersistence) findAndUpdateReplies(tx *sql.Tx, bridgeMessageID string, statusMessageID string) error { | ||
replyMessageIds, err := db.findStatusMessageIdsReplies(tx, bridgeMessageID) | ||
|
@@ -3016,7 +3062,8 @@ func (db sqlitePersistence) findAndUpdateReplies(tx *sql.Tx, bridgeMessageID str | |
} | ||
|
||
func (db sqlitePersistence) findAndUpdateRepliedTo(tx *sql.Tx, discordParentMessageID string, statusMessageID string) error { | ||
repliedMessageID, err := db.findStatusMessageIdsRepliedTo(tx, discordParentMessageID) | ||
// Finds status messages id which are replies for bridgeMessageID | ||
repliedMessageID, err := db.findStatusMessageIdForBridgeMessageId(tx, discordParentMessageID) | ||
if err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of err, maybe we should just ignore and save the message as before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, if it returned the error, it would mean that there is something wrong with db connection and another db operation would fail also. Am I right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't mean return the error. I mean just not
return
. Because since you return, you will miss that message