Skip to content

Commit

Permalink
ban user instead of applying restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
slawiko authored and umputun committed Feb 3, 2024
1 parent 0612662 commit c0b01ba
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/events/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (a *admin) callbackUnbanConfirmed(query *tbapi.CallbackQuery) error {
// onlyIfBanned seems to prevent user from being removed from the chat according to this confusing doc:
// https://core.telegram.org/bots/api#unbanchatmember
_, err = a.tbAPI.Request(tbapi.UnbanChatMemberConfig{
ChatMemberConfig: tbapi.ChatMemberConfig{UserID: userID, ChatID: a.primChatID}})
ChatMemberConfig: tbapi.ChatMemberConfig{UserID: userID, ChatID: a.primChatID}, OnlyIfBanned: true})
if err != nil {
return fmt.Errorf("failed to unban user %d: %w", userID, err)
}
Expand Down
8 changes: 1 addition & 7 deletions app/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,12 @@ func banUserOrChannel(r banRequest) error {
return nil
}

resp, err := r.tbAPI.Request(tbapi.RestrictChatMemberConfig{
resp, err := r.tbAPI.Request(tbapi.BanChatMemberConfig{
ChatMemberConfig: tbapi.ChatMemberConfig{
ChatID: r.chatID,
UserID: r.userID,
},
UntilDate: time.Now().Add(r.duration).Unix(),
Permissions: &tbapi.ChatPermissions{
CanSendMessages: false,
CanSendMediaMessages: false,
CanSendOtherMessages: false,
CanAddWebPagePreviews: false,
},
})
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions app/events/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestTelegramListener_DoWithBotBan(t *testing.T) {
assert.Equal(t, 1, len(mockAPI.SendCalls()))
assert.Equal(t, "bot's answer", mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text)
assert.Equal(t, 1, len(mockAPI.RequestCalls()))
assert.Equal(t, int64(123), mockAPI.RequestCalls()[0].C.(tbapi.RestrictChatMemberConfig).ChatID)
assert.Equal(t, int64(123), mockAPI.RequestCalls()[0].C.(tbapi.BanChatMemberConfig).ChatID)
})

t.Run("test ban of the channel", func(t *testing.T) {
Expand Down Expand Up @@ -450,8 +450,8 @@ func TestTelegramListener_DoWithForwarded(t *testing.T) {
assert.Equal(t, 2, len(mockAPI.RequestCalls()))
assert.Equal(t, int64(123), mockAPI.RequestCalls()[0].C.(tbapi.DeleteMessageConfig).ChatID)
assert.Equal(t, 999999, mockAPI.RequestCalls()[0].C.(tbapi.DeleteMessageConfig).MessageID)
assert.Equal(t, int64(123), mockAPI.RequestCalls()[1].C.(tbapi.RestrictChatMemberConfig).ChatID)
assert.Equal(t, int64(88), mockAPI.RequestCalls()[1].C.(tbapi.RestrictChatMemberConfig).UserID)
assert.Equal(t, int64(123), mockAPI.RequestCalls()[1].C.(tbapi.BanChatMemberConfig).ChatID)
assert.Equal(t, int64(88), mockAPI.RequestCalls()[1].C.(tbapi.BanChatMemberConfig).UserID)

assert.Equal(t, 1, len(b.RemoveApprovedUserCalls()))
assert.Equal(t, int64(88), b.RemoveApprovedUserCalls()[0].ID)
Expand Down Expand Up @@ -897,8 +897,8 @@ func TestTelegramListener_DoWithAdminBanConfirmedTraining(t *testing.T) {
assert.Equal(t, 0, len(kb), "buttons cleared")
assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.EditMessageTextConfig).Text, "confirmed by admin in ")
require.Equal(t, 2, len(mockAPI.RequestCalls()))
assert.Equal(t, int64(999), mockAPI.RequestCalls()[0].C.(tbapi.RestrictChatMemberConfig).UserID, "user banned")
assert.Equal(t, int64(123), mockAPI.RequestCalls()[0].C.(tbapi.RestrictChatMemberConfig).ChatID, "chat id")
assert.Equal(t, int64(999), mockAPI.RequestCalls()[0].C.(tbapi.BanChatMemberConfig).UserID, "user banned")
assert.Equal(t, int64(123), mockAPI.RequestCalls()[0].C.(tbapi.BanChatMemberConfig).ChatID, "chat id")
assert.Equal(t, 987654, mockAPI.RequestCalls()[1].C.(tbapi.DeleteMessageConfig).MessageID, "message deleted")

assert.Equal(t, 1, len(b.UpdateSpamCalls()))
Expand Down

0 comments on commit c0b01ba

Please sign in to comment.