Add auto push notification on users when publish is used

This commit is contained in:
Bartuccio Antoine 2019-01-02 23:52:01 +01:00
parent dd7a39a158
commit a8d8b6d69e
No known key found for this signature in database
GPG key ID: E7245548C53F904B
5 changed files with 166 additions and 26 deletions

View file

@ -1,16 +1,19 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 14:41:03
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 17:49:51
* @Last Modified by: Bartuccio Antoine
* @Last Modified time: 2019-01-02 22:37:58
*/
package shared
import (
"encoding/json"
"fmt"
"io/ioutil"
"sync"
tb "gopkg.in/tucnak/telebot.v2"
)
type users struct {
@ -23,6 +26,7 @@ type usersFile struct {
path string
}
// Users shared user for commands
var Users users
var uf usersFile
@ -72,6 +76,31 @@ func (u users) GetUsernames() []string {
return usernames
}
// GetUserChat retrieve the chat of the user if registered
func (u users) GetUserChat(username string) (*tb.Chat, error) {
serializedChat, exists := u.Get(username, "private_chat")
if !exists {
return nil, fmt.Errorf("No private chat registered for %s", username)
}
chat := &tb.Chat{}
if json.Unmarshal([]byte(serializedChat), chat) != nil {
return nil, fmt.Errorf("Error while parsing chat for %s", username)
}
return chat, nil
}
// SetUserChat register a private chat for an user
func (u users) SetUserChat(username string, chat *tb.Chat) {
serializedChat, err := json.Marshal(chat)
if err != nil {
return
}
u.Set(username, "private_chat", string(serializedChat))
}
func (u usersFile) read() {
u.mutex.Lock()
defer u.mutex.Unlock()