From 95fed6ad498cede1520cc4ac909e1e3e5d4f2be4 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 22 Jul 2023 19:21:29 +0300 Subject: [PATCH] Expose conversation type in m.bridge info --- database/portal.go | 23 ++++++++++++++--------- database/upgrades/00-latest-revision.sql | 3 ++- database/upgrades/03-portal-type.sql | 2 ++ portal.go | 22 +++++++++++++++++++++- 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 database/upgrades/03-portal-type.sql diff --git a/database/portal.go b/database/portal.go index 7b8b978..4542884 100644 --- a/database/portal.go +++ b/database/portal.go @@ -25,6 +25,8 @@ import ( "github.com/rs/zerolog" "maunium.net/go/mautrix/id" "maunium.net/go/mautrix/util/dbutil" + + "go.mau.fi/mautrix-gmessages/libgm/gmproto" ) type PortalQuery struct { @@ -42,19 +44,19 @@ func (pq *PortalQuery) getDB() *Database { } 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) { - 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) { - 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) { - 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 { @@ -78,6 +80,7 @@ type Portal struct { OtherUserID string MXID id.RoomID + Type gmproto.ConversationType Name string NameSet bool AvatarID string @@ -89,12 +92,14 @@ type Portal struct { func (portal *Portal) Scan(row dbutil.Scannable) (*Portal, error) { 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) { return nil, nil } else if err != nil { return nil, err } + portal.Type = gmproto.ConversationType(convType) portal.MXID = id.RoomID(mxid.String) portal.OutgoingID = selfUserID.String portal.OtherUserID = otherUserID.String @@ -112,13 +117,13 @@ func (portal *Portal) sqlVariables() []any { if 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 { _, 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) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) + 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, $13) `, portal.sqlVariables()...) return err } @@ -126,7 +131,7 @@ func (portal *Portal) Insert(ctx context.Context) error { func (portal *Portal) Update(ctx context.Context) error { _, err := portal.db.Conn(ctx).ExecContext(ctx, ` 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 `, portal.sqlVariables()...) return err diff --git a/database/upgrades/00-latest-revision.sql b/database/upgrades/00-latest-revision.sql index c88c537..b4f43fd 100644 --- a/database/upgrades/00-latest-revision.sql +++ b/database/upgrades/00-latest-revision.sql @@ -1,4 +1,4 @@ --- v0 -> v2: Latest revision +-- v0 -> v3: Latest revision CREATE TABLE "user" ( -- only: postgres @@ -39,6 +39,7 @@ CREATE TABLE portal ( receiver BIGINT NOT NULL, self_user TEXT, other_user TEXT, + type INTEGER NOT NULL, mxid TEXT UNIQUE, name TEXT NOT NULL, name_set BOOLEAN NOT NULL DEFAULT false, diff --git a/database/upgrades/03-portal-type.sql b/database/upgrades/03-portal-type.sql new file mode 100644 index 0000000..cc1d80d --- /dev/null +++ b/database/upgrades/03-portal-type.sql @@ -0,0 +1,2 @@ +-- v3: Store portal conversation type +ALTER TABLE portal ADD COLUMN type INTEGER NOT NULL DEFAULT 0; diff --git a/portal.go b/portal.go index 17fee41..d6f745c 100644 --- a/portal.go +++ b/portal.go @@ -790,7 +790,19 @@ func (portal *Portal) UpdateName(name string, updateInfo bool) bool { func (portal *Portal) UpdateMetadata(user *User, info *gmproto.Conversation) []id.UserID { 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 { + portal.zlog.Debug(). + Str("old_id", portal.OutgoingID). + Str("new_id", info.DefaultOutgoingID). + Msg("Default outgoing participant ID changed") portal.OutgoingID = info.DefaultOutgoingID update = true } @@ -888,7 +900,7 @@ func (portal *Portal) getBridgeInfoStateKey() string { } func (portal *Portal) getBridgeInfo() (string, event.BridgeEventContent) { - return portal.getBridgeInfoStateKey(), event.BridgeEventContent{ + content := event.BridgeEventContent{ BridgeBot: portal.bridge.Bot.UserID, Creator: portal.MainIntent().UserID, Protocol: event.BridgeInfoSection{ @@ -903,6 +915,14 @@ func (portal *Portal) getBridgeInfo() (string, event.BridgeEventContent) { 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() {