2023-07-02 14:21:55 +00:00
|
|
|
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
|
2024-02-24 22:18:06 +00:00
|
|
|
// Copyright (C) 2024 Tulir Asokan
|
2023-07-02 14:21:55 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2023-08-08 13:13:44 +00:00
|
|
|
"go.mau.fi/util/dbutil"
|
2023-07-02 14:21:55 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2023-07-22 16:21:29 +00:00
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-07-02 14:21:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PortalQuery struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
*dbutil.QueryHelper[*Portal]
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 22:18:06 +00:00
|
|
|
func newPortal(qh *dbutil.QueryHelper[*Portal]) *Portal {
|
|
|
|
return &Portal{qh: qh}
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 22:46:56 +00:00
|
|
|
const (
|
|
|
|
getAllPortalsQuery = "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal"
|
|
|
|
getAllPortalsForUserQuery = getAllPortalsQuery + " WHERE receiver=$1"
|
|
|
|
getPortalByKeyQuery = getAllPortalsQuery + " WHERE id=$1 AND receiver=$2"
|
|
|
|
getPortalByOtherUserQuery = getAllPortalsQuery + " WHERE other_user=$1 AND receiver=$2"
|
|
|
|
getPortalByMXIDQuery = getAllPortalsQuery + " WHERE mxid=$1"
|
|
|
|
insertPortalQuery = `
|
|
|
|
INSERT INTO portal (id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
|
|
|
`
|
|
|
|
updatePortalQuery = `
|
|
|
|
UPDATE portal
|
|
|
|
SET self_user=$3, other_user=$4, type=$5, mxid=$6, name=$7, name_set=$8, encrypted=$9, in_space=$10
|
|
|
|
WHERE id=$1 AND receiver=$2
|
|
|
|
`
|
|
|
|
deletePortalQuery = "DELETE FROM portal WHERE id=$1 AND receiver=$2"
|
|
|
|
)
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
func (pq *PortalQuery) GetAll(ctx context.Context) ([]*Portal, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.QueryMany(ctx, getAllPortalsQuery)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PortalQuery) GetAllForUser(ctx context.Context, receiver int) ([]*Portal, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.QueryMany(ctx, getAllPortalsForUserQuery, receiver)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PortalQuery) GetByKey(ctx context.Context, key Key) (*Portal, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.QueryOne(ctx, getPortalByKeyQuery, key.ID, key.Receiver)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 22:34:47 +00:00
|
|
|
func (pq *PortalQuery) GetByOtherUser(ctx context.Context, key Key) (*Portal, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.QueryOne(ctx, getPortalByOtherUserQuery, key.ID, key.Receiver)
|
2023-09-04 22:34:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
func (pq *PortalQuery) GetByMXID(ctx context.Context, mxid id.RoomID) (*Portal, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.QueryOne(ctx, getPortalByMXIDQuery, mxid)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Key struct {
|
|
|
|
ID string
|
|
|
|
Receiver int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Key) String() string {
|
|
|
|
return fmt.Sprintf("%d.%s", p.Receiver, p.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Key) MarshalZerologObject(e *zerolog.Event) {
|
|
|
|
e.Str("id", p.ID).Int("receiver", p.Receiver)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Portal struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
qh *dbutil.QueryHelper[*Portal]
|
2023-07-02 14:21:55 +00:00
|
|
|
|
|
|
|
Key
|
2023-07-19 17:32:01 +00:00
|
|
|
OutgoingID string
|
2023-07-02 14:21:55 +00:00
|
|
|
OtherUserID string
|
|
|
|
MXID id.RoomID
|
|
|
|
|
2023-07-22 16:21:29 +00:00
|
|
|
Type gmproto.ConversationType
|
2023-07-02 14:21:55 +00:00
|
|
|
Name string
|
|
|
|
NameSet bool
|
|
|
|
Encrypted bool
|
|
|
|
InSpace bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Scan(row dbutil.Scannable) (*Portal, error) {
|
|
|
|
var mxid, selfUserID, otherUserID sql.NullString
|
2023-07-22 16:21:29 +00:00
|
|
|
var convType int
|
2024-02-24 22:18:06 +00:00
|
|
|
err := row.Scan(
|
|
|
|
&portal.ID, &portal.Receiver, &selfUserID, &otherUserID, &convType, &mxid,
|
|
|
|
&portal.Name, &portal.NameSet, &portal.Encrypted, &portal.InSpace,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-07-02 14:21:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-22 16:21:29 +00:00
|
|
|
portal.Type = gmproto.ConversationType(convType)
|
2023-07-02 14:21:55 +00:00
|
|
|
portal.MXID = id.RoomID(mxid.String)
|
2023-07-19 17:32:01 +00:00
|
|
|
portal.OutgoingID = selfUserID.String
|
2023-07-02 14:21:55 +00:00
|
|
|
portal.OtherUserID = otherUserID.String
|
|
|
|
return portal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) sqlVariables() []any {
|
2023-09-04 22:18:01 +00:00
|
|
|
return []any{
|
2024-02-24 22:18:06 +00:00
|
|
|
portal.ID, portal.Receiver, dbutil.StrPtr(portal.OutgoingID), dbutil.StrPtr(portal.OtherUserID),
|
|
|
|
int(portal.Type), dbutil.StrPtr(portal.MXID),
|
|
|
|
portal.Name, portal.NameSet, portal.Encrypted, portal.InSpace,
|
2023-09-04 22:18:01 +00:00
|
|
|
}
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Insert(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return portal.qh.Exec(ctx, insertPortalQuery, portal.sqlVariables()...)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Update(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return portal.qh.Exec(ctx, updatePortalQuery, portal.sqlVariables()...)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Delete(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return portal.qh.Exec(ctx, deletePortalQuery, portal.ID, portal.Receiver)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|