/
push_rules.go
162 lines (137 loc) · 4.54 KB
/
push_rules.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
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package processors
import (
"context"
"fmt"
"github.com/finogeeks/ligase/common"
"github.com/finogeeks/ligase/common/config"
"github.com/finogeeks/ligase/dbupdates/dbregistry"
"github.com/finogeeks/ligase/dbupdates/dbupdatetypes"
"github.com/finogeeks/ligase/model/dbtypes"
"github.com/finogeeks/ligase/skunkworks/log"
"github.com/finogeeks/ligase/storage/model"
)
func init() {
dbregistry.Register("push_rules", NewDBPushRulesProcessor, NewCachePushRulesProcessor)
}
type DBPushRulesProcessor struct {
name string
cfg *config.Dendrite
db model.PushAPIDatabase
}
func NewDBPushRulesProcessor(
name string,
cfg *config.Dendrite,
) dbupdatetypes.DBEventSeqProcessor {
p := new(DBPushRulesProcessor)
p.name = name
p.cfg = cfg
return p
}
func (p *DBPushRulesProcessor) Start() {
db, err := common.GetDBInstance("pushapi", p.cfg)
if err != nil {
log.Panicf("failed to connect to pushapi db")
}
p.db = db.(model.PushAPIDatabase)
}
func (p *DBPushRulesProcessor) Process(ctx context.Context, inputs []dbupdatetypes.DBEventDataInput) error {
if len(inputs) == 0 {
return nil
}
switch inputs[0].Event.Key {
case dbtypes.PushRuleUpsertKey:
p.processUpsert(ctx, inputs)
case dbtypes.PushRuleDeleteKey:
p.processDelete(ctx, inputs)
default:
log.Errorf("invalid %s event key %d", p.name, inputs[0].Event.Key)
}
return nil
}
func (p *DBPushRulesProcessor) processUpsert(ctx context.Context, inputs []dbupdatetypes.DBEventDataInput) error {
for _, v := range inputs {
msg := v.Event.PushDBEvents.PushRuleInert
err := p.db.OnAddPushRule(ctx, msg.UserID, msg.RuleID, msg.PriorityClass, msg.Priority, msg.Conditions, msg.Actions)
if err != nil {
log.Error(p.name, "upsert err", err, msg.UserID, msg.RuleID, msg.PriorityClass, msg.Priority, msg.Conditions, msg.Actions)
}
}
return nil
}
func (p *DBPushRulesProcessor) processDelete(ctx context.Context, inputs []dbupdatetypes.DBEventDataInput) error {
for _, v := range inputs {
msg := v.Event.PushDBEvents.PushRuleDelete
err := p.db.OnDeletePushRule(ctx, msg.UserID, msg.RuleID)
if err != nil {
log.Error(p.name, "upsert err", err, msg.UserID, msg.RuleID)
}
}
return nil
}
type CachePushRulesProcessor struct {
name string
cfg *config.Dendrite
pool dbupdatetypes.Pool
}
func NewCachePushRulesProcessor(name string, cfg *config.Dendrite, pool dbupdatetypes.Pool) dbupdatetypes.CacheProcessor {
p := new(CachePushRulesProcessor)
p.name = name
p.cfg = cfg
p.pool = pool
return p
}
func (p *CachePushRulesProcessor) Start() {
}
func (p *CachePushRulesProcessor) Process(ctx context.Context, input dbupdatetypes.CacheInput) error {
key := input.Event.Key
data := input.Event.PushDBEvents
switch key {
case dbtypes.PushRuleUpsertKey:
return p.onPushRuleUpsert(ctx, data.PushRuleInert)
case dbtypes.PushRuleDeleteKey:
return p.onPushRuleDelete(ctx, data.PushRuleDelete)
}
return nil
}
func (p *CachePushRulesProcessor) onPushRuleUpsert(ctx context.Context, msg *dbtypes.PushRuleInert) error {
conn := p.pool.Pool().Get()
defer conn.Close()
ruleKey := fmt.Sprintf("%s:%s:%s", "push_rule", msg.UserID, msg.RuleID)
err := conn.Send("hmset", ruleKey, "user_name", msg.UserID, "rule_id", msg.RuleID, "priority_class", msg.PriorityClass,
"priority", msg.Priority, "conditions", msg.Conditions, "actions", msg.Actions)
if err != nil {
return err
}
err = conn.Send("hmset", fmt.Sprintf("%s:%s", "push_rule_user_list", msg.UserID), ruleKey, ruleKey)
if err != nil {
return err
}
return conn.Flush()
}
func (p *CachePushRulesProcessor) onPushRuleDelete(ctx context.Context, msg *dbtypes.PushRuleDelete) error {
conn := p.pool.Pool().Get()
defer conn.Close()
ruleKey := fmt.Sprintf("%s:%s:%s", "push_rule", msg.UserID, msg.RuleID)
err := conn.Send("del", ruleKey)
if err != nil {
return err
}
err = conn.Send("hdel", fmt.Sprintf("%s:%s", "push_rule_user_list", msg.UserID), ruleKey)
if err != nil {
return err
}
return conn.Flush()
}