-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.go
258 lines (232 loc) · 7.77 KB
/
chat.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package botapi
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"
"github.com/gotd/botapi/internal/oas"
)
func convertToBotAPIChatPermissions(p tg.ChatBannedRights) oas.ChatPermissions {
return oas.ChatPermissions{
CanSendMessages: oas.NewOptBool(p.SendMessages),
CanSendAudios: oas.NewOptBool(p.SendAudios),
CanSendVideos: oas.NewOptBool(p.SendVideos),
CanSendDocuments: oas.NewOptBool(p.SendDocs),
CanSendPhotos: oas.NewOptBool(p.SendPhotos),
CanManageTopics: oas.NewOptBool(p.ManageTopics),
CanSendPolls: oas.NewOptBool(p.SendPolls),
CanSendOtherMessages: oas.NewOptBool(p.SendGames || p.SendStickers || p.SendInline),
CanAddWebPagePreviews: oas.NewOptBool(p.EmbedLinks),
CanChangeInfo: oas.NewOptBool(p.ChangeInfo),
CanInviteUsers: oas.NewOptBool(p.InviteUsers),
CanPinMessages: oas.NewOptBool(p.PinMessages),
}
}
func (b *BotAPI) checkJoinRequest(
ctx context.Context,
chatID oas.ID, userID int64,
cb func(p peers.InviteLinks, ctx context.Context, user tg.InputUserClass) error,
) (*oas.Result, error) {
p, err := b.resolveIDToChat(ctx, chatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
user, err := b.resolveUserID(ctx, userID)
if err != nil {
return nil, errors.Wrap(err, "resolve userID")
}
if err := cb(p.InviteLinks(), ctx, user.InputUser()); err != nil {
return nil, err
}
return resultOK(true), nil
}
// ApproveChatJoinRequest implements oas.Handler.
func (b *BotAPI) ApproveChatJoinRequest(ctx context.Context, req *oas.ApproveChatJoinRequest) (*oas.Result, error) {
return b.checkJoinRequest(ctx, req.ChatID, req.UserID, peers.InviteLinks.ApproveJoin)
}
// DeclineChatJoinRequest implements oas.Handler.
func (b *BotAPI) DeclineChatJoinRequest(ctx context.Context, req *oas.DeclineChatJoinRequest) (*oas.Result, error) {
return b.checkJoinRequest(ctx, req.ChatID, req.UserID, peers.InviteLinks.DeclineJoin)
}
// DeleteChatPhoto implements oas.Handler.
func (b *BotAPI) DeleteChatPhoto(ctx context.Context, req *oas.DeleteChatPhoto) (*oas.Result, error) {
p, err := b.resolveIDToChat(ctx, req.ChatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
switch p := p.(type) {
case peers.Channel:
_, err = b.raw.ChannelsEditPhoto(ctx, &tg.ChannelsEditPhotoRequest{
Channel: p.InputChannel(),
Photo: &tg.InputChatPhotoEmpty{},
})
case peers.Chat:
_, err = b.raw.MessagesEditChatPhoto(ctx, &tg.MessagesEditChatPhotoRequest{
ChatID: p.ID(),
Photo: &tg.InputChatPhotoEmpty{},
})
default:
return nil, errors.Errorf("unexpected type %T", p)
}
if err != nil {
return nil, errors.Wrap(err, "delete photo")
}
return resultOK(true), nil
}
// DeleteChatStickerSet implements oas.Handler.
func (b *BotAPI) DeleteChatStickerSet(ctx context.Context, req *oas.DeleteChatStickerSet) (*oas.Result, error) {
p, err := b.resolveIDToChat(ctx, req.ChatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
ch, ok := p.(peers.Channel)
if !ok {
return nil, &BadRequestError{Message: "Bad Request: method is available only for supergroups"}
}
s, ok := ch.ToSupergroup()
if !ok {
return nil, &BadRequestError{Message: "Bad Request: method is available only for supergroups"}
}
if err := s.ResetStickerSet(ctx); err != nil {
return nil, err
}
return resultOK(true), nil
}
// GetChat implements oas.Handler.
func (b *BotAPI) GetChat(ctx context.Context, req *oas.GetChat) (*oas.ResultChat, error) {
p, err := b.resolveID(ctx, req.ChatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
var (
id = p.TDLibPeerID()
chat oas.Chat
)
switch p := p.(type) {
case peers.User:
chat = fillBotAPIChatPrivate(p)
raw := p.Raw()
full, err := p.FullRaw(ctx)
if err != nil {
return nil, errors.Wrap(err, "get full")
}
chat.Photo = b.setUserPhoto(id, raw.AccessHash, raw.Photo)
chat.Bio = optString(full.GetAbout)
if full.PrivateForwardName != "" {
chat.HasPrivateForwards.SetTo(true)
}
case peers.Chat:
chat = fillBotAPIChatGroup(p)
full, err := p.FullRaw(ctx)
if err != nil {
return nil, errors.Wrap(err, "get full")
}
chat.Photo = b.setChatPhoto(id, 0, p.Raw().Photo)
chat.Description = oas.NewOptString(full.GetAbout())
if invite, ok := full.GetExportedInvite(); ok {
switch invite := invite.(type) {
case *tg.ChatInviteExported:
chat.InviteLink.SetTo(invite.Link)
case *tg.ChatInvitePublicJoinRequests:
// TODO: handle?
}
}
// TODO(tdakkota): resolve pinned.
if v, ok := p.DefaultBannedRights(); ok {
chat.Permissions.SetTo(convertToBotAPIChatPermissions(v))
}
// TODO(tdakkota): set AllMembersAreAdministrators
case peers.Channel:
chat = fillBotAPIChatGroup(p)
raw := p.Raw()
full, err := p.FullRaw(ctx)
if err != nil {
return nil, errors.Wrap(err, "get full")
}
chat.Photo = b.setChatPhoto(id, raw.AccessHash, raw.Photo)
chat.Description = oas.NewOptString(full.GetAbout())
if invite, ok := full.GetExportedInvite(); ok {
switch invite := invite.(type) {
case *tg.ChatInviteExported:
chat.InviteLink.SetTo(invite.Link)
case *tg.ChatInvitePublicJoinRequests:
// TODO: handle?
}
}
// TODO(tdakkota): resolve pinned.
if v, ok := p.DefaultBannedRights(); ok {
chat.Permissions.SetTo(convertToBotAPIChatPermissions(v))
}
if s, ok := full.GetSlowmodeSeconds(); ok {
chat.SlowModeDelay.SetTo(s)
}
if s, ok := full.GetStickerset(); ok {
chat.StickerSetName.SetTo(s.ShortName)
}
chat.LinkedChatID = optInt64(full.GetLinkedChatID)
if loc, ok := full.Location.(*tg.ChannelLocation); ok {
if p, ok := loc.GeoPoint.AsNotEmpty(); ok {
chat.Location.SetTo(oas.ChatLocation{
Location: convertToBotAPILocation(p),
Address: loc.Address,
})
}
}
// TODO(tdakkota): set AllMembersAreAdministrators
}
return &oas.ResultChat{
Result: oas.NewOptChat(chat),
Ok: true,
}, nil
}
// SetChatAdministratorCustomTitle implements oas.Handler.
func (b *BotAPI) SetChatAdministratorCustomTitle(ctx context.Context, req *oas.SetChatAdministratorCustomTitle) (*oas.Result, error) {
return nil, &NotImplementedError{}
}
// SetChatDescription implements oas.Handler.
func (b *BotAPI) SetChatDescription(ctx context.Context, req *oas.SetChatDescription) (*oas.Result, error) {
p, err := b.resolveIDToChat(ctx, req.ChatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
if err := p.SetDescription(ctx, req.Description.Value); err != nil {
return nil, err
}
return resultOK(true), nil
}
// SetChatPermissions implements oas.Handler.
func (b *BotAPI) SetChatPermissions(ctx context.Context, req *oas.SetChatPermissions) (*oas.Result, error) {
return nil, &NotImplementedError{}
}
// SetChatPhoto implements oas.Handler.
func (b *BotAPI) SetChatPhoto(ctx context.Context, req *oas.SetChatPhoto) (*oas.Result, error) {
return nil, &NotImplementedError{}
}
// SetChatStickerSet implements oas.Handler.
func (b *BotAPI) SetChatStickerSet(ctx context.Context, req *oas.SetChatStickerSet) (*oas.Result, error) {
return nil, &NotImplementedError{}
}
// SetChatTitle implements oas.Handler.
func (b *BotAPI) SetChatTitle(ctx context.Context, req *oas.SetChatTitle) (*oas.Result, error) {
p, err := b.resolveIDToChat(ctx, req.ChatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
if err := p.SetTitle(ctx, req.Title); err != nil {
return nil, err
}
return resultOK(true), nil
}
// LeaveChat implements oas.Handler.
func (b *BotAPI) LeaveChat(ctx context.Context, req *oas.LeaveChat) (*oas.Result, error) {
p, err := b.resolveIDToChat(ctx, req.ChatID)
if err != nil {
return nil, errors.Wrap(err, "resolve chatID")
}
if !p.Left() {
if err := p.Leave(ctx); err != nil {
return nil, err
}
}
return resultOK(true), nil
}