-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshared.js
More file actions
107 lines (86 loc) · 2.96 KB
/
Copy pathshared.js
File metadata and controls
107 lines (86 loc) · 2.96 KB
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
Subjects = new Meteor.Collection("subjects")
CityLocations = new Meteor.Collection("city-locations")
CityLocations.findByCountry = function (country) {
return CityLocations.find({country: country || "UK"}, {sort: [["name", "ASC"]]})
}
Tutors = new Meteor.Collection("tutors")
Tutors.findBySubject = function (subject) {
return Tutors.find({subject: new RegExp(subject, "i")}, {sort: [["name", "ASC"]]})
}
Tutors.findByPuid = function (puid) {
return Tutors.findOne({puid: puid})
}
Conversations = new Meteor.Collection("conversations")
/**
* @param {Object|String} conv A conversation object or the recipient ID
* @param {String} text Message to send
* @param {Function} cb
*/
Conversations.sendMessage = function (conv, text, cb) {
var recipientId = null
if (!conv) return cb(new Meteor.Error(400, 'No conversation or recipient id'))
if (Object.prototype.toString.call(conv) == "[object String]") {
recipientId = conv
conv = null
}
var userId = Meteor.userId()
if (!userId) return cb(new Meteor.Error(401, "Login to send messages"))
var message = {
text: text.trim(),
by: userId,
created: Date.now()
}
// Attempt to find an existing conversation, if a recipientId has been passed
if (recipientId) {
conv = Conversations.findOne({owner: userId, users: {$elemMatch: {userId: recipientId}}})
}
var sender = Meteor.user()
if (!sender) return cb(new Meteor.Error(401, "Login to send messages"))
// Create if not
if (!conv) {
console.log("Creating conversation between", sender.profile.name, "and", recipientId)
Conversations.insert({
owner: sender._id,
users: [{
userId: sender._id,
name: sender.profile.name,
photo: sender.profile.photo
}, {
// Add in the user ID and wait for the server to update the doc
// with additional user details the client is allowed to see
userId: recipientId
}],
messages: [message],
updated: Date.now()
}, function (er, id) {
if (er) return cb(er)
cb(null, Conversations.findOne(id))
})
} else {
console.log("Updating conversation between", conv.users.map(function (u) { return u.name }).join(" and "))
Conversations.update(conv._id, {$push: {messages: message}}, function (er) {
if (er) return cb(er)
cb(null, Conversations.findOne(conv._id))
})
}
}
Qualifications = new Meteor.Collection("qualifications")
Qualifications.findBySubject = function (subject) {
console.log("Finding qualifications by subject", subject)
return Qualifications.find({
$or: [
{subjects: new RegExp(subject, "i")},
{subjects: "*"}
]
}, {sort: [["name", "ASC"]]})
}
Experiences = new Meteor.Collection("experiences")
Experiences.findBySubject = function (subject) {
console.log("Finding experiences by subject", subject)
return Experiences.find({
$or: [
{subjects: new RegExp(subject, "i")},
{subjects: "*"}
]
}, {sort: [["name", "ASC"]]})
}