-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.go
145 lines (114 loc) · 3.77 KB
/
notification.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package insapp
import (
"time"
"gopkg.in/mgo.v2/bson"
)
// NotificationUser defines how to model a NotificationUser
type NotificationUser struct {
ID bson.ObjectId `bson:"_id,omitempty"`
UserId bson.ObjectId `json:"userid"`
Token string `json:"token" bson:",omitempty"`
Os string `json:"os"`
}
// Notification defines how to model a Notification
type Notification struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Sender bson.ObjectId `json:"sender"`
Receiver bson.ObjectId `json:"receiver"`
Content bson.ObjectId `json:"content"`
Comment Comment `json:"comment,omitempty" bson:",omitempty"`
Message string `json:"message"`
Seen bool `json:"seen"`
Date time.Time `json:"date"`
Type string `json:"type"`
}
type Notifications []Notification
func GetNotificationUserForUser(userID bson.ObjectId) NotificationUser {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification_user")
var result NotificationUser
db.Find(bson.M{"userid": userID}).One(&result)
return result
}
func CreateOrUpdateNotificationUser(user NotificationUser) {
if len(user.Token) == 0 {
return
}
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification_user")
res, _ := db.Find(bson.M{"token": user.Token}).Count()
if res > 0 {
db.Update(bson.M{"token": user.Token}, bson.M{"$set": bson.M{"token": nil}})
}
res, _ = db.Find(bson.M{"userid": user.UserId}).Count()
if res > 0 {
db.Update(bson.M{"userid": user.UserId}, bson.M{"$set": bson.M{"token": user.Token, "os": user.Os}})
} else {
db.Insert(user)
}
}
func AddNotification(notification Notification) Notification {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
notification.ID = bson.NewObjectId()
notification.Date = time.Now()
notification.Seen = false
db.Insert(notification)
return notification
}
func GetNotificationsForUser(userID bson.ObjectId) Notifications {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
var result Notifications
db.Find(bson.M{"receiver": userID}).Sort("-date").Limit(30).All(&result)
return result
}
func GetUnreadNotificationsForUser(userID bson.ObjectId) Notifications {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
var result Notifications
db.Find(bson.M{"receiver": userID, "seen": false}).Sort("-date").Limit(30).All(&result)
return result
}
func ReadNotificationForUser(userID bson.ObjectId, notifID bson.ObjectId) Notifications {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
db.Update(bson.M{"_id": notifID}, bson.M{"$set": bson.M{"seen": true}})
return GetNotificationsForUser(userID)
}
func DeleteNotificationsForUser(id bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
db.RemoveAll(bson.M{"receiver": id})
}
func DeleteNotificationsForComment(id bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
db.RemoveAll(bson.M{"comment._id": id})
}
func DeleteNotificationsForPost(id bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
db.RemoveAll(bson.M{"content": id})
}
func DeleteNotificationsForEvent(id bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification")
db.RemoveAll(bson.M{"content": id})
}
func DeleteNotificationTokenForUser(id bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("notification_user")
db.RemoveAll(bson.M{"userid": id})
}