-
Notifications
You must be signed in to change notification settings - Fork 0
/
subhandlers.go
83 lines (70 loc) · 2.07 KB
/
subhandlers.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
package internet
import (
"errors"
log "github.com/Sirupsen/logrus"
"github.com/getblank/blank-router/berrors"
"github.com/getblank/blank-router/intranet"
"github.com/getblank/blank-router/taskq"
"github.com/getblank/wango"
)
func subUserHandler(c *wango.Conn, uri string, args ...interface{}) (interface{}, error) {
extra := c.GetExtra()
if extra == nil {
return nil, berrors.ErrForbidden
}
cred, ok := extra.(credentials)
if !ok {
log.WithField("extra", extra).Warn("Invalid type of extra on connection when sub com.user handler")
return nil, berrors.ErrError
}
return nil, intranet.AddSubscription(cred.apiKey, c.ID(), uri, nil)
}
func subConfigHandler(c *wango.Conn, uri string, args ...interface{}) (interface{}, error) {
extra := c.GetExtra()
if extra == nil {
return nil, berrors.ErrForbidden
}
cred := extra.(credentials)
t := taskq.Task{
Type: taskq.UserConfig,
UserID: cred.userID,
}
resChan := taskq.Push(t)
res := <-resChan
if res.Err != "" {
return nil, errors.New(res.Err)
}
return res.Result, nil
}
func subStoresHandler(c *wango.Conn, uri string, args ...interface{}) (interface{}, error) {
// TODO: task to check permissions
extra := c.GetExtra()
if extra == nil {
return nil, berrors.ErrForbidden
}
cred, ok := extra.(credentials)
if !ok {
log.WithField("extra", extra).Warn("Invalid type of extra on connection when sub stores handler")
return nil, berrors.ErrError
}
var data interface{}
if len(args) > 0 {
data = args[0]
}
return nil, intranet.AddSubscription(cred.apiKey, c.ID(), uri, data)
}
func unsubStoresHandler(c *wango.Conn, uri string, args ...interface{}) (interface{}, error) {
extra := c.GetExtra()
if extra == nil {
return nil, berrors.ErrForbidden
}
cred, ok := extra.(credentials)
if !ok {
log.WithField("extra", extra).Warn("Invalid type of extra on connection when unsub stores handler")
return nil, berrors.ErrError
}
return nil, intranet.DeleteSubscription(cred.apiKey, c.ID(), uri)
}
func pubConfigHandler(uri string, event interface{}, extra interface{}) (bool, interface{}) {
return true, nil
}