1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\Model\Table; |
14
|
|
|
|
15
|
|
|
use App\Lib\Model\Table\AppTable; |
16
|
|
|
use Cake\ORM\Query; |
17
|
|
|
use Cake\ORM\RulesChecker; |
18
|
|
|
|
19
|
|
|
class UserIgnoresTable extends AppTable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int 3 months |
23
|
|
|
*/ |
24
|
|
|
public const DURATION = 8035200; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
|
|
public function initialize(array $config) |
30
|
|
|
{ |
31
|
|
|
$this->addBehavior( |
32
|
|
|
'Cron.Cron', |
33
|
|
|
[ |
34
|
|
|
'removeOld' => [ |
35
|
|
|
'id' => 'UserIgnore.removeOld', |
36
|
|
|
'due' => '+1 day', |
37
|
|
|
] |
38
|
|
|
] |
39
|
|
|
); |
40
|
|
|
// cache by how many other a user is ignored |
41
|
|
|
$this->addBehavior('CounterCache', ['Users' => ['ignore_count']]); |
42
|
|
|
$this->addBehavior('Timestamp'); |
43
|
|
|
|
44
|
|
|
$this->belongsTo('Users', ['foreignKey' => 'blocked_user_id']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function buildRules(RulesChecker $rules) |
51
|
|
|
{ |
52
|
|
|
$rules->add($rules->existsIn('user_id', 'Users')); |
53
|
|
|
|
54
|
|
|
$rules->add($rules->existsIn('blocked_user_id', 'Users')); |
55
|
|
|
|
56
|
|
|
return $rules; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* User $userId starts to block user $blockedUserId. |
61
|
|
|
* |
62
|
|
|
* @param int $userId user-ID |
63
|
|
|
* @param int $blockedUserId user-ID |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function ignore(int $userId, int $blockedUserId): void |
67
|
|
|
{ |
68
|
|
|
$exists = $this->_get($userId, $blockedUserId); |
69
|
|
|
if ($exists) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
$data = [ |
73
|
|
|
'user_id' => $userId, |
74
|
|
|
'blocked_user_id' => $blockedUserId, |
75
|
|
|
'timestamp' => bDate() |
76
|
|
|
]; |
77
|
|
|
$entity = $this->newEntity($data); |
78
|
|
|
$this->save($entity); |
79
|
|
|
|
80
|
|
|
$this->dispatchDbEvent( |
81
|
|
|
'Event.Saito.User.afterIgnore', |
82
|
|
|
[ |
83
|
|
|
'blockedUserId' => $blockedUserId, |
84
|
|
|
'userId' => $userId |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* unignore |
91
|
|
|
* |
92
|
|
|
* @param int $userId user-ID |
93
|
|
|
* @param int $blockedId user-ID |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function unignore(int $userId, int $blockedId): void |
97
|
|
|
{ |
98
|
|
|
$entity = $this->_get($userId, $blockedId); |
99
|
|
|
if (empty($entity)) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
$this->delete($entity); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get a single record |
107
|
|
|
* |
108
|
|
|
* @param int $userId user-ID |
109
|
|
|
* @param int $blockedId user-ID |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
protected function _get(int $userId, int $blockedId) |
113
|
|
|
{ |
114
|
|
|
return $this->find( |
115
|
|
|
'all', |
116
|
|
|
[ |
117
|
|
|
'conditions' => [ |
118
|
|
|
'user_id' => $userId, |
119
|
|
|
'blocked_user_id' => $blockedId |
120
|
|
|
] |
121
|
|
|
] |
122
|
|
|
)->first(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* get all users ignored by $userId |
127
|
|
|
* |
128
|
|
|
* @param int $userId user-ID |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function getAllIgnoredBy(int $userId) |
132
|
|
|
{ |
133
|
|
|
$results = $this->find() |
134
|
|
|
->contain( |
135
|
|
|
[ |
136
|
|
|
'Users' => function (Query $query) { |
137
|
|
|
$query->select(['Users.id', 'Users.username']); |
138
|
|
|
|
139
|
|
|
return $query; |
140
|
|
|
} |
141
|
|
|
] |
142
|
|
|
) |
143
|
|
|
->where(['user_id' => $userId]) |
144
|
|
|
->order(['Users.username' => 'ASC']) |
145
|
|
|
->all(); |
146
|
|
|
|
147
|
|
|
return $results->extract('user'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Delete all records affectiong a particular user |
152
|
|
|
* |
153
|
|
|
* @param int $userId user-ID |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function deleteUser(int $userId): void |
157
|
|
|
{ |
158
|
|
|
$this->deleteAll(['user_id' => $userId]); |
159
|
|
|
$this->deleteAll(['blocked_user_id' => $userId]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* counts how many users ignore the user with ID $id |
164
|
|
|
* |
165
|
|
|
* @param int $id user-ID |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
public function countIgnored(int $id): int |
169
|
|
|
{ |
170
|
|
|
return count($this->getIgnored($id)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* get ignored |
175
|
|
|
* |
176
|
|
|
* @param int $id user-ID |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function getIgnored(int $id): array |
180
|
|
|
{ |
181
|
|
|
return $this->find( |
182
|
|
|
'all', |
183
|
|
|
[ |
184
|
|
|
'conditions' => ['blocked_user_id' => $id] |
185
|
|
|
] |
186
|
|
|
)->toArray(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* remove old |
191
|
|
|
* |
192
|
|
|
* @return void |
193
|
|
|
*/ |
194
|
|
|
public function removeOld(): void |
195
|
|
|
{ |
196
|
|
|
$this->deleteAll( |
197
|
|
|
['timestamp <' => bDate(time() - self::DURATION)] |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|