@@ -42,6 +42,23 @@ func (c *Cache) Set(key string, value string) error {
42
42
return nil
43
43
}
44
44
45
+ func (c * Cache ) GetTyped (key string , v interface {}) error {
46
+ s , err := c .Get (key )
47
+ if err != nil {
48
+ return err
49
+ }
50
+
51
+ return json .Unmarshal ([]byte (s ), v )
52
+ }
53
+
54
+ func (c * Cache ) SetTyped (key string , v interface {}) error {
55
+ b , err := json .Marshal (v )
56
+ if err != nil {
57
+ return err
58
+ }
59
+ return c .Set (key , string (b ))
60
+ }
61
+
45
62
func (c * Cache ) Inc (key string , by int64 ) (int64 , error ) {
46
63
return c .Rdb .IncrBy (c .Ctx , key , by ).Result ()
47
64
}
@@ -70,14 +87,16 @@ func (c *Cache) Subscribe(send chan internal.Command, token, channel string, clo
70
87
return
71
88
}
72
89
73
- // for non DB events we change the type to MsgTypeChanOut
74
- if ! msg .IsDBEvent () {
90
+ // TODO: this will need more thinking
91
+ if msg .Type == internal . MsgTypeChanIn {
75
92
msg .Type = internal .MsgTypeChanOut
76
- } else if c .HasPermission (token , channel , msg .Data ) == false {
93
+ } else if msg .IsSystemEvent {
94
+
95
+ } else if msg .IsDBEvent () && c .HasPermission (token , channel , msg .Data ) == false {
77
96
continue
78
97
}
79
98
send <- msg
80
- case _ = <- close :
99
+ case <- close :
81
100
_ = pubsub .Close ()
82
101
return
83
102
}
@@ -93,6 +112,22 @@ func (c *Cache) Publish(msg internal.Command) error {
93
112
ctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
94
113
defer cancel ()
95
114
115
+ // Publish the event to system so server-side function can trigger
116
+ go func (sysmsg internal.Command ) {
117
+ sysmsg .IsSystemEvent = true
118
+ b , err := json .Marshal (sysmsg )
119
+ if err != nil {
120
+ log .Println ("error marshaling the system msg: " , err )
121
+ return
122
+ }
123
+
124
+ sysctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
125
+ defer cancel ()
126
+ if err := c .Rdb .Publish (sysctx , "sbsys" , string (b )).Err (); err != nil {
127
+ log .Println ("error publishing to system channel: " , err )
128
+ }
129
+ }(msg )
130
+
96
131
return c .Rdb .Publish (ctx , msg .Channel , string (b )).Err ()
97
132
}
98
133
@@ -129,8 +164,8 @@ func (c *Cache) PublishDocument(channel, typ string, v interface{}) {
129
164
}
130
165
131
166
func (c * Cache ) HasPermission (token , repo , payload string ) bool {
132
- me , ok := internal .Tokens [ token ]
133
- if ! ok {
167
+ var me internal.Auth
168
+ if err := c . GetTyped ( token , & me ); err != nil {
134
169
return false
135
170
}
136
171
0 commit comments