/
chat.go
296 lines (247 loc) · 7.34 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package peers
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/constant"
"github.com/gotd/td/tg"
)
// Chat is chat peer.
type Chat struct {
raw *tg.Chat
m *Manager
}
// Chat creates new Chat, attached to this manager.
func (m *Manager) Chat(u *tg.Chat) Chat {
m.needsUpdate(chatPeerID(u.ID))
return Chat{
raw: u,
m: m,
}
}
// GetChat gets Chat using given id.
func (m *Manager) GetChat(ctx context.Context, id int64) (Chat, error) {
ch, err := m.getChat(ctx, id)
if err != nil {
return Chat{}, err
}
return m.Chat(ch), nil
}
// Raw returns raw *tg.Chat.
func (c Chat) Raw() *tg.Chat {
return c.raw
}
// ID returns entity ID.
func (c Chat) ID() int64 {
return c.raw.GetID()
}
// TDLibPeerID returns TDLibPeerID for this entity.
func (c Chat) TDLibPeerID() constant.TDLibPeerID {
return chatPeerID(c.raw.GetID())
}
// VisibleName returns visible name of peer.
//
// It returns FirstName + " " + LastName for users, and title for chats and channels.
func (c Chat) VisibleName() string {
return c.raw.GetTitle()
}
// Username returns peer username, if any.
func (c Chat) Username() (string, bool) {
return "", false
}
// Restricted whether this user/chat/channel is restricted.
func (c Chat) Restricted() ([]tg.RestrictionReason, bool) {
return nil, false
}
// Verified whether this user/chat/channel is verified by Telegram.
func (c Chat) Verified() bool {
return false
}
// Scam whether this user/chat/channel is probably a scam.
func (c Chat) Scam() bool {
return false
}
// Fake whether this user/chat/channel was reported by many users as a fake or scam: be
// careful when interacting with it.
func (c Chat) Fake() bool {
return false
}
// InputPeer returns input peer for this peer.
func (c Chat) InputPeer() tg.InputPeerClass {
return c.raw.AsInputPeer()
}
// Sync updates current object.
func (c Chat) Sync(ctx context.Context) error {
raw, err := c.m.updateChat(ctx, c.raw.ID)
if err != nil {
return errors.Wrap(err, "get chat")
}
*c.raw = *raw
return nil
}
// Manager returns attached Manager.
func (c Chat) Manager() *Manager {
return c.m
}
// Report reports a peer for violation of telegram's Terms of Service.
func (c Chat) Report(ctx context.Context, reason tg.ReportReasonClass, message string) error {
if _, err := c.m.api.AccountReportPeer(ctx, &tg.AccountReportPeerRequest{
Peer: c.InputPeer(),
Reason: reason,
Message: message,
}); err != nil {
return errors.Wrap(err, "report")
}
return nil
}
// Photo returns peer photo, if any.
func (c Chat) Photo(ctx context.Context) (*tg.Photo, bool, error) {
full, err := c.FullRaw(ctx)
if err != nil {
return nil, false, errors.Wrap(err, "get full chat")
}
chatPhoto, ok := full.GetChatPhoto()
if !ok {
return nil, false, nil
}
p, ok := chatPhoto.AsNotEmpty()
return p, ok, nil
}
// FullRaw returns *tg.ChatFull for this Chat.
func (c Chat) FullRaw(ctx context.Context) (*tg.ChatFull, error) {
return c.m.getChatFull(ctx, c.ID())
}
// InviteLinks returns InviteLinks for this peer.
func (c Chat) InviteLinks() InviteLinks {
return InviteLinks{
peer: c,
m: c.m,
}
}
// ToBroadcast tries to convert this Chat to Broadcast.
func (c Chat) ToBroadcast() (Broadcast, bool) {
return Broadcast{}, c.IsBroadcast()
}
// IsBroadcast whether this Chat is Broadcast.
func (c Chat) IsBroadcast() bool {
return false
}
// ToSupergroup tries to convert this Chat to Supergroup.
func (c Chat) ToSupergroup() (Supergroup, bool) {
return Supergroup{}, c.IsSupergroup()
}
// IsSupergroup whether this Chat is Supergroup.
func (c Chat) IsSupergroup() bool {
return false
}
// Creator whether the current user is the creator of this group.
func (c Chat) Creator() bool {
return c.raw.GetCreator()
}
// Left whether the current user has left this group.
func (c Chat) Left() bool {
return c.raw.GetLeft()
}
// Deactivated whether the group was migrated.
func (c Chat) Deactivated() bool {
return c.raw.GetDeactivated()
}
// CallActive whether a group call or livestream is currently active.
func (c Chat) CallActive() bool {
return c.raw.GetCallActive()
}
// CallNotEmpty whether there's anyone in the group call or livestream.
func (c Chat) CallNotEmpty() bool {
return c.raw.GetCallNotEmpty()
}
// NoForwards whether that message forwarding from this channel is not allowed.
func (c Chat) NoForwards() bool {
return c.raw.GetNoforwards()
}
// MigratedTo returns a supergroup to which this chat migrated.
func (c Chat) MigratedTo() (tg.InputChannelClass, bool) {
return c.raw.GetMigratedTo()
}
// ParticipantsCount returns count of participants.
func (c Chat) ParticipantsCount() int {
return c.raw.GetParticipantsCount()
}
// AdminRights returns admin rights of the user in this channel.
//
// See https://core.telegram.org/api/rights.
func (c Chat) AdminRights() (tg.ChatAdminRights, bool) {
// TODO(tdakkota): add wrapper for raw object?
return c.raw.GetAdminRights()
}
// DefaultBannedRights returns default chat rights.
//
// See https://core.telegram.org/api/rights.
func (c Chat) DefaultBannedRights() (tg.ChatBannedRights, bool) {
// TODO(tdakkota): add wrapper for raw object?
return c.raw.GetDefaultBannedRights()
}
// ActualChat returns Channel to which this chat migrated.
//
// Also see MigratedTo.
func (c Chat) ActualChat(ctx context.Context) (Channel, bool, error) {
m, ok := c.MigratedTo()
if !ok {
return Channel{}, false, nil
}
ch, err := c.m.GetChannel(ctx, m)
if err != nil {
return Channel{}, false, errors.Wrap(err, "get actual chat")
}
return ch, true, nil
}
// Leave leaves this chat.
func (c Chat) Leave(ctx context.Context) error {
return c.deleteMe(ctx, false)
}
// SetTitle sets new title for this Chat.
func (c Chat) SetTitle(ctx context.Context, title string) error {
if _, err := c.m.api.MessagesEditChatTitle(ctx, &tg.MessagesEditChatTitleRequest{
ChatID: c.ID(),
Title: title,
}); err != nil {
return errors.Wrap(err, "edit chat title")
}
return nil
}
// SetDescription sets new description for this Chat.
func (c Chat) SetDescription(ctx context.Context, about string) error {
return c.m.editAbout(ctx, c.InputPeer(), about)
}
// SetReactions sets list of available reactions.
//
// Empty list disables reactions at all.
func (c Chat) SetReactions(ctx context.Context, reactions ...tg.ReactionClass) error {
return c.m.editReactions(ctx, c.InputPeer(), &tg.ChatReactionsSome{
Reactions: reactions,
})
}
// DisableReactions disables reactions.
func (c Chat) DisableReactions(ctx context.Context) error {
return c.m.editReactions(ctx, c.InputPeer(), &tg.ChatReactionsNone{})
}
// LeaveAndDelete leaves this chat and removes the entire chat history of this user in this chat.
func (c Chat) LeaveAndDelete(ctx context.Context) error {
return c.deleteMe(ctx, true)
}
func (c Chat) deleteMe(ctx context.Context, revokeHistory bool) error {
return c.deleteUser(ctx, &tg.InputUserSelf{}, revokeHistory)
}
func (c Chat) deleteUser(ctx context.Context, user tg.InputUserClass, revokeHistory bool) error {
if _, err := c.m.api.MessagesDeleteChatUser(ctx, &tg.MessagesDeleteChatUserRequest{
RevokeHistory: revokeHistory,
ChatID: c.raw.GetID(),
UserID: user,
}); err != nil {
_, self := user.(*tg.InputUserSelf)
if self {
return errors.Wrapf(err, "leave (revoke: %v)", revokeHistory)
}
return errors.Wrapf(err, "delete user (revoke: %v)", revokeHistory)
}
return nil
}
// TODO(tdakkota): add more getters, helpers and convertors