mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-17 03:43:11 -06:00
72 lines
2.7 KiB
Go
72 lines
2.7 KiB
Go
package shared
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/matrix-org/dendrite/common"
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
)
|
|
|
|
// Database is a temporary struct until we have made syncserver.go the same for both pq/sqlite
|
|
// For now this contains the shared functions
|
|
type Database struct {
|
|
DB *sql.DB
|
|
Invites tables.Invites
|
|
AccountData tables.AccountData
|
|
}
|
|
|
|
// AddInviteEvent stores a new invite event for a user.
|
|
// If the invite was successfully stored this returns the stream ID it was stored at.
|
|
// Returns an error if there was a problem communicating with the database.
|
|
func (d *Database) AddInviteEvent(
|
|
ctx context.Context, inviteEvent gomatrixserverlib.HeaderedEvent,
|
|
) (sp types.StreamPosition, err error) {
|
|
err = common.WithTransaction(d.DB, func(txn *sql.Tx) error {
|
|
sp, err = d.Invites.InsertInviteEvent(ctx, txn, inviteEvent)
|
|
return err
|
|
})
|
|
return
|
|
}
|
|
|
|
// RetireInviteEvent removes an old invite event from the database.
|
|
// Returns an error if there was a problem communicating with the database.
|
|
func (d *Database) RetireInviteEvent(
|
|
ctx context.Context, inviteEventID string,
|
|
) error {
|
|
// TODO: Record that invite has been retired in a stream so that we can
|
|
// notify the user in an incremental sync.
|
|
err := d.Invites.DeleteInviteEvent(ctx, inviteEventID)
|
|
return err
|
|
}
|
|
|
|
// GetAccountDataInRange returns all account data for a given user inserted or
|
|
// updated between two given positions
|
|
// Returns a map following the format data[roomID] = []dataTypes
|
|
// If no data is retrieved, returns an empty map
|
|
// If there was an issue with the retrieval, returns an error
|
|
func (d *Database) GetAccountDataInRange(
|
|
ctx context.Context, userID string, oldPos, newPos types.StreamPosition,
|
|
accountDataFilterPart *gomatrixserverlib.EventFilter,
|
|
) (map[string][]string, error) {
|
|
return d.AccountData.SelectAccountDataInRange(ctx, userID, oldPos, newPos, accountDataFilterPart)
|
|
}
|
|
|
|
// UpsertAccountData keeps track of new or updated account data, by saving the type
|
|
// of the new/updated data, and the user ID and room ID the data is related to (empty)
|
|
// room ID means the data isn't specific to any room)
|
|
// If no data with the given type, user ID and room ID exists in the database,
|
|
// creates a new row, else update the existing one
|
|
// Returns an error if there was an issue with the upsert
|
|
func (d *Database) UpsertAccountData(
|
|
ctx context.Context, userID, roomID, dataType string,
|
|
) (sp types.StreamPosition, err error) {
|
|
err = common.WithTransaction(d.DB, func(txn *sql.Tx) error {
|
|
sp, err = d.AccountData.InsertAccountData(ctx, txn, userID, roomID, dataType)
|
|
return err
|
|
})
|
|
return
|
|
}
|