1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Models\Forum;
7
8/**
9 * @property int $forum_id
10 * @property int $mark_time
11 * @property int $topic_id
12 * @property int $user_id
13 */
14class TopicTrack extends Model
15{
16 public $incrementing = false;
17 public $timestamps = false;
18
19 protected $casts = ['mark_time' => 'datetime'];
20 protected $dateFormat = 'U';
21 protected $primaryKey = ':composite';
22 protected $primaryKeys = ['topic_id', 'user_id'];
23 protected $table = 'phpbb_topics_track';
24
25 public static function readStatus($user, $topics)
26 {
27 if ($user === null) {
28 return [];
29 }
30
31 $readStatus = static::where('user_id', '=', $user->getKey())
32 ->whereIn('topic_id', array_pluck($topics, 'topic_id'))
33 ->get()
34 ->keyBy('topic_id');
35
36 $forumReadStatus = ForumTrack::where('user_id', '=', $user->getKey())
37 ->whereIn('forum_id', array_pluck($topics, 'forum_id'))
38 ->get()
39 ->keyBy('forum_id');
40
41 $result = [];
42
43 foreach ($topics as $topic) {
44 $topicTime = $topic->topic_last_post_time;
45 $topicId = $topic->getKey();
46 $forumId = $topic->forum_id;
47
48 $result[$topicId] =
49 $topicTime <= $user->user_lastmark ||
50 (isset($readStatus[$topicId]) && $topicTime <= $readStatus[$topicId]->mark_time) ||
51 (isset($forumReadStatus[$forumId]) && $topicTime <= $forumReadStatus[$forumId]->mark_time);
52 }
53
54 return $result;
55 }
56}