forked from Sora233/DDBOT
/
notify.go
171 lines (160 loc) · 4.95 KB
/
notify.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package lsp
import (
"context"
"fmt"
"github.com/Mrs4s/MiraiGo/message"
"github.com/starskim/DDBOT/lsp/concern"
"github.com/starskim/DDBOT/lsp/mmsg"
"github.com/starskim/DDBOT/utils"
"github.com/starskim/DDBOT/utils/msgstringer"
"github.com/sirupsen/logrus"
"runtime/debug"
"time"
)
func (l *Lsp) ConcernNotify() {
defer func() {
if err := recover(); err != nil {
logger.WithField("stack", string(debug.Stack())).Errorf("concern notify recoverd %v", err)
go l.ConcernNotify()
}
}()
l.wg.Add(1)
defer l.wg.Done()
for {
select {
case _inotify, ok := <-l.concernNotify:
if !ok {
return
}
if _inotify == nil {
continue
}
var inotify = _inotify
target := mmsg.NewGroupTarget(inotify.GetGroupCode())
nLogger := inotify.Logger()
if l.LspStateManager.IsMuted(inotify.GetGroupCode(), utils.GetBot().GetUin()) {
nLogger.Info("BOT群内被禁言,跳过本次推送")
continue
}
c, err := concern.GetConcernBySiteAndType(inotify.Site(), inotify.Type())
if err != nil {
nLogger.Errorf("GetConcernBySiteAndType error %v", err)
continue
}
cfg := c.GetStateManager().GetGroupConcernConfig(inotify.GetGroupCode(), inotify.GetUid())
cfg.NotifyBeforeCallback(inotify)
// 注意notify可能会缓存MSG
var m = l.NotifyMessage(inotify).Clone()
// atConfig
var atBeforeHook = cfg.AtBeforeHook(inotify)
if !atBeforeHook.Pass {
nLogger.WithField("Reason", atBeforeHook.Reason).Debug("notify @at filtered by hook AtBeforeHook")
} else {
// 有@全体成员 或者 @Someone
var qqadmin = atBeforeHook.Pass &&
l.PermissionStateManager.CheckGroupAdministrator(inotify.GetGroupCode(), utils.GetBot().GetUin())
var checkAtAll = qqadmin &&
cfg.GetGroupConcernAt().CheckAtAll(inotify.Type())
var atAllMark = checkAtAll &&
c.GetStateManager().CheckAndSetAtAllMark(inotify.GetGroupCode(), inotify.GetUid())
nLogger.WithFields(logrus.Fields{
"qqAdmin": qqadmin,
"checkAtAll": checkAtAll,
"atMark": atAllMark,
}).Trace("at_all condition")
if atBeforeHook.Pass && qqadmin && checkAtAll && atAllMark {
nLogger = nLogger.WithField("at_all", true)
newAtAllMsg(m)
} else {
ids := cfg.GetGroupConcernAt().GetAtSomeoneList(inotify.Type())
nLogger = nLogger.WithField("at_QQ", ids)
newAtIdsMsg(m, ids)
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
if err := l.msgLimit.Acquire(ctx, 1); err != nil {
cancel()
nLogger.WithField("Content", msgstringer.MsgToString(m.Elements())).
Errorf("BOT负载过高,推送已积压超过一分钟,将舍弃本次推送。")
continue
}
cancel()
l.notifyWg.Add(1)
nLogger.Info("notify")
go func() {
defer l.notifyWg.Done()
defer func() {
l.msgLimit.Release(1)
if e := recover(); e != nil {
nLogger.WithField("stack", string(debug.Stack())).
Errorf("notify panic recovered: %v", e)
}
}()
msgs := l.GM(l.SendMsg(m, target))
if len(msgs) > 0 {
cfg.NotifyAfterCallback(inotify, msgs[0])
} else {
cfg.NotifyAfterCallback(inotify, nil)
}
if atBeforeHook.Pass {
var atIdsOnce bool
for _, msg := range msgs {
if msg.Id == -1 {
// 检查有没有@全体成员
e := utils.MessageFilter(msg.Elements, func(element message.IMessageElement) bool {
return element.Type() == message.At && element.(*message.AtElement).Target == 0
})
if len(e) == 0 {
continue
}
// 2022/09/24 现在@全员不会再作为单独一条消息
// 有@全体成员的消息应该去掉之后重试
secondM := mmsg.NewMSGFromGroupMessage(msg)
secondM.Drop(func(e message.IMessageElement, _ int) bool {
return e.Type() == message.At && e.(*message.AtElement).Target == 0
})
secondRes := l.GM(l.SendMsg(secondM, target))
// secondRes一定是一条
if len(secondRes) != 1 {
panic(fmt.Sprintf("INTERNAL: len(secondRes) is %v", len(secondRes)))
}
if secondRes[0].Id == -1 {
// 去掉@全员还是发送失败
continue
}
if !atIdsOnce {
// 去掉@全员之后发送成功,可能是次数到了,尝试@列表
atIdsOnce = true
}
}
}
if atIdsOnce {
ids := cfg.GetGroupConcernAt().GetAtSomeoneList(inotify.Type())
if len(ids) != 0 {
nLogger = nLogger.WithField("at_QQ", ids)
nLogger.Debug("notify atAll failed, try at someone")
l.SendMsg(newAtIdsMsg(mmsg.NewMSG(), ids), target)
} else {
nLogger.Debug("notify atAll failed, at someone not config")
}
}
}
}()
}
}
}
func (l *Lsp) NotifyMessage(inotify concern.Notify) *mmsg.MSG {
return inotify.ToMessage()
}
func newAtAllMsg(m *mmsg.MSG) *mmsg.MSG {
return m.AtAll(true)
}
func newAtIdsMsg(m *mmsg.MSG, ids []int64) *mmsg.MSG {
if len(ids) > 0 {
m.Cut()
for _, id := range ids {
m.At(id)
}
}
return m
}