Expose conversation type in m.bridge info

This commit is contained in:
Tulir Asokan 2023-07-22 19:21:29 +03:00
parent 6e2c66c224
commit 95fed6ad49
4 changed files with 39 additions and 11 deletions

View file

@ -25,6 +25,8 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/util/dbutil" "maunium.net/go/mautrix/util/dbutil"
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
) )
type PortalQuery struct { type PortalQuery struct {
@ -42,19 +44,19 @@ func (pq *PortalQuery) getDB() *Database {
} }
func (pq *PortalQuery) GetAll(ctx context.Context) ([]*Portal, error) { func (pq *PortalQuery) GetAll(ctx context.Context) ([]*Portal, error) {
return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal") return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal")
} }
func (pq *PortalQuery) GetAllForUser(ctx context.Context, receiver int) ([]*Portal, error) { func (pq *PortalQuery) GetAllForUser(ctx context.Context, receiver int) ([]*Portal, error) {
return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal WHERE receiver=$1", receiver) return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal WHERE receiver=$1", receiver)
} }
func (pq *PortalQuery) GetByKey(ctx context.Context, key Key) (*Portal, error) { func (pq *PortalQuery) GetByKey(ctx context.Context, key Key) (*Portal, error) {
return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal WHERE id=$1 AND receiver=$2", key.ID, key.Receiver) return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal WHERE id=$1 AND receiver=$2", key.ID, key.Receiver)
} }
func (pq *PortalQuery) GetByMXID(ctx context.Context, mxid id.RoomID) (*Portal, error) { func (pq *PortalQuery) GetByMXID(ctx context.Context, mxid id.RoomID) (*Portal, error) {
return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal WHERE mxid=$1", mxid) return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space FROM portal WHERE mxid=$1", mxid)
} }
type Key struct { type Key struct {
@ -78,6 +80,7 @@ type Portal struct {
OtherUserID string OtherUserID string
MXID id.RoomID MXID id.RoomID
Type gmproto.ConversationType
Name string Name string
NameSet bool NameSet bool
AvatarID string AvatarID string
@ -89,12 +92,14 @@ type Portal struct {
func (portal *Portal) Scan(row dbutil.Scannable) (*Portal, error) { func (portal *Portal) Scan(row dbutil.Scannable) (*Portal, error) {
var mxid, selfUserID, otherUserID sql.NullString var mxid, selfUserID, otherUserID sql.NullString
err := row.Scan(&portal.ID, &portal.Receiver, &selfUserID, &otherUserID, &mxid, &portal.Name, &portal.NameSet, &portal.AvatarID, &portal.AvatarMXC, &portal.AvatarSet, &portal.Encrypted, &portal.InSpace) var convType int
err := row.Scan(&portal.ID, &portal.Receiver, &selfUserID, &otherUserID, &convType, &mxid, &portal.Name, &portal.NameSet, &portal.AvatarID, &portal.AvatarMXC, &portal.AvatarSet, &portal.Encrypted, &portal.InSpace)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
return nil, nil return nil, nil
} else if err != nil { } else if err != nil {
return nil, err return nil, err
} }
portal.Type = gmproto.ConversationType(convType)
portal.MXID = id.RoomID(mxid.String) portal.MXID = id.RoomID(mxid.String)
portal.OutgoingID = selfUserID.String portal.OutgoingID = selfUserID.String
portal.OtherUserID = otherUserID.String portal.OtherUserID = otherUserID.String
@ -112,13 +117,13 @@ func (portal *Portal) sqlVariables() []any {
if portal.OtherUserID != "" { if portal.OtherUserID != "" {
otherUserID = &portal.OtherUserID otherUserID = &portal.OtherUserID
} }
return []any{portal.ID, portal.Receiver, selfUserID, otherUserID, mxid, portal.Name, portal.NameSet, portal.AvatarID, &portal.AvatarMXC, portal.AvatarSet, portal.Encrypted, portal.InSpace} return []any{portal.ID, portal.Receiver, selfUserID, otherUserID, int(portal.Type), mxid, portal.Name, portal.NameSet, portal.AvatarID, &portal.AvatarMXC, portal.AvatarSet, portal.Encrypted, portal.InSpace}
} }
func (portal *Portal) Insert(ctx context.Context) error { func (portal *Portal) Insert(ctx context.Context) error {
_, err := portal.db.Conn(ctx).ExecContext(ctx, ` _, err := portal.db.Conn(ctx).ExecContext(ctx, `
INSERT INTO portal (id, receiver, self_user, other_user, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space) INSERT INTO portal (id, receiver, self_user, other_user, type, mxid, name, name_set, avatar_id, avatar_mxc, avatar_set, encrypted, in_space)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
`, portal.sqlVariables()...) `, portal.sqlVariables()...)
return err return err
} }
@ -126,7 +131,7 @@ func (portal *Portal) Insert(ctx context.Context) error {
func (portal *Portal) Update(ctx context.Context) error { func (portal *Portal) Update(ctx context.Context) error {
_, err := portal.db.Conn(ctx).ExecContext(ctx, ` _, err := portal.db.Conn(ctx).ExecContext(ctx, `
UPDATE portal UPDATE portal
SET self_user=$3, other_user=$4, mxid=$5, name=$6, name_set=$7, avatar_id=$8, avatar_mxc=$9, avatar_set=$10, encrypted=$11, in_space=$12 SET self_user=$3, other_user=$4, type=$5, mxid=$6, name=$7, name_set=$8, avatar_id=$9, avatar_mxc=$10, avatar_set=$11, encrypted=$12, in_space=$13
WHERE id=$1 AND receiver=$2 WHERE id=$1 AND receiver=$2
`, portal.sqlVariables()...) `, portal.sqlVariables()...)
return err return err

View file

@ -1,4 +1,4 @@
-- v0 -> v2: Latest revision -- v0 -> v3: Latest revision
CREATE TABLE "user" ( CREATE TABLE "user" (
-- only: postgres -- only: postgres
@ -39,6 +39,7 @@ CREATE TABLE portal (
receiver BIGINT NOT NULL, receiver BIGINT NOT NULL,
self_user TEXT, self_user TEXT,
other_user TEXT, other_user TEXT,
type INTEGER NOT NULL,
mxid TEXT UNIQUE, mxid TEXT UNIQUE,
name TEXT NOT NULL, name TEXT NOT NULL,
name_set BOOLEAN NOT NULL DEFAULT false, name_set BOOLEAN NOT NULL DEFAULT false,

View file

@ -0,0 +1,2 @@
-- v3: Store portal conversation type
ALTER TABLE portal ADD COLUMN type INTEGER NOT NULL DEFAULT 0;

View file

@ -790,7 +790,19 @@ func (portal *Portal) UpdateName(name string, updateInfo bool) bool {
func (portal *Portal) UpdateMetadata(user *User, info *gmproto.Conversation) []id.UserID { func (portal *Portal) UpdateMetadata(user *User, info *gmproto.Conversation) []id.UserID {
participants, update := portal.SyncParticipants(user, info) participants, update := portal.SyncParticipants(user, info)
if portal.Type != info.Type {
portal.zlog.Debug().
Str("old_type", portal.Type.String()).
Str("new_type", info.Type.String()).
Msg("Conversation type changed")
portal.Type = info.Type
update = true
}
if portal.OutgoingID != info.DefaultOutgoingID { if portal.OutgoingID != info.DefaultOutgoingID {
portal.zlog.Debug().
Str("old_id", portal.OutgoingID).
Str("new_id", info.DefaultOutgoingID).
Msg("Default outgoing participant ID changed")
portal.OutgoingID = info.DefaultOutgoingID portal.OutgoingID = info.DefaultOutgoingID
update = true update = true
} }
@ -888,7 +900,7 @@ func (portal *Portal) getBridgeInfoStateKey() string {
} }
func (portal *Portal) getBridgeInfo() (string, event.BridgeEventContent) { func (portal *Portal) getBridgeInfo() (string, event.BridgeEventContent) {
return portal.getBridgeInfoStateKey(), event.BridgeEventContent{ content := event.BridgeEventContent{
BridgeBot: portal.bridge.Bot.UserID, BridgeBot: portal.bridge.Bot.UserID,
Creator: portal.MainIntent().UserID, Creator: portal.MainIntent().UserID,
Protocol: event.BridgeInfoSection{ Protocol: event.BridgeInfoSection{
@ -903,6 +915,14 @@ func (portal *Portal) getBridgeInfo() (string, event.BridgeEventContent) {
AvatarURL: portal.AvatarMXC.CUString(), AvatarURL: portal.AvatarMXC.CUString(),
}, },
} }
if portal.Type == gmproto.ConversationType_SMS {
content.Protocol.ID = "gmessages-sms"
content.Protocol.DisplayName = "Google Messages (SMS)"
} else if portal.Type == gmproto.ConversationType_RCS {
content.Protocol.ID = "gmessages-rcs"
content.Protocol.DisplayName = "Google Messages (RCS)"
}
return portal.getBridgeInfoStateKey(), content
} }
func (portal *Portal) UpdateBridgeInfo() { func (portal *Portal) UpdateBridgeInfo() {