Add support for setting group avatar from Matrix

This commit is contained in:
Tulir Asokan 2022-02-10 12:46:25 +02:00
parent 663abeeb04
commit 94e880c2cc
5 changed files with 44 additions and 16 deletions

View file

@ -2,6 +2,7 @@
* Added tracking for incoming events from the primary device to warn the user * Added tracking for incoming events from the primary device to warn the user
if their phone is offline for too long. if their phone is offline for too long.
* (Re-)Added support for setting group avatar from Matrix.
* Fixed some issues with read receipt bridging * Fixed some issues with read receipt bridging
* Fixed `!wa open` not working with new-style group IDs. * Fixed `!wa open` not working with new-style group IDs.
* Fixed panic in disappearing message handling code if a portal is deleted with * Fixed panic in disappearing message handling code if a portal is deleted with

View file

@ -15,9 +15,9 @@
* [x] Invite * [x] Invite
* [x] Leave * [x] Leave
* [x] Kick * [x] Kick
* [ ] Room metadata changes * [x] Room metadata changes
* [x] Name * [x] Name
* [ ] Avatar * [x] Avatar
* [x] Topic * [x] Topic
* [ ] Initial room metadata * [ ] Initial room metadata
* WhatsApp → Matrix * WhatsApp → Matrix

2
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/prometheus/client_golang v1.11.0 github.com/prometheus/client_golang v1.11.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/tidwall/gjson v1.13.0 github.com/tidwall/gjson v1.13.0
go.mau.fi/whatsmeow v0.0.0-20220204210537-a425ddb0b16c go.mau.fi/whatsmeow v0.0.0-20220210104450-b05cf0cef136
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 golang.org/x/image v0.0.0-20211028202545-6944b10bf410
google.golang.org/protobuf v1.27.1 google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

4
go.sum
View file

@ -140,8 +140,8 @@ github.com/tidwall/sjson v1.2.4 h1:cuiLzLnaMeBhRmEv00Lpk3tkYrcxpmbU81tAY4Dw0tc=
github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM= github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM=
go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910 h1:9FFhG0OmkuMau5UEaTgiUQ+7cSbtbOQ7hiWKdN8OI3I= go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910 h1:9FFhG0OmkuMau5UEaTgiUQ+7cSbtbOQ7hiWKdN8OI3I=
go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910/go.mod h1:AufGrvVh+00Nc07Jm4hTquh7yleZyn20tKJI2wCPAKg= go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910/go.mod h1:AufGrvVh+00Nc07Jm4hTquh7yleZyn20tKJI2wCPAKg=
go.mau.fi/whatsmeow v0.0.0-20220204210537-a425ddb0b16c h1:hwuZ1W55J2uSwm029dREAr6crSVa+i5VsF91ltK389k= go.mau.fi/whatsmeow v0.0.0-20220210104450-b05cf0cef136 h1:AeE8izwMOwAbzNGC+GGOY821w6EWSpPxVmQcpRgyff4=
go.mau.fi/whatsmeow v0.0.0-20220204210537-a425ddb0b16c/go.mod h1:8jUjOAi3xtGubxcZgG8uSHpAdyQXBRbWAfxkctX/4y4= go.mau.fi/whatsmeow v0.0.0-20220210104450-b05cf0cef136/go.mod h1:8jUjOAi3xtGubxcZgG8uSHpAdyQXBRbWAfxkctX/4y4=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View file

@ -202,6 +202,7 @@ type Portal struct {
roomCreateLock sync.Mutex roomCreateLock sync.Mutex
encryptLock sync.Mutex encryptLock sync.Mutex
backfillLock sync.Mutex backfillLock sync.Mutex
avatarLock sync.Mutex
recentlyHandled [recentlyHandledLength]recentlyHandledWrapper recentlyHandled [recentlyHandledLength]recentlyHandledWrapper
recentlyHandledLock sync.Mutex recentlyHandledLock sync.Mutex
@ -821,6 +822,8 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo)
} }
func (portal *Portal) UpdateAvatar(user *User, setBy types.JID, updateInfo bool) bool { func (portal *Portal) UpdateAvatar(user *User, setBy types.JID, updateInfo bool) bool {
portal.avatarLock.Lock()
defer portal.avatarLock.Unlock()
avatar, err := user.Client.GetProfilePictureInfo(portal.Key.JID, false) avatar, err := user.Client.GetProfilePictureInfo(portal.Key.JID, false)
if err != nil { if err != nil {
if !errors.Is(err, whatsmeow.ErrProfilePictureUnauthorized) { if !errors.Is(err, whatsmeow.ErrProfilePictureUnauthorized) {
@ -2717,28 +2720,52 @@ func (portal *Portal) HandleMatrixInvite(sender *User, target *Puppet) {
} }
func (portal *Portal) HandleMatrixMeta(sender *User, evt *event.Event) { func (portal *Portal) HandleMatrixMeta(sender *User, evt *event.Event) {
var err error
switch content := evt.Content.Parsed.(type) { switch content := evt.Content.Parsed.(type) {
case *event.RoomNameEventContent: case *event.RoomNameEventContent:
if content.Name == portal.Name { if content.Name == portal.Name {
return return
} }
portal.Name = content.Name portal.Name = content.Name
err = sender.Client.SetGroupName(portal.Key.JID, content.Name) err := sender.Client.SetGroupName(portal.Key.JID, content.Name)
if err != nil {
portal.log.Errorln("Failed to update group name:", err)
}
case *event.TopicEventContent: case *event.TopicEventContent:
if content.Topic == portal.Topic { if content.Topic == portal.Topic {
return return
} }
portal.Topic = content.Topic portal.Topic = content.Topic
err = sender.Client.SetGroupTopic(portal.Key.JID, "", "", content.Topic) err := sender.Client.SetGroupTopic(portal.Key.JID, "", "", content.Topic)
if err != nil {
portal.log.Errorln("Failed to update group description:", err)
}
case *event.RoomAvatarEventContent: case *event.RoomAvatarEventContent:
// TODO implement portal.avatarLock.Lock()
return defer portal.avatarLock.Unlock()
} if content.URL == portal.AvatarURL || (content.URL.IsEmpty() && portal.Avatar == "remove") {
if err != nil { return
portal.log.Errorln("Failed to update metadata:", err) }
} else { var data []byte
//out := <-resp var err error
//portal.log.Debugln("Successfully updated metadata:", out) if !content.URL.IsEmpty() {
data, err = portal.MainIntent().DownloadBytes(content.URL)
if err != nil {
portal.log.Errorfln("Failed to download updated avatar %s: %v", content.URL, err)
return
}
portal.log.Debugfln("%s set the group avatar to %s", sender.MXID, content.URL)
} else {
portal.log.Debugfln("%s removed the group avatar", sender.MXID)
}
newID, err := sender.Client.SetGroupPhoto(portal.Key.JID, data)
if err != nil {
portal.log.Errorfln("Failed to update group avatar: %v", err)
return
}
portal.log.Debugfln("Successfully updated group avatar to %s", newID)
portal.Avatar = newID
portal.AvatarURL = content.URL
portal.UpdateBridgeInfo()
portal.Update()
} }
} }