This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
70 lines (56 loc) · 1.37 KB
/
store.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
package teledisq
import (
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
const (
SubscriptionKind = "Subscription"
)
type Subscription struct {
ChatID int64
Theme string
}
func SubscribeChat(ctx context.Context, chat int64, theme string) (bool, error) {
key := datastore.NewKey(ctx, SubscriptionKind, "", chat, nil)
s := Subscription{}
err := datastore.Get(ctx, key, &s)
if err == datastore.ErrNoSuchEntity {
_, err := datastore.Put(ctx, key, &Subscription{ChatID: chat, Theme: theme})
if err != nil {
return false, err
}
return true, nil
}
if err != nil {
return false, err
}
return false, nil
}
func UnsubscribeChat(ctx context.Context, chat int64, _ string) (bool, error) {
key := datastore.NewKey(ctx, SubscriptionKind, "", chat, nil)
err := datastore.Delete(ctx, key)
if err == datastore.ErrNoSuchEntity {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func NotifySubscribersByTheme(ctx context.Context, theme string, notify Notify) {
q := datastore.NewQuery(SubscriptionKind).Filter("Theme=", theme)
for t := q.Run(ctx); ; {
var s Subscription
_, err := t.Next(&s)
if err == datastore.Done {
break
}
if err != nil {
log.Errorf(ctx, "Problem on subscribers query %s", err.Error())
return
}
// Send notification
notify(s.ChatID)
}
}