1
0
Fork 0
mirror of https://github.com/mautrix/signal.git synced 2026-05-15 21:56:53 -04:00
mautrix-signal/pkg/signalmeow/store/group_store.go

75 lines
2.4 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:54:35 +02:00
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Scott Weber
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-01-05 13:44:41 +02:00
package store
2023-09-14 16:40:27 -04:00
import (
"context"
"database/sql"
"errors"
2024-01-04 01:06:45 +02:00
"go.mau.fi/util/dbutil"
"go.mau.fi/mautrix-signal/pkg/signalmeow/types"
2023-09-14 16:40:27 -04:00
)
var _ GroupStore = (*sqlStore)(nil)
2023-09-14 16:40:27 -04:00
type dbGroup struct {
OurAciUuid string
GroupIdentifier types.GroupIdentifier
2024-01-05 13:44:41 +02:00
GroupMasterKey types.SerializedGroupMasterKey
2023-09-14 16:40:27 -04:00
}
type GroupStore interface {
2024-01-05 13:44:41 +02:00
MasterKeyFromGroupIdentifier(ctx context.Context, groupID types.GroupIdentifier) (types.SerializedGroupMasterKey, error)
StoreMasterKey(ctx context.Context, groupID types.GroupIdentifier, key types.SerializedGroupMasterKey) error
2023-09-14 16:40:27 -04:00
}
2024-01-04 01:06:45 +02:00
const (
getGroupByIDQuery = `SELECT account_id, group_identifier, master_key FROM signalmeow_groups WHERE account_id=$1 AND group_identifier=$2`
2024-01-04 01:06:45 +02:00
upsertGroupMasterKeyQuery = `
INSERT INTO signalmeow_groups (account_id, group_identifier, master_key)
2024-01-04 01:06:45 +02:00
VALUES ($1, $2, $3)
ON CONFLICT (account_id, group_identifier) DO UPDATE
2024-01-04 01:06:45 +02:00
SET master_key = excluded.master_key;
`
)
func scanGroup(row dbutil.Scannable) (*dbGroup, error) {
2023-09-14 16:40:27 -04:00
var g dbGroup
err := row.Scan(&g.OurAciUuid, &g.GroupIdentifier, &g.GroupMasterKey)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
} else if err != nil {
return nil, err
}
return &g, nil
}
func (s *sqlStore) MasterKeyFromGroupIdentifier(ctx context.Context, groupID types.GroupIdentifier) (types.SerializedGroupMasterKey, error) {
g, err := scanGroup(s.db.QueryRow(ctx, getGroupByIDQuery, s.AccountID, groupID))
2023-09-14 16:40:27 -04:00
if g == nil {
2024-01-04 01:06:45 +02:00
return "", err
} else {
return g.GroupMasterKey, nil
2023-09-14 16:40:27 -04:00
}
}
func (s *sqlStore) StoreMasterKey(ctx context.Context, groupID types.GroupIdentifier, key types.SerializedGroupMasterKey) error {
_, err := s.db.Exec(ctx, upsertGroupMasterKeyQuery, s.AccountID, groupID, key)
2023-09-14 16:40:27 -04:00
return err
}