mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-01-18 18:46:44 +01:00
154 lines
3.0 KiB
Go
154 lines
3.0 KiB
Go
/*
|
|
* @Author: Bartuccio Antoine
|
|
* @Date: 2018-07-24 01:27:11
|
|
* @Last Modified by: Bartuccio Antoine
|
|
* @Last Modified time: 2019-01-05 17:45:18
|
|
*/
|
|
|
|
package shared
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"sync"
|
|
)
|
|
|
|
type history struct {
|
|
mutex sync.Mutex
|
|
size int
|
|
data map[int64][]string
|
|
}
|
|
|
|
type historyFile struct {
|
|
mutex sync.Mutex
|
|
path string
|
|
}
|
|
|
|
// History manages acces to chat history
|
|
var History *history
|
|
var hf historyFile
|
|
|
|
// InitHistory init a chit history of a given size
|
|
func InitHistory(size int, historyFilePath string) {
|
|
|
|
hf = historyFile{}
|
|
hf.path = historyFilePath
|
|
History = &history{}
|
|
|
|
History.mutex.Lock()
|
|
defer History.mutex.Unlock()
|
|
|
|
History.size = size
|
|
History.data = make(map[int64][]string)
|
|
hf.read()
|
|
}
|
|
|
|
// Size get the number of messages saved in the history
|
|
func (h *history) Size() int {
|
|
|
|
h.mutex.Lock()
|
|
defer h.mutex.Unlock()
|
|
|
|
return h.size
|
|
}
|
|
|
|
// Message get a selected message in a chat history
|
|
func (h *history) Message(chatID int64, n int) string {
|
|
|
|
h.mutex.Lock()
|
|
defer h.mutex.Unlock()
|
|
|
|
array, exists := h.data[chatID]
|
|
if exists && n >= 0 && n < len(array) {
|
|
return array[n]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// AddMessage append a message to a given chat
|
|
func (h *history) AddMessage(chatID int64, m string) {
|
|
|
|
h.mutex.Lock()
|
|
defer h.mutex.Unlock()
|
|
|
|
if _, exists := h.data[chatID]; !exists {
|
|
h.data[chatID] = make([]string, h.size, h.size)
|
|
}
|
|
h.append(chatID, m)
|
|
}
|
|
|
|
// LastMessage get the last message of a given chat
|
|
func (h *history) LastMessage(chatID int64) string {
|
|
|
|
h.mutex.Lock()
|
|
defer h.mutex.Unlock()
|
|
|
|
if _, exists := h.data[chatID]; exists {
|
|
return h.data[chatID][h.size-1]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ChatHistory get a copy of a given chat history
|
|
func (h *history) ChatHistory(chatID int64) []string {
|
|
|
|
h.mutex.Lock()
|
|
defer h.mutex.Unlock()
|
|
|
|
array, exists := h.data[chatID]
|
|
if exists {
|
|
c := make([]string, h.size, h.size)
|
|
copy(c, array)
|
|
return c
|
|
}
|
|
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)
|
|
array, _ := h.data[chatID]
|
|
copy(c, array[1:])
|
|
for i, val := range c {
|
|
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
|
|
}
|
|
if err := json.Unmarshal(data, &History.data); err != nil {
|
|
fmt.Printf("Error while unmarshaling user history with path %s, error : %v\n", h.path, err)
|
|
}
|
|
}
|
|
|
|
func (h *historyFile) write() {
|
|
|
|
h.mutex.Lock()
|
|
defer h.mutex.Unlock()
|
|
|
|
History.mutex.Lock()
|
|
defer History.mutex.Unlock()
|
|
|
|
data, err := json.Marshal(History.data)
|
|
if err != nil {
|
|
fmt.Printf("Error while marshaling history file with path %s, error : %v\n", h.path, err)
|
|
return
|
|
}
|
|
if err := ioutil.WriteFile(h.path, data, 0770); err != nil {
|
|
fmt.Printf("Error writing history file with path %s, error %v\n", h.path, err)
|
|
}
|
|
}
|