Skip to content

Commit

Permalink
add MarkAllReadInCommunity (#2364)
Browse files Browse the repository at this point in the history
add MarkAllReadInCommunity
  • Loading branch information
bitgamma authored Sep 20, 2021
1 parent b4f46f8 commit fb21876
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.86.7
0.87.0
56 changes: 56 additions & 0 deletions protocol/message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,28 @@ func (db sqlitePersistence) AllMessagesFromChatsAndCommunitiesWhichMatchTerm(com
return result, nil
}

func (db sqlitePersistence) AllChatIDsByCommunity(communityID string) ([]string, error) {
rows, err := db.db.Query("SELECT id FROM chats WHERE community_id = ?", communityID)

if err != nil {
return nil, err
}
defer rows.Close()

var rst []string

for rows.Next() {
var chatID string
err = rows.Scan(&chatID)
if err != nil {
return nil, err
}
rst = append(rst, chatID)
}

return rst, nil
}

// PinnedMessageByChatID returns all pinned messages for a given chatID in descending order.
// Ordering is accomplished using two concatenated values: ClockValue and ID.
// These two values are also used to compose a cursor which is returned to the result.
Expand Down Expand Up @@ -1254,6 +1276,40 @@ func (db sqlitePersistence) MarkAllRead(chatID string) error {
return err
}

func (db sqlitePersistence) MarkAllReadMultiple(chatIDs []string) error {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
}()

idsArgs := make([]interface{}, 0, len(chatIDs))
for _, id := range chatIDs {
idsArgs = append(idsArgs, id)
}

inVector := strings.Repeat("?, ", len(chatIDs)-1) + "?"

q := "UPDATE user_messages SET seen = 1 WHERE local_chat_id IN (%s) AND seen != 1"
q = fmt.Sprintf(q, inVector)
_, err = tx.Exec(q, idsArgs...)
if err != nil {
return err
}

q = "UPDATE chats SET unviewed_mentions_count = 0, unviewed_message_count = 0 WHERE id IN (%s)"
q = fmt.Sprintf(q, inVector)
_, err = tx.Exec(q, idsArgs...)
return err
}

func (db sqlitePersistence) MarkMessagesSeen(chatID string, ids []string) (uint64, uint64, error) {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3294,6 +3294,32 @@ func (m *Messenger) MarkAllRead(chatID string) error {
return nil
}

func (m *Messenger) MarkAllReadInCommunity(communityID string) ([]string, error) {
chatIDs, err := m.persistence.AllChatIDsByCommunity(communityID)
if err != nil {
return nil, err
}

err = m.persistence.MarkAllReadMultiple(chatIDs)
if err != nil {
return nil, err
}

for _, chatID := range chatIDs {
chat, ok := m.allChats.Load(chatID)

if ok {
chat.UnviewedMessagesCount = 0
chat.UnviewedMentionsCount = 0
m.allChats.Store(chat.ID, chat)
} else {
err = errors.New(fmt.Sprintf("chat with chatID %s not found", chatID))
}
}

return chatIDs, err
}

// MuteChat signals to the messenger that we don't want to be notified
// on new messages from this chat
func (m *Messenger) MuteChat(chatID string) error {
Expand Down
4 changes: 4 additions & 0 deletions services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ func (api *PublicAPI) MarkAllRead(chatID string) error {
return api.service.messenger.MarkAllRead(chatID)
}

func (api *PublicAPI) MarkAllReadInCommunity(communityID string) ([]string, error) {
return api.service.messenger.MarkAllReadInCommunity(communityID)
}

func (api *PublicAPI) AddContact(ctx context.Context, pubKey string) (*protocol.MessengerResponse, error) {
return api.service.messenger.AddContact(ctx, pubKey)
}
Expand Down

0 comments on commit fb21876

Please sign in to comment.