-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadiscussion.js
118 lines (104 loc) · 3.95 KB
/
metadiscussion.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
Discussions = new Mongo.Collection("discussions");
Router.configure({
layoutTemplate: 'ApplicationLayout'
});
Router.route('/', function () {
this.render('Home');
});
Router.route('/discussion/:_id', function () {
this.render('Discussion', {
data: function () {
if(typeof Discussions.findOne({_id: this.params._id}) == "undefined") {
Meteor.call("newDiscussion", this.params._id);
}
Session.set('currentDiscussion', this.params._id);
return Discussions.findOne({_id: this.params._id});
}
});
});
Meteor.methods({
newDiscussion: function (discussionId) {
if (discussionId) {
return Discussions.insert({
_id: discussionId,
speakers: [],
owner: Meteor.userId(),
createdAt: new Date(),
count: 0
});
} else {
return Discussions.insert({
speakers: [],
owner: Meteor.userId(),
createdAt: new Date(),
count: 0
});
}
},
addSpeaker: function (discussionId) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
var allSpeakers = Discussions.findOne({_id: discussionId}).speakers;
allSpeakers.forEach(function(speaker) {
if(speaker.owner === Meteor.userId()) {
throw new Meteor.Error("already-in-list");
}
});
var count = Discussions.findOne({_id: discussionId}).count;
Discussions.update({_id: discussionId}, {$addToSet: {speakers: {
username: Meteor.user().username,
owner: Meteor.userId(),
count: count,
createdAt: new Date()
}}});
Discussions.update({_id: discussionId}, { $inc: {count: 1}});
},
deleteSpeaker: function (discussionId, speakerId, speakerCount) {
var currentDiscussion = Discussions.findOne({_id: discussionId});
var currentSpeaker = currentDiscussion.speakers[0];
if (typeof currentSpeaker == "undefined") {
throw new Meteor.Error("no-more-speakers");
}
if (Meteor.userId() !== speakerId &&
Meteor.userId() !== currentDiscussion.owner) {
throw new Meteor.Error("not-authorized");
}
Discussions.update({_id: discussionId}, {$pull: {speakers: {owner: speakerId}}});
Discussions.update({_id: discussionId, "speakers.count": {$gt: speakerCount}}, {$inc: {"speakers.$.count": -1}}, {multi: true});
Discussions.update({_id: discussionId}, {$inc: {count: -1}});
},
moveUp: function (discussionId, speakerId, speakerCount) {
if (Meteor.userId() !== speakerId &&
Meteor.userId() !== Discussions.findOne({_id: discussionId}).owner) {
throw new Meteor.Error("not-authorized");
}
var speakerCountPlusOne = speakerCount + 1;
if ( Discussions.update({_id: discussionId, "speakers.count": speakerCountPlusOne}, {$inc: {"speakers.$.count": -1}}) ) {
Discussions.update({_id: discussionId, "speakers.owner": speakerId}, {$inc: {"speakers.$.count": 1}});
}
},
moveDown: function (discussionId, speakerId, speakerCount) {
if (Meteor.userId() !== speakerId &&
Meteor.userId() !== Discussions.findOne({_id: discussionId}).owner) {
throw new Meteor.Error("not-authorized");
}
var speakerCountMinusOne = speakerCount - 1;
if ( Discussions.update({_id: discussionId, "speakers.count": speakerCountMinusOne}, {$inc: {"speakers.$.count": 1}}) ) {
Discussions.update({_id: discussionId, "speakers.owner": speakerId}, {$inc: {"speakers.$.count": -1}});
}
},
next: function (discussionId) {
if (Meteor.userId() !== Discussions.findOne({_id: discussionId}).owner) {
throw new Meteor.Error("not-authorized");
}
var discussion = Discussions.findOne({_id: discussionId});
var speakers = _.sortBy(discussion.speakers, function(speaker) {
return speaker.count;
});
Meteor.call("deleteSpeaker", discussionId, speakers[0].owner, 0);
},
setFirstSpeaker: function (discussionId, firstSpeakerId) {
var discussion = Discussions.update({_id: discussionId}, {$set: {firstSpeaker: firstSpeakerId}});
}
});