Encode auto notify into the user data
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2019-01-02 19:24:27 +01:00
parent 019a57e7cf
commit dd7a39a158
Signed by: klmp200
GPG Key ID: E7245548C53F904B
1 changed files with 35 additions and 4 deletions

View File

@ -24,6 +24,7 @@ type userSubscription struct {
AutoNotify bool
}
// subscriptions are stored in format ChatID(int64),AutoNotify(bool):ChatID,AutoNotify
func getUserSubscribedChats(username string) []userSubscription {
serializedSubscriptions, exists := shared.Users.Get(username, "subscribed_chats")
@ -32,13 +33,37 @@ func getUserSubscribedChats(username string) []userSubscription {
}
subscriptions := []userSubscription{}
for _, chatID := range strings.Split(serializedSubscriptions, ":") {
if id, err := strconv.ParseInt(chatID, 10, 64); err == nil {
for _, sub := range strings.Split(serializedSubscriptions, ":") {
splitedSub := strings.Split(sub, ",")
// malformated
if len(splitedSub) == 0 {
continue
}
chatID, err := strconv.ParseInt(splitedSub[0], 10, 64)
if err != nil {
continue
}
// only ChatID
if len(splitedSub) == 1 {
subscriptions = append(subscriptions, userSubscription{
ChatID: id,
ChatID: chatID,
AutoNotify: true,
})
continue
}
autoNotify, err := strconv.ParseBool(splitedSub[1])
if err != nil {
autoNotify = true
}
subscriptions = append(subscriptions, userSubscription{
ChatID: chatID,
AutoNotify: autoNotify,
})
}
return subscriptions
@ -48,7 +73,13 @@ func setUserSubscribedChats(username string, subscriptions []userSubscription) {
subs := make([]string, len(subscriptions))
for i, sub := range subscriptions {
subs[i] = strconv.FormatInt(sub.ChatID, 10)
subs[i] = strings.Join(
[]string{
strconv.FormatInt(sub.ChatID, 10),
strconv.FormatBool(sub.AutoNotify),
},
",",
)
}
shared.Users.Set(username, "subscribed_chats", strings.Join(subs, ":"))