This commit is contained in:
KLIPFEL Arthur 2018-07-25 11:57:15 +02:00
parent 54c3bc8f59
commit d5a7922779
13 changed files with 360 additions and 37 deletions

14
shared/bot.go Normal file
View file

@ -0,0 +1,14 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 11:56:47
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 11:58:34
*/
package shared
import (
tb "gopkg.in/tucnak/telebot.v2"
)
var Bot *tb.Bot

View file

@ -2,12 +2,14 @@
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 01:27:11
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 02:09:53
* @Last Modified time: 2018-07-24 12:54:21
*/
package shared
import (
"encoding/json"
"io/ioutil"
"sync"
)
@ -17,22 +19,34 @@ type history struct {
data map[int64][]string
}
var History history
type historyFile struct {
mutex sync.Mutex
path string
}
func InitHistory(size int) {
var History history
var hf historyFile
// Init a chat history of a given size
func InitHistory(size int, history_file_path string) {
hf = historyFile{}
hf.path = history_file_path
History = history{}
History.mutex.Lock()
defer History.mutex.Unlock()
History.size = size
History.data = make(map[int64][]string)
hf.read()
}
// Get the number of messages saved in the history
func (h history) Size() int {
h.mutex.Lock()
defer h.mutex.Unlock()
return h.size
}
// Get a selected message in a chat history
func (h history) Message(chatID int64, n int) string {
h.mutex.Lock()
defer h.mutex.Unlock()
@ -43,6 +57,7 @@ func (h history) Message(chatID int64, n int) string {
return ""
}
// Append a message to a given chat
func (h history) AddMessage(chatID int64, m string) {
h.mutex.Lock()
defer h.mutex.Unlock()
@ -52,6 +67,7 @@ func (h history) AddMessage(chatID int64, m string) {
h.append(chatID, m)
}
// Get the last message of a given chat
func (h history) LastMessage(chatID int64) string {
h.mutex.Lock()
defer h.mutex.Unlock()
@ -61,6 +77,7 @@ func (h history) LastMessage(chatID int64) string {
return ""
}
// Get a copy of a given chat history
func (h history) ChatHistory(chatID int64) []string {
h.mutex.Lock()
defer h.mutex.Unlock()
@ -73,6 +90,7 @@ func (h history) ChatHistory(chatID int64) []string {
return nil
}
// Add a message at the end of a chat and move everithyng up
// Assert that the slice exists and mutex already locked
func (h history) append(chatID int64, m string) {
c := make([]string, h.size-1, h.size-1)
@ -82,4 +100,25 @@ func (h history) append(chatID int64, m string) {
array[i] = val
}
array[h.size-1] = m
go hf.write()
}
func (h historyFile) read() {
h.mutex.Lock()
defer h.mutex.Unlock()
data, err := ioutil.ReadFile(h.path)
if err != nil {
// File doesn't exist, skip import
return
}
json.Unmarshal(data, &History.data)
}
func (h historyFile) write() {
h.mutex.Lock()
defer h.mutex.Unlock()
History.mutex.Lock()
defer History.mutex.Unlock()
data, _ := json.Marshal(History.data)
ioutil.WriteFile(h.path, data, 0770)
}

82
shared/users.go Normal file
View file

@ -0,0 +1,82 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2018-07-24 14:41:03
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-24 17:49:51
*/
package shared
import (
"encoding/json"
"io/ioutil"
"sync"
)
type users struct {
mutex sync.Mutex
data map[string]map[string]string
}
type usersFile struct {
mutex sync.Mutex
path string
}
var Users users
var uf usersFile
func InitUsers(users_file_path string) {
uf = usersFile{}
uf.path = users_file_path
Users = users{}
Users.mutex.Lock()
defer Users.mutex.Unlock()
Users.data = make(map[string]map[string]string)
uf.read()
}
// Get an info about a given user
func (u users) Get(username string, key string) (string, bool) {
u.mutex.Lock()
defer u.mutex.Unlock()
user, exists := u.data[username]
if !exists {
return "", false
}
if _, exists = user[key]; !exists {
return "", false
}
return user[key], true
}
// Add an info about a given user
func (u users) Set(username string, key, data string) {
u.mutex.Lock()
defer u.mutex.Unlock()
if _, exists := u.data[username]; !exists {
u.data[username] = make(map[string]string)
}
u.data[username][key] = data
go uf.write()
}
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)
}