feat_: Add sync chat endpoint

This commit is contained in:
Andrea Maria Piana 2023-11-24 14:51:09 +00:00
parent 4086e24365
commit e1f61515ef
4 changed files with 94 additions and 0 deletions

View File

@ -633,3 +633,24 @@ func (m *Messenger) clearHistory(id string) (*MessengerResponse, error) {
response.AddChat(chat)
return response, nil
}
func (m *Messenger) SyncChat(request *requests.SyncChat) error {
if err := request.Validate(); err != nil {
return err
}
id := request.ID
chat, ok := m.allChats.Load(id)
if !ok {
return ErrChatNotFound
}
_, err := m.SyncChatOneMonth(chat.ID)
if err != nil {
return err
}
return nil
}

View File

@ -945,3 +945,53 @@ func (m *Messenger) ConnectionChanged(state connection.State) {
m.connectionState = state
}
func (m *Messenger) SyncChatOneMonth(chatID string) (uint32, error) {
to := uint32(m.getTimesource().GetCurrentTime() / 1000)
from := to - oneMonthInSeconds
_, err := m.performMailserverRequest(func() (*MessengerResponse, error) {
pubsubTopic, topics, err := m.topicsForChat(chatID)
if err != nil {
return nil, nil
}
chat, ok := m.allChats.Load(chatID)
if !ok {
return nil, ErrChatNotFound
}
batch := MailserverBatch{
ChatIDs: []string{chatID},
From: from,
To: to,
PubsubTopic: pubsubTopic,
Topics: topics,
}
if m.config.messengerSignalsHandler != nil {
m.config.messengerSignalsHandler.HistoryRequestStarted(1)
}
err = m.processMailserverBatch(batch)
if err != nil {
return nil, err
}
if m.config.messengerSignalsHandler != nil {
m.config.messengerSignalsHandler.HistoryRequestCompleted()
}
if chat.SyncedFrom == 0 || chat.SyncedFrom > batch.From {
chat.SyncedFrom = batch.From
}
m.logger.Debug("setting sync timestamps", zap.Int64("from", int64(batch.From)), zap.Int64("to", int64(chat.SyncedTo)), zap.String("chatID", chatID))
err = m.persistence.SetSyncTimestamps(batch.From, chat.SyncedTo, chat.ID)
from = batch.From
return nil, err
})
if err != nil {
return 0, err
}
return from, nil
}

View File

@ -0,0 +1,19 @@
package requests
import (
"errors"
)
var ErrSyncChatInvalidID = errors.New("sync-chat: invalid id")
type SyncChat struct {
ID string `json:"id"`
}
func (c *SyncChat) Validate() error {
if len(c.ID) == 0 {
return ErrSyncChatInvalidID
}
return nil
}

View File

@ -1681,6 +1681,10 @@ func (api *PublicAPI) PromoteSelfToControlMode(communityID string) error {
return err
}
func (api *PublicAPI) SyncChat(request *requests.SyncChat) error {
return api.service.messenger.SyncChat(request)
}
// -----
// HELPER
// -----