-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathTopicTracker.php
236 lines (191 loc) · 7.09 KB
/
TopicTracker.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
<?php namespace RainLab\Forum\Classes;
use Db;
use App;
use Carbon\Carbon;
use Cookie;
/**
* TopicTracker tracks read status of topics as a cookie
*/
class TopicTracker
{
/**
* cookieName
*/
public $cookieName = 'forum_cookie_tracker';
/**
* instance creates a new instance of this singleton
*/
public static function instance(): static
{
return App::make('rainlab.forum.tracker');
}
//
// Topics
//
/**
* markTopicTracked flags a topic as being tracked by a member
* @param Topic $topic Forum topic
*/
public function markTopicTracked($topic)
{
$trackedTopics = $this->getTrackedTopics();
$trackedTopics['topics'][$topic->id] = Carbon::now()->getTimestamp();
$this->setTrackedTopics($trackedTopics);
}
/**
* setFlagsOnTopics sets the watched flag (hasNew) for an array of topics
* @param array $topics Collection of topics
* @param Member $member Forum member
*/
public function setFlagsOnTopics($topics, $member)
{
if (!count($topics) || !$member || !$member->user) {
return $topics;
}
$lastLogin = $member->user->last_login;
$trackedTopics = $this->getTrackedTopics();
foreach ($topics as $topic) {
$trackedTopic = !empty($trackedTopics['topics'][$topic->id])
? Carbon::createFromTimestamp($trackedTopics['topics'][$topic->id])
: null;
$topic->hasNew = $topic->last_post_at &&
$topic->last_post_at->gt($lastLogin) &&
(!$trackedTopic || $topic->last_post_at->gt($trackedTopic));
}
return $topics;
}
/**
* markChannelTracked flags a channel as being tracked by a member
* @param Channel $channel Forum channel
*/
public function markChannelTracked($channel)
{
$trackedTopics = $this->getTrackedTopics();
$trackedTopics['channels'][$channel->id] = Carbon::now()->getTimestamp();
$this->setTrackedTopics($trackedTopics);
}
/**
* setFlagsOnChannels sets the has new flag (hasNew) for an array of channels
* @param array $channels Collection of channels
* @param Member $member Forum member
*/
public function setFlagsOnChannels($channels, $member)
{
if (!count($channels) || !$member || !$member->user) {
return $channels;
}
$lastLogin = $member->user->last_login;
$trackedTopics = $this->getTrackedTopics();
$newTopics = $this->getNewTopics($member);
foreach ($channels as $channel) {
if (!$firstTopic = $channel->first_topic) {
continue;
}
$trackedChannel = !empty($trackedTopics['channels'][$channel->id])
? Carbon::createFromTimestamp($trackedTopics['channels'][$channel->id])
: null;
$newPosts = $firstTopic->last_post_at &&
$firstTopic->last_post_at->gt($lastLogin) &&
(!$trackedChannel || $firstTopic->last_post_at->gt($trackedChannel));
// There are new posts, check if they have been read already.
if ($newPosts) {
$newChannelTopics = array_get($newTopics, $channel->id, []);
foreach ($newChannelTopics as $topicId => $timestamp) {
$topicStamp = Carbon::parse($timestamp);
$trackedTopic = !empty($trackedTopics['topics'][$topicId])
? Carbon::createFromTimestamp($trackedTopics['topics'][$topicId])
: null;
if (
(!$trackedTopic || $trackedTopic->lt($topicStamp)) &&
(!$trackedChannel || $trackedChannel->lt($topicStamp))
) {
$channel->hasNew = true;
break;
}
}
}
}
return $channels;
}
/**
* getNewTopics returns new topics across all channels for a member
* @param Member $member Forum member
*/
public function getNewTopics($member)
{
$lastLogin = $member->user->last_login ?: Carbon::now();
$results = Db::table('rainlab_forum_topics')->select([
'rainlab_forum_topics.id',
'rainlab_forum_topics.channel_id',
'rainlab_forum_topics.last_post_at'
])
->join('rainlab_forum_channels', 'rainlab_forum_channels.id', '=', 'rainlab_forum_topics.channel_id')
->where('rainlab_forum_topics.last_post_at', '>', $lastLogin)
->get()
;
$topics = [];
foreach ($results as $result) {
$topics[$result->channel_id][$result->id] = $result->last_post_at;
}
return $topics;
}
/**
* getTrackedTopics returns an array of tracked channels and topics from the
* user's cookie storage.
*/
public function getTrackedTopics()
{
$cookieData = Cookie::get($this->cookieName, false);
if (!$cookieData) {
return ['topics' => [], 'channels' => []];
}
if (strlen($cookieData) > 4048) {
return ['topics' => [], 'channels' => []];
}
/*
* Unserialize data from cookie
*/
$trackedTopics = ['topics' => [], 'channels' => []];
foreach (explode(';', $cookieData) as $idData) {
switch (substr($idData, 0, 1)) {
case 'c': $type = 'channels'; break;
case 't': $type = 'topics'; break;
default: continue 2;
}
$id = intval(substr($idData, 1));
if (($pos = strpos($idData, '=')) === false) {
continue;
}
$timestamp = intval(substr($idData, $pos + 1));
if ($id > 0 && $timestamp > 0) {
$trackedTopics[$type][$id] = $timestamp;
}
}
return $trackedTopics;
}
/**
* setTrackedTopics sets the tracked topics in cookie storage.
*/
public function setTrackedTopics($trackedTopics)
{
$cookieData = '';
if (!empty($trackedTopics)) {
// Sort the arrays (latest read first)
arsort($trackedTopics['topics'], SORT_NUMERIC);
arsort($trackedTopics['channels'], SORT_NUMERIC);
// Homebrew serialization (to avoid having to run unserialize() on cookie data)
foreach ($trackedTopics['topics'] as $id => $timestamp) {
$cookieData .= 't'.$id.'='.$timestamp.';';
}
foreach ($trackedTopics['channels'] as $id => $timestamp) {
$cookieData .= 'c'.$id.'='.$timestamp.';';
}
// Enforce a 4048 byte size limit (4096 minus some space for the cookie name)
if (strlen($cookieData) > 4048) {
$cookieData = substr($cookieData, 0, 4048);
$cookieData = substr($cookieData, 0, strrpos($cookieData, ';')).';';
}
}
Cookie::queue($this->cookieName, $cookieData, 60);
}
}