Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(members): add ChannelQuery type #726

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions telegram/peers/members/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
// ChannelMembers is channel Members.
type ChannelMembers struct {
m *peers.Manager
filter tg.ChannelParticipantsFilterClass
channel peers.Channel
}

func (c *ChannelMembers) query(ctx context.Context, offset, limit int) (*tg.ChannelsChannelParticipants, error) {
raw := c.m.API()
p, err := raw.ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
Channel: c.channel.InputChannel(),
Filter: &tg.ChannelParticipantsRecent{},
Filter: c.filter,
Offset: offset,
Limit: limit,
})
Expand Down Expand Up @@ -100,7 +101,7 @@ func (c *ChannelMembers) ForEach(ctx context.Context, cb Callback) error {
if err != nil {
return errors.Wrapf(err, "get member %d", userID)
}
member := ChannelMember{
chm := ChannelMember{
parent: c,
creatorDate: channelDate,
user: user,
Expand All @@ -112,10 +113,10 @@ func (c *ChannelMembers) ForEach(ctx context.Context, cb Callback) error {
if err != nil {
return errors.Wrapf(err, "get inviter %d", inviterID)
}
member.inviter = inviter
chm.inviter = inviter
}

if err := cb(member); err != nil {
if err := cb(chm); err != nil {
return errors.Wrapf(err, "callback (index: %d)", i)
}
}
Expand Down Expand Up @@ -209,8 +210,5 @@ func (c *ChannelMembers) EditAdminRights(

// Channel returns recent channel members.
func Channel(channel peers.Channel) *ChannelMembers {
return &ChannelMembers{
m: channel.Manager(),
channel: channel,
}
return ChannelQuery{Channel: channel}.Recent()
}
59 changes: 59 additions & 0 deletions telegram/peers/members/channel_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package members

import (
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"
)

// ChannelQuery is builder for channel members querying.
type ChannelQuery struct {
Channel peers.Channel
}

func (q ChannelQuery) query(filter tg.ChannelParticipantsFilterClass) *ChannelMembers {
return &ChannelMembers{
m: q.Channel.Manager(),
filter: filter,
channel: q.Channel,
}
}

// Recent queries recent members.
func (q ChannelQuery) Recent() *ChannelMembers {
return q.query(&tg.ChannelParticipantsRecent{})
}

// Admins queries admins members.
func (q ChannelQuery) Admins() *ChannelMembers {
return q.query(&tg.ChannelParticipantsAdmins{})
}

// Kicked queries kicked members.
func (q ChannelQuery) Kicked(query string) *ChannelMembers {
return q.query(&tg.ChannelParticipantsKicked{Q: query})
}

// Bots queries bots members.
func (q ChannelQuery) Bots() *ChannelMembers {
return q.query(&tg.ChannelParticipantsBots{})
}

// Banned queries banned members.
func (q ChannelQuery) Banned(query string) *ChannelMembers {
return q.query(&tg.ChannelParticipantsBanned{Q: query})
}

// Search queries members by given name.
func (q ChannelQuery) Search(query string) *ChannelMembers {
return q.query(&tg.ChannelParticipantsSearch{Q: query})
}

// Contacts queries members that are also contacts.
func (q ChannelQuery) Contacts(query string) *ChannelMembers {
return q.query(&tg.ChannelParticipantsContacts{Q: query})
}

// Custom creates query with custom filter.
func (q ChannelQuery) Custom(filter tg.ChannelParticipantsFilterClass) *ChannelMembers {
return q.query(filter)
}
Loading