power levels: prevent sending to blocked users
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -377,6 +377,17 @@ func (t *TelegramClient) GetChatInfo(ctx context.Context, portal *bridgev2.Porta
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TelegramClient) getDMPowerLevels(ghost *bridgev2.Ghost) *bridgev2.PowerLevelOverrides {
|
||||
var plo bridgev2.PowerLevelOverrides
|
||||
if ghost.Metadata.(*GhostMetadata).Blocked {
|
||||
// Don't allow sending messages to blocked users
|
||||
plo.EventsDefault = superadminPowerLevel
|
||||
} else {
|
||||
plo.EventsDefault = anyonePowerLevel
|
||||
}
|
||||
return &plo
|
||||
}
|
||||
|
||||
func (t *TelegramClient) getGroupChatPowerLevels(entity tg.ChatClass) *bridgev2.PowerLevelOverrides {
|
||||
dbrAble, ok := entity.(interface {
|
||||
GetDefaultBannedRights() (tg.ChatBannedRights, bool)
|
||||
|
||||
@@ -189,6 +189,9 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
||||
dispatcher.OnChatDefaultBannedRights(func(ctx context.Context, e tg.Entities, update *tg.UpdateChatDefaultBannedRights) error {
|
||||
return client.onChatDefaultBannedRights(ctx, e, update)
|
||||
})
|
||||
dispatcher.OnPeerBlocked(func(ctx context.Context, e tg.Entities, update *tg.UpdatePeerBlocked) error {
|
||||
return client.onPeerBlocked(ctx, update)
|
||||
})
|
||||
|
||||
client.ScopedStore = tc.Store.GetScopedStore(telegramUserID)
|
||||
|
||||
|
||||
@@ -147,6 +147,7 @@ type GhostMetadata struct {
|
||||
IsPremium bool `json:"is_premium,omitempty"`
|
||||
IsBot bool `json:"is_bot,omitempty"`
|
||||
IsChannel bool `json:"is_channel,omitempty"`
|
||||
Blocked bool `json:"blocked,omitempty"`
|
||||
}
|
||||
|
||||
type PortalMetadata struct {
|
||||
|
||||
@@ -850,3 +850,40 @@ func (t *TelegramClient) onChatDefaultBannedRights(ctx context.Context, entities
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TelegramClient) onPeerBlocked(ctx context.Context, update *tg.UpdatePeerBlocked) error {
|
||||
var userID networkid.UserID
|
||||
if peer, ok := update.PeerID.(*tg.PeerUser); ok {
|
||||
userID = ids.MakeUserID(peer.UserID)
|
||||
} else {
|
||||
return fmt.Errorf("unexpected peer type in peer blocked update %T", update.PeerID)
|
||||
}
|
||||
|
||||
// Update the ghost
|
||||
ghost, err := t.main.Bridge.GetGhostByID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ghost.UpdateInfo(ctx, &bridgev2.UserInfo{
|
||||
ExtraUpdates: func(ctx context.Context, g *bridgev2.Ghost) bool {
|
||||
updated := g.Metadata.(*GhostMetadata).Blocked != update.Blocked
|
||||
g.Metadata.(*GhostMetadata).Blocked = update.Blocked
|
||||
return updated
|
||||
},
|
||||
})
|
||||
|
||||
// Find portals that are DMs with the user
|
||||
t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatResync{
|
||||
ChatInfo: &bridgev2.ChatInfo{
|
||||
Members: &bridgev2.ChatMemberList{
|
||||
PowerLevels: t.getDMPowerLevels(ghost),
|
||||
},
|
||||
CanBackfill: true,
|
||||
},
|
||||
EventMeta: simplevent.EventMeta{
|
||||
Type: bridgev2.RemoteEventChatResync,
|
||||
PortalKey: t.makePortalKeyFromPeer(update.PeerID),
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user