Use a dedicated struct for user subscriptions

This commit is contained in:
Antoine Bartuccio 2019-01-02 19:06:47 +01:00
parent f6f17e7a7b
commit 019a57e7cf
Signed by: klmp200
GPG Key ID: E7245548C53F904B
1 changed files with 26 additions and 15 deletions

View File

@ -19,28 +19,36 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)
func getUserSubscribedChats(username string) []int64 {
type userSubscription struct {
ChatID int64
AutoNotify bool
}
func getUserSubscribedChats(username string) []userSubscription {
serializedSubscriptions, exists := shared.Users.Get(username, "subscribed_chats")
if !exists {
return []int64{}
return []userSubscription{}
}
subscriptions := []int64{}
subscriptions := []userSubscription{}
for _, chatID := range strings.Split(serializedSubscriptions, ":") {
if id, err := strconv.ParseInt(chatID, 10, 64); err == nil {
subscriptions = append(subscriptions, id)
subscriptions = append(subscriptions, userSubscription{
ChatID: id,
AutoNotify: true,
})
}
}
return subscriptions
}
func setUserSubscribedChats(username string, subscriptions []int64) {
func setUserSubscribedChats(username string, subscriptions []userSubscription) {
subs := make([]string, len(subscriptions))
for i, sub := range subscriptions {
subs[i] = strconv.FormatInt(sub, 10)
subs[i] = strconv.FormatInt(sub.ChatID, 10)
}
shared.Users.Set(username, "subscribed_chats", strings.Join(subs, ":"))
@ -92,7 +100,7 @@ func Subscribe(m *tb.Message) {
alreadySuscribed := false
for _, sub := range subscriptions {
if sub == m.Chat.ID {
if sub.ChatID == m.Chat.ID {
alreadySuscribed = true
break
}
@ -104,7 +112,10 @@ func Subscribe(m *tb.Message) {
}
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
subscriptions = append(subscriptions, m.Chat.ID)
subscriptions = append(subscriptions, userSubscription{
ChatID: m.Chat.ID,
AutoNotify: true,
})
setUserSubscribedChats(m.Sender.Username, subscriptions)
}
@ -130,11 +141,11 @@ func Unsubscribe(m *tb.Message) {
return
}
filteredSubscriptions := []int64{}
filteredSubscriptions := []userSubscription{}
wasSuscribed := false
for _, sub := range subscriptions {
if sub == m.Chat.ID {
if sub.ChatID == m.Chat.ID {
wasSuscribed = true
continue
}
@ -236,7 +247,7 @@ func Unpublish(m *tb.Message) {
// If performed in Group Chat : retrieved all published messages for this chat
// Command syntax : /retrieve
func Retrieve(m *tb.Message) {
chatList := []int64{}
chatList := []userSubscription{}
hasMessage := false
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate {
@ -255,13 +266,13 @@ func Retrieve(m *tb.Message) {
}
if m.Chat.Type == tb.ChatGroup || m.Chat.Type == tb.ChatSuperGroup {
chatList = append(chatList, m.Chat.ID)
chatList = append(chatList, userSubscription{ChatID: m.Chat.ID})
}
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
for _, chatID := range chatList {
messages := getPublishedMessages(chatID)
for _, sub := range chatList {
messages := getPublishedMessages(sub.ChatID)
if len(messages) > 0 {
hasMessage = true
}
@ -283,7 +294,7 @@ func getSubscribers(chatID int64) []string {
for _, username := range shared.Users.GetUsernames() {
subscriptions := getUserSubscribedChats(username)
for _, sub := range subscriptions {
if sub == chatID {
if sub.ChatID == chatID {
subscribers = append(subscribers, username)
break
}