aboutsummaryrefslogtreecommitdiff
path: root/persistence/sessions.go
diff options
context:
space:
mode:
Diffstat (limited to 'persistence/sessions.go')
-rw-r--r--persistence/sessions.go100
1 files changed, 89 insertions, 11 deletions
diff --git a/persistence/sessions.go b/persistence/sessions.go
index 29c4918..0454d97 100644
--- a/persistence/sessions.go
+++ b/persistence/sessions.go
@@ -3,6 +3,7 @@ package persistence
import (
"github.com/pkg/errors"
"io/ioutil"
+ "sync"
"time"
"dev.narayana.im/narayana/telegabber/yamldb"
@@ -34,16 +35,18 @@ type SessionsMap struct {
// Session is a key-values subtree
type Session struct {
- Login string `yaml:":login"`
- Timezone string `yaml:":timezone"`
- KeepOnline bool `yaml:":keeponline"`
- RawMessages bool `yaml:":rawmessages"`
- AsciiArrows bool `yaml:":asciiarrows"`
- OOBMode bool `yaml:":oobmode"`
- Carbons bool `yaml:":carbons"`
- HideIds bool `yaml:":hideids"`
- Receipts bool `yaml:":receipts"`
- NativeEdits bool `yaml:":nativeedits"`
+ Login string `yaml:":login"`
+ Timezone string `yaml:":timezone"`
+ KeepOnline bool `yaml:":keeponline"`
+ RawMessages bool `yaml:":rawmessages"`
+ AsciiArrows bool `yaml:":asciiarrows"`
+ OOBMode bool `yaml:":oobmode"`
+ Carbons bool `yaml:":carbons"`
+ HideIds bool `yaml:":hideids"`
+ Receipts bool `yaml:":receipts"`
+ NativeEdits bool `yaml:":nativeedits"`
+ IgnoredChats []int64 `yaml:":ignoredchats"`
+ ignoredChatsMap map[int64]bool `yaml:"-"`
}
var configKeys = []string{
@@ -59,14 +62,21 @@ var configKeys = []string{
}
var sessionDB *SessionsYamlDB
+var sessionsLock sync.Mutex
// SessionMarshaller implementation for YamlDB
func SessionMarshaller() ([]byte, error) {
cleanedMap := SessionsMap{}
emptySessionsMap(&cleanedMap)
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
for jid, session := range sessionDB.Data.Sessions {
if session.Login != "" {
+ session.IgnoredChats = make([]int64, 0, len(session.ignoredChatsMap))
+ for chatID := range session.ignoredChatsMap {
+ session.IgnoredChats = append(session.IgnoredChats, chatID)
+ }
cleanedMap.Sessions[jid] = session
}
}
@@ -108,6 +118,16 @@ func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
emptySessionsMap(dataPtr)
}
+ // convert ignored users slice to map
+ for jid, session := range dataPtr.Sessions {
+ session.ignoredChatsMap = make(map[int64]bool)
+ for _, chatID := range session.IgnoredChats {
+ session.ignoredChatsMap[chatID] = true
+ }
+ session.IgnoredChats = nil
+ dataPtr.Sessions[jid] = session
+ }
+
return &SessionsYamlDB{
YamlDB: yamldb.YamlDB{
Path: path,
@@ -119,6 +139,13 @@ func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
// Get retrieves a session value
func (s *Session) Get(key string) (string, error) {
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
+
+ return s.get(key)
+}
+
+func (s *Session) get(key string) (string, error) {
switch key {
case "timezone":
return s.Timezone, nil
@@ -145,9 +172,12 @@ func (s *Session) Get(key string) (string, error) {
// ToMap converts the session to a map
func (s *Session) ToMap() map[string]string {
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
+
m := make(map[string]string)
for _, configKey := range configKeys {
- value, _ := s.Get(configKey)
+ value, _ := s.get(configKey)
m[configKey] = value
}
@@ -156,6 +186,9 @@ func (s *Session) ToMap() map[string]string {
// Set sets a session value
func (s *Session) Set(key string, value string) (string, error) {
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
+
switch key {
case "timezone":
s.Timezone = value
@@ -232,6 +265,51 @@ func (s *Session) TimezoneToLocation() *time.Location {
return zeroLocation
}
+// IgnoreChat adds a chat id to ignore list, returns false if already ignored
+func (s *Session) IgnoreChat(chatID int64) bool {
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
+
+ if s.ignoredChatsMap == nil {
+ s.ignoredChatsMap = make(map[int64]bool)
+ } else if _, ok := s.ignoredChatsMap[chatID]; ok {
+ return false
+ }
+
+ s.ignoredChatsMap[chatID] = true
+ return true
+}
+
+// UnignoreChat removes a chat id from ignore list, returns false if not already ignored
+func (s *Session) UnignoreChat(chatID int64) bool {
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
+
+ if s.ignoredChatsMap == nil {
+ return false
+ }
+
+ if _, ok := s.ignoredChatsMap[chatID]; !ok {
+ return false
+ }
+
+ delete(s.ignoredChatsMap, chatID)
+ return true
+}
+
+// IsChatIgnored checks the chat id against the ignore list
+func (s *Session) IsChatIgnored(chatID int64) bool {
+ sessionsLock.Lock()
+ defer sessionsLock.Unlock()
+
+ if s.ignoredChatsMap == nil {
+ return false
+ }
+
+ _, ok := s.ignoredChatsMap[chatID]
+ return ok
+}
+
func fromBool(b bool) string {
if b {
return "true"