From d501ce0578dec93cc3f5bcb6826f0bdc2ba7c930 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 10 Jun 2024 22:15:10 +0300 Subject: [PATCH] Add portal receiver to DMs --- go.mod | 2 +- go.sum | 4 ++-- pkg/connector/connector.go | 45 +++++++++----------------------------- 3 files changed, 13 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 7d766d8a..c602f496 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( golang.org/x/net v0.25.0 google.golang.org/protobuf v1.34.1 gopkg.in/yaml.v3 v3.0.1 - maunium.net/go/mautrix v0.18.2-0.20240607095501-6466bf945260 + maunium.net/go/mautrix v0.18.2-0.20240610191402-39ce0103d40b nhooyr.io/websocket v1.8.11 ) diff --git a/go.sum b/go.sum index 78957fa7..f67f9ad4 100644 --- a/go.sum +++ b/go.sum @@ -95,7 +95,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= -maunium.net/go/mautrix v0.18.2-0.20240607095501-6466bf945260 h1:PRjq8wEtssdiUhwVpo9h2LAlVEB04r3y/YR2XZsdlbg= -maunium.net/go/mautrix v0.18.2-0.20240607095501-6466bf945260/go.mod h1:P/FV8cXY262MezYX7ViuhfzeJ0nK4+M8K6ZmxEC/aEA= +maunium.net/go/mautrix v0.18.2-0.20240610191402-39ce0103d40b h1:sEwWJqdshbCZFGlvj6j3IPxBDznQLHG79GZdrMCEZvk= +maunium.net/go/mautrix v0.18.2-0.20240610191402-39ce0103d40b/go.mod h1:P/FV8cXY262MezYX7ViuhfzeJ0nK4+M8K6ZmxEC/aEA= nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0= nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index 69ec6b35..180a30e9 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -28,7 +28,6 @@ import ( "github.com/rs/zerolog" "go.mau.fi/util/dbutil" "go.mau.fi/util/variationselector" - "golang.org/x/exp/slices" "google.golang.org/protobuf/proto" "maunium.net/go/mautrix/bridgev2" @@ -345,43 +344,14 @@ func parseUserID(userID networkid.UserID) (uuid.UUID, error) { } func (s *SignalClient) parsePortalID(portalID networkid.PortalID) (userID libsignalgo.ServiceID, groupID types.GroupIdentifier, err error) { - parts := strings.Split(string(portalID), "|") - if len(parts) == 1 { - if len(parts[0]) == 44 { - groupID = types.GroupIdentifier(parts[0]) - } else { - err = fmt.Errorf("invalid portal ID: expected group ID to be 44 characters") - } - } else if len(parts) == 2 { - ourACI := s.Client.Store.ACI.String() - if parts[0] == ourACI { - userID, err = libsignalgo.ServiceIDFromString(parts[1]) - } else if parts[1] == ourACI { - userID, err = libsignalgo.ServiceIDFromString(parts[0]) - } else { - err = fmt.Errorf("invalid portal ID: expected one side to be our ACI") - } + if len(portalID) == 44 { + groupID = types.GroupIdentifier(portalID) } else { - err = fmt.Errorf("invalid portal ID: unexpected number of pipe-separated parts") + userID, err = libsignalgo.ServiceIDFromString(string(portalID)) } return } -func (s *SignalClient) getPortalID(chatID string) networkid.PortalID { - if len(chatID) == 44 { - // Group ID - return networkid.PortalID(chatID) - } else if strings.HasPrefix(chatID, "PNI:") { - // Temporary new DM ID: always put our own ACI first, the portal will never be shared anyway - return networkid.PortalID(fmt.Sprintf("%s|%s", s.Client.Store.ACI, chatID)) - } else { - // DM ID: sort the two parts so the ID is always the same regardless of which side is receiving the message - parts := []string{s.Client.Store.ACI.String(), chatID} - slices.Sort(parts) - return networkid.PortalID(strings.Join(parts, "|")) - } -} - func parseMessageID(messageID networkid.MessageID) (sender uuid.UUID, timestamp uint64, err error) { parts := strings.Split(string(messageID), "|") if len(parts) != 2 { @@ -471,8 +441,13 @@ func (evt *Bv2ChatEvent) GetType() bridgev2.RemoteEventType { return bridgev2.RemoteEventUnknown } -func (evt *Bv2ChatEvent) GetPortalID() networkid.PortalID { - return evt.s.getPortalID(evt.Info.ChatID) +func (evt *Bv2ChatEvent) GetPortalKey() networkid.PortalKey { + key := networkid.PortalKey{ID: networkid.PortalID(evt.Info.ChatID)} + // For non-group chats, add receiver + if len(evt.Info.ChatID) != 44 { + key.Receiver = makeUserLoginID(evt.s.Client.Store.ACI) + } + return key } func (evt *Bv2ChatEvent) ShouldCreatePortal() bool {