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"
|
2016-02-26 15:45:30 -08:00
|
|
|
|
2018-03-14 20:21:21 -07:00
|
|
|
"github.com/tinode/chat/server/auth"
|
2016-02-26 15:45:30 -08:00
|
|
|
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 {
|
2018-06-08 22:10:35 +03:00
|
|
|
// General
|
|
|
|
|
|
|
|
// Open and configure the adapter
|
2019-10-16 15:07:16 +05:00
|
|
|
Open(config json.RawMessage) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// Close the adapter
|
2015-10-22 10:00:39 -07:00
|
|
|
Close() error
|
2018-06-08 22:10:35 +03:00
|
|
|
// IsOpen checks if the adapter is ready for use
|
2015-10-22 10:00:39 -07:00
|
|
|
IsOpen() bool
|
2019-06-06 20:36:50 -07:00
|
|
|
// GetDbVersion returns current database version.
|
|
|
|
GetDbVersion() (int, error)
|
2018-06-08 22:10:35 +03:00
|
|
|
// CheckDbVersion checks if the actual database version matches adapter version.
|
2018-02-03 12:04:07 -08:00
|
|
|
CheckDbVersion() error
|
2018-06-08 22:10:35 +03:00
|
|
|
// GetName returns the name of the adapter
|
2018-02-20 13:17:47 -08:00
|
|
|
GetName() string
|
2019-03-20 17:55:15 +03:00
|
|
|
// SetMaxResults configures how many results can be returned in a single DB call.
|
|
|
|
SetMaxResults(val int) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// CreateDb creates the database optionally dropping an existing database first.
|
2015-10-23 17:20:26 -07:00
|
|
|
CreateDb(reset bool) error
|
2019-06-06 20:36:50 -07:00
|
|
|
// 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
|
2018-06-08 22:10:35 +03:00
|
|
|
|
|
|
|
// UserCreate creates user record
|
2019-10-15 09:00:23 +05:00
|
|
|
UserCreate(user *t.User) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// UserGet returns record for a given user ID
|
2019-10-15 09:00:23 +05:00
|
|
|
UserGet(uid t.Uid) (*t.User, error)
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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)
|
2018-06-08 22:10:35 +03:00
|
|
|
// UserDelete deletes user record
|
2019-10-15 09:00:23 +05:00
|
|
|
UserDelete(uid t.Uid, hard bool) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// UserUpdate updates user record
|
2023-08-24 11:14:11 +03:00
|
|
|
UserUpdate(uid t.Uid, update map[string]any) error
|
2019-05-30 17:21:16 -07:00
|
|
|
// 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)
|
2019-03-12 18:24:10 +03:00
|
|
|
// UserUnreadCount returns the total number of unread messages in all topics with
|
2022-04-24 12:53:55 -07:00
|
|
|
// 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
|
2018-06-08 22:10:35 +03:00
|
|
|
|
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)
|
2019-06-01 21:34:49 -07:00
|
|
|
// 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)
|
2019-06-06 20:36:50 -07:00
|
|
|
// 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
|
2018-06-08 22:10:35 +03:00
|
|
|
// CredConfirm marks given credential as validated.
|
2018-03-16 18:48:16 -07:00
|
|
|
CredConfirm(uid t.Uid, method string) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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-08 22:10:35 +03:00
|
|
|
|
2018-06-25 18:01:46 +03:00
|
|
|
// AuthGetUniqueRecord returns authentication record for a given unique value i.e. login.
|
2018-04-27 10:52:12 -07:00
|
|
|
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.
|
2018-04-27 10:52:12 -07:00
|
|
|
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)
|
2021-08-17 17:11:27 -07:00
|
|
|
// 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
|
|
|
|
2018-06-08 22:10:35 +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)
|
2017-06-08 17:00:37 -07:00
|
|
|
// TopicsForUser loads subscriptions for a given user. Reads public value.
|
2021-06-12 15:13:16 -07:00
|
|
|
// 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)
|
2018-12-09 20:46:14 +03:00
|
|
|
// OwnTopics loads a slice of topic names where the user is the owner.
|
2019-10-23 18:06:15 +05:00
|
|
|
OwnTopics(uid t.Uid) ([]string, error)
|
2021-02-18 17:31:37 -08:00
|
|
|
// 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)
|
2018-06-08 22:10:35 +03:00
|
|
|
// TopicShare creates topc subscriptions
|
2019-10-24 15:50:35 +05:00
|
|
|
TopicShare(subs []*t.Subscription) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// TopicDelete deletes topic, subscription, messages
|
2022-05-23 19:55:30 -07:00
|
|
|
TopicDelete(topic string, isChan, hard bool) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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
|
2019-10-24 18:10:29 +05:00
|
|
|
TopicOwnerChange(topic string, newOwner t.Uid) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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)
|
2021-04-02 16:58:14 -07:00
|
|
|
// 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
|
|
|
|
2018-06-08 22:10:35 +03:00
|
|
|
// Search
|
|
|
|
|
2023-03-20 09:24:19 -07:00
|
|
|
// 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
|
2018-06-08 22:10:35 +03:00
|
|
|
|
|
|
|
// MessageSave saves message to database
|
2015-12-07 14:01:17 -08:00
|
|
|
MessageSave(msg *t.Message) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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)
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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)
|
2018-06-08 22:10:35 +03:00
|
|
|
|
|
|
|
// DeviceUpsert creates or updates a device record
|
2016-10-05 18:52:47 -07:00
|
|
|
DeviceUpsert(uid t.Uid, dev *t.DeviceDef) error
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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)
|
2018-06-08 22:10:35 +03:00
|
|
|
// DeviceDelete deletes a device record
|
2018-01-07 20:08:57 -08:00
|
|
|
DeviceDelete(uid t.Uid, deviceID string) error
|
2018-06-08 22:10:35 +03:00
|
|
|
|
2018-06-15 14:29:48 +03:00
|
|
|
// File upload records. The files are stored outside of the database.
|
2018-06-08 22:10:35 +03:00
|
|
|
|
2021-07-01 14:04:56 -07:00
|
|
|
// FileStartUpload initializes a file upload.
|
2018-06-08 22:10:35 +03:00
|
|
|
FileStartUpload(fd *t.FileDef) error
|
2018-07-07 21:30:48 +03:00
|
|
|
// FileFinishUpload marks file upload as completed, successfully or otherwise.
|
2021-06-07 18:28:52 -07:00
|
|
|
FileFinishUpload(fd *t.FileDef, success bool, size int64) (*t.FileDef, error)
|
2018-06-08 22:10:35 +03:00
|
|
|
// 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.
|
2018-06-26 11:00:39 +03:00
|
|
|
// 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
|
|
|
}
|