From dd7a39a158447c10268de9a065d89e34dacb2756 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 2 Jan 2019 19:24:27 +0100 Subject: [PATCH] Encode auto notify into the user data --- commands/subscribe.go | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/commands/subscribe.go b/commands/subscribe.go index e6c05e7..9bce285 100644 --- a/commands/subscribe.go +++ b/commands/subscribe.go @@ -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, ":"))