-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.go
185 lines (162 loc) · 8.53 KB
/
agent.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package push
import (
"context"
"fmt"
"github.com/cestlascorpion/offlinepush/core"
"github.com/cestlascorpion/offlinepush/proto"
)
type Agent interface {
PushSingleByCid(ctx context.Context, request *SingleReq, token string) (map[string]map[string]string, error)
PushSingleByAlias(ctx context.Context, request *SingleReq, token string) (map[string]map[string]string, error)
PushBatchByCid(ctx context.Context, request *BatchReq, token string) (map[string]map[string]string, error)
PushBatchByAlias(ctx context.Context, request *BatchReq, token string) (map[string]map[string]string, error)
CreateMsg(ctx context.Context, request *CreateReq, token string) (string, error)
PushListByCid(ctx context.Context, request *ListReq, token string) (map[string]map[string]string, error)
PushListByAlias(ctx context.Context, request *ListReq, token string) (map[string]map[string]string, error)
PushAll(ctx context.Context, request *AllReq, token string) (string, error)
PushByTag(ctx context.Context, request *ByTagReq, token string) (string, error)
PushByFastCustomTag(ctx context.Context, request *ByTagReq, token string) (string, error)
StopPush(ctx context.Context, taskId, token string) (bool, error)
DeleteScheduleTask(ctx context.Context, taskId, token string) (bool, error)
QueryScheduleTask(ctx context.Context, taskId, token string) (map[string]string, error)
QueryDetail(ctx context.Context, taskId, cId, token string) ([][2]string, error)
Close()
}
type AgentMgr struct {
agents map[core.UniqueId]Agent
}
func NewAgentMgr() (*AgentMgr, error) {
return &AgentMgr{
agents: make(map[core.UniqueId]Agent),
}, nil
}
func (m *AgentMgr) RegisterAgent(uniqueId core.UniqueId, agent Agent) error {
m.agents[uniqueId] = agent
return nil
}
func (m *AgentMgr) PushSingle(ctx context.Context, uniqueId core.UniqueId, request *SingleReq, token string) (map[string]map[string]string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return nil, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
if len(request.Audience.Cid) != 0 {
return agent.PushSingleByCid(ctx, request, token)
}
return agent.PushSingleByAlias(ctx, request, token)
}
func (m *AgentMgr) PushBatch(ctx context.Context, uniqueId core.UniqueId, request *BatchReq, token string) (map[string]map[string]string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return nil, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
if len(request.MsgList[0].Audience.Cid) != 0 {
return agent.PushBatchByCid(ctx, request, token)
}
return agent.PushBatchByAlias(ctx, request, token)
}
func (m *AgentMgr) CreateMsg(ctx context.Context, uniqueId core.UniqueId, request *CreateReq, token string) (string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return "", fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
return agent.CreateMsg(ctx, request, token)
}
func (m *AgentMgr) PushList(ctx context.Context, uniqueId core.UniqueId, request *ListReq, token string) (map[string]map[string]string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return nil, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
if len(request.Audience.Cid) != 0 {
return agent.PushListByCid(ctx, request, token)
}
return agent.PushListByAlias(ctx, request, token)
}
func (m *AgentMgr) PushAll(ctx context.Context, uniqueId core.UniqueId, request *AllReq, token string) (string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return "", fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
return agent.PushAll(ctx, request, token)
}
func (m *AgentMgr) PushByTag(ctx context.Context, uniqueId core.UniqueId, request *ByTagReq, token string) (string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return "", fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
if len(request.Audience.FastCustomTag) != 0 {
return agent.PushByFastCustomTag(ctx, request, token)
}
return agent.PushByTag(ctx, request, token)
}
func (m *AgentMgr) StopPush(ctx context.Context, uniqueId core.UniqueId, taskId, token string) (bool, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return false, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
return agent.StopPush(ctx, taskId, token)
}
func (m *AgentMgr) DeleteScheduleTask(ctx context.Context, uniqueId core.UniqueId, taskId, token string) (bool, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return false, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
return agent.DeleteScheduleTask(ctx, taskId, token)
}
func (m *AgentMgr) QueryScheduleTask(ctx context.Context, uniqueId core.UniqueId, taskId, token string) (map[string]string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return nil, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
return agent.QueryScheduleTask(ctx, taskId, token)
}
func (m *AgentMgr) QueryDetail(ctx context.Context, uniqueId core.UniqueId, taskId, cId, token string) ([][2]string, error) {
agent, ok := m.agents[uniqueId]
if !ok {
return nil, fmt.Errorf("unsupported uniqueId %s", uniqueId)
}
return agent.QueryDetail(ctx, taskId, cId, token)
}
func (m *AgentMgr) Close() {
for _, agent := range m.agents {
agent.Close()
}
}
type SingleReq struct {
RequestId string `json:"request_id"` // 必须字段,请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
Audience *proto.Audience `json:"audience"` // 必须字段,cid数组,只能填一个cid
Settings *proto.Settings `json:"settings,omitempty"` // 非必须,推送条件设置
PushMessage *proto.PushMessage `json:"push_message"` // 必须字段,个推推送消息参数
PushChannel *proto.PushChannel `json:"push_channel,omitempty"` // 非必须,厂商推送消息参数,包含ios消息参数,android厂商消息参数
}
type BatchReq struct {
IsAsync bool `json:"is_async,omitempty"` // 非必须,默认值:false,是否异步推送,异步推送不会返回data,is_async为false时返回data
MsgList []*SingleReq `json:"msg_list"` // 必须,默认值:无,消息内容,数组长度不大于 200
}
type CreateReq struct {
RequestId string `json:"request_id,omitempty"` // 非必须,请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
GroupName string `json:"group_name,omitempty"` // 非必须,任务组名
Settings *proto.Settings `json:"settings,omitempty"` // 非必须,推送条件设置
PushMessage *proto.PushMessage `json:"push_message"` // 必须字段,个推推送消息参数
PushChannel *proto.PushChannel `json:"push_channel,omitempty"` // 非必须,厂商推送消息参数,包含ios消息参数,android厂商消息参数
}
type ListReq struct {
Audience *proto.Audience `json:"audience"` // 必须字段,用cid数组,多个cid,注意这里!!数组长度不大于200
IsAsync bool `json:"is_async,omitempty"` // 非必须,默认值:false,是否异步推送,异步推送不会返回data,is_async为false时返回data
TaskId string `json:"taskid"` // 必须字段,默认值:无,使用创建消息接口返回的taskId,可以多次使用
}
type AllReq struct {
RequestId string `json:"request_id"` // 必须,请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
GroupName string `json:"group_name,omitempty"` // 非必须,任务组名
Audience string `json:"audience"` // 必须字段,必须为all
Settings *proto.Settings `json:"settings,omitempty"` // 非必须,推送条件设置
PushMessage *proto.PushMessage `json:"push_message"` // 必须字段,个推推送消息参数
PushChannel *proto.PushChannel `json:"push_channel,omitempty"` // 非必须,厂商推送消息参数,包含ios消息参数,android厂商消息参数
}
type ByTagReq struct {
RequestId string `json:"request_id"` // 必须,请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
GroupName string `json:"group_name,omitempty"` // 非必须,任务组名
Settings *proto.Settings `json:"settings,omitempty"` // 非必须,推送条件设置
Audience *proto.Audience `json:"audience"` // 必须字段,tag数组
PushMessage *proto.PushMessage `json:"push_message"` // 必须字段,个推推送消息参数
PushChannel *proto.PushChannel `json:"push_channel,omitempty"` // 非必须,厂商推送消息参数,包含ios消息参数,android厂商消息参数
}