-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.js
183 lines (173 loc) · 5.21 KB
/
model.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
/**
* This is greatly inspired by the Meteor Parties example which can be found
* here: http://goo.gl/qydjY
*/
// Create a Meteor.Collection to hold our Events.
Events = new Meteor.Collection("Events");
/**
* Permissions configuration of our Events collection. This gives us fine
* control of which operations are allowed by whom via the Collections API.
* Other operations must be defined as Meteor.methods.
* @return {Boolean} true if allowed. false if not allowed.
*/
Events.allow({
/**
* Do not allow any wild inserts. Use createEvent method instead!
* @param {String} userId ID of logged in user.
* @param {Object} event Event object.
* @return {false} API inserts are never allowed.
*/
insert: function(userId, event) {
return false;
},
/**
* Users can only update their own events. And they cannot put users on the
* rsvps list. They have to do it themselves.
* @param {String} userId ID of logged in user.
* @param {Object} event Event object.
* @param {Array} fields Fields to be changed.
* @param {String} modifier Description of the change in mongoDB syntax.
* @return {Boolean} Whether or not to allow the update.
*/
update: function(userId, event, fields, modifier) {
console.log('UPDATING...');
if (userId !== event.owner._id) {
return false; // kein Besitzer
}
// List of allowed fields to modify
var allowed = [
"title",
"description",
"hashtag",
"where",
"when",
"public"
];
// Check for disallowed fields
if (_.difference(fields, allowed).length) {
return false;
}
return true;
},
/**
* Users can only delete their own events.
* @param {String} userId ID of logged in user.
* @param {Object} event Event object.
* @return {Boolean} Wheter or not to allow the delete.
*/
remove: function(userId, event) {
return event.owner === userId;
}
});
/**
* Lists methods which allow advanced operations or wrap disallowed operations
* to avoid abuse.
*/
Meteor.methods({
/**
* Provides validation and creates an event object.
* @param {Object} options Form data.
* @return {Events.insert()} Preconfigured Events.insert() method.
*/
createEvent: function(options) {
options = options || {};
console.log(options);
// Validate the Event object.
// Is a field missing?
if (!(
// Is there a title?
typeof options.title === "string" &&
options.title.length &&
// Is there a description?
typeof options.description === "string" &&
options.description.length &&
// Is there a location?
typeof options.where === "string" &&
options.where.length &&
// Is there a date?
typeof options.when === "string" &&
options.when.length
))
throw new Meteor.Error(400, "Required parameter missing");
if (options.title.length > 100)
throw new Meteor.Error(413, "Title too long");
if (options.description.length > 1000)
throw new Meteor.Error(413, "Description too long");
if (options.where.length > 1000)
throw new Meteor.Error(413, "Address too long.");
if (!this.userId)
throw new Meteor.Error(403, "You must be logged in");
return Events.insert({
owner: Meteor.user(),
title: options.title,
hashtag: options.hashtag,
description: options.description,
where: options.where,
when: options.when,
public: options.public,
invited: [],
rsvps: [Meteor.user()]
});
},
/**
* Inserts the user object into the rsvps list.
* @param {String} eventId ID of the selected event object.
* @return {Events.update()} Preconfigured Events.update() method.
*/
attend: function(eventId) {
return Events.update({
_id: eventId
}, {
$push: {
rsvps: Meteor.user()
}
});
},
unattend: function(eventId) {
return Events.update({
_id: eventId
}, {
$pull: {rsvps: {_id: {$in: [Meteor.user()._id]}}}
});
},
invite: function(eventId, userId, invitee) {
var event = Events.findOne(eventId);
if (!event || event.owner._id !== userId)
throw new Meteor.Error(404, "No such event");
if (event.public)
throw new Meteor.Error(400,
"That event is public. No need to invite people.");
if (userId == event.owner._id && !_.contains(event.invited, invitee)) {
Events.update(eventId, {
$addToSet: {
invited: invitee
}
});
}
},
uninvite: function (eventId, userId, invitee) {
var event = Events.findOne(eventId);
if (userId == event.owner._id && _.contains(event.invited, invitee)) {
Events.update({
_id: eventId
}, {
$pull: {
invited: invitee
}
});
}
},
// Users
displayName: function(user) {
if (user.profile && user.profile.name)
return user.profile.name;
return user.emails[0].address;
}
});
var contactEmail = function(user) {
if (user.emails && user.emails.length)
return user.emails[0].address;
if (user.services && user.services.facebook && user.services.facebook.email)
return user.services.facebook.email;
return null;
};