-
Notifications
You must be signed in to change notification settings - Fork 7
/
tag.go
92 lines (76 loc) · 2.11 KB
/
tag.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
package main
import (
"log"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
)
type tagJSON struct {
UUID string `json:"uuid" binding:"required"`
TagList []string `json:"tag_list" binding:"required"`
}
// SetTag 把uuid和tag绑定起来
func SetTag(c *gin.Context) {
var jsonDict tagJSON
if c.BindJSON(&jsonDict) != nil {
Fail(c, 400, nil, "Invalid json object.")
return
}
conn := redisPool.Get()
defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", genUUIDKey(jsonDict.UUID)))
if !exists {
Fail(c, 400, nil, "The given UUID not valid.")
return
}
conn.Send("MULTI")
for _, tag := range jsonDict.TagList {
conn.Send("ZADD", genTagListKey(jsonDict.UUID), "NX", 1, tag)
conn.Send("ZADD", genTagKey(tag), "NX", 1, jsonDict.UUID)
}
conn.Send("EXEC")
err = conn.Flush()
if err != nil {
log.Printf("failed to update tag: %s", err)
Fail(c, 500, nil, "Failed to update tag.")
}
Success(c, 200, nil, "")
}
type pushByTagJSON struct {
Tag string `json:"tag" binding:"required"`
Content string `json:"content" binding:"required"`
}
// PushByTag 按tag推送
func PushByTag(c *gin.Context) {
var jsonDict pushByTagJSON
if c.BindJSON(&jsonDict) != nil {
Fail(c, 400, nil, "Invalid json object.")
return
}
conn := redisPool.Get()
defer conn.Close()
// 分批次推送
count, err := redis.Int(conn.Do("ZCARD", genTagKey(jsonDict.Tag)))
if err != nil {
log.Printf("failed to get by tag(%s) with error: %s", jsonDict.Tag, err)
return
}
for i := 0; i < count; i += (*step) {
// get uuid list
uuidList, err := redis.Strings(conn.Do("ZRANGE", genTagKey(jsonDict.Tag), i, i+(*step)))
if err != nil {
log.Printf("failed to get by tag(%s) with error: %s", jsonDict.Tag, err)
continue
}
// get device token list
uuidTagList := make([]interface{}, *step)
for i, uuid := range uuidList {
uuidTagList[i] = genUUIDKey(uuid)
}
deviceTokenList, err := redis.Strings(conn.Do("MGET", uuidTagList...))
// sent it to push daemon
for i, uuid := range uuidList {
push(conn, deviceTokenList[i], uuid, jsonDict.Content)
}
}
Success(c, 200, nil, "")
}