refactor to mautrix 0.17.x; update deps
This commit is contained in:
53
vendor/maunium.net/go/mautrix/syncstore.go
generated
vendored
53
vendor/maunium.net/go/mautrix/syncstore.go
generated
vendored
@@ -1,21 +1,26 @@
|
||||
package mautrix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
var _ SyncStore = (*MemorySyncStore)(nil)
|
||||
var _ SyncStore = (*AccountDataStore)(nil)
|
||||
|
||||
// SyncStore is an interface which must be satisfied to store client data.
|
||||
//
|
||||
// You can either write a struct which persists this data to disk, or you can use the
|
||||
// provided "MemorySyncStore" which just keeps data around in-memory which is lost on
|
||||
// restarts.
|
||||
type SyncStore interface {
|
||||
SaveFilterID(userID id.UserID, filterID string)
|
||||
LoadFilterID(userID id.UserID) string
|
||||
SaveNextBatch(userID id.UserID, nextBatchToken string)
|
||||
LoadNextBatch(userID id.UserID) string
|
||||
SaveFilterID(ctx context.Context, userID id.UserID, filterID string) error
|
||||
LoadFilterID(ctx context.Context, userID id.UserID) (string, error)
|
||||
SaveNextBatch(ctx context.Context, userID id.UserID, nextBatchToken string) error
|
||||
LoadNextBatch(ctx context.Context, userID id.UserID) (string, error)
|
||||
}
|
||||
|
||||
// Deprecated: renamed to SyncStore
|
||||
@@ -32,23 +37,25 @@ type MemorySyncStore struct {
|
||||
}
|
||||
|
||||
// SaveFilterID to memory.
|
||||
func (s *MemorySyncStore) SaveFilterID(userID id.UserID, filterID string) {
|
||||
func (s *MemorySyncStore) SaveFilterID(ctx context.Context, userID id.UserID, filterID string) error {
|
||||
s.Filters[userID] = filterID
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadFilterID from memory.
|
||||
func (s *MemorySyncStore) LoadFilterID(userID id.UserID) string {
|
||||
return s.Filters[userID]
|
||||
func (s *MemorySyncStore) LoadFilterID(ctx context.Context, userID id.UserID) (string, error) {
|
||||
return s.Filters[userID], nil
|
||||
}
|
||||
|
||||
// SaveNextBatch to memory.
|
||||
func (s *MemorySyncStore) SaveNextBatch(userID id.UserID, nextBatchToken string) {
|
||||
func (s *MemorySyncStore) SaveNextBatch(ctx context.Context, userID id.UserID, nextBatchToken string) error {
|
||||
s.NextBatch[userID] = nextBatchToken
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadNextBatch from memory.
|
||||
func (s *MemorySyncStore) LoadNextBatch(userID id.UserID) string {
|
||||
return s.NextBatch[userID]
|
||||
func (s *MemorySyncStore) LoadNextBatch(ctx context.Context, userID id.UserID) (string, error) {
|
||||
return s.NextBatch[userID], nil
|
||||
}
|
||||
|
||||
// NewMemorySyncStore constructs a new MemorySyncStore.
|
||||
@@ -72,34 +79,35 @@ type accountData struct {
|
||||
NextBatch string `json:"next_batch"`
|
||||
}
|
||||
|
||||
func (s *AccountDataStore) SaveFilterID(userID id.UserID, filterID string) {
|
||||
func (s *AccountDataStore) SaveFilterID(ctx context.Context, userID id.UserID, filterID string) error {
|
||||
if userID.String() != s.client.UserID.String() {
|
||||
panic("AccountDataStore must only be used with a single account")
|
||||
}
|
||||
s.FilterID = filterID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AccountDataStore) LoadFilterID(userID id.UserID) string {
|
||||
func (s *AccountDataStore) LoadFilterID(ctx context.Context, userID id.UserID) (string, error) {
|
||||
if userID.String() != s.client.UserID.String() {
|
||||
panic("AccountDataStore must only be used with a single account")
|
||||
}
|
||||
return s.FilterID
|
||||
return s.FilterID, nil
|
||||
}
|
||||
|
||||
func (s *AccountDataStore) SaveNextBatch(userID id.UserID, nextBatchToken string) {
|
||||
func (s *AccountDataStore) SaveNextBatch(ctx context.Context, userID id.UserID, nextBatchToken string) error {
|
||||
if userID.String() != s.client.UserID.String() {
|
||||
panic("AccountDataStore must only be used with a single account")
|
||||
} else if nextBatchToken == s.nextBatch {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
data := accountData{
|
||||
NextBatch: nextBatchToken,
|
||||
}
|
||||
|
||||
err := s.client.SetAccountData(s.EventType, data)
|
||||
err := s.client.SetAccountData(ctx, s.EventType, data)
|
||||
if err != nil {
|
||||
s.client.Log.Warn().Err(err).Msg("Failed to save next batch token to account data")
|
||||
return fmt.Errorf("failed to save next batch token to account data: %w", err)
|
||||
} else {
|
||||
s.client.Log.Debug().
|
||||
Str("old_token", s.nextBatch).
|
||||
@@ -107,28 +115,29 @@ func (s *AccountDataStore) SaveNextBatch(userID id.UserID, nextBatchToken string
|
||||
Msg("Saved next batch token")
|
||||
s.nextBatch = nextBatchToken
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AccountDataStore) LoadNextBatch(userID id.UserID) string {
|
||||
func (s *AccountDataStore) LoadNextBatch(ctx context.Context, userID id.UserID) (string, error) {
|
||||
if userID.String() != s.client.UserID.String() {
|
||||
panic("AccountDataStore must only be used with a single account")
|
||||
}
|
||||
|
||||
data := &accountData{}
|
||||
|
||||
err := s.client.GetAccountData(s.EventType, data)
|
||||
err := s.client.GetAccountData(ctx, s.EventType, data)
|
||||
if err != nil {
|
||||
if errors.Is(err, MNotFound) {
|
||||
s.client.Log.Debug().Msg("No next batch token found in account data")
|
||||
return "", nil
|
||||
} else {
|
||||
s.client.Log.Warn().Err(err).Msg("Failed to load next batch token from account data")
|
||||
return "", fmt.Errorf("failed to load next batch token from account data: %w", err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
s.nextBatch = data.NextBatch
|
||||
s.client.Log.Debug().Str("next_batch", data.NextBatch).Msg("Loaded next batch token from account data")
|
||||
|
||||
return s.nextBatch
|
||||
return s.nextBatch, nil
|
||||
}
|
||||
|
||||
// NewAccountDataStore returns a new AccountDataStore, which stores
|
||||
|
||||
Reference in New Issue
Block a user