-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
peers.go
140 lines (121 loc) · 3.32 KB
/
peers.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package botapi
import (
"context"
"strconv"
"github.com/go-faster/errors"
"github.com/gotd/td/constant"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"
"github.com/gotd/botapi/internal/oas"
)
func fillBotAPIChatPrivate(user peers.User) oas.Chat {
return oas.Chat{
ID: int64(user.TDLibPeerID()),
Type: oas.ChatTypePrivate,
Username: optString(user.Username),
FirstName: optString(user.FirstName),
LastName: optString(user.LastName),
}
}
func fillBotAPIChatGroup(chat Chat) oas.Chat {
r := oas.Chat{
ID: int64(chat.TDLibPeerID()),
Type: oas.ChatTypeGroup,
Title: oas.NewOptString(chat.VisibleName()),
Username: optString(chat.Username),
HasProtectedContent: oas.NewOptBool(chat.NoForwards()),
}
switch ch := chat.(type) {
case peers.Channel:
if _, ok := ch.ToBroadcast(); ok {
r.Type = oas.ChatTypeChannel
} else {
r.Type = oas.ChatTypeSupergroup
}
}
return r
}
func (b *BotAPI) getChatByPeer(ctx context.Context, p tg.PeerClass) (oas.Chat, error) {
peer, err := b.peers.ResolvePeer(ctx, p)
if err != nil {
return oas.Chat{}, errors.Wrapf(err, "find peer: %+v", p)
}
if user, ok := peer.(peers.User); ok {
return fillBotAPIChatPrivate(user), nil
}
ch, ok := peer.(Chat)
if !ok {
return oas.Chat{}, errors.Errorf("unexpected type %T", peer)
}
return fillBotAPIChatGroup(ch), nil
}
func (b *BotAPI) resolveUserID(ctx context.Context, id int64) (peers.User, error) {
user, err := b.peers.GetUser(ctx, &tg.InputUser{UserID: id})
if err != nil {
return peers.User{}, errors.Wrapf(err, "find user: %d", id)
}
return user, nil
}
// Chat is generic interface for peers.Chat, peers.Channel and friends.
type Chat interface {
peers.Peer
Creator() bool
Left() bool
NoForwards() bool
CallActive() bool
CallNotEmpty() bool
ParticipantsCount() int
AdminRights() (tg.ChatAdminRights, bool)
DefaultBannedRights() (tg.ChatBannedRights, bool)
Leave(ctx context.Context) error
SetTitle(ctx context.Context, title string) error
SetDescription(ctx context.Context, about string) error
InviteLinks() peers.InviteLinks
ToSupergroup() (peers.Supergroup, bool)
ToBroadcast() (peers.Broadcast, bool)
}
var _ = []Chat{
peers.Chat{},
peers.Channel{},
}
func (b *BotAPI) resolveIDToChat(ctx context.Context, id oas.ID) (Chat, error) {
p, err := b.resolveID(ctx, id)
if err != nil {
return nil, err
}
ch, ok := p.(Chat)
if !ok {
return nil, chatNotFound()
}
return ch, nil
}
func (b *BotAPI) resolveID(ctx context.Context, id oas.ID) (peers.Peer, error) {
if id.IsInt64() {
return b.resolveIntID(ctx, id.Int64)
}
username := id.String
switch {
case len(username) < 1:
return nil, &BadRequestError{Message: "Bad Request: chat_id is empty"}
case username[0] != '@':
parsedID, err := strconv.ParseInt(username, 10, 64)
if err != nil {
return nil, chatNotFound()
}
return b.resolveIntID(ctx, parsedID)
}
// Cut @.
username = username[1:]
p, err := b.peers.ResolveDomain(ctx, username)
if err != nil {
return nil, errors.Wrapf(err, "resolve %q", username)
}
return p, nil
}
func (b *BotAPI) resolveIntID(ctx context.Context, id int64) (peers.Peer, error) {
p, err := b.peers.ResolveTDLibID(ctx, constant.TDLibPeerID(id))
if err != nil {
return nil, errors.Wrapf(err, "find peer %d", id)
}
return p, nil
}