-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboosts.go
More file actions
143 lines (120 loc) · 3.9 KB
/
Copy pathboosts.go
File metadata and controls
143 lines (120 loc) · 3.9 KB
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
package botapi
import (
"context"
"github.com/gotd/td/tg"
)
// ChatBoostSource is a sealed union describing how a chat boost was obtained.
//
// Concrete variants: ChatBoostSourcePremium, ChatBoostSourceGiftCode,
// ChatBoostSourceGiveaway.
type ChatBoostSource interface {
isChatBoostSource()
}
// ChatBoostSourcePremium is a boost from a user subscribing to Telegram Premium
// or gifting a Premium subscription to another user.
type ChatBoostSourcePremium struct {
Source string `json:"source"`
User User `json:"user"`
}
// ChatBoostSourceGiftCode is a boost from a user using a gift code made by the
// chat (for example as part of a giveaway).
type ChatBoostSourceGiftCode struct {
Source string `json:"source"`
User User `json:"user"`
}
// ChatBoostSourceGiveaway is a boost from a giveaway prize created by the chat.
type ChatBoostSourceGiveaway struct {
Source string `json:"source"`
// GiveawayMessageID is the id of the message with the giveaway; it may be 0
// if the message was deleted.
GiveawayMessageID int `json:"giveaway_message_id"`
// User is the user that won the prize, if any.
User *User `json:"user,omitempty"`
// IsUnclaimed reports whether the giveaway prize was not claimed.
IsUnclaimed bool `json:"is_unclaimed,omitempty"`
}
func (ChatBoostSourcePremium) isChatBoostSource() {}
func (ChatBoostSourceGiftCode) isChatBoostSource() {}
func (ChatBoostSourceGiveaway) isChatBoostSource() {}
// Chat boost source discriminators.
const (
chatBoostSourcePremium = "premium"
chatBoostSourceGiftCode = "gift_code"
chatBoostSourceGiveaway = "giveaway"
)
// ChatBoost describes a boost applied to a chat.
type ChatBoost struct {
// BoostID is the unique identifier of the boost.
BoostID string `json:"boost_id"`
// AddDate is the Unix time when the chat was boosted.
AddDate int `json:"add_date"`
// ExpirationDate is the Unix time when the boost will automatically expire.
ExpirationDate int `json:"expiration_date"`
// Source is the source of the added boost.
Source ChatBoostSource `json:"source"`
}
// chatBoostFromTg converts an MTProto boost into the Bot API ChatBoost. users
// resolves the booster by id.
func chatBoostFromTg(boost tg.Boost, users map[int64]*tg.User) ChatBoost {
var booster User
if u, ok := users[boost.UserID]; ok {
booster = userFromTgUser(u)
} else {
booster = User{ID: boost.UserID}
}
var source ChatBoostSource
switch {
case boost.Giveaway:
g := ChatBoostSourceGiveaway{
Source: chatBoostSourceGiveaway,
GiveawayMessageID: boost.GiveawayMsgID,
IsUnclaimed: boost.Unclaimed,
}
if boost.UserID != 0 {
source = withGiveawayUser(g, booster)
} else {
source = g
}
case boost.Gift:
source = ChatBoostSourceGiftCode{Source: chatBoostSourceGiftCode, User: booster}
default:
source = ChatBoostSourcePremium{Source: chatBoostSourcePremium, User: booster}
}
return ChatBoost{
BoostID: boost.ID,
AddDate: boost.Date,
ExpirationDate: boost.Expires,
Source: source,
}
}
// withGiveawayUser attaches the winning user to a giveaway boost source.
func withGiveawayUser(g ChatBoostSourceGiveaway, user User) ChatBoostSourceGiveaway {
u := user
g.User = &u
return g
}
// GetUserChatBoosts returns the list of boosts added to a chat by a user. The
// bot must be an administrator in the chat.
func (b *Bot) GetUserChatBoosts(ctx context.Context, chat ChatID, userID int64) ([]ChatBoost, error) {
peer, err := b.resolveInputPeer(ctx, chat)
if err != nil {
return nil, err
}
user, err := b.resolveInputUser(ctx, userID)
if err != nil {
return nil, err
}
res, err := b.raw.PremiumGetUserBoosts(ctx, &tg.PremiumGetUserBoostsRequest{
Peer: peer,
UserID: user,
})
if err != nil {
return nil, asAPIError(err)
}
users := usersByID(res.Users)
out := make([]ChatBoost, 0, len(res.Boosts))
for _, boost := range res.Boosts {
out = append(out, chatBoostFromTg(boost, users))
}
return out, nil
}