-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifications.go
81 lines (71 loc) · 1.68 KB
/
notifications.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
package summer
import (
"sync"
"time"
"github.com/night-codes/mgo-wrapper"
"gopkg.in/mgo.v2"
)
type (
// NotifyStruct data struct
NotifyStruct struct {
ID uint64 `json:"id" bson:"_id"`
UserID uint64 `json:"userId" bson:"userId"`
Title string `json:"title" bson:"title" binding:"required,min=3"`
Text string `json:"text" bson:"text"`
Created uint `json:"-" bson:"created"`
Updated uint `json:"-" bson:"updated"`
Deleted bool `json:"-" bson:"deleted"`
Demo bool
}
notify struct {
*Panel
list map[uint64]*NotifyStruct // key - login
collection *mgo.Collection
sync.Mutex
}
)
func (n *notify) init(panel *Panel) {
n.Mutex = sync.Mutex{}
n.Panel = panel
n.collection = mongo.DB(panel.DBName).C(panel.NotifyCollection)
n.list = map[uint64]*NotifyStruct{}
go func() {
n.tick()
for range time.Tick(time.Second * 10) {
n.tick()
}
}()
}
// Add new notify from struct
func (n *notify) Add(ntf NotifyStruct) error {
ntf.ID = n.AI.Next(n.Panel.NotifyCollection)
ntf.Created = uint(time.Now().Unix() / 60)
ntf.Updated = ntf.Created
if err := n.collection.Insert(ntf); err != nil {
return err
}
n.Lock()
defer n.Unlock()
if len(n.list) == 0 {
n.collection.EnsureIndex(mgo.Index{Key: []string{"login"}, Unique: true})
}
n.list[ntf.ID] = &ntf
return nil
}
// get array of notify
func (n *notify) tick() {
result := []NotifyStruct{}
n.collection.Find(obj{"deleted": false}).All(&result)
n.Lock()
defer n.Unlock()
n.list = map[uint64]*NotifyStruct{}
for key, ntf := range result {
n.list[ntf.ID] = &result[key]
}
}
// Length of array of notify
func (n *notify) Length() int {
n.Lock()
defer n.Unlock()
return len(n.list)
}