-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
scheduled.go
110 lines (90 loc) · 2.66 KB
/
scheduled.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
package message
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/tg"
)
// ScheduledManager is a scheduled messages manager.
type ScheduledManager struct {
peer peerPromise
sender *Sender
}
// Send sends scheduled messages.
func (m *ScheduledManager) Send(ctx context.Context, id int, ids ...int) (tg.UpdatesClass, error) {
p, err := m.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
upd, err := m.sender.sendScheduledMessages(ctx, &tg.MessagesSendScheduledMessagesRequest{
Peer: p,
ID: append([]int{id}, ids...),
})
if err != nil {
return nil, xerrors.Errorf("send scheduled messages: %w", err)
}
return upd, nil
}
// Delete deletes scheduled messages.
func (m *ScheduledManager) Delete(ctx context.Context, id int, ids ...int) (tg.UpdatesClass, error) {
p, err := m.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
upd, err := m.sender.deleteScheduledMessages(ctx, &tg.MessagesDeleteScheduledMessagesRequest{
Peer: p,
ID: append([]int{id}, ids...),
})
if err != nil {
return nil, xerrors.Errorf("delete scheduled messages: %w", err)
}
return upd, nil
}
// Get gets scheduled messages.
func (m *ScheduledManager) Get(ctx context.Context, id int, ids ...int) (tg.ModifiedMessagesMessages, error) {
p, err := m.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
msgs, err := m.sender.getScheduledMessages(ctx, &tg.MessagesGetScheduledMessagesRequest{
Peer: p,
ID: append([]int{id}, ids...),
})
if err != nil {
return nil, xerrors.Errorf("get scheduled messages: %w", err)
}
modified, ok := msgs.AsModified()
if !ok {
return nil, xerrors.Errorf("unexpected type %T", msgs)
}
return modified, nil
}
// HistoryWithHash gets scheduled messages history.
func (m *ScheduledManager) HistoryWithHash(ctx context.Context, hash int) (tg.ModifiedMessagesMessages, error) {
p, err := m.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
msgs, err := m.sender.getScheduledHistory(ctx, &tg.MessagesGetScheduledHistoryRequest{
Peer: p,
Hash: hash,
})
if err != nil {
return nil, xerrors.Errorf("get scheduled messages history: %w", err)
}
modified, ok := msgs.AsModified()
if !ok {
return nil, xerrors.Errorf("unexpected type %T", msgs)
}
return modified, nil
}
// History gets scheduled messages history.
func (m *ScheduledManager) History(ctx context.Context) (tg.ModifiedMessagesMessages, error) {
return m.HistoryWithHash(ctx, 0)
}
// Scheduled creates new ScheduledManager using resolved peer.
func (b *RequestBuilder) Scheduled() *ScheduledManager {
return &ScheduledManager{
peer: b.peer,
sender: b.sender,
}
}