-
Notifications
You must be signed in to change notification settings - Fork 6
/
response.go
76 lines (70 loc) · 1.67 KB
/
response.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
package control
import (
"errors"
"strconv"
)
// InitResponse ...
func (manager *Manager[CTX]) initResponse() error {
return manager.D.Create("__resp", &ResponseGroup{})
}
var respCache = make(map[int64]string)
// Response opens the resp of the gid
func (manager *Manager[CTX]) Response(gid int64) error {
if manager.CanResponse(gid) {
return errors.New("group " + strconv.FormatInt(gid, 10) + " already in response")
}
manager.Lock()
respCache[gid] = ""
err := manager.D.Insert("__resp", &ResponseGroup{GroupID: gid})
manager.Unlock()
return err
}
// Silence will drop its extra data
func (manager *Manager[CTX]) Silence(gid int64) error {
if !manager.CanResponse(gid) {
return errors.New("group " + strconv.FormatInt(gid, 10) + " already in silence")
}
manager.Lock()
respCache[gid] = "-"
err := manager.D.Del("__resp", "where gid = ?", gid)
manager.Unlock()
return err
}
// CanResponse ...
func (manager *Manager[CTX]) CanResponse(gid int64) bool {
manager.RLock()
ext, ok := respCache[0] // all status
manager.RUnlock()
if ok && ext != "-" {
return true
}
manager.RLock()
ext, ok = respCache[gid]
manager.RUnlock()
if ok {
return ext != "-"
}
var rsp ResponseGroup
manager.RLock()
err := manager.D.Find("__resp", &rsp, "where gid = 0") // all status
manager.RUnlock()
if err == nil && rsp.Extra != "-" {
manager.Lock()
respCache[0] = rsp.Extra
manager.Unlock()
return true
}
manager.RLock()
err = manager.D.Find("__resp", &rsp, "where gid = ?", gid)
manager.RUnlock()
if err != nil {
manager.Lock()
respCache[gid] = "-"
manager.Unlock()
return false
}
manager.Lock()
respCache[gid] = rsp.Extra
manager.Unlock()
return rsp.Extra != "-"
}