-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
delete.go
81 lines (67 loc) · 2.12 KB
/
delete.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
package message
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/telegram/message/peer"
"github.com/gotd/td/tg"
)
// DeleteBuilder is a intermediate builder to delete messages.
// Unlike RevokeBuilder will keep messages for other users.
type DeleteBuilder struct {
sender *Sender
}
// Delete creates new DeleteBuilder.
func (s *Sender) Delete() *DeleteBuilder {
return &DeleteBuilder{sender: s}
}
// Messages deletes messages by given IDs, but keeps it for other users.
//
// NB: Telegram counts message IDs globally for private chats (but not for channels). This method does not check that
// all given message IDs from one chat.
func (b *DeleteBuilder) Messages(ctx context.Context, ids ...int) (*tg.MessagesAffectedMessages, error) {
r, err := b.sender.deleteMessages(ctx, &tg.MessagesDeleteMessagesRequest{
ID: ids,
})
if err != nil {
return nil, xerrors.Errorf("delete messages: %w", err)
}
return r, nil
}
// RevokeBuilder is a intermediate builder to delete messages.
// Unlike DeleteBuilder will not keep messages for other users.
type RevokeBuilder struct {
builder *RequestBuilder
}
// Revoke creates new RevokeBuilder.
func (b *RequestBuilder) Revoke() *RevokeBuilder {
return &RevokeBuilder{builder: b}
}
// Messages deletes messages by given IDs.
//
// NB: Telegram counts message IDs globally for private chats (but not for channels). This method does not check that
// all given message IDs from one chat.
func (b *RevokeBuilder) Messages(ctx context.Context, ids ...int) (*tg.MessagesAffectedMessages, error) {
p, err := b.builder.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
ch, isChannel := peer.ToInputChannel(p)
if isChannel {
r, err := b.builder.sender.deleteChannelMessages(ctx, &tg.ChannelsDeleteMessagesRequest{
Channel: ch,
ID: ids,
})
if err != nil {
return nil, xerrors.Errorf("delete channel messages: %w", err)
}
return r, nil
}
r, err := b.builder.sender.deleteMessages(ctx, &tg.MessagesDeleteMessagesRequest{
Revoke: true,
ID: ids,
})
if err != nil {
return nil, xerrors.Errorf("delete messages: %w", err)
}
return r, nil
}