mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-01-18 18:46:44 +01:00
Use a dedicated struct for user subscriptions
This commit is contained in:
parent
f6f17e7a7b
commit
019a57e7cf
@ -19,28 +19,36 @@ import (
|
|||||||
tb "gopkg.in/tucnak/telebot.v2"
|
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")
|
serializedSubscriptions, exists := shared.Users.Get(username, "subscribed_chats")
|
||||||
if !exists {
|
if !exists {
|
||||||
return []int64{}
|
return []userSubscription{}
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriptions := []int64{}
|
subscriptions := []userSubscription{}
|
||||||
for _, chatID := range strings.Split(serializedSubscriptions, ":") {
|
for _, chatID := range strings.Split(serializedSubscriptions, ":") {
|
||||||
if id, err := strconv.ParseInt(chatID, 10, 64); err == nil {
|
if id, err := strconv.ParseInt(chatID, 10, 64); err == nil {
|
||||||
subscriptions = append(subscriptions, id)
|
subscriptions = append(subscriptions, userSubscription{
|
||||||
|
ChatID: id,
|
||||||
|
AutoNotify: true,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return subscriptions
|
return subscriptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func setUserSubscribedChats(username string, subscriptions []int64) {
|
func setUserSubscribedChats(username string, subscriptions []userSubscription) {
|
||||||
|
|
||||||
subs := make([]string, len(subscriptions))
|
subs := make([]string, len(subscriptions))
|
||||||
for i, sub := range 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, ":"))
|
shared.Users.Set(username, "subscribed_chats", strings.Join(subs, ":"))
|
||||||
@ -92,7 +100,7 @@ func Subscribe(m *tb.Message) {
|
|||||||
alreadySuscribed := false
|
alreadySuscribed := false
|
||||||
|
|
||||||
for _, sub := range subscriptions {
|
for _, sub := range subscriptions {
|
||||||
if sub == m.Chat.ID {
|
if sub.ChatID == m.Chat.ID {
|
||||||
alreadySuscribed = true
|
alreadySuscribed = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -104,7 +112,10 @@ func Subscribe(m *tb.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
|
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)
|
setUserSubscribedChats(m.Sender.Username, subscriptions)
|
||||||
}
|
}
|
||||||
@ -130,11 +141,11 @@ func Unsubscribe(m *tb.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filteredSubscriptions := []int64{}
|
filteredSubscriptions := []userSubscription{}
|
||||||
wasSuscribed := false
|
wasSuscribed := false
|
||||||
|
|
||||||
for _, sub := range subscriptions {
|
for _, sub := range subscriptions {
|
||||||
if sub == m.Chat.ID {
|
if sub.ChatID == m.Chat.ID {
|
||||||
wasSuscribed = true
|
wasSuscribed = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -236,7 +247,7 @@ func Unpublish(m *tb.Message) {
|
|||||||
// If performed in Group Chat : retrieved all published messages for this chat
|
// If performed in Group Chat : retrieved all published messages for this chat
|
||||||
// Command syntax : /retrieve
|
// Command syntax : /retrieve
|
||||||
func Retrieve(m *tb.Message) {
|
func Retrieve(m *tb.Message) {
|
||||||
chatList := []int64{}
|
chatList := []userSubscription{}
|
||||||
hasMessage := false
|
hasMessage := false
|
||||||
|
|
||||||
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate {
|
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 {
|
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 ---")
|
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
|
||||||
|
|
||||||
for _, chatID := range chatList {
|
for _, sub := range chatList {
|
||||||
messages := getPublishedMessages(chatID)
|
messages := getPublishedMessages(sub.ChatID)
|
||||||
if len(messages) > 0 {
|
if len(messages) > 0 {
|
||||||
hasMessage = true
|
hasMessage = true
|
||||||
}
|
}
|
||||||
@ -283,7 +294,7 @@ func getSubscribers(chatID int64) []string {
|
|||||||
for _, username := range shared.Users.GetUsernames() {
|
for _, username := range shared.Users.GetUsernames() {
|
||||||
subscriptions := getUserSubscribedChats(username)
|
subscriptions := getUserSubscribedChats(username)
|
||||||
for _, sub := range subscriptions {
|
for _, sub := range subscriptions {
|
||||||
if sub == chatID {
|
if sub.ChatID == chatID {
|
||||||
subscribers = append(subscribers, username)
|
subscribers = append(subscribers, username)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user