-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
284 lines (217 loc) · 7.61 KB
/
core.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (c) 2010 Digaku.com
* writen by Robin Syihab (r[at]nosql.asia)
*
* License MIT
*
* Copyright (c) 2009 The Go Authors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ndayak
import (
"fmt"
"net"
"os"
"mongo"
"strings"
)
var (
con net.PacketConn
db *mongo.Database
dbcon *mongo.Connection
)
var atreps map[string]string
type oidSearch map[string]mongo.ObjectId
type Settings struct {
DbServer string
DbPort int
DbName string
}
var settings *Settings;
func Init(_con net.PacketConn, st *Settings, verbosity int){
con = _con
settings = st
SetVerbosity(verbosity)
var err os.Error
dbcon, err = mongo.Connect(st.DbServer,st.DbPort)
if err != nil{fmt.Println("DB connection error.",err); return;}
db = dbcon.GetDB(st.DbName)
ColStream = db.GetCollection("ndayak_streams")
ColPost = db.GetCollection("user_post")
ColChan = db.GetCollection("channel")
ColTun = db.GetCollection("tunnel")
ColUser = db.GetCollection("user")
ColUserSettings = db.GetCollection("user_settings")
ColStream.EnsureIndex("ndayax_1",map[string]int{"userid":1,"postid":1})
atreps = map[string]string{
"_origin_id":"origin_id_",
"_metaname_":"metaname_",
"_followed_user_ids":"followed_user_ids_",
"_writer_id":"writerid",
"_popular_post":"popular_post_",
"_user_id":"user_id_",
}
}
func ProcessPost(post_id string){
if dbcon == nil { Error("Database not connected.\n"); return;}
// get post
qfind, err := mongo.Marshal(oidSearch{"_id":mongo.ObjectId{post_id}}, atreps)
if err != nil{Error("Cannot marshal. %s\n", err); return;}
doc, err := ColPost.FindOne(qfind)
if err != nil{
Warn("Cannot find post by id `%s`. %s.\n",post_id,err)
return
}
var post UserPost
mongo.Unmarshal(doc.Bytes(), &post, atreps)
Info2("Got post id: %v, writer: %s, origin: %s\n",strid(post.Id_), post.WriterId, post.Origin_id_)
// get writer
writer, err := GetUser(post.WriterId)
if err != nil{
Warn("Cannot get writer with id `%s` for post id `%s`. err: %v\n", post.WriterId, strid(post.Id_), err)
return
}
//fmt.Printf("writer: %v\n", writer.Name)
InsertPostStream(strid(writer.Id_), post_id)
var writer_id string = strid(writer.Id_)
//fmt.Printf("writer_id: %s\n", writer_id)
// get all followers
qfind, err = mongo.Marshal(map[string]string{"_followed_user_ids":writer_id}, atreps)
if err != nil{Error("Cannot marshal. %s\n", err); return;}
// broadcast to all followers
cursor, err := ColUser.FindAll(qfind)
if err == nil{
for cursor.HasMore(){
doc, err = cursor.GetNext()
if err != nil{Error("Cannot get next. e: %v\n", err); break}
var follower User
mongo.Unmarshal(doc.Bytes(), &follower, atreps)
if strid(follower.Id_) == post.Origin_id_{
continue
}
Info2("broadcast to follower: id: %v, name: %v\n", strid(follower.Id_), follower.Name)
// insert to follower streams
InsertPostStream(strid(follower.Id_), post_id)
}
}else{
Warn("Cannot find post by id `%s`. %s.\n",post_id,err)
}
// get origin
if len(strings.Trim(post.Origin_id_," \n\t\r")) == 0{
Warn("post with id `%s` has no origin id\n", strid(post.Id_))
return
}
doc, err = GetOrigin(post.Origin_id_)
if err == nil{
var spdoc SuperDoc
mongo.Unmarshal(doc.Bytes(), &spdoc, atreps)
switch spdoc.Metaname_{
case "Channel":
var ch Channel
mongo.Unmarshal(doc.Bytes(), &ch, atreps)
//var ch_id string = strid(ch.Id_)
// fmt.Printf("ch_id: %s\n", ch_id)
// get all members
qfind, err := mongo.Marshal(map[string]string{"_channel_ids":strid(ch.Id_)}, atreps)
if err != nil{Error("Cannot marshal. %s\n", err); return;}
// broadcast to all members
cursor, err := ColUser.FindAll(qfind)
if err == nil{
for cursor.HasMore(){
doc, err = cursor.GetNext()
if err != nil{Error("Cannot get next. e: %v\n", err); break}
var follower User
mongo.Unmarshal(doc.Bytes(), &follower, atreps)
Info2("broadcast to member: id: %v, name: %v\n", strid(follower.Id_), follower.Name)
// insert to follower streams
InsertPostStream(strid(follower.Id_), post_id)
}
}else{
Warn("Cannot find post by id `%s`. %s.\n",post_id,err)
}
case "User":
Info2("Inserting into origin=User. origin_id: %v\n", post.Origin_id_)
InsertPostStream(post.Origin_id_, post_id)
}
}else{
Warn("Cannot get origin id `%s`\n", post.Origin_id_)
}
}
func TopUpPost(postId string) {
if dbcon == nil { Error("Database not connected.\n"); return;}
RmPostStream(postId)
post, err := GetPost(postId)
if err != nil{
Error("TopUpPost: post doesn't exists for id `%v`\n", postId)
return
}
Info2("TopUpPost: got post with id `%v`\n", strid(post.Id_))
qfind, err := mongo.Marshal(oidSearch{"_id":mongo.ObjectId{postId}}, atreps)
if err != nil{Error("TopUpPost: Cannot marshal. %s\n", err); return;}
doc, err := mongo.Marshal(map[string]map[string]bool{"$set":{"_popular_post":true}}, atreps)
if err != nil{Error("TopUpPost: Cannot marshal. %s\n", err); return;}
err = ColPost.Update(qfind, doc);
if err != nil{Error("TopUpPost: Cannot update.\n"); return;}
BroadcastAll(postId)
}
func BroadcastAll(postId string) {
if dbcon == nil { Error("Database not connected.\n"); return;}
// get post
qfind, err := mongo.Marshal(oidSearch{"_id":mongo.ObjectId{postId}}, atreps)
if err != nil{Error("Cannot marshal. %s\n", err); return;}
doc, err := ColPost.FindOne(qfind)
if err != nil{
Warn("Cannot find post by id `%s`. %s.\n",postId,err)
return
}
var post UserPost
mongo.Unmarshal(doc.Bytes(), &post, atreps)
Info2("Got post id: %v, writer: %s, origin: %s\n",strid(post.Id_), post.WriterId, post.Origin_id_)
// rm old refs
RmPostStream(postId)
// broadcast to all users
qfind, err = mongo.Marshal(map[string]string{}, atreps)
if err != nil{Error("Cannot marshal. %s\n", err); return;}
cursor, err := ColUser.FindAll(qfind)
if err == nil{
for cursor.HasMore(){
doc, err = cursor.GetNext()
if err != nil{Error("Cannot get next. e: %v\n", err); break}
var user User
mongo.Unmarshal(doc.Bytes(), &user, atreps)
user_id := strid(user.Id_)
Info2("broadcast to all: id: %v, name: %v\n", user_id, user.Name)
// get user settings, is accept feed pop article
st, err := GetUserSettings(user_id)
if err != nil{
Error("BroadcastAll: Cannot get user settings for user id `%v`\n", user_id)
continue
}
if st.Feed_pop_article == false{
Info2("BroadcastAll: Ignore feed article for user id `%v`\n", user_id)
continue
}
// insert to follower streams
InsertPostStream(user_id, postId)
}
}else{
Warn("Cannot find post by id `%s`. %s.\n",postId,err)
}
}