|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/gotd/td/telegram/peers" |
| 8 | +) |
| 9 | + |
| 10 | +// resolveInviteLinks resolves a ChatID to its invite-link manager. |
| 11 | +func (b *Bot) resolveInviteLinks(ctx context.Context, chat ChatID) (peers.InviteLinks, error) { |
| 12 | + p, err := b.resolvePeer(ctx, chat) |
| 13 | + if err != nil { |
| 14 | + return peers.InviteLinks{}, err |
| 15 | + } |
| 16 | + type linkable interface { |
| 17 | + InviteLinks() peers.InviteLinks |
| 18 | + } |
| 19 | + l, ok := p.(linkable) |
| 20 | + if !ok { |
| 21 | + return peers.InviteLinks{}, &Error{Code: 400, Description: "Bad Request: method is not available in private chats"} |
| 22 | + } |
| 23 | + return l.InviteLinks(), nil |
| 24 | +} |
| 25 | + |
| 26 | +// convertInviteLink converts a gotd invite link into the Bot API type. It |
| 27 | +// resolves the creator, which may require a network round trip. |
| 28 | +func (b *Bot) convertInviteLink(ctx context.Context, link peers.InviteLink) (*ChatInviteLink, error) { |
| 29 | + out := &ChatInviteLink{ |
| 30 | + InviteLink: link.Link(), |
| 31 | + CreatesJoinRequest: link.RequestNeeded(), |
| 32 | + IsPrimary: link.Permanent(), |
| 33 | + IsRevoked: link.Revoked(), |
| 34 | + } |
| 35 | + if creator, err := link.Creator(ctx); err == nil { |
| 36 | + out.Creator = userFromTgUser(creator.Raw()) |
| 37 | + } |
| 38 | + if name, ok := link.Title(); ok { |
| 39 | + out.Name = name |
| 40 | + } |
| 41 | + if expire, ok := link.ExpireDate(); ok { |
| 42 | + out.ExpireDate = int(expire.Unix()) |
| 43 | + } |
| 44 | + if limit, ok := link.UsageLimit(); ok { |
| 45 | + out.MemberLimit = limit |
| 46 | + } |
| 47 | + if requested, ok := link.Requested(); ok { |
| 48 | + out.PendingJoinRequestCount = requested |
| 49 | + } |
| 50 | + return out, nil |
| 51 | +} |
| 52 | + |
| 53 | +// InviteLinkOption configures a create/edit invite-link call. |
| 54 | +type InviteLinkOption func(*peers.ExportLinkOptions) |
| 55 | + |
| 56 | +// WithInviteLinkName sets the invite link name (0-32 characters). |
| 57 | +func WithInviteLinkName(name string) InviteLinkOption { |
| 58 | + return func(o *peers.ExportLinkOptions) { o.Title = name } |
| 59 | +} |
| 60 | + |
| 61 | +// WithInviteLinkExpire sets the Unix time when the link will expire. |
| 62 | +func WithInviteLinkExpire(unixTime int) InviteLinkOption { |
| 63 | + return func(o *peers.ExportLinkOptions) { o.ExpireDate = time.Unix(int64(unixTime), 0) } |
| 64 | +} |
| 65 | + |
| 66 | +// WithInviteLinkMemberLimit caps how many users may join via this link. |
| 67 | +func WithInviteLinkMemberLimit(limit int) InviteLinkOption { |
| 68 | + return func(o *peers.ExportLinkOptions) { o.UsageLimit = limit } |
| 69 | +} |
| 70 | + |
| 71 | +// WithInviteLinkJoinRequest requires administrators to approve users joining via |
| 72 | +// this link. |
| 73 | +func WithInviteLinkJoinRequest() InviteLinkOption { |
| 74 | + return func(o *peers.ExportLinkOptions) { o.RequestNeeded = true } |
| 75 | +} |
| 76 | + |
| 77 | +// ExportChatInviteLink generates a new primary invite link for a chat, revoking |
| 78 | +// any previous one, and returns it. |
| 79 | +func (b *Bot) ExportChatInviteLink(ctx context.Context, chat ChatID) (string, error) { |
| 80 | + links, err := b.resolveInviteLinks(ctx, chat) |
| 81 | + if err != nil { |
| 82 | + return "", err |
| 83 | + } |
| 84 | + link, err := links.ExportNew(ctx, peers.ExportLinkOptions{}) |
| 85 | + if err != nil { |
| 86 | + return "", asAPIError(err) |
| 87 | + } |
| 88 | + return link.Link(), nil |
| 89 | +} |
| 90 | + |
| 91 | +// CreateChatInviteLink creates an additional invite link for a chat. |
| 92 | +func (b *Bot) CreateChatInviteLink(ctx context.Context, chat ChatID, opts ...InviteLinkOption) (*ChatInviteLink, error) { |
| 93 | + links, err := b.resolveInviteLinks(ctx, chat) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + var o peers.ExportLinkOptions |
| 98 | + for _, opt := range opts { |
| 99 | + opt(&o) |
| 100 | + } |
| 101 | + link, err := links.AddNew(ctx, o) |
| 102 | + if err != nil { |
| 103 | + return nil, asAPIError(err) |
| 104 | + } |
| 105 | + return b.convertInviteLink(ctx, link) |
| 106 | +} |
| 107 | + |
| 108 | +// EditChatInviteLink edits a non-primary invite link created by the bot. |
| 109 | +func (b *Bot) EditChatInviteLink(ctx context.Context, chat ChatID, inviteLink string, opts ...InviteLinkOption) (*ChatInviteLink, error) { |
| 110 | + links, err := b.resolveInviteLinks(ctx, chat) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + var o peers.ExportLinkOptions |
| 115 | + for _, opt := range opts { |
| 116 | + opt(&o) |
| 117 | + } |
| 118 | + link, err := links.Edit(ctx, inviteLink, o) |
| 119 | + if err != nil { |
| 120 | + return nil, asAPIError(err) |
| 121 | + } |
| 122 | + return b.convertInviteLink(ctx, link) |
| 123 | +} |
| 124 | + |
| 125 | +// RevokeChatInviteLink revokes an invite link created by the bot. |
| 126 | +func (b *Bot) RevokeChatInviteLink(ctx context.Context, chat ChatID, inviteLink string) (*ChatInviteLink, error) { |
| 127 | + links, err := b.resolveInviteLinks(ctx, chat) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + link, err := links.Revoke(ctx, inviteLink) |
| 132 | + if err != nil { |
| 133 | + return nil, asAPIError(err) |
| 134 | + } |
| 135 | + return b.convertInviteLink(ctx, link) |
| 136 | +} |
0 commit comments