2023-12-15 16:16:18 +00:00
|
|
|
package peersyncing
|
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
import "github.com/status-im/status-go/eth-node/types"
|
|
|
|
|
2023-12-15 16:16:18 +00:00
|
|
|
type PeerSyncing struct {
|
|
|
|
persistence SyncMessagePersistence
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(config Config) *PeerSyncing {
|
|
|
|
syncMessagePersistence := config.SyncMessagePersistence
|
|
|
|
if syncMessagePersistence == nil {
|
|
|
|
syncMessagePersistence = NewSyncMessageSQLitePersistence(config.Database)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PeerSyncing{
|
|
|
|
config: config,
|
|
|
|
persistence: syncMessagePersistence,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PeerSyncing) Add(message SyncMessage) error {
|
|
|
|
return p.persistence.Add(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PeerSyncing) AvailableMessages() ([]SyncMessage, error) {
|
|
|
|
return p.persistence.All()
|
|
|
|
}
|
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
func (p *PeerSyncing) AvailableMessagesMapByChatIDs(groupIDs [][]byte, limit int) (map[string][][]byte, error) {
|
|
|
|
availableMessages, err := p.persistence.ByChatIDs(groupIDs, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
availableMessagesMap := make(map[string][][]byte)
|
|
|
|
for _, m := range availableMessages {
|
|
|
|
chatID := types.Bytes2Hex(m.ChatID)
|
|
|
|
availableMessagesMap[chatID] = append(availableMessagesMap[chatID], m.ID)
|
|
|
|
}
|
|
|
|
return availableMessagesMap, err
|
2023-12-15 16:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PeerSyncing) MessagesByIDs(messageIDs [][]byte) ([]SyncMessage, error) {
|
|
|
|
return p.persistence.ByMessageIDs(messageIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PeerSyncing) OnOffer(messages []SyncMessage) ([]SyncMessage, error) {
|
|
|
|
return p.persistence.Complement(messages)
|
|
|
|
}
|