Update bridge info when portal metadata changes

This commit is contained in:
Tulir Asokan 2020-06-15 14:56:52 +03:00
parent b8de8544e1
commit ec86259a20
2 changed files with 62 additions and 33 deletions

View File

@ -342,7 +342,7 @@ func (portal *Portal) SyncParticipants(metadata *whatsappExt.GroupInfo) {
} }
} }
func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInfo) bool { func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInfo, updateInfo bool) bool {
if avatar == nil { if avatar == nil {
var err error var err error
avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID) avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID)
@ -382,10 +382,13 @@ func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInf
} }
} }
portal.Avatar = avatar.Tag portal.Avatar = avatar.Tag
if updateInfo {
portal.UpdateBridgeInfo()
}
return true return true
} }
func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool { func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID, updateInfo bool) bool {
if portal.Name != name { if portal.Name != name {
intent := portal.MainIntent() intent := portal.MainIntent()
if len(setBy) > 0 { if len(setBy) > 0 {
@ -394,6 +397,9 @@ func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
_, err := intent.SetRoomName(portal.MXID, name) _, err := intent.SetRoomName(portal.MXID, name)
if err == nil { if err == nil {
portal.Name = name portal.Name = name
if updateInfo {
portal.UpdateBridgeInfo()
}
return true return true
} }
portal.log.Warnln("Failed to set room name:", err) portal.log.Warnln("Failed to set room name:", err)
@ -401,7 +407,7 @@ func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
return false return false
} }
func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool { func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID, updateInfo bool) bool {
if portal.Topic != topic { if portal.Topic != topic {
intent := portal.MainIntent() intent := portal.MainIntent()
if len(setBy) > 0 { if len(setBy) > 0 {
@ -410,6 +416,9 @@ func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
_, err := intent.SetRoomTopic(portal.MXID, topic) _, err := intent.SetRoomTopic(portal.MXID, topic)
if err == nil { if err == nil {
portal.Topic = topic portal.Topic = topic
if updateInfo {
portal.UpdateBridgeInfo()
}
return true return true
} }
portal.log.Warnln("Failed to set room topic:", err) portal.log.Warnln("Failed to set room topic:", err)
@ -422,8 +431,8 @@ func (portal *Portal) UpdateMetadata(user *User) bool {
return false return false
} else if portal.IsStatusBroadcastRoom() { } else if portal.IsStatusBroadcastRoom() {
update := false update := false
update = portal.UpdateName("WhatsApp Status Broadcast", "") || update update = portal.UpdateName("WhatsApp Status Broadcast", "", false) || update
update = portal.UpdateTopic("WhatsApp status updates from your contacts", "") || update update = portal.UpdateTopic("WhatsApp status updates from your contacts", "", false) || update
return update return update
} }
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID) metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
@ -443,8 +452,8 @@ func (portal *Portal) UpdateMetadata(user *User) bool {
portal.SyncParticipants(metadata) portal.SyncParticipants(metadata)
update := false update := false
update = portal.UpdateName(metadata.Name, metadata.NameSetBy) || update update = portal.UpdateName(metadata.Name, metadata.NameSetBy, false) || update
update = portal.UpdateTopic(metadata.Topic, metadata.TopicSetBy) || update update = portal.UpdateTopic(metadata.Topic, metadata.TopicSetBy, false) || update
return update return update
} }
@ -506,10 +515,11 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
update := false update := false
update = portal.UpdateMetadata(user) || update update = portal.UpdateMetadata(user) || update
if !portal.IsStatusBroadcastRoom() { if !portal.IsStatusBroadcastRoom() {
update = portal.UpdateAvatar(user, nil) || update update = portal.UpdateAvatar(user, nil, false) || update
} }
if update { if update {
portal.Update() portal.Update()
portal.UpdateBridgeInfo()
} }
} }
@ -802,6 +812,40 @@ var (
StateHalfShotBridgeInfo = event.Type{Type: "uk.half-shot.bridge", Class: event.StateEventType} StateHalfShotBridgeInfo = event.Type{Type: "uk.half-shot.bridge", Class: event.StateEventType}
) )
func (portal *Portal) getBridgeInfo() (string, event.Content) {
bridgeInfo := event.Content{
Parsed: BridgeInfoContent{
BridgeBot: portal.bridge.Bot.UserID,
Creator: portal.MainIntent().UserID,
Protocol: BridgeInfoSection{
ID: "whatsapp",
DisplayName: "WhatsApp",
AvatarURL: id.ContentURIString(portal.bridge.Config.AppService.Bot.Avatar),
ExternalURL: "https://www.whatsapp.com/",
},
Channel: BridgeInfoSection{
ID: portal.Key.JID,
DisplayName: portal.Name,
AvatarURL: portal.AvatarURL.CUString(),
},
},
}
bridgeInfoStateKey := fmt.Sprintf("net.maunium.whatsapp://whatsapp/%s", portal.Key.JID)
return bridgeInfoStateKey, bridgeInfo
}
func (portal *Portal) UpdateBridgeInfo() {
stateKey, content := portal.getBridgeInfo()
_, err := portal.MainIntent().SendStateEvent(portal.MXID, StateBridgeInfo, stateKey, content)
if err != nil {
portal.log.Warnln("Failed to update m.bridge:", err)
}
_, err = portal.MainIntent().SendStateEvent(portal.MXID, StateHalfShotBridgeInfo, stateKey, content)
if err != nil {
portal.log.Warnln("Failed to update uk.half-shot.bridge:", err)
}
}
func (portal *Portal) CreateMatrixRoom(user *User) error { func (portal *Portal) CreateMatrixRoom(user *User) error {
portal.roomCreateLock.Lock() portal.roomCreateLock.Lock()
defer portal.roomCreateLock.Unlock() defer portal.roomCreateLock.Unlock()
@ -837,25 +881,10 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
portal.Name = metadata.Name portal.Name = metadata.Name
portal.Topic = metadata.Topic portal.Topic = metadata.Topic
} }
portal.UpdateAvatar(user, nil) portal.UpdateAvatar(user, nil, false)
} }
bridgeInfo := event.Content{ bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo()
Parsed: BridgeInfoContent{
BridgeBot: portal.bridge.Bot.UserID,
Creator: portal.MainIntent().UserID,
Protocol: BridgeInfoSection{
ID: "whatsapp",
DisplayName: "WhatsApp",
AvatarURL: id.ContentURIString(portal.bridge.Config.AppService.Bot.Avatar),
ExternalURL: "https://www.whatsapp.com/",
},
Channel: BridgeInfoSection{
ID: portal.Key.JID,
},
},
}
bridgeInfoStateKey := fmt.Sprintf("net.maunium.whatsapp://whatsapp/%s", portal.Key.JID)
initialState := []*event.Event{{ initialState := []*event.Event{{
Type: event.StatePowerLevels, Type: event.StatePowerLevels,
Content: event.Content{ Content: event.Content{

View File

@ -807,7 +807,7 @@ func (user *User) HandleCommand(cmd whatsappExt.Command) {
go puppet.UpdateAvatar(user, cmd.ProfilePicInfo) go puppet.UpdateAvatar(user, cmd.ProfilePicInfo)
} else { } else {
portal := user.GetPortalByJID(cmd.JID) portal := user.GetPortalByJID(cmd.JID)
go portal.UpdateAvatar(user, cmd.ProfilePicInfo) go portal.UpdateAvatar(user, cmd.ProfilePicInfo, true)
} }
case whatsappExt.CommandDisconnect: case whatsappExt.CommandDisconnect:
var msg string var msg string
@ -836,11 +836,11 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) {
switch cmd.Data.Action { switch cmd.Data.Action {
case whatsappExt.ChatActionNameChange: case whatsappExt.ChatActionNameChange:
go portal.UpdateName(cmd.Data.NameChange.Name, cmd.Data.SenderJID) go portal.UpdateName(cmd.Data.NameChange.Name, cmd.Data.SenderJID, true)
case whatsappExt.ChatActionAddTopic: case whatsappExt.ChatActionAddTopic:
go portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID) go portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID, true)
case whatsappExt.ChatActionRemoveTopic: case whatsappExt.ChatActionRemoveTopic:
go portal.UpdateTopic("", cmd.Data.SenderJID) go portal.UpdateTopic("", cmd.Data.SenderJID, true)
case whatsappExt.ChatActionPromote: case whatsappExt.ChatActionPromote:
go portal.ChangeAdminStatus(cmd.Data.PermissionChange.JIDs, true) go portal.ChangeAdminStatus(cmd.Data.PermissionChange.JIDs, true)
case whatsappExt.ChatActionDemote: case whatsappExt.ChatActionDemote: