From 96557c99739477d74fa5a59ccf83e570b059464f Mon Sep 17 00:00:00 2001 From: Karmanyaah Malhotra Date: Sat, 1 May 2021 18:22:02 -0400 Subject: [PATCH] dms kind work now? --- database/portal.go | 30 ++++++++++++--------- groupmeExt/client.go | 8 ++++-- matrix.go | 2 ++ portal.go | 62 +++++++++++++++++++++++++------------------- user.go | 26 +++++++------------ 5 files changed, 71 insertions(+), 57 deletions(-) diff --git a/database/portal.go b/database/portal.go index 54a89c5..dcaf13b 100644 --- a/database/portal.go +++ b/database/portal.go @@ -37,19 +37,25 @@ type PortalKey struct { func ParsePortalKey(inp types.GroupMeID) *PortalKey { parts := strings.Split(inp, "+") - if len(parts) != 2 { - return nil - } - if i, err := strconv.Atoi(parts[0]); i == 0 || err != nil { - return nil - } - if i, err := strconv.Atoi(parts[1]); i == 0 || err != nil { - return nil - } + if len(parts) == 1 { + if i, err := strconv.Atoi(inp); i == 0 || err != nil { + return nil + } + return &PortalKey{inp, inp} + } else if len(parts) == 2 { + if i, err := strconv.Atoi(parts[0]); i == 0 || err != nil { + return nil + } + if i, err := strconv.Atoi(parts[1]); i == 0 || err != nil { + return nil + } - return &PortalKey{ - JID: parts[0], - Receiver: parts[1], + return &PortalKey{ + JID: parts[1], + Receiver: parts[0], + } + } else { + return nil } } diff --git a/groupmeExt/client.go b/groupmeExt/client.go index 45e83c7..c7aef86 100644 --- a/groupmeExt/client.go +++ b/groupmeExt/client.go @@ -37,7 +37,7 @@ func (c Client) IndexAllChats() ([]*groupme.Chat, error) { func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFromMe bool, private bool) ([]*groupme.Message, error) { if private { - i, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{ + ans, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{ SinceID: groupme.ID(lastMessageID), //Limit: num, }) @@ -45,7 +45,11 @@ func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFrom if e != nil { return nil, e } - return i.Messages, nil + + for i, j := 0, len(ans.Messages)-1; i < j; i, j = i+1, j-1 { + ans.Messages[i], ans.Messages[j] = ans.Messages[j], ans.Messages[i] + } + return ans.Messages, nil } else { i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{ AfterID: groupme.ID(lastMessageID), diff --git a/matrix.go b/matrix.go index 5526291..be20b14 100644 --- a/matrix.go +++ b/matrix.go @@ -165,6 +165,8 @@ func (mx *MatrixHandler) handlePrivatePortal(roomID id.RoomID, inviter *User, pu func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) { portal.MXID = roomID portal.Topic = "WhatsApp private chat" + portal.Key = database.PortalKey{puppet.JID, inviter.JID} + _, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic) if portal.bridge.Config.Bridge.PrivateChatPortalMeta { m, _ := mx.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) diff --git a/portal.go b/portal.go index 6e6b995..126d72d 100644 --- a/portal.go +++ b/portal.go @@ -159,8 +159,7 @@ func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal { const recentlyHandledLength = 100 type PortalMessage struct { - chat string - group bool + chat database.PortalKey source *User data *groupme.Message timestamp uint64 @@ -270,17 +269,15 @@ func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid i } func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI { - if info.UserID.String() == user.GetJID() { //from me - return portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal) //TODO why is this - } else if portal.IsPrivateChat() { + if portal.IsPrivateChat() { + if info.UserID.String() == user.GetJID() { //from me + return portal.bridge.GetPuppetByJID(user.JID).DefaultIntent() + } return portal.MainIntent() } else if len(info.UserID.String()) == 0 { - // if len(info.Source.GetParticipant()) != 0 { - // info.SenderJid = info.Source.GetParticipant() - // } else { - // return nil - // } println("TODO weird uid stuff") + } else if info.UserID.String() == user.GetJID() { //from me + return portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal) } return portal.bridge.GetPuppetByJID(info.UserID.String()).IntentFor(portal) } @@ -305,7 +302,7 @@ func (portal *Portal) startHandling(source *User, info *groupme.Message) *appser if intent != nil { portal.log.Debugfln("Starting handling of %s (ts: %d)", info.ID, info.CreatedAt) } else { - portal.log.Debugfln("Not handling %s: sender is not known") + portal.log.Debugfln("Not handling %s: sender is not known", info.ID.String()) } return intent } @@ -742,7 +739,8 @@ func (portal *Portal) beginBackfill() func() { portal.privateChatBackfillInvitePuppet = nil portal.backfillLock.Unlock() if privateChatPuppet != nil && privateChatPuppetInvited { - _, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID) + //_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID) + //why this shouldn't really happen NOTE } } } @@ -865,7 +863,7 @@ func (portal *Portal) handleHistory(user *User, messages []*groupme.Message) { if portal.privateChatBackfillInvitePuppet != nil && message.UserID.String() == user.JID && portal.IsPrivateChat() { portal.privateChatBackfillInvitePuppet() } - portal.handleMessage(PortalMessage{portal.Key.JID, portal.Key.JID == portal.Key.Receiver, user, message, uint64(message.CreatedAt.ToTime().Unix())}) + portal.handleMessage(PortalMessage{portal.Key, user, message, uint64(message.CreatedAt.ToTime().Unix())}) } } @@ -941,7 +939,6 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { portal.log.Infoln("Creating Matrix room. Info source:", user.MXID) var metadata *groupme.Group - fmt.Println(portal.IsPrivateChat(), portal.Key, portal.Key.Receiver, portal.Key.JID) return nil if portal.IsPrivateChat() { puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) @@ -1030,6 +1027,8 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { }) if err != nil { return err + } else if len(resp.RoomID) == 0 { + return errors.New("Empty room ID") } portal.MXID = resp.RoomID portal.Update() @@ -2142,8 +2141,10 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) ([]*g // } // info := groupme.Message{ - SourceGUID: evt.ID.String(), //TODO Figure out for multiple messages - GroupID: groupme.ID(portal.Key.JID), + GroupID: groupme.ID(portal.Key.String()), + ConversationID: groupme.ID(portal.Key.String()), + ChatID: groupme.ID(portal.Key.String()), + RecipientID: groupme.ID(portal.Key.JID), } replyToID := content.GetReplyTo() if len(replyToID) > 0 { @@ -2318,10 +2319,10 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { return } for _, i := range info { - portal.log.Debugln("Sending event", evt.ID, "to WhatsApp", info[0].ID) + portal.log.Debugln("Sending event", evt.ID, "to GroupMe", info[0].ID) var err error - i, err = portal.sendRaw(sender, evt, info[0], false) //TODO deal with multiple messages for longer messages + i, err = portal.sendRaw(sender, evt, info[0], -1) //TODO deal with multiple messages for longer messages if err != nil { portal.log.Warnln("Unable to handle message from Matrix", evt.ID) //TODO handle deleted room and such @@ -2333,21 +2334,30 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) { } -func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *groupme.Message, isRetry bool) (*groupme.Message, error) { +func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *groupme.Message, retries int) (*groupme.Message, error) { + if retries == -1 { + retries = 2 + } + + var m *groupme.Message + var err error + + if portal.IsPrivateChat() { + m, err = sender.Client.CreateDirectMessage(context.TODO(), info) + } else { + m, err = sender.Client.CreateMessage(context.TODO(), info.GroupID, info) + } - m, err := sender.Client.CreateMessage(context.TODO(), info.GroupID, info) id := "" if m != nil { id = m.ID.String() } if err != nil { portal.log.Warnln(err, id, info.GroupID.String()) - } - if isRetry && err != nil { - m, err = sender.Client.CreateMessage(context.TODO(), info.GroupID, info) - } - if err != nil { - return nil, err + + if retries > 0 { + return portal.sendRaw(sender, evt, info, retries-1) + } } return m, nil // errChan := make(chan error, 1) diff --git a/user.go b/user.go index 597f083..ebae6e0 100644 --- a/user.go +++ b/user.go @@ -864,7 +864,7 @@ func (user *User) handleMessageLoop() { case msg := <-user.messageOutput: user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput)) puppet := user.bridge.GetPuppetByJID(msg.data.UserID.String()) - portal := user.bridge.GetPortalByJID(database.GroupPortalKey(msg.chat)) + portal := user.bridge.GetPortalByJID(msg.chat) if puppet != nil { puppet.Sync(user, portal.MXID, groupme.Member{ UserID: msg.data.UserID, @@ -912,25 +912,17 @@ func (user *User) handleMessageLoop() { //} func (user *User) HandleTextMessage(message groupme.Message) { - var group bool - var id string + id := database.ParsePortalKey(message.GroupID.String()) - if message.GroupID.String() != "" { - group = true - id = message.GroupID.String() - } else if message.ConversationID.String() != "" { - group = false - pk := database.ParsePortalKey(message.ConversationID.String()) - if pk == nil { - user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message") - return - } - id = pk.JID - } else { - user.log.Errorln("Message received without conversation or groupid") + if id == nil { + id = database.ParsePortalKey(message.ConversationID.String()) + } + if id == nil { + user.log.Errorln("Error parsing conversationid/portalkey", message.ConversationID.String(), "ignoring message") return } - user.messageInput <- PortalMessage{id, group, user, &message, uint64(message.CreatedAt.ToTime().Unix())} + + user.messageInput <- PortalMessage{*id, user, &message, uint64(message.CreatedAt.ToTime().Unix())} } func (user *User) HandleLike(msg groupme.Message) {