-
Notifications
You must be signed in to change notification settings - Fork 12
/
pro-users.go
118 lines (105 loc) · 2.14 KB
/
pro-users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2021 Alexey Krivonogov. All rights reserved.
package main
import (
"bytes"
"encoding/gob"
"fmt"
"os"
"path/filepath"
"time"
"github.com/kataras/golog"
)
type UserPro struct {
Forms map[string][]map[string]interface{}
path string
}
var (
usersPro = make(map[uint32]*UserPro)
usersPath string
)
func LoadUsers(path string) {
usersPath = path
for _, u := range proStorage.Users {
var user UserPro
path := filepath.Join(path, fmt.Sprintf("%x.pro", u.ID))
if _, err := os.Stat(path); err == nil {
if data, err := os.ReadFile(path); err == nil {
user.path = path
dec := gob.NewDecoder(bytes.NewBuffer(data))
if err = dec.Decode(&user); err != nil {
golog.Error(err)
}
}
}
usersPro[u.ID] = &user
}
}
func DeleteUser(id uint32) {
if u, ok := usersPro[id]; ok {
if len(u.path) > 0 {
os.Remove(u.path)
}
delete(usersPro, id)
}
}
func SetUserForms(id uint32, ref string, m map[string]interface{}) error {
var (
user *UserPro
ok bool
)
if user, ok = usersPro[id]; !ok {
var u UserPro
user = &u
usersPro[id] = user
}
if user.Forms == nil {
user.Forms = make(map[string][]map[string]interface{})
}
if _, ok := m[`_name`]; !ok {
m[`_name`] = ``
}
m[`_time`] = time.Now().Unix()
if !m["_afcheck"].(bool) {
m[`_name`] = `---`
}
name := m[`_name`]
ret := []map[string]interface{}{m}
for _, v := range user.Forms[ref] {
if fmt.Sprint(v["_name"]) == name || !v["_afcheck"].(bool) {
continue
}
if t, ok := v["_time"]; ok {
prev := time.Unix(t.(int64), 0)
if time.Now().After(prev.AddDate(0, 0, 50)) {
continue
}
} else {
continue
}
ret = append(ret, v)
if len(ret) == 7 {
break
}
}
user.Forms[ref] = ret
return SaveUserSettings(id)
}
func SaveUserSettings(id uint32) error {
if u, ok := usersPro[id]; ok {
if len(u.path) == 0 {
u.path = filepath.Join(usersPath, fmt.Sprintf("%x.pro", id))
}
var (
data bytes.Buffer
err error
)
enc := gob.NewEncoder(&data)
if err = enc.Encode(u); err != nil {
return err
}
if err = os.WriteFile(u.path, data.Bytes(), 0777 /*os.ModePerm*/); err != nil {
return err
}
}
return nil
}