2018-07-24 15:24:09 +02:00
|
|
|
/*
|
|
|
|
* @Author: Bartuccio Antoine
|
|
|
|
* @Date: 2018-07-24 14:41:03
|
2019-01-02 23:52:01 +01:00
|
|
|
* @Last Modified by: Bartuccio Antoine
|
2019-01-04 10:55:51 +01:00
|
|
|
* @Last Modified time: 2019-01-04 10:52:13
|
2018-07-24 15:24:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-01-02 23:52:01 +01:00
|
|
|
"fmt"
|
2018-07-24 15:24:09 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"sync"
|
2019-01-02 23:52:01 +01:00
|
|
|
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2018-07-24 15:24:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type users struct {
|
|
|
|
mutex sync.Mutex
|
2018-07-24 17:52:04 +02:00
|
|
|
data map[string]map[string]string
|
2018-07-24 15:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type usersFile struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2019-01-02 23:52:01 +01:00
|
|
|
// Users shared user for commands
|
2018-07-24 15:24:09 +02:00
|
|
|
var Users users
|
|
|
|
var uf usersFile
|
|
|
|
|
2019-01-04 10:55:51 +01:00
|
|
|
// InitUsers inits the User info storage
|
2018-07-24 15:24:09 +02:00
|
|
|
func InitUsers(users_file_path string) {
|
|
|
|
uf = usersFile{}
|
|
|
|
uf.path = users_file_path
|
|
|
|
Users = users{}
|
|
|
|
Users.mutex.Lock()
|
|
|
|
defer Users.mutex.Unlock()
|
2018-07-24 17:52:04 +02:00
|
|
|
Users.data = make(map[string]map[string]string)
|
2018-07-24 15:24:09 +02:00
|
|
|
uf.read()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an info about a given user
|
2018-07-24 17:52:04 +02:00
|
|
|
func (u users) Get(username string, key string) (string, bool) {
|
2018-07-24 15:24:09 +02:00
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
2018-07-24 17:52:04 +02:00
|
|
|
user, exists := u.data[username]
|
2018-07-24 15:24:09 +02:00
|
|
|
if !exists {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
if _, exists = user[key]; !exists {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return user[key], true
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:55:51 +01:00
|
|
|
// Set add an info about a given user
|
2018-07-24 17:52:04 +02:00
|
|
|
func (u users) Set(username string, key, data string) {
|
2018-07-24 15:24:09 +02:00
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
2018-07-24 17:52:04 +02:00
|
|
|
if _, exists := u.data[username]; !exists {
|
|
|
|
u.data[username] = make(map[string]string)
|
2018-07-24 15:24:09 +02:00
|
|
|
}
|
2018-07-24 17:52:04 +02:00
|
|
|
u.data[username][key] = data
|
2018-07-24 15:24:09 +02:00
|
|
|
go uf.write()
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:55:51 +01:00
|
|
|
// GetUsernames get all usernames stored in settings
|
2018-12-31 02:46:05 +01:00
|
|
|
func (u users) GetUsernames() []string {
|
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
|
|
|
var usernames []string
|
2019-01-04 10:55:51 +01:00
|
|
|
for username := range u.data {
|
2018-12-31 02:46:05 +01:00
|
|
|
usernames = append(usernames, username)
|
|
|
|
}
|
|
|
|
return usernames
|
|
|
|
}
|
|
|
|
|
2019-01-02 23:52:01 +01:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:24:09 +02:00
|
|
|
func (u usersFile) read() {
|
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
|
|
|
data, err := ioutil.ReadFile(u.path)
|
|
|
|
if err != nil {
|
|
|
|
// File doesn't exist, skip import
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json.Unmarshal(data, &Users.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u usersFile) write() {
|
|
|
|
u.mutex.Lock()
|
|
|
|
defer u.mutex.Unlock()
|
|
|
|
Users.mutex.Lock()
|
|
|
|
defer Users.mutex.Unlock()
|
|
|
|
data, _ := json.Marshal(Users.data)
|
|
|
|
ioutil.WriteFile(u.path, data, 0770)
|
|
|
|
}
|