-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDiscussRepository.php
846 lines (717 loc) · 27.8 KB
/
DiscussRepository.php
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<?php
namespace App\Repositories;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use DB;
use Exception;
use BadMethodCallException;
use App\Http\Discuss\ContextFactory;
use App\Models\Initialization\Morphs;
use App\Helpers\StringHelper;
use App\Events\{
ForumPostCreated,
ForumPostEdited,
ForumPostLikeCreated
};
use App\Models\{
Account,
ForumDiscussion,
ForumGroup,
ForumThread,
ForumPost,
ForumPostLike,
ModelBase
};
use App\Repositories\ValueObjects\{
ForumGroupsValue,
ForumPostsInThreadValue,
ForumThreadForEntityValue,
ForumThreadMetadataValue,
ForumThreadsInGroupValue,
ForumThreadValue
};
use Carbon\Carbon;
class DiscussRepository
{
private $_contextFactory;
public function __construct(ContextFactory $contextFactory)
{
$this->_contextFactory = $contextFactory;
}
/**
* Gets an associative array with the following keys:
* * `groups`: all available groups in the system.
*
* @return ForumGroupsValue
*/
public function getGroups()
{
$groups = ForumGroup::orderBy('name')->get();
$numberOfThreads = ForumThread::groupBy('forum_group_id')
->select('forum_group_id', DB::raw('count(*) as count'))
->pluck('count', 'forum_group_id');
return new ForumGroupsValue([
'groups' => $groups,
'number_of_threads' => $numberOfThreads
]);
}
/**
* Gets active account contributors in the specified groups.
*
* @return Collection
*/
public function getAccountsInGroup(Collection $groups)
{
$groupIds = $groups->map(function ($group) {
return $group->id;
});
$accountIds = DB::table('forum_threads')
->join('forum_posts', 'forum_posts.forum_thread_id', '=', 'forum_threads.id')
->join('accounts', 'accounts.id', '=', 'forum_posts.account_id')
->whereIn('forum_group_id', $groupIds)
->where('forum_posts.created_at', '>=', Carbon::now()->addMonth(-12))
->where('accounts.has_avatar', 1)
->orderBy('forum_posts.created_at', 'desc')
->select('forum_posts.account_id', 'forum_group_id', 'accounts.has_avatar', 'accounts.nickname')
->distinct()
->get()
->mapToDictionary(function ($a) {
$account = new Account([
'nickname' => $a->nickname,
'has_avatar' => $a->has_avatar,
]);
$account->id = $a->account_id; // `id` is not fillable
return [
$a->forum_group_id => $account
];
});
return $accountIds;
}
/**
* Gets an associative array with the following keys:
* * `group`: the ForumGroup with the corresponding `$groupId`.
*
* @param integer $groupId
* @return ForumGroup
*/
public function getGroup(int $groupId)
{
$group = ForumGroup::findOrFail($groupId);
return $group;
}
/**
* Gets an associative array with the following keys:
* * `current_page`: the page for which the threads correspond.
* * `group`: the ForumGroup (`$group`) specified in the request.
* * `threads`: a curated array of ForumThread associated with the `$group`.
* * `no_of_pages`: number of pages for the pagination of threads.
* * `pages`: an array of pages from 0 to `no_of_pages - 1`.
*
* @param ForumGroup $group
* @param Account $account
* @param integer $pageNumber
* @return ForumThreadsInGroupValue
*/
public function getThreadDataInGroup(ForumGroup $group, Account $account = null, int $pageNumber = 0)
{
$this->resolveAccount($account);
$noOfThreadsPerPage = config('ed.forum_thread_resultset_max_length');
$noOfPages = intval(ceil(ForumThread::inGroup($group->id)->count() / $noOfThreadsPerPage));
$currentPage = min($noOfPages, max(1, intval($pageNumber)));
$threads = ForumThread::inGroup($group->id)
->with('account')
->orderBy('is_sticky', 'desc')
->orderBy('updated_at', 'desc')
->orderBy('created_at', 'desc')
->skip(($currentPage - 1) * $noOfThreadsPerPage)
->take($noOfThreadsPerPage)
->get();
// Filter out threads that the user is not authorized to see.
$threads = $threads->filter(function ($thread) use($account) {
return $this->checkThreadAuthorization($account, $thread);
});
$pages = $this->createPageArray($noOfPages);
return new ForumThreadsInGroupValue([
'current_page' => $currentPage,
'group' => $group,
'threads' => $threads,
'no_of_pages' => $noOfPages,
'pages' => $pages
]);
}
/**
* Gets an associative array with the following keys:
* * `context`: the context associated with the thread with the corresponding `$threadId`.
* * `thread`: the thread with the corresponding `$threadId`.
*
* @param integer $threadId
* @return ForumThreadValue
*/
public function getThreadData(int $threadId)
{
$thread = ForumThread::findOrFail($threadId);
$context = $this->_contextFactory->create($thread->entity_type);
return new ForumThreadValue([
'context' => $context,
'thread' => $thread
]);
}
/**
* Gets an associative array with the following keys:
* * `posts`: contains a curated array of posts associated with the specified thread.
* * `current_page`: an integer denoting the current page.
* * `pages`: an array of pages from 0 to `no_of_pages - 1`.
* * `no_of_pages`: number of pages for the pagination of posts within the thread.
*
* @param ForumThread $thread
* @param Account $account
* @param string $direction
* @param integer $pageNumber
* @param integer $jumpToId
* @return ForumPostsInThreadValue
*/
public function getPostDataInThread(ForumThread $thread, Account $account = null, $direction = 'desc',
int $pageNumber = 0, int $jumpToId = 0)
{
$this->resolveAccount($account);
if (! $this->checkThreadAuthorization($account, $thread)) {
if ($account === null) {
throw new AuthenticationException;
}
abort(403);
}
$loadingOptions = [
// eager load _account_, but grab only information relevant for the view.
'account' => function ($query) {
$query->select('id', 'nickname', 'has_avatar', 'tengwar');
}
];
if ($account !== null) {
// has the current user liked the post? Don't care about the rest.
$loadingOptions['forum_post_likes'] = function ($query) use ($account) {
$query->where('account_id', $account->id)
->select('account_id', 'forum_post_id');
};
}
// Direction is either descending (default) or ascending.
// Ascending order results in the largest ID being the major ID, whereas
// descending order results in the smallest ID being the major ID.
if (! in_array($direction, ['asc', 'desc'])) {
throw new Exception('Invalid sort order - expected "asc" or "desc".');
}
$ascending = $direction === 'asc';
// Retrieve the maximum size of the result set, and determine whether
// the major ID should be initialized (see above) or retrieved from the
// input parameters.
$maxLength = config('ed.forum_resultset_max_length');
// Hidden posts are automatically excluded. Deleted posts might still be shown, which is why
// we are not filtering out deleted.
$filters = [
['is_hidden', 0],
['is_deleted', 0]
];
// Determine the number of 'pages' there are, which is relevant when
// retrieving things in an ascending order.
$noOfPages = 0;
if ($ascending) {
$noOfPages = ceil($thread->forum_posts()
->where($filters)
->count() / $maxLength
);
}
// composer "filters" (where-conditions for the query fetching the posts). This is a
// quite interesting process, as it depends entirely on the sort order:
//
// ASC (ascending): The API offers a pagination as a means to sift through posts. The
// default state is nonetheless the _n_ latest posts, assuming that the
// client is interested in the _latest_ posts, albeit presented in an
// ascending order. The major ID, in this situation, acts as the page number.
//
// DESC (descending): The API offers an infinite scroll-like experience, where pageNumber is
// always the least ID of the result set. The result set is 'paginated'
// by the client continuously sending the last, least major ID to the API.
//
$skip = 0;
if ($ascending) {
if ($jumpToId !== 0) {
// if the the client is, in fact, requesting to oad a specific post, we
// must determine the page on which the post can be found.
$pageNumber = $noOfPages;
do {
// retrieve an array of IDs within each page, until the page
// with the sought-after ID is found.
$ok = $thread->forum_posts()->where($filters)
->orderBy('id', 'asc')
->skip(($pageNumber - 1) * $maxLength)
->take($maxLength)
->pluck('id')
->search($jumpToId);
if ($ok !== false) {
break;
}
$pageNumber -= 1;
} while ($pageNumber > 1);
}
// The default page should always be the last page, as it is what the user is interested in seeing (= most recent).
if ($pageNumber <= 0) {
$pageNumber = $noOfPages;
}
$skip = ($pageNumber - 1) * $maxLength;
} else {
throw new BadMethodCallException(sprintf('%s is currently not supported.', $direction));
}
// The first post in the thread is always the first element of the posts collection.
// Note how we deliberately do not apply filters here (i.e. is_deleted or is_hidden).
$firstPostInThread = $thread->forum_posts()
->with($loadingOptions)
->orderBy('id', 'asc')
->first();
$firstPostInThreadId = 0;
// Create an empty collection in the event that the thread is empty (i.e. all posts have been deleted)
$posts = new Collection();
if ($firstPostInThread !== null) {
$query = $thread->forum_posts()
->with($loadingOptions)
->where($filters)
->orderBy('id', $direction);
if ($skip > 0) {
$query = $query->skip($skip);
}
if ($maxLength > 0) {
$query = $query->take($maxLength);
}
$posts = $query->get();
// Prepend the first post in the thread to the resulting collection if it does not already exist.
$firstPostInThreadId = $firstPostInThread->id;
if (! $posts->contains(function ($post) use ($firstPostInThreadId) {
return $post->id === $firstPostInThreadId;
})) {
$posts->prepend($firstPostInThread);
}
}
$pages = $this->createPageArray($noOfPages);
return new ForumPostsInThreadValue([
'posts' => $posts,
'current_page' => $pageNumber,
'pages' => $pages,
'no_of_pages' => $noOfPages,
'thread_id' => $thread->id ?: null,
'thread_post_id' => $firstPostInThreadId,
'jump_post_id' => $jumpToId
]);
}
/**
* Gets the latest threads in Discuss, regardless of groups (unless specified).
* @param $forumGroupId (optional) latest threads within the specified group
* @return Collection
*/
public function getLatestThreads(Account $account = null, int $forumGroupId = 0, int $take = 10)
{
$this->resolveAccount($account);
$threads = ForumThread::orderBy('id', 'desc')
->take($take);
if ($forumGroupId !== 0) {
$threads = $threads->where('forum_group_id', $forumGroupId);
}
return $threads->get();
}
/**
* Gets the latest posts in Discuss, regardless of groups (unless specified).
* @param $forumGroupId (optional) latest posts within the specified group
* @return Collection
*/
public function getLatestPosts(Account $account = null, int $forumGroupId = 0, int $take = 20, int $fromId = 0)
{
$this->resolveAccount($account);
$posts = ForumPost::active()->orderBy('id', 'desc')->take($take);
if ($forumGroupId !== 0) {
$posts = $posts->whereHas('forum_thread', function ($q) use ($forumGroupId) {
$q->where('forum_group_id', $forumGroupId);
});
}
if ($fromId !== 0) {
$posts = $posts->where('id', '<', $fromId);
}
$allPosts = $posts->get();
if ($allPosts->count() === 0) {
return $allPosts;
}
$posts = $allPosts->filter(function ($post) use ($account) {
return $this->checkThreadAuthorization($account, $post->forum_thread);
});
$numberOfMissingPosts = $take - $posts->count();
if ($allPosts->count() !== $posts->count() && $numberOfMissingPosts > 0) {
$complimentaryPosts = $this->getLatestPosts($account, $forumGroupId, $numberOfMissingPosts, $allPosts->last()->id);
if ($complimentaryPosts->count() > 0) {
$posts = $posts->union($complimentaryPosts);
}
}
return $posts;
}
/**
* Gets a thread entity (either existing thread or a new instance of a thread) associated with the specified entity.
* @param ModelBase|string $entityType
* @param int $id
* @param boolean $createIfNotExists
* @param Account $account
* @return ForumThreadForEntityValue
*/
public function getThreadDataForEntity($entityType, int $id = null, $createIfNotExists = false, Account $account = null)
{
if (is_object($entityType) && $entityType instanceof ModelBase) {
$id = $entityType->id;
$entityType = Morphs::getAlias($entityType);
} else if (! is_string($entityType)) {
throw new Exception(sprintf('Unsupported entity %s.', serialize($entityType)));
}
$forumPostId = 0;
// if the entity is a post, the thread is available as a relation on the ForumPost entity
if ($entityType === Morphs::getAlias(ForumPost::class)) {
$post = ForumPost::find($id);
if ($post === null) {
return null;
}
$thread = $post->forum_thread;
$forumPostId = $post->id;
} else {
$context = $this->_contextFactory->create($entityType);
if ($context === null) {
throw new Exception(sprintf('Unsupported discuss entity "%s" with ID %d.', $entityType, $id));
}
$this->resolveAccount($account);
if (! $context->available($id, $account)) {
throw new AuthenticationException;
}
$data = [
'entity_type' => $entityType,
'entity_id' => $id
];
$thread = ForumThread::where($data)->first();
if ($thread === null) {
if (! $createIfNotExists) {
return null;
}
$entity = $context->resolveById($id);
if ($entity === null) {
return null;
}
if (! $entity->exists) {
if ($account === null) {
throw new AuthenticationException;
}
$entity->account_id = $account->id;
$entity->save();
}
$defaultGroup = $this->getDefaultForumGroupByEntity($entityType);
$thread = new ForumThread([
'account_id' => $account === null ? null : $account->id,
'entity_id' => $entity->id,
'entity_type' => $entityType,
'forum_group_id' => $defaultGroup->id,
'subject' => $context->getName($entity)
]);
}
}
return new ForumThreadForEntityValue([
'thread' => $thread,
'forum_post_id' => $forumPostId
]);
}
/**
* Gets the number of posts associated with the specified entities
* @param string $className class mane for the corresponding entity
* @param array $ids IDs to the corresponding entities
*/
public function getNumberOfPostsForEntities(string $className, array $ids)
{
$entityType = Morphs::getAlias($className);
$sum = ForumThread::whereIn('entity_id', $ids)
->where('entity_type', $entityType)
->select('entity_id', DB::raw('SUM(number_of_posts) as number_of_posts'))
->groupBy('entity_id')
->pluck('number_of_posts', /* key: */ 'entity_id')
->toArray();
return $sum;
}
/**
* Gets the default forum group associated with the specified entity type.
* @param string $entityType entity type ("morph")
* @return ForumGroup
*/
public function getDefaultForumGroupByEntity(string $entityType)
{
$group = ForumGroup::where('role', $entityType)
->first();
// If no forum group is associated with the specified entity, then defer to the default
// 'catch all' entity, `ForumDiscussion`.
if ($group === null) {
$discussionMorph = Morphs::getAlias(ForumDiscussion::class);
$group = ForumGroup::where('role', $discussionMorph)
->first();
if ($group === null) {
throw new Exception(sprintf('No forum group is configured for %s.', $discussionMorph));
}
}
return $group;
}
public function getThreadMetadataData(int $threadId, array $postIds, Account $account = null)
{
$this->resolveAccount($account);
$allLikes = ForumPostLike::whereIn('forum_post_id', $postIds)
->get();
$likedByAccount = [];
if ($account !== null) {
$likedByAccount = $allLikes->reduce(function ($carry, $l) use ($account) {
if ($l->account_id === $account->id) {
$carry[] = $l->forum_post_id;
}
return $carry;
}, []);
}
$countPerPost = $allLikes->countBy(function ($l) {
return $l->forum_post_id;
});
$countPerPostIds = $countPerPost->keys();
$missingPostIds = array_filter($postIds, function ($id) use($countPerPostIds) {
return ! $countPerPostIds->contains($id);
});
foreach ($missingPostIds as $postId) {
$countPerPost[$postId] = 0;
}
return new ForumThreadMetadataValue([
'forum_post_id' => $postIds,
'likes' => $likedByAccount,
'likes_per_post' => $countPerPost
]);
}
/**
* @return ForumPost
*/
public function getPost(int $postId, Account $account = null, $includeDeleted = false)
{
$this->resolveAccount($account);
$post = ForumPost::where([
['id', $postId],
['is_hidden', 0]
])->first();
if ($post === null) {
return null;
}
if ($post->is_deleted) {
if (! $includeDeleted || ! $this->checkPostAuthorization($account, $post)) {
return null;
}
} else if (! $this->checkThreadAuthorization($account, $post->forum_thread)) {
return null;
}
$post->load('account');
return $post;
}
public function saveThread(ForumThread $thread)
{
if (empty($thread->subject)) {
throw new Exception('You must specify a subject before you can save the thread.');
}
if ($thread->entity === null) {
throw new Exception('You must associate a thread with an entity.');
}
$thread->normalized_subject = StringHelper::normalize($thread->subject);
$thread->save();
}
/**
* Stores the post with as a reply to the the thread.
* @param ForumPost $originalPost reference to the post to be stored (and ultimately stored) in the database.
* @param ForumThread $thread the thread that the post should be associated with
* @param Account $account (optional) post author
* @return boolean
*/
public function savePost(ForumPost &$originalPost, ForumThread $thread, Account $account = null)
{
$this->resolveAccount($account);
if ($originalPost->exists && ! $this->checkPostAuthorization($account, $originalPost, $thread)) {
return false;
} else if (! $originalPost->exists && ! $this->checkThreadAuthorization($account, $thread)) {
return false;
}
$event = null;
try {
DB::beginTransaction();
$this->saveThread($thread);
if ($originalPost->exists) {
// update the existing post.
$originalPost->save();
$event = new ForumPostEdited($originalPost, $account->id);
} else {
$post = ForumPost::create([
'account_id' => $account->id,
'forum_thread_id' => $thread->id,
'number_of_likes' => 0,
'content' => $originalPost->content,
'parent_form_post_id' => $originalPost->parent_form_post_id,
]);
// Abandon the original post by pointing at the post we just created.
$originalPost = $post;
$event = new ForumPostCreated($originalPost, $account->id);
}
$thread->account_id = $account->id;
$this->updateForumThread($thread);
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
if ($event !== null) {
event($event);
}
return true;
}
public function saveLike(int $postId, Account $account = null)
{
$this->resolveAccount($account);
if ($account === null) {
return false;
}
$post = ForumPost::find($postId);
if ($post === null) {
return false;
}
$like = null;
try {
DB::beginTransaction();
$like = ForumPostLike::forAccount($account)
->where('forum_post_id', $post->id)
->first();
if ($like === null) {
$like = ForumPostLike::create([
'account_id' => $account->id,
'forum_post_id' => $post->id
]);
} else {
$like->delete();
$like = null;
}
// Count number of likes on a post level
$post->number_of_likes = $post->forum_post_likes()->count();
$post->save();
// Count number of likes on a thread level
$this->updateForumThread($post->forum_thread);
DB::commit();
} catch (Exception $ex) {
DB::rollBack();
throw $ex;
}
// notify only when a like is created.
if ($like !== null) {
event(new ForumPostLikeCreated($post, $account->id));
}
return $like;
}
public function deletePost(ForumPost $post, Account $account = null)
{
$this->resolveAccount($account);
if (! $this->checkPostAuthorization($account, $post)) {
return false;
}
try {
DB::beginTransaction();
$post->is_deleted = 1;
$post->is_hidden = 0;
$post->save();
$this->updateForumThread($post->forum_thread);
DB::commit();
}
catch (Exception $ex) {
DB::rollBack();
throw $ex;
}
return true;
}
public function saveSticky(int $threadId, bool $sticky)
{
ForumThread::where('id', $threadId)
->update(['is_sticky' => $sticky]);
return $sticky;
}
private function checkPostAuthorization(?Account $account, ForumPost $post, ForumThread $thread = null)
{
if ($thread === null) {
$thread = $post->forum_thread;
}
if (! $this->checkThreadAuthorization($account, $thread)) {
return false;
}
if ($post->id === 0) {
return true;
}
return $post->account_id === $account->id || $account->isAdministrator();
}
/**
* Determines whether the specified thread `$thread` can be accessed by `$account`.
*
* @param ForumThread $thread
* @param Account $account
* @return bool
*/
private function checkThreadAuthorization(?Account $account, ForumThread $thread)
{
$context = $this->_contextFactory->create($thread->entity_type);
return $context->available($thread->entity_id, $account);
}
private function updateForumThread(ForumThread $thread)
{
$postIds = $thread->forum_posts() //
->orderBy('created_at', 'asc')
->where([
['is_deleted', '<>', 1],
['is_hidden', '<>', 1]
]) //
->select('id') //
->pluck('id');
$noOfPosts = $postIds->count();
$noOfLikes = $noOfPosts === 0 //
? 0 : ForumPostLike::whereIn('forum_post_id', $postIds)->count();
// Handle deletion case - do not 'bump' the thread when people remove their posts.
if ($thread->number_of_posts >= $noOfPosts) {
$thread->timestamps = false;
}
$thread->number_of_posts = $noOfPosts;
$thread->number_of_likes = $noOfLikes;
// Make sure that `account_id` reflects the `account_id` for the last record associated with
// the given thread.
if ($noOfPosts > 0) {
$postId = $postIds->last();
$latest = ForumPost::where('id', $postId) //
->select('account_id') //
->pluck('account_id');
$thread->account_id = $latest->first();
}
$thread->save();
}
/**
* Updates the reference to point to the account associated with the request, if the current reference is `null`.
*
* @param Account $account
* @return void
*/
private function resolveAccount(Account &$account = null)
{
if ($account === null) {
$account = Auth::user();
}
}
/**
* Creates an array of incrementing numbers from 1 to `$noOfPages`. `0` is supported.
*
* @param integer $noOfPages
* @return void
*/
private function createPageArray(int $noOfPages)
{
$pages = [];
for ($i = 0; $i < $noOfPages; $i += 1) {
$pages[$i] = $i + 1;
}
return $pages;
}
}