diff --git a/telegram/peers/channel.go b/telegram/peers/channel.go index 7d55710e51..6310c3584c 100644 --- a/telegram/peers/channel.go +++ b/telegram/peers/channel.go @@ -130,7 +130,7 @@ func (c Channel) FullRaw(ctx context.Context) (*tg.ChannelFull, error) { // ToBroadcast tries to convert this Channel to Broadcast. func (c Channel) ToBroadcast() (Broadcast, bool) { - if !c.raw.Broadcast { + if !c.IsBroadcast() { return Broadcast{}, false } return Broadcast{ @@ -145,7 +145,7 @@ func (c Channel) IsBroadcast() bool { // ToSupergroup tries to convert this Channel to Supergroup. func (c Channel) ToSupergroup() (Supergroup, bool) { - if !c.raw.Megagroup { + if !c.IsSupergroup() { return Supergroup{}, false } return Supergroup{ @@ -311,8 +311,8 @@ func (c Channel) KickUser(ctx context.Context, participant tg.InputUserClass, re // KickParticipant kicks participant. func (c Channel) KickParticipant(ctx context.Context, participant tg.InputPeerClass) error { - return c.editParticipantRights(ctx, participant, ParticipantRights{ - ViewMessages: true, + return c.EditParticipantRights(ctx, participant, ParticipantRights{ + DenyViewMessages: true, }) } @@ -341,7 +341,7 @@ func (c Channel) EditRights(ctx context.Context, options ParticipantRights) erro return c.m.editDefaultRights(ctx, c.InputPeer(), options) } -// EditAdminRights edits admin rights in this channel. +// EditAdminRights edits admin rights of given user in this channel. func (c Channel) EditAdminRights( ctx context.Context, admin tg.InputUserClass, diff --git a/telegram/peers/channel_test.go b/telegram/peers/channel_test.go index 6f2b2e97af..7d3e11a42c 100644 --- a/telegram/peers/channel_test.go +++ b/telegram/peers/channel_test.go @@ -73,6 +73,24 @@ func TestChannelGetters(t *testing.T) { a.Equal(b.raw.Broadcast, ok) a.Equal(b.raw.Signatures, b.Signatures()) } + { + v, ok := u.AdminRights() + v2, ok2 := u.raw.GetAdminRights() + a.Equal(ok, ok2) + a.Equal(v2, v) + } + { + v, ok := u.BannedRights() + v2, ok2 := u.raw.GetBannedRights() + a.Equal(ok, ok2) + a.Equal(v2, v) + } + { + v, ok := u.DefaultBannedRights() + v2, ok2 := u.raw.GetDefaultBannedRights() + a.Equal(ok2, ok) + a.Equal(v2, v) + } } func TestChannel_Leave(t *testing.T) { @@ -134,3 +152,101 @@ func TestChannel_SetDescription(t *testing.T) { }).ThenTrue() a.NoError(ch.SetDescription(ctx, about)) } + +func TestChannel_Join(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + ch := m.Channel(getTestChannel()) + + mock.ExpectCall(&tg.ChannelsJoinChannelRequest{ + Channel: ch.InputChannel(), + }).ThenRPCErr(getTestError()) + a.Error(ch.Join(ctx)) + + mock.ExpectCall(&tg.ChannelsJoinChannelRequest{ + Channel: ch.InputChannel(), + }).ThenResult(&tg.Updates{}) + a.NoError(ch.Join(ctx)) +} + +func TestChannel_Delete(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + ch := m.Channel(getTestChannel()) + + mock.ExpectCall(&tg.ChannelsDeleteChannelRequest{ + Channel: ch.InputChannel(), + }).ThenRPCErr(getTestError()) + a.Error(ch.Delete(ctx)) + + mock.ExpectCall(&tg.ChannelsDeleteChannelRequest{ + Channel: ch.InputChannel(), + }).ThenResult(&tg.Updates{}) + a.NoError(ch.Delete(ctx)) +} + +func TestChannel_KickUser(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + u := m.User(getTestUser()) + ch := m.Channel(getTestChannel()) + rights := tg.ChatBannedRights{ + ViewMessages: true, + } + rights.SetFlags() + + mock.ExpectCall(&tg.ChannelsEditBannedRequest{ + Channel: ch.InputChannel(), + Participant: u.InputPeer(), + BannedRights: rights, + }).ThenRPCErr(getTestError()) + a.Error(ch.KickUser(ctx, u.InputUser(), false)) + + mock.ExpectCall(&tg.ChannelsEditBannedRequest{ + Channel: ch.InputChannel(), + Participant: u.InputPeer(), + BannedRights: rights, + }).ThenResult(&tg.Updates{}) + a.NoError(ch.KickUser(ctx, u.InputUser(), false)) +} + +func TestChannel_EditAdminRights(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + u := m.User(getTestUser()) + ch := m.Channel(getTestChannel()) + rights := tg.ChatAdminRights{ + AddAdmins: true, + } + rights.SetFlags() + + mock.ExpectCall(&tg.ChannelsEditAdminRequest{ + Channel: ch.InputChannel(), + UserID: u.InputUser(), + AdminRights: rights, + Rank: "rank", + }).ThenRPCErr(getTestError()) + a.Error(ch.EditAdminRights(ctx, u.InputUser(), AdminRights{ + Rank: "rank", + AddAdmins: true, + })) + + mock.ExpectCall(&tg.ChannelsEditAdminRequest{ + Channel: ch.InputChannel(), + UserID: u.InputUser(), + AdminRights: rights, + Rank: "rank", + }).ThenResult(&tg.Updates{}) + a.NoError(ch.EditAdminRights(ctx, u.InputUser(), AdminRights{ + Rank: "rank", + AddAdmins: true, + })) +} diff --git a/telegram/peers/chat.go b/telegram/peers/chat.go index 11541e31b9..a53d2ebed3 100644 --- a/telegram/peers/chat.go +++ b/telegram/peers/chat.go @@ -139,7 +139,7 @@ func (c Chat) InviteLinks() InviteLinks { // ToBroadcast tries to convert this Chat to Broadcast. func (c Chat) ToBroadcast() (Broadcast, bool) { - return Broadcast{}, false + return Broadcast{}, c.IsBroadcast() } // IsBroadcast whether this Chat is Broadcast. @@ -149,7 +149,7 @@ func (c Chat) IsBroadcast() bool { // ToSupergroup tries to convert this Chat to Supergroup. func (c Chat) ToSupergroup() (Supergroup, bool) { - return Supergroup{}, false + return Supergroup{}, c.IsSupergroup() } // IsSupergroup whether this Chat is Supergroup. diff --git a/telegram/peers/multichat_test.go b/telegram/peers/multichat_test.go index 95f9f5b20e..ad10b67d07 100644 --- a/telegram/peers/multichat_test.go +++ b/telegram/peers/multichat_test.go @@ -70,3 +70,29 @@ func TestReactions(t *testing.T) { a.NoError(p.DisableReactions(ctx)) } } + +func TestEditRights(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + rights := tg.ChatBannedRights{ + SendInline: true, + } + rights.SetFlags() + req := func(p Peer) *tgmock.RequestBuilder { + return mock.ExpectCall(&tg.MessagesEditChatDefaultBannedRightsRequest{ + Peer: p.InputPeer(), + BannedRights: rights, + }) + } + for _, p := range []multiChat{ + m.Chat(getTestChat()), + m.Channel(getTestChannel()), + } { + req(p).ThenRPCErr(getTestError()) + a.Error(p.EditRights(ctx, ParticipantRights{DenySendInline: true})) + req(p).ThenResult(&tg.Updates{}) + a.NoError(p.EditRights(ctx, ParticipantRights{DenySendInline: true})) + } +} diff --git a/telegram/peers/rights.go b/telegram/peers/rights.go index caad2b42a5..77af86ac34 100644 --- a/telegram/peers/rights.go +++ b/telegram/peers/rights.go @@ -58,29 +58,31 @@ func (b AdminRights) IntoChatAdminRights() (r tg.ChatAdminRights) { // ParticipantRights represents participant right settings. type ParticipantRights struct { // If set, does not allow a user to view messages in a supergroup/channel/chat. - ViewMessages bool + // + // In fact, user will be kicked. + DenyViewMessages bool // If set, does not allow a user to send messages in a supergroup/chat. - SendMessages bool + DenySendMessages bool // If set, does not allow a user to send any media in a supergroup/chat. - SendMedia bool + DenySendMedia bool // If set, does not allow a user to send stickers in a supergroup/chat. - SendStickers bool + DenySendStickers bool // If set, does not allow a user to send gifs in a supergroup/chat. - SendGifs bool + DenySendGifs bool // If set, does not allow a user to send games in a supergroup/chat. - SendGames bool + DenySendGames bool // If set, does not allow a user to use inline bots in a supergroup/chat. - SendInline bool + DenySendInline bool // If set, does not allow a user to embed links in the messages of a supergroup/chat. - EmbedLinks bool + DenyEmbedLinks bool // If set, does not allow a user to send polls in a supergroup/chat. - SendPolls bool + DenySendPolls bool // If set, does not allow any user to change the description of a supergroup/chat. - ChangeInfo bool + DenyChangeInfo bool // If set, does not allow any user to invite users in a supergroup/chat. - InviteUsers bool + DenyInviteUsers bool // If set, does not allow any user to pin messages in a supergroup/chat. - PinMessages bool + DenyPinMessages bool // Validity of said permissions (it is considered forever any value less than 30 seconds or more than 366 days). // // If value is zero, value will not be used. @@ -95,18 +97,18 @@ func (b *ParticipantRights) SetUntil(d time.Duration) { // IntoChatBannedRights converts ParticipantRights into tg.ChatBannedRights. func (b ParticipantRights) IntoChatBannedRights() (r tg.ChatBannedRights) { r = tg.ChatBannedRights{ - ViewMessages: b.ViewMessages, - SendMessages: b.SendMessages, - SendMedia: b.SendMedia, - SendStickers: b.SendStickers, - SendGifs: b.SendGifs, - SendGames: b.SendGames, - SendInline: b.SendInline, - EmbedLinks: b.EmbedLinks, - SendPolls: b.SendPolls, - ChangeInfo: b.ChangeInfo, - InviteUsers: b.InviteUsers, - PinMessages: b.PinMessages, + ViewMessages: b.DenyViewMessages, + SendMessages: b.DenySendMessages, + SendMedia: b.DenySendMedia, + SendStickers: b.DenySendStickers, + SendGifs: b.DenySendGifs, + SendGames: b.DenySendGames, + SendInline: b.DenySendInline, + EmbedLinks: b.DenyEmbedLinks, + SendPolls: b.DenySendPolls, + ChangeInfo: b.DenyChangeInfo, + InviteUsers: b.DenyInviteUsers, + PinMessages: b.DenyPinMessages, } if !b.UntilDate.IsZero() { r.UntilDate = int(b.UntilDate.Unix()) diff --git a/telegram/peers/user_test.go b/telegram/peers/user_test.go index 4a576713b5..9727290144 100644 --- a/telegram/peers/user_test.go +++ b/telegram/peers/user_test.go @@ -1,6 +1,7 @@ package peers import ( + "context" "testing" "github.com/stretchr/testify/require" @@ -92,6 +93,18 @@ func TestUserGetters(t *testing.T) { a.Equal(ok, ok2) a.Equal(v, v2) } + { + v, ok := u.raw.GetStatus() + v2, ok2 := u.Status() + a.Equal(ok, ok2) + a.Equal(v, v2) + } + { + v, ok := u.raw.GetLangCode() + v2, ok2 := u.LangCode() + a.Equal(ok, ok2) + a.Equal(v, v2) + } b, ok := u.ToBot() a.True(ok) @@ -131,3 +144,51 @@ func TestUser_VisibleName(t *testing.T) { LastName: "LastName", }}.VisibleName()) } + +func TestUser_ReportSpam(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + u := m.User(getTestUser()) + + mock.ExpectCall(&tg.MessagesReportSpamRequest{Peer: u.InputPeer()}). + ThenRPCErr(getTestError()) + a.Error(u.ReportSpam(ctx)) + + mock.ExpectCall(&tg.MessagesReportSpamRequest{Peer: u.InputPeer()}). + ThenTrue() + a.NoError(u.ReportSpam(ctx)) +} + +func TestUser_Block(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + u := m.User(getTestUser()) + + mock.ExpectCall(&tg.ContactsBlockRequest{ID: u.InputPeer()}). + ThenRPCErr(getTestError()) + a.Error(u.Block(ctx)) + + mock.ExpectCall(&tg.ContactsBlockRequest{ID: u.InputPeer()}). + ThenTrue() + a.NoError(u.Block(ctx)) +} + +func TestUser_Unblock(t *testing.T) { + a := require.New(t) + ctx := context.Background() + mock, m := testManager(t) + + u := m.User(getTestUser()) + + mock.ExpectCall(&tg.ContactsUnblockRequest{ID: u.InputPeer()}). + ThenRPCErr(getTestError()) + a.Error(u.Unblock(ctx)) + + mock.ExpectCall(&tg.ContactsUnblockRequest{ID: u.InputPeer()}). + ThenTrue() + a.NoError(u.Unblock(ctx)) +}