-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplex-request.js
251 lines (210 loc) · 7.11 KB
/
plex-request.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
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
Requests = new Mongo.Collection("requests");
if (Meteor.isClient) {
Meteor.subscribe("requests");
Meteor.subscribe("userData");
Template.body.helpers({
requests: function () {
return Requests.find({upcoming: false}, {sort: {createdAt: -1}});
},
upcoming: function () {
return Requests.find({upcoming: true}, {sort: {createdAt: -1}});
},
completes: function () {
return Requests.find({}, {sort: {completedAt: -1}});
}
});
Template.emailForm.events({
'submit form': function(){
var email = $('#userEmail').val();
if (email.length < 1) {
$('#userEmail').effect( "shake" );
} else {
$('#emailFooter').fadeOut();
toastr.success('you\'ll be notified now', 'Great it worked!');
$('#userEmail').val('');
Meteor.call("addEmail", email);
};
return false;
}
});
Template.requestForm.events({
'submit form': function(){
var title = $('#newRequestTitle').val();
if (title.length < 1) {
$('#newRequestTitle').effect( "shake" );
} else {
$('#newRequestTitle').val('');
Meteor.call("addTask", title);
Meteor.call('slack', title, Meteor.user().username);
};
return false;
}
});
Template.request.events({
"click .toggle-checked": function () {
Meteor.call("setChecked", this._id, ! this.checked);
},
"click .delete": function () {
Meteor.call("deleteTask", this._id);
}
});
Template.upcomingRequestForm.events({
'submit form': function(){
var title = $('#UpcomingRequestTitle').val();
if (title.length < 1) {
$('#UpcomingRequestTitle').effect( "shake" );
} else {
$('#UpcomingRequestTitle').val('');
Meteor.call("addUpcoming", title);
Meteor.call('slack', title, Meteor.user().username);
};
return false;
}
});
Template.upcomingRequestForm.events({
"click .toggle-checked": function () {
Meteor.call("setChecked", this._id, ! this.checked);
},
"click .delete": function () {
Meteor.call("deleteTask", this._id);
}
});
Template.completeRequest.events({
"click .toggle-checked": function () {
Meteor.call("setChecked", this._id, ! this.checked)
},
"click .delete": function () {
Meteor.call("deleteTask", this._id);
}
});
/* ------------------------------------------------
----------------- Global Helpers ------------------
-------------------------------------------------*/
Template.registerHelper('isOwner', function () {
return this.owner === Meteor.userId();
});
Template.registerHelper('isAdminOrOwner', function () {
return (Roles.userIsInRole(Meteor.userId(), 'admin') || this.owner === Meteor.userId());
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
/* ------------------------------------------------
----------------- S E R V E R ---------------------
-------------------------------------------------*/
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.publish(null, function (){
return Meteor.roles.find({})
});
Meteor.publish("requests", function () {
return Requests.find({});
});
Meteor.publish("userData", function() {
if (this.userId) {
return Meteor.users.find(
{_id: this.userId},
{fields: {email: 1}
});
}
});
clement = Meteor.users.findOne({username: "clement"});
if(clement) {
Roles.addUsersToRoles(clement._id, 'admin');
}
Meteor.methods({
slack: function (title, name) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
var time = new Date();
time = moment(time).format('l LT');
var icon_url = "http%3A%2F%2Ficons.iconarchive.com%2Ficons%2Fthebadsaint%2Fmy-mavericks-part-2%2F128%2FPlex-icon.png";
var postMessage = "New plex request from " + name + ": " + title + " @ " + time;
if(Meteor.settings.slack) {
var token = Meteor.settings.slack.token;
HTTP.post("https://slack.com/api/chat.postMessage?token="+token+"&channel=C051U8PCQ&text="+postMessage+"&username=Plex%20Request%20Bot&icon_url="+icon_url+"&pretty=1");
} else {
console.log('Debugging mode: no slack token set');
}
},
addTask: function (title) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
var time = new Date();
Requests.insert({
title: title,
upcoming: false,
owner: Meteor.userId(),
name: Meteor.user().username,
createdAt: time,
completedAt: null
});
},
addEmail: function (email) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
Roles.addUsersToRoles(Meteor.userId(), 'subscriber');
Meteor.users.update(Meteor.userId(), { $set: { email: email } });
},
addUpcoming: function (title) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
var time = new Date();
Requests.insert({
title: title,
upcoming: true,
owner: Meteor.userId(),
name: Meteor.user().username,
createdAt: time,
completedAt: null
});
},
deleteTask: function (requestId) {
var request = Requests.findOne(requestId);
if (request.owner !== Meteor.userId() && !Roles.userIsInRole(Meteor.userId(), 'admin')) {
throw new Meteor.Error("not-authorized");
}
Requests.remove(requestId);
},
sendEmail: function (to, from, subject, text) {
check([to, from, subject, text], [String]);
this.unblock()
Email.send({
to: to,
from: from,
subject: subject,
html: text
});
},
setChecked: function (requestId, setChecked) {
this.unblock();
var request = Requests.findOne(requestId);
if (request.owner !== Meteor.userId() && !Roles.userIsInRole(Meteor.userId(), 'admin')) {
throw new Meteor.Error("not-authorized");
}
// update the status
Requests.update(requestId, { $set: { checked: setChecked, completedAt: new Date() } });
// send confirmation email
var owner = Meteor.users.findOne(request.owner);
console.log('Sending Email to: '+owner.email);
if (owner.email && setChecked === true) {
var to = owner.email;
var from = 'miguelfclement@gmail.com';
var subject = 'Plex Request Completed!';
var message = '<h2>Hey '+ owner.username +',</h2>' +
'<h4>Your request for <strong>' +
request.title + '</strong> has been completed.' +
'</h4>' +
'<br />' +
'Go watch it on <a href=\'http://theia.feralhosting.com:32400/web/index.html\'> plex </a>now!';
Meteor.call('sendEmail', to, from, subject, message);
}
}
});
});
}