feat: delete message for new design (#2922)

This commit is contained in:
yqrashawn 2022-11-17 18:11:58 +08:00 committed by GitHub
parent 2b6c0da1c7
commit 68d2d6bdfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 14 deletions

View File

@ -620,6 +620,36 @@ func (db sqlitePersistence) MessageByChatID(chatID string, currCursor string, li
return result, newCursor, nil
}
// Get last chat message that is not hide or deleted or deleted_for_me
func (db sqlitePersistence) LatestMessageByChatID(chatID string) ([]*common.Message, error) {
args := []interface{}{chatID}
where := `WHERE
NOT(m1.hide) AND NOT(m1.deleted) AND NOT(m1.deleted_for_me) AND m1.local_chat_id = ?
ORDER BY cursor DESC
LIMIT ?`
query := db.buildMessagesQueryWithAdditionalFields(cursorField, where)
rows, err := db.db.Query(
query,
append(args, 2)..., // take one more to figure our whether a cursor should be returned
)
if err != nil {
return nil, err
}
defer rows.Close()
result, _, err := getMessagesAndCursorsFromScanRows(db, rows)
if err != nil {
return nil, err
}
if len(result) > 1 {
result = result[:1]
}
return result, nil
}
func (db sqlitePersistence) latestIncomingMessageClock(chatID string) (uint64, error) {
var clock uint64
err := db.db.QueryRow(

View File

@ -162,6 +162,7 @@ func (s *MessengerDeleteMessageForMeSuite) TestDeleteMessageForMe() {
response, err = s.alice1.DeleteMessageForMeAndSync(context.Background(), chatID, messageID)
s.Require().NoError(err)
s.Require().True(response.Messages()[0].DeletedForMe)
s.Require().Nil(response.Chats()[0].LastMessage)
err = tt.RetryWithBackOff(func() error {
var err error

View File

@ -1270,11 +1270,6 @@ func (m *Messenger) HandleDeleteMessage(state *ReceivedMessageState, deleteMessa
return err
}
err = m.persistence.SetHideOnMessage(deleteMessage.MessageId)
if err != nil {
return err
}
m.logger.Debug("deleting activity center notification for message", zap.String("chatID", chat.ID), zap.String("messageID", deleteMessage.MessageId))
err = m.persistence.DeleteActivityCenterNotificationForMessage(chat.ID, deleteMessage.MessageId)
@ -1343,6 +1338,12 @@ func (m *Messenger) HandleDeleteForMeMessage(state *ReceivedMessageState, delete
return err
}
if chat.LastMessage != nil && chat.LastMessage.ID == originalMessage.ID {
if err := m.updateLastMessage(chat); err != nil {
return err
}
}
state.Response.AddMessage(originalMessage)
state.Response.AddChat(chat)
@ -1351,7 +1352,7 @@ func (m *Messenger) HandleDeleteForMeMessage(state *ReceivedMessageState, delete
func (m *Messenger) updateLastMessage(chat *Chat) error {
// Get last message that is not hidden
messages, _, err := m.persistence.MessageByChatID(chat.ID, "", 1)
messages, err := m.persistence.LatestMessageByChatID(chat.ID)
if err != nil {
return err
}
@ -1500,9 +1501,9 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
return err
}
if receivedMessage.Deleted && (chat.LastMessage == nil || chat.LastMessage.ID == receivedMessage.ID) {
if (receivedMessage.Deleted || receivedMessage.DeletedForMe) && (chat.LastMessage == nil || chat.LastMessage.ID == receivedMessage.ID) {
// Get last message that is not hidden
messages, _, err := m.persistence.MessageByChatID(receivedMessage.LocalChatID, "", 1)
messages, err := m.persistence.LatestMessageByChatID(receivedMessage.LocalChatID)
if err != nil {
return err
}

View File

@ -143,11 +143,6 @@ func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string)
return nil, err
}
err = m.persistence.HideMessage(messageID)
if err != nil {
return nil, err
}
if chat.LastMessage != nil && chat.LastMessage.ID == message.ID {
if err := m.updateLastMessage(chat); err != nil {
return nil, err
@ -189,6 +184,12 @@ func (m *Messenger) DeleteMessageForMeAndSync(ctx context.Context, chatID string
return nil, err
}
if chat.LastMessage != nil && chat.LastMessage.ID == message.ID {
if err := m.updateLastMessage(chat); err != nil {
return nil, err
}
}
response := &MessengerResponse{}
response.AddMessage(message)
response.AddChat(chat)
@ -273,7 +274,7 @@ func (m *Messenger) applyDeleteMessage(messageDeletes []*DeleteMessage, message
return err
}
return m.persistence.HideMessage(message.ID)
return nil
}
func (m *Messenger) applyDeleteForMeMessage(messageDeletes []*DeleteForMeMessage, message *common.Message) error {

View File

@ -247,6 +247,34 @@ func TestMessageByChatID(t *testing.T) {
)
}
func TestLatestMessageByChatID(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
p := newSQLitePersistence(db)
var ids []string
for i := 0; i < 10; i++ {
id := strconv.Itoa(i)
err := insertMinimalMessage(p, id)
require.NoError(t, err)
ids = append(ids, id)
}
id := strconv.Itoa(10)
err = insertMinimalDeletedMessage(p, id)
require.NoError(t, err)
ids = append(ids, id)
id = strconv.Itoa(11)
err = insertMinimalDeletedForMeMessage(p, id)
require.NoError(t, err)
ids = append(ids, id)
m, err := p.LatestMessageByChatID(testPublicChatID)
require.NoError(t, err)
require.Equal(t, m[0].ID, ids[9])
}
func TestOldestMessageWhisperTimestampByChatID(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
@ -781,6 +809,26 @@ func insertMinimalMessage(p *sqlitePersistence, id string) error {
}})
}
func insertMinimalDeletedMessage(p *sqlitePersistence, id string) error {
return p.SaveMessages([]*common.Message{{
ID: id,
Deleted: true,
LocalChatID: testPublicChatID,
ChatMessage: protobuf.ChatMessage{Text: "some-text"},
From: "me",
}})
}
func insertMinimalDeletedForMeMessage(p *sqlitePersistence, id string) error {
return p.SaveMessages([]*common.Message{{
ID: id,
DeletedForMe: true,
LocalChatID: testPublicChatID,
ChatMessage: protobuf.ChatMessage{Text: "some-text"},
From: "me",
}})
}
func insertDiscordMessageWithAttachments(p *sqlitePersistence, id string, discordMessageID string) error {
err := insertMinimalDiscordMessage(p, id, discordMessageID)
if err != nil {