Sync room metadata on start or sync

This commit is contained in:
Karmanyaah Malhotra 2021-03-03 17:09:57 -05:00
parent d8bff50005
commit 1636f92acd
2 changed files with 73 additions and 52 deletions

108
portal.go
View File

@ -417,48 +417,68 @@ func (portal *Portal) SyncParticipants(metadata *groupme.Group) {
} }
func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) bool { func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) bool {
// if avatar == nil { // if len(avatar) == 0 {
// var err error // var err error
// avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID) // avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID)
// if err != nil { // if err != nil {
// portal.log.Errorln(err) // portal.log.Errorln(err)
// return false // return false
// } // }
// } // }
//TODO: duplicated code from puppet.UpdateAvatar
if len(avatar) == 0 {
if len(portal.Avatar) == 0 {
return false
}
err := portal.MainIntent().SetAvatarURL(id.ContentURI{})
if err != nil {
portal.log.Warnln("Failed to remove avatar:", err)
}
portal.AvatarURL = types.ContentURI{}
portal.Avatar = avatar
return true
}
// if avatar.Status != 0 { if portal.Avatar == avatar {
// return false return false
// } }
// if portal.Avatar == avatar.Tag { //TODO check its actually groupme?
// return false response, err := http.Get(avatar + ".large")
// } if err != nil {
portal.log.Warnln("Failed to download avatar:", err)
return false
}
defer response.Body.Close()
// data, err := avatar.DownloadBytes() image, err := ioutil.ReadAll(response.Body)
// if err != nil { if err != nil {
// portal.log.Warnln("Failed to download avatar:", err) portal.log.Warnln("Failed to read downloaded avatar:", err)
// return false return false
// } }
// mimeType := http.DetectContentType(data) mime := response.Header.Get("Content-Type")
// resp, err := portal.MainIntent().UploadBytes(data, mimeType) if len(mime) == 0 {
// if err != nil { mime = http.DetectContentType(image)
// portal.log.Warnln("Failed to upload avatar:", err) }
// return false resp, err := portal.MainIntent().UploadBytes(image, mime)
// } if err != nil {
portal.log.Warnln("Failed to upload avatar:", err)
return false
}
// portal.AvatarURL = resp.ContentURI portal.AvatarURL = types.ContentURI{resp.ContentURI}
// if len(portal.MXID) > 0 { if len(portal.MXID) > 0 {
// _, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI) _, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI)
// if err != nil { if err != nil {
// portal.log.Warnln("Failed to set room topic:", err) portal.log.Warnln("Failed to set room topic:", err)
// return false return false
// } }
// } }
// portal.Avatar = avatar.Tag portal.Avatar = avatar
// if updateInfo { if updateInfo {
// portal.UpdateBridgeInfo() portal.UpdateBridgeInfo()
// } }
return true return true
} }
@ -503,11 +523,6 @@ func (portal *Portal) UpdateTopic(topic string, setBy types.GroupMeID, updateInf
func (portal *Portal) UpdateMetadata(user *User) bool { func (portal *Portal) UpdateMetadata(user *User) bool {
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
return false return false
} else if portal.IsStatusBroadcastRoom() {
update := false
update = portal.UpdateName("WhatsApp Status Broadcast", "", false) || update
update = portal.UpdateTopic("WhatsApp status updates from your contacts", "", false) || update
return update
} }
group, err := user.Client.ShowGroup(context.TODO(), groupme.ID(strings.Replace(portal.Key.JID, groupmeExt.NewUserSuffix, "", 1))) group, err := user.Client.ShowGroup(context.TODO(), groupme.ID(strings.Replace(portal.Key.JID, groupmeExt.NewUserSuffix, "", 1)))
if err != nil { if err != nil {
@ -521,7 +536,7 @@ func (portal *Portal) UpdateMetadata(user *User) bool {
// TODO: update the room, e.g. change priority level // TODO: update the room, e.g. change priority level
// to send messages to moderator // to send messages to moderator
return false //return false
// } // }
portal.SyncParticipants(group) portal.SyncParticipants(group)
@ -591,9 +606,8 @@ func (portal *Portal) Sync(user *User, group groupme.Group) {
update := false update := false
update = portal.UpdateMetadata(user) || update update = portal.UpdateMetadata(user) || update
if !portal.IsStatusBroadcastRoom() { update = portal.UpdateAvatar(user, group.ImageURL, false) || update
update = portal.UpdateAvatar(user, "", false) || update
}
if update { if update {
portal.Update() portal.Update()
portal.UpdateBridgeInfo() portal.UpdateBridgeInfo()
@ -977,7 +991,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
portal.Name = metadata.Name portal.Name = metadata.Name
portal.Topic = metadata.Description portal.Topic = metadata.Description
} }
portal.UpdateAvatar(user, "", false) portal.UpdateAvatar(user, metadata.ImageURL, false)
} }
bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo() bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo()

17
user.go
View File

@ -587,21 +587,28 @@ func (user *User) syncPortals(chatMap map[string]groupme.Group, createAll bool)
} }
now := uint64(time.Now().Unix()) now := uint64(time.Now().Unix())
user.log.Infoln("Syncing portals") user.log.Infoln("Syncing portals")
wg := sync.WaitGroup{}
for i, chat := range chats { for i, chat := range chats {
if chat.LastMessageTime+user.bridge.Config.Bridge.SyncChatMaxAge < now { if chat.LastMessageTime+user.bridge.Config.Bridge.SyncChatMaxAge < now {
break break
} }
create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit go func(chat Chat) {
if len(chat.Portal.MXID) > 0 || create || createAll { wg.Add(1)
go func() { 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) 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)
} }
}() }
}
wg.Done()
}(chat)
} }
wg.Wait()
//TODO: handle leave from groupme side //TODO: handle leave from groupme side
user.UpdateDirectChats(nil) user.UpdateDirectChats(nil)
user.log.Infoln("Finished syncing portals") user.log.Infoln("Finished syncing portals")