-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
join.go
97 lines (82 loc) · 2.37 KB
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package message
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/telegram/message/internal/deeplink"
"github.com/gotd/td/telegram/message/peer"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
// JoinLink joins to private chat using given link or hash.
// Input examples:
//
// t.me/joinchat/AAAAAAAAAAAAAAAA
// https://t.me/joinchat/AAAAAAAAAAAAAAAA
// tg:join?invite=AAAAAAAAAAAAAAAA
// tg://join?invite=AAAAAAAAAAAAAAAA
//
func (s *Sender) JoinLink(ctx context.Context, link string) (tg.UpdatesClass, error) {
hash := link
if deeplink.IsDeeplinkLike(link) {
l, err := deeplink.Expect(link, deeplink.Join)
if err != nil {
return nil, err
}
hash = l.Args.Get("invite")
}
return s.JoinHash(ctx, hash)
}
// JoinHash joins to private chat using given hash.
func (s *Sender) JoinHash(ctx context.Context, hash string) (tg.UpdatesClass, error) {
return s.importChatInvite(ctx, hash)
}
// Join joins resolved channel.
// NB: if resolved peer is not a channel, error will be returned.
func (b *RequestBuilder) Join(ctx context.Context) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
input, ok := peer.ToInputChannel(p)
if !ok {
return nil, xerrors.Errorf("unexpected type %T", p)
}
return b.sender.joinChannel(ctx, input)
}
func (b *RequestBuilder) leave(ctx context.Context, revoke bool) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
input, ok := peer.ToInputChannel(p)
if ok {
r, err := b.sender.leaveChannel(ctx, input)
if err != nil {
return nil, xerrors.Errorf("leave channel: %w", err)
}
return r, nil
}
chat, ok := p.(*tg.InputPeerChat)
if !ok {
return &tg.Updates{}, nil
}
r, err := b.sender.deleteChatUser(ctx, &tg.MessagesDeleteChatUserRequest{
RevokeHistory: revoke,
ChatID: chat.ChatID,
UserID: &tg.InputUserSelf{},
})
if err != nil {
// Happens if chat was deactivated.
if tgerr.Is(err, tg.ErrPeerIDInvalid) {
return &tg.Updates{}, nil
}
return nil, xerrors.Errorf("leave chat: %w", err)
}
return r, nil
}
// Leave leaves resolved peer.
//
// NB: if resolved peer is not a channel or chat, or chat is deactivated, empty *tg.Updates will be returned.
func (b *RequestBuilder) Leave(ctx context.Context) (tg.UpdatesClass, error) {
return b.leave(ctx, false)
}