From e2daccc250e35a7f8f02721d6dddb6bce72f2ca6 Mon Sep 17 00:00:00 2001 From: Karmanyaah Malhotra Date: Thu, 29 Apr 2021 00:16:19 -0400 Subject: [PATCH] progress --- commands.go | 4 ++-- database/statestore.go | 7 +++---- groupmeExt/client.go | 4 ++++ portal.go | 25 +++++++++++++++---------- puppet.go | 2 +- user.go | 24 +++++++++++++++++++++--- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/commands.go b/commands.go index 0d9f6f8..d529660 100644 --- a/commands.go +++ b/commands.go @@ -867,10 +867,10 @@ func (handler *CommandHandler) CommandOpen(ce *CommandEvent) { handler.log.Debugln("Importing", jid, "for", user.MXID) portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid)) if len(portal.MXID) > 0 { - portal.Sync(user, contact) + portal.Sync(user, &contact) ce.Reply("Portal room synced.") } else { - portal.Sync(user, contact) + portal.Sync(user, &contact) ce.Reply("Portal room created.") } _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID}) diff --git a/database/statestore.go b/database/statestore.go index 833915a..280c24d 100644 --- a/database/statestore.go +++ b/database/statestore.go @@ -135,7 +135,7 @@ func (store *SQLStateStore) GetMember(roomID id.RoomID, userID id.UserID) *event func (store *SQLStateStore) TryGetMember(roomID id.RoomID, userID id.UserID) (*event.MemberEventContent, bool) { var user MxUserProfile - ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Take(&user) + ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Limit(1).Find(&user) if ans.Error != nil && ans.Error != gorm.ErrRecordNotFound { store.log.Warnfln("Failed to scan member info of %s in %s: %v", userID, roomID, ans.Error) @@ -150,13 +150,12 @@ func (store *SQLStateStore) TryGetMember(roomID id.RoomID, userID id.UserID) (*e } func (store *SQLStateStore) TryGetMemberRaw(roomID id.RoomID, userID id.UserID) (user MxUserProfile, err bool) { - ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Take(&user) + ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Limit(1).Find(&user) if ans.Error == gorm.ErrRecordNotFound { err = true return - } - if ans.Error != nil && ans.Error != gorm.ErrRecordNotFound { + } else if ans.Error != nil { store.log.Warnfln("Failed to scan member info of %s in %s: %v", userID, roomID, ans.Error) err = true return diff --git a/groupmeExt/client.go b/groupmeExt/client.go index e3aab56..45e83c7 100644 --- a/groupmeExt/client.go +++ b/groupmeExt/client.go @@ -25,6 +25,10 @@ func (c Client) IndexAllGroups() ([]*groupme.Group, error) { }) } +func (c Client) IndexAllRelations() ([]*groupme.User, error) { + return c.IndexRelations(context.TODO(), &groupme.IndexChatsQuery{}) +} + func (c Client) IndexAllChats() ([]*groupme.Chat, error) { return c.IndexChats(context.TODO(), &groupme.IndexChatsQuery{ PerPage: 100, //TODO? diff --git a/portal.go b/portal.go index efceb9e..e7c43a5 100644 --- a/portal.go +++ b/portal.go @@ -540,7 +540,7 @@ func (portal *Portal) ensureUserInvited(user *User) { } } -func (portal *Portal) Sync(user *User, group groupme.Group) { +func (portal *Portal) Sync(user *User, group *groupme.Group) { portal.log.Infoln("Syncing portal for", user.MXID) if user.IsRelaybot { @@ -548,12 +548,11 @@ func (portal *Portal) Sync(user *User, group groupme.Group) { portal.hasRelaybot = &yes } - var err error + sub := user.Conn.SubscribeToGroup if portal.IsPrivateChat() { - err = user.Conn.SubscribeToUser(context.TODO(), groupme.ID(portal.Key.JID), user.Token) - } else { - err = user.Conn.SubscribeToGroup(context.TODO(), groupme.ID(portal.Key.JID), user.Token) + sub = user.Conn.SubscribeToDM } + err := sub(context.TODO(), groupme.ID(portal.Key.Receiver), user.Token) if err != nil { portal.log.Errorln("Subscribing failed, live metadata updates won't work", err) } @@ -944,15 +943,21 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { var metadata *groupme.Group if portal.IsPrivateChat() { puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) - m, _ := portal.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) + meta, err := portal.bridge.StateStore.TryGetMemberRaw("", puppet.MXID) + if err { + println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") + return errors.New("Cannot find user information") + } + + //m, _ := portal.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) if portal.bridge.Config.Bridge.PrivateChatPortalMeta { - portal.Name = m.DisplayName - portal.AvatarURL = types.ContentURI{id.MustParseContentURI(m.AvatarURL)} - portal.Avatar = m.Avatar + portal.Name = meta.DisplayName + portal.AvatarURL = types.ContentURI{id.MustParseContentURI(meta.AvatarURL)} + portal.Avatar = meta.Avatar } else { portal.Name = "" } - portal.Topic = "WhatsApp private chat" + portal.Topic = "GroupMe private chat" // } else if portal.IsStatusBroadcastRoom() { // portal.Name = "WhatsApp Status Broadcast" // portal.Topic = "WhatsApp status updates from your contacts" diff --git a/puppet.go b/puppet.go index c134980..ed56a9f 100644 --- a/puppet.go +++ b/puppet.go @@ -248,8 +248,8 @@ func (puppet *Puppet) UpdateName(source *User, portalMXID id.RoomID, contact gro if err == nil { memberRaw.DisplayName = newName // puppet.NameQuality[portalMXID] = quality + puppet.bridge.StateStore.SetMemberRaw(&memberRaw) //TODO handle; maybe .Update() ? go puppet.updatePortalName() - puppet.Update() } else { puppet.log.Warnln("Failed to set display name:", err) } diff --git a/user.go b/user.go index f41af4f..cba398c 100644 --- a/user.go +++ b/user.go @@ -58,8 +58,9 @@ type User struct { ConnectionErrors int CommunityID string - ChatList map[types.GroupMeID]groupme.Chat - GroupList map[types.GroupMeID]groupme.Group + ChatList map[types.GroupMeID]groupme.Chat + GroupList map[types.GroupMeID]groupme.Group + RelationList map[types.GroupMeID]groupme.User cleanDisconnection bool batteryWarningsSent int @@ -561,6 +562,23 @@ func (user *User) HandleChatList() { } user.ChatList = dmMap + userMap := make(map[string]groupme.User) + users, err := user.Client.IndexAllRelations() + if err != nil { + user.log.Errorln("Error syncing user list, continuing sync", err) + } + for _, u := range users { + puppet := user.bridge.GetPuppetByJID(u.ID.String()) + // "" for overall user not related to one group + puppet.Sync(nil, "", groupme.Member{ + UserID: u.ID, + Nickname: u.Name, + ImageURL: u.AvatarURL, + }) + userMap[u.ID.String()] = *u + } + user.RelationList = userMap + user.log.Infoln("Chat list received") user.chatListReceived <- struct{}{} go user.syncPortals(false) @@ -627,7 +645,7 @@ func (user *User) syncPortals(createAll bool) { go func(chat Chat) { create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit if len(chat.Portal.MXID) > 0 || create || createAll { - chat.Portal.Sync(user, *chat.Group) + chat.Portal.Sync(user, chat.Group) err := chat.Portal.BackfillHistory(user, chat.LastMessageTime) if err != nil { chat.Portal.log.Errorln("Error backfilling history:", err)