This commit is contained in:
Karmanyaah Malhotra 2021-04-29 00:16:19 -04:00
parent 0ff534c2df
commit e2daccc250
6 changed files with 46 additions and 20 deletions

View File

@ -867,10 +867,10 @@ func (handler *CommandHandler) CommandOpen(ce *CommandEvent) {
handler.log.Debugln("Importing", jid, "for", user.MXID) handler.log.Debugln("Importing", jid, "for", user.MXID)
portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid)) portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid))
if len(portal.MXID) > 0 { if len(portal.MXID) > 0 {
portal.Sync(user, contact) portal.Sync(user, &contact)
ce.Reply("Portal room synced.") ce.Reply("Portal room synced.")
} else { } else {
portal.Sync(user, contact) portal.Sync(user, &contact)
ce.Reply("Portal room created.") ce.Reply("Portal room created.")
} }
_, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID}) _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID})

View File

@ -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) { func (store *SQLStateStore) TryGetMember(roomID id.RoomID, userID id.UserID) (*event.MemberEventContent, bool) {
var user MxUserProfile 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 { 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) 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) { 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 { if ans.Error == gorm.ErrRecordNotFound {
err = true err = true
return return
} } else if ans.Error != nil {
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) store.log.Warnfln("Failed to scan member info of %s in %s: %v", userID, roomID, ans.Error)
err = true err = true
return return

View File

@ -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) { func (c Client) IndexAllChats() ([]*groupme.Chat, error) {
return c.IndexChats(context.TODO(), &groupme.IndexChatsQuery{ return c.IndexChats(context.TODO(), &groupme.IndexChatsQuery{
PerPage: 100, //TODO? PerPage: 100, //TODO?

View File

@ -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) portal.log.Infoln("Syncing portal for", user.MXID)
if user.IsRelaybot { if user.IsRelaybot {
@ -548,12 +548,11 @@ func (portal *Portal) Sync(user *User, group groupme.Group) {
portal.hasRelaybot = &yes portal.hasRelaybot = &yes
} }
var err error sub := user.Conn.SubscribeToGroup
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
err = user.Conn.SubscribeToUser(context.TODO(), groupme.ID(portal.Key.JID), user.Token) sub = user.Conn.SubscribeToDM
} else {
err = user.Conn.SubscribeToGroup(context.TODO(), groupme.ID(portal.Key.JID), user.Token)
} }
err := sub(context.TODO(), groupme.ID(portal.Key.Receiver), user.Token)
if err != nil { if err != nil {
portal.log.Errorln("Subscribing failed, live metadata updates won't work", err) 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 var metadata *groupme.Group
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) 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 { if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
portal.Name = m.DisplayName portal.Name = meta.DisplayName
portal.AvatarURL = types.ContentURI{id.MustParseContentURI(m.AvatarURL)} portal.AvatarURL = types.ContentURI{id.MustParseContentURI(meta.AvatarURL)}
portal.Avatar = m.Avatar portal.Avatar = meta.Avatar
} else { } else {
portal.Name = "" portal.Name = ""
} }
portal.Topic = "WhatsApp private chat" portal.Topic = "GroupMe private chat"
// } else if portal.IsStatusBroadcastRoom() { // } else if portal.IsStatusBroadcastRoom() {
// portal.Name = "WhatsApp Status Broadcast" // portal.Name = "WhatsApp Status Broadcast"
// portal.Topic = "WhatsApp status updates from your contacts" // portal.Topic = "WhatsApp status updates from your contacts"

View File

@ -248,8 +248,8 @@ func (puppet *Puppet) UpdateName(source *User, portalMXID id.RoomID, contact gro
if err == nil { if err == nil {
memberRaw.DisplayName = newName memberRaw.DisplayName = newName
// puppet.NameQuality[portalMXID] = quality // puppet.NameQuality[portalMXID] = quality
puppet.bridge.StateStore.SetMemberRaw(&memberRaw) //TODO handle; maybe .Update() ?
go puppet.updatePortalName() go puppet.updatePortalName()
puppet.Update()
} else { } else {
puppet.log.Warnln("Failed to set display name:", err) puppet.log.Warnln("Failed to set display name:", err)
} }

24
user.go
View File

@ -58,8 +58,9 @@ type User struct {
ConnectionErrors int ConnectionErrors int
CommunityID string CommunityID string
ChatList map[types.GroupMeID]groupme.Chat ChatList map[types.GroupMeID]groupme.Chat
GroupList map[types.GroupMeID]groupme.Group GroupList map[types.GroupMeID]groupme.Group
RelationList map[types.GroupMeID]groupme.User
cleanDisconnection bool cleanDisconnection bool
batteryWarningsSent int batteryWarningsSent int
@ -561,6 +562,23 @@ func (user *User) HandleChatList() {
} }
user.ChatList = dmMap 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.log.Infoln("Chat list received")
user.chatListReceived <- struct{}{} user.chatListReceived <- struct{}{}
go user.syncPortals(false) go user.syncPortals(false)
@ -627,7 +645,7 @@ func (user *User) syncPortals(createAll bool) {
go func(chat Chat) { go func(chat Chat) {
create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit
if len(chat.Portal.MXID) > 0 || create || createAll { 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) err := chat.Portal.BackfillHistory(user, chat.LastMessageTime)
if err != nil { if err != nil {
chat.Portal.log.Errorln("Error backfilling history:", err) chat.Portal.log.Errorln("Error backfilling history:", err)