tinode-chat/server/db/adapter.go

196 lines
9.6 KiB
Go
Raw Permalink Normal View History

2015-10-22 10:00:39 -07:00
// Package adapter contains the interfaces to be implemented by the database adapter
package adapter
import (
2019-10-16 15:07:16 +05:00
"encoding/json"
2015-10-22 10:00:39 -07:00
"time"
"github.com/tinode/chat/server/auth"
t "github.com/tinode/chat/server/store/types"
2015-10-22 10:00:39 -07:00
)
// Adapter is the interface that must be implemented by a database
// adapter. The current schema supports a single connection by database type.
type Adapter interface {
// General
// Open and configure the adapter
2019-10-16 15:07:16 +05:00
Open(config json.RawMessage) error
// Close the adapter
2015-10-22 10:00:39 -07:00
Close() error
// IsOpen checks if the adapter is ready for use
2015-10-22 10:00:39 -07:00
IsOpen() bool
// GetDbVersion returns current database version.
GetDbVersion() (int, error)
// CheckDbVersion checks if the actual database version matches adapter version.
2018-02-03 12:04:07 -08:00
CheckDbVersion() error
// GetName returns the name of the adapter
2018-02-20 13:17:47 -08:00
GetName() string
// SetMaxResults configures how many results can be returned in a single DB call.
SetMaxResults(val int) error
// CreateDb creates the database optionally dropping an existing database first.
CreateDb(reset bool) error
// UpgradeDb upgrades database to the current adapter version.
UpgradeDb() error
// Version returns adapter version
Version() int
2021-03-13 15:30:51 +03:00
// DB connection stats object.
2023-08-24 11:14:11 +03:00
Stats() any
2015-10-22 10:00:39 -07:00
// User management
// UserCreate creates user record
2019-10-15 09:00:23 +05:00
UserCreate(user *t.User) error
// UserGet returns record for a given user ID
2019-10-15 09:00:23 +05:00
UserGet(uid t.Uid) (*t.User, error)
// UserGetAll returns user records for a given list of user IDs
2015-12-07 14:01:17 -08:00
UserGetAll(ids ...t.Uid) ([]t.User, error)
// UserDelete deletes user record
2019-10-15 09:00:23 +05:00
UserDelete(uid t.Uid, hard bool) error
// UserUpdate updates user record
2023-08-24 11:14:11 +03:00
UserUpdate(uid t.Uid, update map[string]any) error
// UserUpdateTags adds, removes, or resets user's tags
2019-06-12 18:14:06 -07:00
UserUpdateTags(uid t.Uid, add, remove, reset []string) ([]string, error)
2018-09-30 12:22:45 +03:00
// UserGetByCred returns user ID for the given validated credential.
UserGetByCred(method, value string) (t.Uid, error)
// UserUnreadCount returns the total number of unread messages in all topics with
// the R permission. If read fails, the counts are still returned with the original
// user IDs but with the unread count undefined and non-nil error.
UserUnreadCount(ids ...t.Uid) (map[t.Uid]int, error)
2023-01-29 19:27:02 -08:00
// UserGetUnvalidated returns a list of no more than 'limit' uids who never logged in,
// have no validated credentials and which haven't been updated since 'lastUpdatedBefore'.
UserGetUnvalidated(lastUpdatedBefore time.Time, limit int) ([]t.Uid, error)
2015-10-22 10:00:39 -07:00
2018-03-10 11:12:42 -08:00
// Credential management
2019-06-08 15:54:02 -07:00
// CredUpsert adds or updates a credential record. Returns true if record was inserted, false if updated.
CredUpsert(cred *t.Credential) (bool, error)
// CredGetActive returns the currently active credential record for the given method.
CredGetActive(uid t.Uid, method string) (*t.Credential, error)
2019-06-08 15:54:02 -07:00
// CredGetAll returns credential records for the given user and method, validated only or all.
CredGetAll(uid t.Uid, method string, validatedOnly bool) ([]t.Credential, error)
// CredDel deletes credentials for the given method/value. If method is empty, deletes all
// user's credentials.
CredDel(uid t.Uid, method, value string) error
// CredConfirm marks given credential as validated.
2018-03-16 18:48:16 -07:00
CredConfirm(uid t.Uid, method string) error
// CredFail increments count of failed validation attepmts for the given credentials.
2018-03-16 18:48:16 -07:00
CredFail(uid t.Uid, method string) error
2018-03-10 11:12:42 -08:00
// Authentication management for the basic authentication scheme
2018-06-25 18:01:46 +03:00
// AuthGetUniqueRecord returns authentication record for a given unique value i.e. login.
AuthGetUniqueRecord(unique string) (t.Uid, auth.Level, []byte, time.Time, error)
2018-06-25 18:01:46 +03:00
// AuthGetRecord returns authentication record given user ID and method.
AuthGetRecord(user t.Uid, scheme string) (string, auth.Level, []byte, time.Time, error)
2018-06-25 18:01:46 +03:00
// AuthAddRecord creates new authentication record
2019-11-13 17:18:30 +05:00
AuthAddRecord(user t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error
2018-12-01 19:29:15 +03:00
// AuthDelScheme deletes an existing authentication scheme for the user.
AuthDelScheme(user t.Uid, scheme string) error
2018-06-25 18:01:46 +03:00
// AuthDelAllRecords deletes all records of a given user.
2018-03-10 11:12:42 -08:00
AuthDelAllRecords(uid t.Uid) (int, error)
// AuthUpdRecord modifies an authentication record. Only non-default/non-zero values are updated.
2019-11-13 17:18:30 +05:00
AuthUpdRecord(user t.Uid, scheme, unique string, authLvl auth.Level, secret []byte, expires time.Time) error
2017-07-15 12:34:31 +03:00
// Topic management
2015-12-01 14:57:10 -08:00
2015-10-22 10:00:39 -07:00
// TopicCreate creates a topic
2015-12-07 14:01:17 -08:00
TopicCreate(topic *t.Topic) error
2015-10-22 10:00:39 -07:00
// TopicCreateP2P creates a p2p topic
2015-12-07 14:01:17 -08:00
TopicCreateP2P(initiator, invited *t.Subscription) error
2015-10-22 10:00:39 -07:00
// TopicGet loads a single topic by name, if it exists. If the topic does not exist the call returns (nil, nil)
2015-12-07 14:01:17 -08:00
TopicGet(topic string) (*t.Topic, error)
// TopicsForUser loads subscriptions for a given user. Reads public value.
// When the 'opts.IfModifiedSince' query is not nil the subscriptions with UpdatedAt > opts.IfModifiedSince
// are returned, where UpdatedAt can be either a subscription, a topic, or a user update timestamp.
// This is need in order to support paginagion of subscriptions: get subscriptions page by page
// from the oldest updates to most recent:
// 1. Client already has subscriptions with the latest update timestamp X.
// 2. Client asks for N updated subscriptions since X. The server returns N with updates between X and Y.
// 3. Client goes to step 1 with X := Y.
2018-05-15 17:01:40 -07:00
TopicsForUser(uid t.Uid, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error)
// UsersForTopic loads users' subscriptions for a given topic. Public is loaded.
UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error)
// OwnTopics loads a slice of topic names where the user is the owner.
OwnTopics(uid t.Uid) ([]string, error)
// ChannelsForUser loads a slice of topic names where the user is a channel reader and notifications (P) are enabled.
ChannelsForUser(uid t.Uid) ([]string, error)
// TopicShare creates topc subscriptions
TopicShare(subs []*t.Subscription) error
// TopicDelete deletes topic, subscription, messages
TopicDelete(topic string, isChan, hard bool) error
// TopicUpdateOnMessage increments Topic's or User's SeqId value and updates TouchedAt timestamp.
2015-12-07 14:01:17 -08:00
TopicUpdateOnMessage(topic string, msg *t.Message) error
// TopicUpdate updates topic record.
2023-08-24 11:14:11 +03:00
TopicUpdate(topic string, update map[string]any) error
2018-11-25 13:37:33 +03:00
// TopicOwnerChange updates topic's owner
TopicOwnerChange(topic string, newOwner t.Uid) error
// Topic subscriptions
// SubscriptionGet reads a subscription of a user to a topic
2022-01-27 19:14:44 -08:00
SubscriptionGet(topic string, user t.Uid, keepDeleted bool) (*t.Subscription, error)
// SubsForUser loads all subscriptions of a given user. Does NOT load Public or Private values,
// does not load deleted subscriptions.
SubsForUser(user t.Uid) ([]t.Subscription, error)
2018-05-15 17:01:40 -07:00
// SubsForTopic gets a list of subscriptions to a given topic.. Does NOT load Public value.
SubsForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error)
2015-10-22 10:00:39 -07:00
// SubsUpdate updates pasrt of a subscription object. Pass nil for fields which don't need to be updated
2023-08-24 11:14:11 +03:00
SubsUpdate(topic string, user t.Uid, update map[string]any) error
2015-12-10 18:05:15 -08:00
// SubsDelete deletes a single subscription
2015-12-07 14:01:17 -08:00
SubsDelete(topic string, user t.Uid) error
2018-01-23 18:00:11 -08:00
// Search
// FindUsers searches for new contacts given a list of tags.
FindUsers(user t.Uid, req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error)
// FindTopics searches for group topics given a list of tags.
FindTopics(req [][]string, opt []string, activeOnly bool) ([]t.Subscription, error)
2015-10-22 10:00:39 -07:00
// Messages
// MessageSave saves message to database
2015-12-07 14:01:17 -08:00
MessageSave(msg *t.Message) error
// MessageGetAll returns messages matching the query
2018-05-15 17:01:40 -07:00
MessageGetAll(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.Message, error)
// MessageDeleteList marks messages as deleted.
// Soft- or Hard- is defined by forUser value: forUSer.IsZero == true is hard.
2017-11-30 18:44:48 -08:00
MessageDeleteList(topic string, toDel *t.DelMessage) error
// MessageGetDeleted returns a list of deleted message Ids.
2018-05-15 17:01:40 -07:00
MessageGetDeleted(topic string, forUser t.Uid, opts *t.QueryOpt) ([]t.DelMessage, error)
2016-10-02 20:19:24 -07:00
// Devices (for push notifications)
// DeviceUpsert creates or updates a device record
2016-10-05 18:52:47 -07:00
DeviceUpsert(uid t.Uid, dev *t.DeviceDef) error
// DeviceGetAll returns all devices for a given set of users
2016-10-05 18:52:47 -07:00
DeviceGetAll(uid ...t.Uid) (map[t.Uid][]t.DeviceDef, int, error)
// DeviceDelete deletes a device record
2018-01-07 20:08:57 -08:00
DeviceDelete(uid t.Uid, deviceID string) error
2018-06-15 14:29:48 +03:00
// File upload records. The files are stored outside of the database.
2021-07-01 14:04:56 -07:00
// FileStartUpload initializes a file upload.
FileStartUpload(fd *t.FileDef) error
// FileFinishUpload marks file upload as completed, successfully or otherwise.
FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error)
// FileGet fetches a record of a specific file
FileGet(fid string) (*t.FileDef, error)
2018-06-25 18:01:46 +03:00
// FileDeleteUnused deletes records where UseCount is zero. If olderThan is non-zero, deletes
// unused records with UpdatedAt before olderThan.
// Returns array of FileDef.Location of deleted filerecords so actual files can be deleted too.
2018-06-25 18:01:46 +03:00
FileDeleteUnused(olderThan time.Time, limit int) ([]string, error)
2021-07-16 16:36:29 -07:00
// FileLinkAttachments connects given topic or message to the file record IDs from the list.
2021-07-26 10:51:09 -07:00
FileLinkAttachments(topic string, userId, msgId t.Uid, fids []string) error
2023-01-21 12:44:39 -08:00
// Persistent cache management.
// PCacheGet reads a persistent cache entry.
PCacheGet(key string) (string, error)
// PCacheUpsert creates or updates a persistent cache entry.
PCacheUpsert(key string, value string, failOnDuplicate bool) error
// PCacheDelete deletes a single persistent cache entry.
PCacheDelete(key string) error
// PCacheExpire expires older entries with the specified key prefix.
PCacheExpire(keyPrefix string, olderThan time.Time) error
2015-10-22 10:00:39 -07:00
}