-
Notifications
You must be signed in to change notification settings - Fork 32
/
parent.controller.js
610 lines (556 loc) · 22.1 KB
/
parent.controller.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
var ctrl = [ '$scope', '$stateParams', '$timeout', '$location', '$filter', '$state', 'Session', 'Boards', 'Posts', 'Threads', 'Reports', 'Alert', 'BreadcrumbSvc',
function($scope, $stateParams, $timeout, $location, $filter, $state, Session, Boards, Posts, Threads, Reports, Alert, BreadcrumbSvc) {
var ctrl = this;
this.loggedIn = Session.isAuthenticated;
this.dirtyEditor = false;
this.resetEditor = false;
this.showEditor = false;
this.focusEditor = false;
this.quote = '';
this.posting = { post: { body_html: '', body: '' } };
this.editorPosition = 'editor-fixed-bottom';
this.resize = true;
this.moveBoard = {};
this.boards = [];
this.addPoll = false;
this.resetPoll = false;
this.poll = { question: '', answers: ['',''] };
this.pollValid = false;
// Report Permission
this.reportControlAccess = {
reportPosts: Session.hasPermission('reports.createPostReport'),
reportUsers: Session.hasPermission('reports.createUserReport')
};
// Thread Permissions
this.canEditTitle = function() {
var elevatedPrivileges = Session.hasPermission('threads.title.bypass.owner.admin') || Session.hasPermission('threads.title.bypass.owner.mod') || Session.hasPermission('threads.title.bypass.owner.priority');
if (!ctrl.loggedIn()) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!Session.hasPermission('threads.title.allow')) { return false; }
if (!ctrl.writeAccess) { return false; }
if (ctrl.disablePostEdit && !elevatedPrivileges) { return false; }
var title = false;
if (ctrl.thread.user.id === Session.user.id && !ctrl.thread.locked) { title = true; }
else {
if (Session.hasPermission('threads.title.bypass.owner.admin')) { title = true; }
else if (Session.hasPermission('threads.title.bypass.owner.priority') && Session.getPriority() < ctrl.posts[0].user.priority) { title = true; }
else if (Session.hasPermission('threads.title.bypass.owner.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { title = true; }
}
}
return title;
};
this.canLock = function() {
if (!ctrl.loggedIn()) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!Session.hasPermission('threads.lock.allow')) { return false; }
if (!ctrl.writeAccess) { return false; }
var lock = false;
if (ctrl.thread.user.id === Session.user.id) { lock = true; }
else {
if (Session.hasPermission('threads.lock.bypass.owner.admin')) { lock = true; }
else if (Session.hasPermission('threads.lock.bypass.owner.priority') && Session.getPriority() < ctrl.posts[0].user.priority) { lock = true; }
else if (Session.hasPermission('threads.lock.bypass.owner.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { lock = true; }
}
}
return lock;
};
this.canSticky = function() {
if (!ctrl.loggedIn()) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!ctrl.writeAccess) { return false; }
if (!Session.hasPermission('threads.sticky.allow')) { return false; }
var sticky = false;
if (Session.hasPermission('threads.sticky.bypass.owner.admin')) { sticky = true; }
else if (Session.hasPermission('threads.sticky.bypass.owner.priority') && Session.getPriority() < ctrl.posts[0].user.priority) { sticky = true; }
else if (Session.hasPermission('threads.sticky.bypass.owner.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { sticky = true; }
}
return sticky;
};
this.canPurge = function() {
if (!ctrl.loggedIn()) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!ctrl.writeAccess) { return false; }
if (!Session.hasPermission('threads.purge.allow')) { return false; }
var purge = false;
if (Session.hasPermission('threads.purge.bypass.owner.admin')) { purge = true; }
else if (Session.hasPermission('threads.purge.bypass.owner.priority') && Session.getPriority() < ctrl.posts[0].user.priority) { purge = true; }
else if (Session.hasPermission('threads.purge.bypass.owner.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { purge = true; }
}
return purge;
};
this.canMove = function() {
if (!ctrl.loggedIn()) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!ctrl.writeAccess) { return false; }
if (!Session.hasPermission('threads.move.allow')) { return false; }
var move = false;
if (Session.hasPermission('threads.move.bypass.owner.admin')) { move = true; }
else if (Session.hasPermission('threads.move.bypass.owner.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { move = true; }
}
return move;
};
this.showUserControls = function() {
if (!ctrl.loggedIn()) { return false; }
var show = false;
if (!ctrl.thread.watched) { show = true; }
if (ctrl.canCreatePoll()) { show = true; }
return show;
};
this.showThreadControls = function() {
if (!ctrl.loggedIn()) { return false; }
var show = false;
if (ctrl.canLock()) { show = true; }
if (ctrl.canSticky()) { show = true; }
if (ctrl.canPurge()) { show = true; }
if (ctrl.canMove()) { show = true; }
return show;
};
// Poll Permissions
this.canCreatePoll = function() {
if (!ctrl.loggedIn()) { return false; }
if (ctrl.thread.poll) { return false; }
if (!ctrl.writeAccess) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!Session.hasPermission('threads.createPoll.allow')) { return false; }
var create = false;
if (ctrl.thread.user.id === Session.user.id) { create = true; }
else {
if (Session.hasPermission('threads.createPoll.bypass.owner.admin')) { create = true; }
else if (Session.hasPermission('threads.createPoll.bypass.owner.priority') && Session.getPriority() < ctrl.posts[0].user.priority) { create = true; }
else if (Session.hasPermission('threads.createPoll.bypass.owner.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { create = true; }
}
}
return create;
};
// Post Permissions
this.canPost = function() {
if (!ctrl.loggedIn()) { return false; }
if (ctrl.bannedFromBoard) { return false; }
if (!ctrl.writeAccess) { return false; }
if (!Session.hasPermission('posts.create.allow')) { return false; }
if (ctrl.thread.locked) {
if (Session.hasPermission('posts.create.bypass.locked.admin')) { return true; }
else if (Session.hasPermission('posts.create.bypass.locked.mod')) {
if (Session.moderatesBoard(ctrl.thread.board_id)) { return true; }
}
else { return false; }
}
return true;
};
this.canSave = function() {
var text = ctrl.posting.post.body_html;
var imgSrcRegex = /<img[^>]+src="((http:\/\/|https:\/\/|\/)[^">]+)"/g;
var stripTagsRegex = /(<([^>]+)>)/ig;
var images = imgSrcRegex.exec(text);
text = text.replace(stripTagsRegex, '');
text = text.trim();
return text.length || images;
};
// wait for board_id to be populated by child controller
$scope.$watch(function() { return ctrl.board_id; }, function(boardId) {
ctrl.getBoards(boardId); // get boards for mods and admins
});
this.getBoards = function(boardId) {
if (ctrl.canMove()) {
return Boards.moveList().$promise
.then(function(allBoards) {
ctrl.boards = allBoards || [];
ctrl.boards.map(function(board) {
if (board.id === boardId) { ctrl.moveBoard = board; }
board.name = $filter('decode')(board.name); // decode html entities
});
});
}
};
/* Thread Methods */
var threadTitle = '';
this.editThread = false;
this.openEditThread = function() {
threadTitle = ctrl.thread.title;
ctrl.editThread = true;
};
this.closeEditThread = function() {
ctrl.editThread = false;
ctrl.thread.title = threadTitle;
};
this.updateThreadTitle = function() {
var title = ctrl.thread.title;
return Threads.title({id: ctrl.thread.id}, {title: title}).$promise
.then(function() { ctrl.editThread = false; })
.then(function() {
Alert.success('Thread\'s title changed to: ' + title);
BreadcrumbSvc.updateLabelInPlace(title);
})
.catch(function() { Alert.error('Error changing thread title'); });
};
this.updateThreadLock = function() {
// let angular digest the change in lock status
$timeout(function() {
var newLockStatus = !ctrl.thread.locked;
return Threads.lock({id: ctrl.thread.id}, {status: newLockStatus}).$promise
.then(function(lockThread) { ctrl.thread.locked = lockThread.locked; })
.catch(function() {
var type = newLockStatus ? 'Locking' : 'Unlocking';
Alert.error('Error ' + type + ' Thread');
});
});
};
this.updateThreadSticky = function() {
// let angular digest the change in lock status
$timeout(function() {
var newStickyStatus = !ctrl.thread.sticky;
return Threads.sticky({id: ctrl.thread.id}, {status: newStickyStatus}).$promise
.then(function(stickyThread) { ctrl.thread.sticky = stickyThread.sticky; })
.catch(function() {
var type = newStickyStatus ? 'Stickying' : 'Unstickying';
Alert.error('Error ' + type + ' Thread');
});
});
};
this.showMoveThreadModal = false;
this.closeMoveThreadModal = function() {
$timeout(function() { ctrl.showMoveThreadModal = false; });
};
this.openMoveThreadModal = function() { ctrl.showMoveThreadModal = true; };
this.moveThread = function() {
var newBoardId = ctrl.moveBoard.id;
return Threads.move({id: ctrl.thread.id}, {new_board_id: newBoardId}).$promise
.then(function() { $state.go($state.$current, null, {reload:true}); })
.catch(function() { Alert.error('Error Moving Thread'); })
.finally(function() { ctrl.showMoveThreadModal = false; });
};
this.showPurgeThreadModal = false;
this.closePurgeThreadModal = function() {
$timeout(function() { ctrl.showPurgeThreadModal = false; });
};
this.openPurgeThreadModal = function() { ctrl.showPurgeThreadModal = true; };
this.purgeThread = function() {
Threads.delete({id: ctrl.thread.id}).$promise
.then(function() { $state.go('threads.data', {boardId: ctrl.board_id}); })
.catch(function() { Alert.error('Error Purging Thread'); })
.finally(function() { ctrl.showPurgeThreadModal = false; });
};
/* Poll Methods */
this.createPoll = function() {
var requestParams = { thread_id: ctrl.thread.id };
Threads.createPoll(requestParams, ctrl.poll).$promise
.then(function(data) {
ctrl.thread.poll = data;
ctrl.addPoll = false;
ctrl.resetPoll = true;
})
.catch(function() { Alert.error('There was an error creating the poll'); });
};
/* Post Methods */
var discardAlert = function() {
if (ctrl.dirtyEditor) {
var message = 'It looks like you were working on something. ';
message += 'Are you sure you want to leave that behind?';
return confirm(message);
}
else { return true; }
};
function closeEditor() {
ctrl.posting.post.id = '';
ctrl.posting.post.title = '';
ctrl.posting.post.body_html = '';
ctrl.posting.post.body = '';
ctrl.posting.page = '';
ctrl.resetEditor = true;
ctrl.showEditor = false;
}
this.addQuote = function(post) {
var timeDuration = 0;
if (ctrl.showEditor === false) {
ctrl.showEditor = true;
timeDuration = 100;
}
$timeout(function() {
if (post) {
ctrl.quote = {
username: post.user.username,
threadId: ctrl.thread.id,
page: ctrl.page,
postId: post.id,
createdAt: new Date(post.created_at).getTime(),
body: post.body || post.body_html
};
}
}, timeDuration);
};
this.loadEditor = function(post) {
post = post || {};
if (discardAlert()) {
var editorPost = ctrl.posting.post;
editorPost.id = post.id || '';
editorPost.title = post.title || '';
editorPost.body_html = post.body_html || '';
editorPost.body = post.body || '';
editorPost.page = ctrl.page || 1;
ctrl.resetEditor = true;
ctrl.showEditor = true;
ctrl.focusEditor = true;
}
};
this.postSubmitted = false;
this.savePost = function() {
var post = ctrl.posting.post;
var type = post.id ? 'update' : 'create';
post.title = post.title || 'Re: ' + ctrl.thread.title;
post.thread_id = ctrl.thread.id;
var postPromise;
if (post.id) { postPromise = Posts.update(post).$promise; }
else { postPromise = Posts.save(post).$promise; }
if (!ctrl.postSubmitted) {
ctrl.postSubmitted = true;
postPromise.then(function(data) {
if (type === 'create') {
// Increment post count and recalculate ctrl.pageCount
ctrl.thread.post_count++;
ctrl.pageCount = Math.ceil(ctrl.thread.post_count / ctrl.limit);
// Go to last page in the thread and scroll to new post
var lastPage = ctrl.pageCount;
var params = angular.copy($stateParams);
params.page = lastPage;
delete params['#'];
delete params['start'];
delete params['threadId'];
$location.search(params).hash(data.id);
if (ctrl.page === lastPage) { ctrl.pullPage(); }
}
else if (type === 'update') {
var filtered = ctrl.posts.filter(function(p) { return p.id === data.id; });
var editPost = filtered.length > 0 && filtered[0] || {};
editPost.body_html = data.body_html;
editPost.body = data.body;
editPost.updated_at = data.updated_at;
editPost.metadata = data.metadata;
}
})
.then(closeEditor)
.catch(function(err) {
var error = err.data.message;
if (err.status === 429) { error = 'Post Rate Limit Exceeded'; }
Alert.error(error);
})
.finally(function() { ctrl.postSubmitted = false; });
}
};
this.cancelPost = function() { if (discardAlert()) { closeEditor(); } };
this.deletePostIndex = -1;
this.deleteAndLock = false;
this.showDeleteModal = false;
this.deletePostLocked = false;
this.openDeleteModal = function(index, locked) {
ctrl.deleteAndLock = ctrl.canPostLockQuick(index);
ctrl.deletePostIndex = index;
ctrl.deletePostLocked = locked;
ctrl.showDeleteModal = true;
};
this.deletePost = function() {
ctrl.showDeleteModal = false;
var locked = ctrl.deleteAndLock;
var index = ctrl.deletePostIndex;
var post = ctrl.posts && ctrl.posts[index] || '';
if (post) {
Posts.delete({id: post.id, locked: locked}).$promise
.then(function() {
// $state.go($state.$current, null, {reload:true});
Alert.success('Post Hidden');
post.deleted = true;
post.hidden = true;
post.locked = locked;
})
.catch(function(e) {
var msg = 'Failed to hide post';
if (e.data && e.data.message === "Forbidden") {
msg = 'Insufficient permissions to hide this user\'s post';
}
else { msg = e.data.message; }
Alert.error(msg);
});
}
};
this.undeletePostIndex = -1;
this.showUndeleteModal = false;
this.openUndeleteModal = function(index) {
ctrl.undeletePostIndex = index;
ctrl.showUndeleteModal = true;
};
this.undeletePost = function() {
ctrl.showUndeleteModal = false;
var index = ctrl.undeletePostIndex;
var post = ctrl.posts && ctrl.posts[index] || '';
if (post) {
Posts.undelete({id: post.id}).$promise
.then(function() {
delete post.deleted;
delete post.hidden;
Alert.success('Post Unhidden');
})
.catch(function(e) {
var msg = 'Failed to unhide post';
if (e.data && e.data.message === "Forbidden") {
msg = 'Insufficient permissions to unhide this user\'s post';
}
else { msg = e.data.message; }
Alert.error(msg);
});
}
};
this.lockPost = function(post) {
Posts.lock({id: post.id}).$promise
.then(function() { post.locked = true; })
.then(function() { Alert.success('Post Locked'); })
.catch(function(e) {
var msg = 'Failed to lock post';
if (e.data && e.data.message === "Forbidden") {
msg = 'Insufficient permissions to lock this user\'s post';
}
else { msg = e.data.message; }
Alert.error(msg);
});
};
this.unlockPost = function(post) {
Posts.unlock({id: post.id}).$promise
.then(function() { post.locked = false; })
.then(function() { Alert.success('Post Unlocked.'); })
.catch(function(e) {
var msg = 'Failed to unlock post';
if (e.status === 403 || (e.data && e.data.statusCode === 403)) {
if (e.data && e.data.message === "Forbidden") {
msg = 'Insufficient permissions to unlock this user\'s post';
}
else { msg = e.data.message; }
}
Alert.error(msg);
});
};
this.purgePostIndex = -1;
this.showPurgeModal = false;
this.openPurgeModal = function(index) {
ctrl.purgePostIndex = index;
ctrl.showPurgeModal = true;
};
this.purgePost = function() {
ctrl.showPurgeModal = false;
var index = ctrl.purgePostIndex;
var post = ctrl.posts && ctrl.posts[index] || '';
var nearestPost;
if (index > 0) {
nearestPost = ctrl.posts[index - 1];
}
else if (index === 0 && ctrl.posts.length > 1) {
nearestPost = ctrl.posts && ctrl.posts[index + 1];
}
if (post) {
Posts.purge({id: post.id}).$promise
.then(function() {
if (nearestPost) {
$state.go($state.$current, { start: nearestPost.position, purged: 'true', '#': nearestPost.id}, {reload:true});
}
else { // deleted all posts on this page, load prev page
// Increment post count and recalculate ctrl.pageCount
ctrl.thread.post_count--;
ctrl.pageCount = Math.ceil(ctrl.thread.post_count / ctrl.limit);
// Go to last page in the thread and scroll to new post
var prevPage = ctrl.page - 1;
var params = angular.copy($stateParams);
var page = Number(params.page) || Number($location.search().page);
params.page = (page || 2) - 1;
delete params['#'];
delete params['purged'];
delete params['threadId'];
delete params['start'];
$location.hash('last');
$location.search(params);
Alert.success('Sucessfully purged post!');
}
})
.catch(function() { Alert.error('Failed to purge Post'); });
}
};
this.isMinimized = true;
this.fullscreen = function() {
if (ctrl.isMinimized) {
ctrl.isMinimized = false;
this.editorPosition = 'editor-full-screen';
this.resize = false;
}
else {
ctrl.isMinimized = true;
this.editorPosition = 'editor-fixed-bottom';
this.resize = true;
}
};
/* User/Post Reporting */
this.reportedPost = {}; // Object being reported
this.showReportModal = false; // Visible state of modal
this.offendingId = undefined; // Is the post or user id being reported
this.reportReason = ''; // The reason for the report
this.reportSubmitted = false; // Has the report been submitted
this.reportBtnLabel = 'Submit Report'; // Button label for modal
this.user = Session.user; // Logged in user
this.openReportModal = function(post) {
ctrl.reportedPost = post;
ctrl.showReportModal = true;
};
this.closeReportModal = function(userReport) {
$timeout(function() {
if (userReport) {
ctrl.reportedPost.reported_author = true;
}
else {
ctrl.reportedPost.reported = true;
}
ctrl.showReportModal = false;
ctrl.offendingId = undefined;
ctrl.reportReason = '';
ctrl.reportedPost = {};
ctrl.reportSubmitted = false;
ctrl.reportBtnLabel = 'Submit Report';
}, 500);
};
this.submitReport = function() {
ctrl.reportSubmitted = true;
ctrl.reportBtnLabel = 'Submitting...';
var report = { // build report
reporter_user_id: ctrl.user.id,
reporter_reason: ctrl.reportReason
};
var reportPromise;
var reportUser = true;
if (ctrl.offendingId === ctrl.reportedPost.id) { // Post report
report.offender_post_id = ctrl.offendingId;
reportUser = false;
reportPromise = Reports.createPostReport(report).$promise;
}
else { // User report
report.offender_user_id = ctrl.offendingId;
reportPromise = Reports.createUserReport(report).$promise;
}
reportPromise.then(function() {
ctrl.closeReportModal(reportUser);
$timeout(function() { Alert.success('Successfully sent report'); }, 500);
})
.catch(function() {
ctrl.closeReportModal(reportUser);
$timeout(function() { Alert.error('Error sending report, please try again later'); }, 500);
});
};
}
];
// include the poll creator directive
require('./../../../modules/ept-polls/poll_creator.directive');
require('./../../../modules/ept-polls/poll_viewer.directive');
require('./../../../modules/ept-posts/directives/editor.directive');
require('./../../../modules/ept-posts/directives/resizeable.directive');
require('./../../../modules/ept-images/image-uploader.directive');
module.exports = angular.module('ept.posts.parentCtrl', [])
.controller('PostsParentCtrl', ctrl);