Give room admin permission to newsletter admins

This commit is contained in:
Tulir Asokan 2023-10-18 13:59:43 +03:00
parent c02e657d22
commit e94f611002

View file

@ -1505,7 +1505,7 @@ func (portal *Portal) UpdateParentGroup(source *User, parent types.JID, updateIn
return false return false
} }
func (portal *Portal) UpdateMetadata(user *User, groupInfo *types.GroupInfo) bool { func (portal *Portal) UpdateMetadata(user *User, groupInfo *types.GroupInfo, newsletterMetadata *types.NewsletterMetadata) bool {
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
return false return false
} else if portal.IsStatusBroadcastList() { } else if portal.IsStatusBroadcastList() {
@ -1529,12 +1529,15 @@ func (portal *Portal) UpdateMetadata(user *User, groupInfo *types.GroupInfo) boo
return update return update
} }
if groupInfo == nil && portal.IsNewsletter() { if groupInfo == nil && portal.IsNewsletter() {
newsletterInfo, err := user.Client.GetNewsletterInfo(portal.Key.JID) if newsletterMetadata == nil {
if err != nil { var err error
portal.zlog.Err(err).Msg("Failed to get newsletter info") newsletterMetadata, err = user.Client.GetNewsletterInfo(portal.Key.JID)
return false if err != nil {
portal.zlog.Err(err).Msg("Failed to get newsletter info")
return false
}
} }
groupInfo = newsletterToGroupInfo(newsletterInfo) groupInfo = newsletterToGroupInfo(newsletterMetadata)
} }
if groupInfo == nil { if groupInfo == nil {
var err error var err error
@ -1564,6 +1567,9 @@ func (portal *Portal) UpdateMetadata(user *User, groupInfo *types.GroupInfo) boo
portal.RestrictMessageSending(groupInfo.IsAnnounce) portal.RestrictMessageSending(groupInfo.IsAnnounce)
portal.RestrictMetadataChanges(groupInfo.IsLocked) portal.RestrictMetadataChanges(groupInfo.IsLocked)
if newsletterMetadata != nil {
portal.PromoteNewsletterUser(user, newsletterMetadata.ViewerMeta.Role)
}
return update return update
} }
@ -1586,7 +1592,7 @@ func (portal *Portal) UpdateMatrixRoom(user *User, groupInfo *types.GroupInfo, n
} }
update := false update := false
update = portal.UpdateMetadata(user, groupInfo) || update update = portal.UpdateMetadata(user, groupInfo, newsletterMetadata) || update
if !portal.IsPrivateChat() && !portal.IsBroadcastList() && !portal.IsNewsletter() { if !portal.IsPrivateChat() && !portal.IsBroadcastList() && !portal.IsNewsletter() {
update = portal.UpdateAvatar(user, types.EmptyJID, false) || update update = portal.UpdateAvatar(user, types.EmptyJID, false) || update
} else if newsletterMetadata != nil { } else if newsletterMetadata != nil {
@ -1697,6 +1703,35 @@ func (portal *Portal) RestrictMessageSending(restrict bool) id.EventID {
} }
} }
func (portal *Portal) PromoteNewsletterUser(user *User, role types.NewsletterRole) id.EventID {
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
if err != nil {
levels = portal.GetBasePowerLevels()
}
newLevel := 0
switch role {
case types.NewsletterRoleAdmin:
newLevel = 50
case types.NewsletterRoleOwner:
newLevel = 95
}
changed := portal.applyPowerLevelFixes(levels)
changed = levels.EnsureUserLevel(user.MXID, newLevel) || changed
if !changed {
return ""
}
resp, err := portal.MainIntent().SetPowerLevels(portal.MXID, levels)
if err != nil {
portal.log.Errorln("Failed to change power levels:", err)
return ""
} else {
return resp.EventID
}
}
func (portal *Portal) RestrictMetadataChanges(restrict bool) id.EventID { func (portal *Portal) RestrictMetadataChanges(restrict bool) id.EventID {
levels, err := portal.MainIntent().PowerLevels(portal.MXID) levels, err := portal.MainIntent().PowerLevels(portal.MXID)
if err != nil { if err != nil {
@ -1901,6 +1936,14 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, n
powerLevels.EnsureEventLevel(event.StateTopic, 50) powerLevels.EnsureEventLevel(event.StateTopic, 50)
} }
} }
if newsletterMetadata != nil {
switch newsletterMetadata.ViewerMeta.Role {
case types.NewsletterRoleAdmin:
powerLevels.EnsureUserLevel(user.MXID, 50)
case types.NewsletterRoleOwner:
powerLevels.EnsureUserLevel(user.MXID, 95)
}
}
bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo() bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo()