-
Notifications
You must be signed in to change notification settings - Fork 4
/
d.js
212 lines (174 loc) · 4.65 KB
/
d.js
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
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var server
var nowjs
var everyone
exports.initialize = function (theServer, nowObj, erryOne) {
server = theServer
nowjs = nowObj
everyone = erryOne
module.exports = everyone.now;
var usercoll;
var mediacoll;
var db = new Db('djs', new Server("localhost", 27017, {}), {native_parser:false});
db.open(function(err, conn) {
db = conn;
db.collection('userinfo', function(err, coll) {
usercoll = coll;
});
db.collection('mediainfo', function(err, coll) {
mediacoll = coll;
});
});
everyone.now.SbroadcastMedia = function (mId, roomId) {
var self = this
if (roomId != undefined) {
mediacoll.findOne({_id: new db.bson_serializer.ObjectID(mId)}, function (err, doc) {
var room = nowjs.getGroup(roomId);
room.now.playback(doc);
});
} else {
mediacoll.findOne({_id: new db.bson_serializer.ObjectID(mId)}, function (err, doc) {
everyone.now.playback(doc);
});
}
}
everyone.now.SgetCurrentMedia = function () {
}
everyone.now.SfindMedia = function (searchQuery, callback) {
searchQuery = ("" + searchQuery).toLowerCase();
var self = this;
if (searchQuery === "") {
var cursor = mediacoll.find({});
cursor.toArray(function(err, array) {
callback(array);
});
} else {
var cursor = mediacoll.find({tags: searchQuery});
cursor.toArray(function(err, array) {
callback(array);
});
}
}
everyone.now.SaddMedia = function (path, tags, metadata) {
mediacoll.insert(
{
path: path,
tags: tags,
metadata: metadata
});
}
everyone.now.SaddToRoom = function (roomId) {
var self = this;
var group = nowjs.getGroup(roomId);
group.addUser(self.user.clientId);
}
everyone.now.SleaveRoom = function (roomId) {
var self = this;
this.getGroups(function (groups) {
if (roomId) {
for (var i = groups.length; i--;) {
if (groups[i] == roomId) {
nowjs.getGroup(groups[i]).removeUser(self.user.clientId);
}
}
} else {
for (var i = groups.length; i--;) {
nowjs.getGroup(groups[i]).removeUser(self.user.clientId);
}
}
});
}
everyone.now.Sapply = function (functionName, args, roomId) {
var room
var fn
if (args === undefined && roomId === undefined) { //did not provide args or roomId
roomId = "everyone"
args = []
} else if (args instanceof Array && roomId === undefined) { //provided args
roomId = "everyone"
} else if (args instanceof String) { //provided roomId
roomId = args
args = []
} //provided errything
var room = nowjs.getGroup(roomId);
var fn = room.now[functionName]
fn.apply(fn, args)
}
//groups prototypes
everyone.now.SaddUser = function (uId) {
var self = this;
usercoll.insert(
{
uId: uId,
groups: [ ]
});
}
everyone.now.SremoveUser = function (uId) {
var self = this;
usercoll.remove(
{
uId: uId
});
}
everyone.now.SloginUser = function (uId, callback, args) {
var self = this;
usercoll.findOne({uId: uId}, function (err,doc) {
for( var i in doc.groups){
nowjs.getGroup(doc.groups[i]).addUser(self.user.clientId)
}
if (args) {
var room = nowjs.getGroup('everyone')
var fn = room.now[callback]
fn.apply(fn, args)
} else if (callback) {
var room = nowjs.getGroup('everyone')
var fn = room.now[callback]
fn.apply(fn, [])
}
});
}
everyone.now.SlogoutUser = function (uId, callback, args) {
var self = this;
usercoll.findOne({uId: uId}, function (err,doc) {
for( var i in doc.groups){
nowjs.getGroup(doc.groups[i]).removeUser(self.user.clientId)
}
if (args) {
var room = nowjs.getGroup('everyone')
var fn = room.now[callback]
fn.apply(fn, args)
} else if (callback) {
var room = nowjs.getGroup('everyone')
var fn = room.now[callback]
fn.apply(fn, [])
}
});
}
everyone.now.subscribeToGroup = function (uId, groupId) {
nowjs.getGroup(groupId).addUser(this.user.clientId);
var self = this;
usercoll.findOne({uId: uId}, function (err,doc) {
var newdoc = doc.groups.push(groupId);
usercoll.update({uId: uId}, doc, function (err, doc){
});
});
}
everyone.now.unsubscribeFromGroup = function (uId, groupId){
nowjs.getGroup(groupId).removeUser(this.user.clientId);
var self = this;
usercoll.findOne({uId: uId}, function (err,doc) {
var i = 0;
while( doc.groups[i] != groupId && i < doc.groups.length ){
if (i == doc.groups.length-1){
return;
}
i = i + 1;
}
newdoc = doc.groups.splice(i,1);
usercoll.update({groups: newdoc}, doc, function (err, doc){
});
})
}
}