add vendoring
This commit is contained in:
214
vendor/gitlab.com/etke.cc/linkpearl/store/crypto.go
generated
vendored
Normal file
214
vendor/gitlab.com/etke.cc/linkpearl/store/crypto.go
generated
vendored
Normal file
@@ -0,0 +1,214 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"maunium.net/go/mautrix/crypto"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// NOTE: functions in that file are for crypto.Store implementation
|
||||
// ref: https://pkg.go.dev/maunium.net/go/mautrix/crypto#Store
|
||||
|
||||
// Flush does nothing for this implementation as data is already persisted in the database.
|
||||
// nolint // interface cannot be changed
|
||||
func (s *Store) Flush() error {
|
||||
s.log.Debug("flushing crypto store")
|
||||
return nil
|
||||
}
|
||||
|
||||
// PutNextBatch stores the next sync batch token for the current account.
|
||||
func (s *Store) PutNextBatch(nextBatch string) error {
|
||||
s.log.Debug("storing next batch token")
|
||||
return s.s.PutNextBatch(nextBatch)
|
||||
}
|
||||
|
||||
// GetNextBatch retrieves the next sync batch token for the current account.
|
||||
func (s *Store) GetNextBatch() (string, error) {
|
||||
s.log.Debug("loading next batch token")
|
||||
return s.s.GetNextBatch()
|
||||
}
|
||||
|
||||
// PutAccount stores an OlmAccount in the database.
|
||||
func (s *Store) PutAccount(account *crypto.OlmAccount) error {
|
||||
s.log.Debug("storing olm account")
|
||||
return s.s.PutAccount(account)
|
||||
}
|
||||
|
||||
// GetAccount retrieves an OlmAccount from the database.
|
||||
func (s *Store) GetAccount() (*crypto.OlmAccount, error) {
|
||||
s.log.Debug("loading olm account")
|
||||
return s.s.GetAccount()
|
||||
}
|
||||
|
||||
// HasSession returns whether there is an Olm session for the given sender key.
|
||||
func (s *Store) HasSession(key id.SenderKey) bool {
|
||||
s.log.Debug("check if olm session exists for the key %s", key)
|
||||
return s.s.HasSession(key)
|
||||
}
|
||||
|
||||
// GetSessions returns all the known Olm sessions for a sender key.
|
||||
func (s *Store) GetSessions(key id.SenderKey) (crypto.OlmSessionList, error) {
|
||||
s.log.Debug("loading olm session for the key %s", key)
|
||||
return s.s.GetSessions(key)
|
||||
}
|
||||
|
||||
// GetLatestSession retrieves the Olm session for a given sender key from the database that has the largest ID.
|
||||
func (s *Store) GetLatestSession(key id.SenderKey) (*crypto.OlmSession, error) {
|
||||
s.log.Debug("loading latest session for the key %s", key)
|
||||
return s.s.GetLatestSession(key)
|
||||
}
|
||||
|
||||
// AddSession persists an Olm session for a sender in the database.
|
||||
func (s *Store) AddSession(key id.SenderKey, session *crypto.OlmSession) error {
|
||||
s.log.Debug("adding new olm session for the key %s", key)
|
||||
return s.s.AddSession(key, session)
|
||||
}
|
||||
|
||||
// UpdateSession replaces the Olm session for a sender in the database.
|
||||
func (s *Store) UpdateSession(key id.SenderKey, session *crypto.OlmSession) error {
|
||||
s.log.Debug("update olm session for the key %s", key)
|
||||
return s.s.UpdateSession(key, session)
|
||||
}
|
||||
|
||||
// PutGroupSession stores an inbound Megolm group session for a room, sender and session.
|
||||
func (s *Store) PutGroupSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID, session *crypto.InboundGroupSession) error {
|
||||
s.log.Debug("storing inbound group session for the room %s", roomID)
|
||||
return s.s.PutGroupSession(roomID, senderKey, sessionID, session)
|
||||
}
|
||||
|
||||
// GetGroupSession retrieves an inbound Megolm group session for a room, sender and session.
|
||||
func (s *Store) GetGroupSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID) (*crypto.InboundGroupSession, error) {
|
||||
s.log.Debug("loading inbound group session for the room %s", roomID)
|
||||
return s.s.GetGroupSession(roomID, senderKey, sessionID)
|
||||
}
|
||||
|
||||
// PutWithheldGroupSession tells the store that a specific Megolm session was withheld.
|
||||
// nolint // method is part of interface and cannot be changed
|
||||
func (s *Store) PutWithheldGroupSession(content event.RoomKeyWithheldEventContent) error {
|
||||
s.log.Debug("storing withheld group session")
|
||||
return s.s.PutWithheldGroupSession(content)
|
||||
}
|
||||
|
||||
// GetWithheldGroupSession gets the event content that was previously inserted with PutWithheldGroupSession.
|
||||
func (s *Store) GetWithheldGroupSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID) (*event.RoomKeyWithheldEventContent, error) {
|
||||
s.log.Debug("loading withheld group session")
|
||||
return s.s.GetWithheldGroupSession(roomID, senderKey, sessionID)
|
||||
}
|
||||
|
||||
// GetGroupSessionsForRoom gets all the inbound Megolm sessions for a specific room. This is used for creating key
|
||||
// export files. Unlike GetGroupSession, this should not return any errors about withheld keys.
|
||||
func (s *Store) GetGroupSessionsForRoom(roomID id.RoomID) ([]*crypto.InboundGroupSession, error) {
|
||||
s.log.Debug("loading group session for the room %s", roomID)
|
||||
return s.s.GetGroupSessionsForRoom(roomID)
|
||||
}
|
||||
|
||||
// GetAllGroupSessions gets all the inbound Megolm sessions in the store. This is used for creating key export
|
||||
// files. Unlike GetGroupSession, this should not return any errors about withheld keys.
|
||||
func (s *Store) GetAllGroupSessions() ([]*crypto.InboundGroupSession, error) {
|
||||
s.log.Debug("loading all group sessions")
|
||||
return s.s.GetAllGroupSessions()
|
||||
}
|
||||
|
||||
// AddOutboundGroupSession stores an outbound Megolm session, along with the information about the room and involved devices.
|
||||
func (s *Store) AddOutboundGroupSession(session *crypto.OutboundGroupSession) (err error) {
|
||||
s.log.Debug("storing outbound group session")
|
||||
return s.s.AddOutboundGroupSession(session)
|
||||
}
|
||||
|
||||
// UpdateOutboundGroupSession replaces an outbound Megolm session with for same room and session ID.
|
||||
func (s *Store) UpdateOutboundGroupSession(session *crypto.OutboundGroupSession) error {
|
||||
s.log.Debug("updating outbound group session")
|
||||
return s.s.UpdateOutboundGroupSession(session)
|
||||
}
|
||||
|
||||
// GetOutboundGroupSession retrieves the outbound Megolm session for the given room ID.
|
||||
func (s *Store) GetOutboundGroupSession(roomID id.RoomID) (*crypto.OutboundGroupSession, error) {
|
||||
s.log.Debug("loading outbound group session")
|
||||
return s.s.GetOutboundGroupSession(roomID)
|
||||
}
|
||||
|
||||
// RemoveOutboundGroupSession removes the outbound Megolm session for the given room ID.
|
||||
func (s *Store) RemoveOutboundGroupSession(roomID id.RoomID) error {
|
||||
s.log.Debug("removing outbound group session")
|
||||
return s.s.RemoveOutboundGroupSession(roomID)
|
||||
}
|
||||
|
||||
// ValidateMessageIndex returns whether the given event information match the ones stored in the database
|
||||
// for the given sender key, session ID and index.
|
||||
// If the event information was not yet stored, it's stored now.
|
||||
func (s *Store) ValidateMessageIndex(senderKey id.SenderKey, sessionID id.SessionID, eventID id.EventID, index uint, timestamp int64) (bool, error) {
|
||||
s.log.Debug("validating message index")
|
||||
return s.s.ValidateMessageIndex(senderKey, sessionID, eventID, index, timestamp)
|
||||
}
|
||||
|
||||
// GetDevices returns a map of device IDs to device identities, including the identity and signing keys, for a given user ID.
|
||||
func (s *Store) GetDevices(userID id.UserID) (map[id.DeviceID]*id.Device, error) {
|
||||
s.log.Debug("loading devices of the %s", userID)
|
||||
return s.s.GetDevices(userID)
|
||||
}
|
||||
|
||||
// GetDevice returns the device dentity for a given user and device ID.
|
||||
func (s *Store) GetDevice(userID id.UserID, deviceID id.DeviceID) (*id.Device, error) {
|
||||
s.log.Debug("loading device %s for the %s", deviceID, userID)
|
||||
return s.s.GetDevice(userID, deviceID)
|
||||
}
|
||||
|
||||
// FindDeviceByKey finds a specific device by its sender key.
|
||||
func (s *Store) FindDeviceByKey(userID id.UserID, identityKey id.IdentityKey) (*id.Device, error) {
|
||||
s.log.Debug("loading device of the %s by the key %s", userID, identityKey)
|
||||
return s.s.FindDeviceByKey(userID, identityKey)
|
||||
}
|
||||
|
||||
// PutDevice stores a single device for a user, replacing it if it exists already.
|
||||
func (s *Store) PutDevice(userID id.UserID, device *id.Device) error {
|
||||
s.log.Debug("storing device of the %s", userID)
|
||||
return s.s.PutDevice(userID, device)
|
||||
}
|
||||
|
||||
// PutDevices stores the device identity information for the given user ID.
|
||||
func (s *Store) PutDevices(userID id.UserID, devices map[id.DeviceID]*id.Device) error {
|
||||
s.log.Debug("storing devices of the %s", userID)
|
||||
return s.s.PutDevices(userID, devices)
|
||||
}
|
||||
|
||||
// FilterTrackedUsers finds all of the user IDs out of the given ones for which the database contains identity information.
|
||||
func (s *Store) FilterTrackedUsers(users []id.UserID) ([]id.UserID, error) {
|
||||
s.log.Debug("filtering tracked users")
|
||||
return s.s.FilterTrackedUsers(users)
|
||||
}
|
||||
|
||||
// PutCrossSigningKey stores a cross-signing key of some user along with its usage.
|
||||
func (s *Store) PutCrossSigningKey(userID id.UserID, usage id.CrossSigningUsage, key id.Ed25519) error {
|
||||
s.log.Debug("storing crosssigning key of the %s", userID)
|
||||
return s.s.PutCrossSigningKey(userID, usage, key)
|
||||
}
|
||||
|
||||
// GetCrossSigningKeys retrieves a user's stored cross-signing keys.
|
||||
func (s *Store) GetCrossSigningKeys(userID id.UserID) (map[id.CrossSigningUsage]id.CrossSigningKey, error) {
|
||||
s.log.Debug("loading crosssigning keys of the %s", userID)
|
||||
return s.s.GetCrossSigningKeys(userID)
|
||||
}
|
||||
|
||||
// PutSignature stores a signature of a cross-signing or device key along with the signer's user ID and key.
|
||||
func (s *Store) PutSignature(signedUserID id.UserID, signedKey id.Ed25519, signerUserID id.UserID, signerKey id.Ed25519, signature string) error {
|
||||
s.log.Debug("storing signature")
|
||||
return s.s.PutSignature(signedUserID, signedKey, signerUserID, signerKey, signature)
|
||||
}
|
||||
|
||||
// GetSignaturesForKeyBy retrieves the stored signatures for a given cross-signing or device key, by the given signer.
|
||||
func (s *Store) GetSignaturesForKeyBy(userID id.UserID, key id.Ed25519, signerID id.UserID) (map[id.Ed25519]string, error) {
|
||||
s.log.Debug("loading signatures")
|
||||
return s.s.GetSignaturesForKeyBy(userID, key, signerID)
|
||||
}
|
||||
|
||||
// IsKeySignedBy returns whether a cross-signing or device key is signed by the given signer.
|
||||
func (s *Store) IsKeySignedBy(userID id.UserID, key id.Ed25519, signerID id.UserID, signerKey id.Ed25519) (bool, error) {
|
||||
s.log.Debug("checking if key is signed by")
|
||||
return s.s.IsKeySignedBy(userID, key, signerID, signerKey)
|
||||
}
|
||||
|
||||
// DropSignaturesByKey deletes the signatures made by the given user and key from the store. It returns the number of signatures deleted.
|
||||
func (s *Store) DropSignaturesByKey(userID id.UserID, key id.Ed25519) (int64, error) {
|
||||
s.log.Debug("removing signatures by the %s/%s", userID, key)
|
||||
return s.s.DropSignaturesByKey(userID, key)
|
||||
}
|
||||
209
vendor/gitlab.com/etke.cc/linkpearl/store/linkpearl.go
generated
vendored
Normal file
209
vendor/gitlab.com/etke.cc/linkpearl/store/linkpearl.go
generated
vendored
Normal file
@@ -0,0 +1,209 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
var acceptedMembershipTypes = []event.Membership{
|
||||
event.MembershipJoin,
|
||||
event.MembershipInvite,
|
||||
event.MembershipBan,
|
||||
event.MembershipLeave,
|
||||
}
|
||||
|
||||
// IsEncrypted returns whether a room is encrypted.
|
||||
func (s *Store) IsEncrypted(roomID id.RoomID) bool {
|
||||
if !s.encryption {
|
||||
return false
|
||||
}
|
||||
|
||||
s.log.Debug("checking if room %s is encrypted", roomID)
|
||||
return s.GetEncryptionEvent(roomID) != nil
|
||||
}
|
||||
|
||||
// SetEncryptionEvent creates or updates room's encryption event info
|
||||
func (s *Store) SetEncryptionEvent(evt *event.Event) {
|
||||
if !s.encryption {
|
||||
return
|
||||
}
|
||||
if evt == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var encryptionEventJSON []byte
|
||||
encryptionEventJSON, err := json.Marshal(evt)
|
||||
if err != nil {
|
||||
s.log.Debug("cannot marshal encryption event: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
s.log.Error("cannot begin transaction: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var insert string
|
||||
switch s.dialect {
|
||||
case "sqlite3":
|
||||
insert = "INSERT OR IGNORE INTO rooms VALUES (?, ?)"
|
||||
case "postgres":
|
||||
insert = "INSERT INTO rooms VALUES ($1, $2) ON CONFLICT DO NOTHING"
|
||||
}
|
||||
update := "UPDATE rooms SET encryption_event = $1 WHERE room_id = $2"
|
||||
|
||||
_, err = tx.Exec(update, encryptionEventJSON, evt.RoomID)
|
||||
if err != nil {
|
||||
s.log.Error("cannot update encryption event: %v", err)
|
||||
// nolint // we already have err to return
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
_, err = tx.Exec(insert, evt.RoomID, encryptionEventJSON)
|
||||
if err != nil {
|
||||
s.log.Error("cannot insert encryption event: %v", err)
|
||||
// nolint // interface doesn't allow to return error
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
s.log.Error("cannot commit transaction: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// SetMembership saves room members
|
||||
func (s *Store) SetMembership(evt *event.Event) {
|
||||
s.log.Debug("saving membership event for %s", evt.RoomID)
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
s.log.Error("cannot begin transaction: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var insert string
|
||||
switch s.dialect {
|
||||
case "sqlite3":
|
||||
insert = "INSERT OR IGNORE INTO room_members VALUES (?, ?)"
|
||||
case "postgres":
|
||||
insert = "INSERT INTO room_members VALUES ($1, $2) ON CONFLICT DO NOTHING"
|
||||
}
|
||||
del := "DELETE FROM room_members WHERE room_id = $1 AND user_id = $2"
|
||||
|
||||
membership := evt.Content.AsMember().Membership
|
||||
if s.shouldIgnoreMembership(membership) {
|
||||
return
|
||||
}
|
||||
if membership.IsInviteOrJoin() {
|
||||
_, err := tx.Exec(insert, evt.RoomID, evt.GetStateKey())
|
||||
if err != nil {
|
||||
s.log.Error("cannot insert membership event: %v", err)
|
||||
// nolint // interface doesn't allow to return error
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err := tx.Exec(del, evt.RoomID, evt.GetStateKey())
|
||||
if err != nil {
|
||||
s.log.Error("cannot delete membership event: %v", err)
|
||||
// nolint // interface doesn't allow to return error
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
commitErr := tx.Commit()
|
||||
if commitErr != nil {
|
||||
s.log.Error("cannot commit transaction: %v", commitErr)
|
||||
// nolint // interface doesn't allow to return error
|
||||
tx.Rollback()
|
||||
}
|
||||
}
|
||||
|
||||
// GetRoomMembers ...
|
||||
func (s *Store) GetRoomMembers(roomID id.RoomID) []id.UserID {
|
||||
s.log.Debug("loading room members of %s", roomID)
|
||||
query := "SELECT user_id FROM room_members WHERE room_id = $1"
|
||||
rows, err := s.db.Query(query, roomID)
|
||||
users := make([]id.UserID, 0)
|
||||
if err != nil {
|
||||
s.log.Error("cannot load room members: %v", err)
|
||||
return users
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var userID id.UserID
|
||||
for rows.Next() {
|
||||
if err := rows.Scan(&userID); err == nil {
|
||||
users = append(users, userID)
|
||||
}
|
||||
}
|
||||
return users
|
||||
}
|
||||
|
||||
// SaveSession to DB
|
||||
func (s *Store) SaveSession(userID id.UserID, deviceID id.DeviceID, accessToken string) {
|
||||
s.log.Debug("saving session credentials of %s/%s", userID, deviceID)
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
s.log.Error("cannot begin transaction: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var insert string
|
||||
switch s.dialect {
|
||||
case "sqlite3":
|
||||
insert = "INSERT OR IGNORE INTO session VALUES (?, ?, ?)"
|
||||
case "postgres":
|
||||
insert = "INSERT INTO session VALUES ($1, $2, $3) ON CONFLICT DO NOTHING"
|
||||
}
|
||||
update := "UPDATE session SET access_token = $1, device_id = $2 WHERE user_id = $3"
|
||||
|
||||
if _, err = tx.Exec(update, accessToken, deviceID, userID); err != nil {
|
||||
s.log.Error("cannot update session credentials: %v", err)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = tx.Exec(insert, userID, deviceID, accessToken); err != nil {
|
||||
s.log.Error("cannot insert session credentials: %v", err)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
s.log.Error("cannot commit transaction: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// LoadSession from DB (user ID, device ID, access token)
|
||||
func (s *Store) LoadSession() (id.UserID, id.DeviceID, string) {
|
||||
s.log.Debug("loading session credentials...")
|
||||
row := s.db.QueryRow("SELECT * FROM session LIMIT 1")
|
||||
var userID id.UserID
|
||||
var deviceID id.DeviceID
|
||||
var accessToken string
|
||||
if err := row.Scan(&userID, &deviceID, &accessToken); err != nil {
|
||||
s.log.Error("cannot load session credentials: %v", err)
|
||||
return "", "", ""
|
||||
}
|
||||
return userID, deviceID, accessToken
|
||||
}
|
||||
|
||||
func (s *Store) shouldIgnoreMembership(membership event.Membership) bool {
|
||||
for _, mtype := range acceptedMembershipTypes {
|
||||
if membership == mtype {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
66
vendor/gitlab.com/etke.cc/linkpearl/store/migrations.go
generated
vendored
Normal file
66
vendor/gitlab.com/etke.cc/linkpearl/store/migrations.go
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
package store
|
||||
|
||||
var migrations = []string{
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS user_filter_ids (
|
||||
user_id VARCHAR(255) PRIMARY KEY,
|
||||
filter_id VARCHAR(255)
|
||||
)
|
||||
`,
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS user_batch_tokens (
|
||||
user_id VARCHAR(255) PRIMARY KEY,
|
||||
next_batch_token VARCHAR(255)
|
||||
)
|
||||
`,
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS rooms (
|
||||
room_id VARCHAR(255) PRIMARY KEY,
|
||||
encryption_event VARCHAR(65535) NULL
|
||||
)
|
||||
`,
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS room_members (
|
||||
room_id VARCHAR(255),
|
||||
user_id VARCHAR(255),
|
||||
PRIMARY KEY (room_id, user_id)
|
||||
)
|
||||
`,
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS session (
|
||||
user_id VARCHAR(255),
|
||||
device_id VARCHAR(255),
|
||||
access_token VARCHAR(255)
|
||||
)
|
||||
`,
|
||||
}
|
||||
|
||||
// CreateTables applies all the pending database migrations.
|
||||
func (s *Store) CreateTables() error {
|
||||
s.log.Debug("migrating database...")
|
||||
tx, beginErr := s.db.Begin()
|
||||
if beginErr != nil {
|
||||
s.log.Error("cannot begin transaction: %v", beginErr)
|
||||
return beginErr
|
||||
}
|
||||
|
||||
for _, query := range migrations {
|
||||
_, execErr := tx.Exec(query)
|
||||
if execErr != nil {
|
||||
s.log.Error("cannot apply migration: %v", execErr)
|
||||
// nolint // we already have the execErr to return
|
||||
tx.Rollback()
|
||||
return execErr
|
||||
}
|
||||
}
|
||||
|
||||
commitErr := tx.Commit()
|
||||
if commitErr != nil {
|
||||
s.log.Error("cannot commit transaction: %v", commitErr)
|
||||
// nolint // we already have the commitErr to return
|
||||
tx.Rollback()
|
||||
return commitErr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
63
vendor/gitlab.com/etke.cc/linkpearl/store/state.go
generated
vendored
Normal file
63
vendor/gitlab.com/etke.cc/linkpearl/store/state.go
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// NOTE: functions in that file are for crypto.StateStore implementation
|
||||
// ref: https://pkg.go.dev/maunium.net/go/mautrix/crypto#StateStore
|
||||
|
||||
// GetEncryptionEvent returns the encryption event's content for an encrypted room.
|
||||
func (s *Store) GetEncryptionEvent(roomID id.RoomID) *event.EncryptionEventContent {
|
||||
if !s.encryption {
|
||||
return nil
|
||||
}
|
||||
s.log.Debug("finding encryption event of %s", roomID)
|
||||
query := "SELECT encryption_event FROM rooms WHERE room_id = $1"
|
||||
row := s.db.QueryRow(query, roomID)
|
||||
|
||||
var encryptionEventJSON []byte
|
||||
err := row.Scan(&encryptionEventJSON)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
s.log.Error("cannot find encryption event: %v", err)
|
||||
return nil
|
||||
}
|
||||
var encryptionEvent event.EncryptionEventContent
|
||||
if err := json.Unmarshal(encryptionEventJSON, &encryptionEvent); err != nil {
|
||||
s.log.Debug("cannot unmarshal encryption event: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return &encryptionEvent
|
||||
}
|
||||
|
||||
// FindSharedRooms returns the encrypted rooms that another user is also in for a user ID.
|
||||
func (s *Store) FindSharedRooms(userID id.UserID) []id.RoomID {
|
||||
if !s.encryption {
|
||||
return nil
|
||||
}
|
||||
s.log.Debug("loading shared rooms for %s", userID)
|
||||
query := "SELECT room_id FROM room_members WHERE user_id = $1"
|
||||
rows, queryErr := s.db.Query(query, userID)
|
||||
rooms := make([]id.RoomID, 0)
|
||||
if queryErr != nil {
|
||||
s.log.Error("cannot load room members: %s", queryErr)
|
||||
return rooms
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var roomID id.RoomID
|
||||
for rows.Next() {
|
||||
scanErr := rows.Scan(&roomID)
|
||||
if scanErr != nil {
|
||||
continue
|
||||
}
|
||||
rooms = append(rooms, roomID)
|
||||
}
|
||||
|
||||
return rooms
|
||||
}
|
||||
55
vendor/gitlab.com/etke.cc/linkpearl/store/store.go
generated
vendored
Normal file
55
vendor/gitlab.com/etke.cc/linkpearl/store/store.go
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
// Package store implements crypto.Store, crypto.StateStore, mautrix.Storer and some additional "glue methods"
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"maunium.net/go/mautrix/crypto"
|
||||
"maunium.net/go/mautrix/id"
|
||||
"maunium.net/go/mautrix/util/dbutil"
|
||||
|
||||
"gitlab.com/etke.cc/linkpearl/config"
|
||||
)
|
||||
|
||||
// Store for the matrix
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
dialect string
|
||||
log config.Logger
|
||||
encryption bool
|
||||
s *crypto.SQLCryptoStore
|
||||
}
|
||||
|
||||
// New store
|
||||
func New(db *sql.DB, dialect string, log config.Logger) *Store {
|
||||
return &Store{
|
||||
db: db,
|
||||
log: log,
|
||||
dialect: dialect,
|
||||
}
|
||||
}
|
||||
|
||||
// WithCrypto adds crypto store support
|
||||
func (s *Store) WithCrypto(userID id.UserID, deviceID id.DeviceID, logger config.Logger) error {
|
||||
s.log.Debug("crypto store enabled")
|
||||
s.encryption = true
|
||||
db, err := dbutil.NewWithDB(s.db, s.dialect)
|
||||
if err != nil {
|
||||
logger.Error("cannot init database: %v", err)
|
||||
return err
|
||||
}
|
||||
s.s = crypto.NewSQLCryptoStore(
|
||||
db,
|
||||
dbutil.NoopLogger,
|
||||
userID.String(),
|
||||
deviceID,
|
||||
[]byte(userID),
|
||||
)
|
||||
|
||||
return s.s.Upgrade()
|
||||
}
|
||||
|
||||
// GetDialect returns database dialect
|
||||
func (s *Store) GetDialect() string {
|
||||
return s.dialect
|
||||
}
|
||||
126
vendor/gitlab.com/etke.cc/linkpearl/store/storer.go
generated
vendored
Normal file
126
vendor/gitlab.com/etke.cc/linkpearl/store/storer.go
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// NOTE: functions in that file are for mautrix.Storer implementation
|
||||
// ref: https://pkg.go.dev/maunium.net/go/mautrix#Storer
|
||||
|
||||
// SaveFilterID to DB
|
||||
func (s *Store) SaveFilterID(userID id.UserID, filterID string) {
|
||||
s.log.Debug("saving filter ID %s for %s", filterID, userID)
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
s.log.Error("cannot begin transaction: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var insert string
|
||||
switch s.dialect {
|
||||
case "sqlite3":
|
||||
insert = "INSERT OR IGNORE INTO user_filter_ids VALUES (?, ?)"
|
||||
case "postgres":
|
||||
insert = "INSERT INTO user_filter_ids VALUES ($1, $2) ON CONFLICT DO NOTHING"
|
||||
}
|
||||
update := "UPDATE user_filter_ids SET filter_id = $1 WHERE user_id = $2"
|
||||
|
||||
_, updateErr := tx.Exec(update, filterID, userID)
|
||||
if updateErr != nil {
|
||||
s.log.Error("cannot update filter ID: %v", updateErr)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
_, insertErr := tx.Exec(insert, userID, filterID)
|
||||
if insertErr != nil {
|
||||
s.log.Error("cannot create filter ID: %v", insertErr)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
commitErr := tx.Commit()
|
||||
if commitErr != nil {
|
||||
s.log.Error("cannot upsert filter ID: %v", commitErr)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
}
|
||||
}
|
||||
|
||||
// LoadFilterID from DB
|
||||
func (s *Store) LoadFilterID(userID id.UserID) string {
|
||||
s.log.Debug("loading filter ID for %s", userID)
|
||||
query := "SELECT filter_id FROM user_filter_ids WHERE user_id = $1"
|
||||
row := s.db.QueryRow(query, userID)
|
||||
var filterID string
|
||||
if err := row.Scan(&filterID); err != nil {
|
||||
s.log.Error("cannot load filter ID: %s", err)
|
||||
return ""
|
||||
}
|
||||
return filterID
|
||||
}
|
||||
|
||||
// SaveNextBatch to DB
|
||||
func (s *Store) SaveNextBatch(userID id.UserID, nextBatchToken string) {
|
||||
s.log.Debug("saving next batch token for %s", userID)
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
s.log.Error("cannot begin transaction: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var insert string
|
||||
switch s.dialect {
|
||||
case "sqlite3":
|
||||
insert = "INSERT OR IGNORE INTO user_batch_tokens VALUES (?, ?)"
|
||||
case "postgres":
|
||||
insert = "INSERT INTO user_batch_tokens VALUES ($1, $2) ON CONFLICT DO NOTHING"
|
||||
}
|
||||
update := "UPDATE user_batch_tokens SET next_batch_token = $1 WHERE user_id = $2"
|
||||
|
||||
if _, err := tx.Exec(update, nextBatchToken, userID); err != nil {
|
||||
s.log.Error("cannot update next batch token: %v", err)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(insert, userID, nextBatchToken); err != nil {
|
||||
s.log.Error("cannot insert next batch token: %v", err)
|
||||
// nolint // no need to check error here
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
commitErr := tx.Commit()
|
||||
if commitErr != nil {
|
||||
s.log.Error("cannot commit transaction: %v", commitErr)
|
||||
}
|
||||
}
|
||||
|
||||
// LoadNextBatch from DB
|
||||
func (s *Store) LoadNextBatch(userID id.UserID) string {
|
||||
s.log.Debug("loading next batch token for %s", userID)
|
||||
query := "SELECT next_batch_token FROM user_batch_tokens WHERE user_id = $1"
|
||||
row := s.db.QueryRow(query, userID)
|
||||
var batchToken string
|
||||
if err := row.Scan(&batchToken); err != nil {
|
||||
s.log.Error("cannot load next batch token: %v", err)
|
||||
return ""
|
||||
}
|
||||
return batchToken
|
||||
}
|
||||
|
||||
// SaveRoom to DB, not implemented
|
||||
func (s *Store) SaveRoom(room *mautrix.Room) {
|
||||
s.log.Debug("saving room %s (stub, not implemented)", room.ID)
|
||||
}
|
||||
|
||||
// LoadRoom from DB, not implemented
|
||||
func (s *Store) LoadRoom(roomID id.RoomID) *mautrix.Room {
|
||||
s.log.Debug("loading room %s (stub, not implemented)", roomID)
|
||||
return mautrix.NewRoom(roomID)
|
||||
}
|
||||
Reference in New Issue
Block a user