This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
/
ConversationMessage.php
530 lines (458 loc) · 16.6 KB
/
ConversationMessage.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
<?php
class SV_ConversationImprovements_Search_DataHandler_ConversationMessage extends XenForo_Search_DataHandler_Abstract
{
/** @var bool */
protected $enabled = false;
/** @var SV_ConversationImprovements_XenForo_Model_Conversation|null */
protected $_conversationModel = null;
/** @var XenForo_Model_User|null */
protected $_userModel = null;
public function __construct()
{
// use the proxy class existence as a cheap check for if this addon is enabled.
$this->_getConversationModel();
$this->enabled = class_exists('XFCP_SV_ConversationImprovements_XenForo_Model_Conversation', false);
}
public function getCustomMapping(array $mapping = [])
{
if (!($this->enabled))
{
return $mapping;
}
$mapping['properties']['recipients'] = ["type" => "long"];
$mapping['properties']['conversation'] = ["type" => "long"];
return $mapping;
}
protected function _insertIntoIndex(XenForo_Search_Indexer $indexer, array $data, array $parentData = null)
{
if (!($this->enabled))
{
return;
}
$metadata = [];
$title = '';
if ($parentData)
{
$conversation = $parentData;
if ($data['message_id'] == $conversation['first_message_id'] || $conversation['first_message_id'] === 0)
{
$title = $conversation['title'];
if (!empty($conversation['prefix_id']))
{
$metadata['prefix'] = $conversation['prefix_id'];
}
}
if (!isset($conversation['all_recipients']))
{
$conversation['all_recipients'] = $this->_getConversationModel()->getConversationRecipientsForSearch(
$conversation['conversation_id']
);
}
$metadata['recipients'] = array_keys($conversation['all_recipients']);
}
$metadata['conversation'] = $data['conversation_id'];
$indexer->insertIntoIndex(
'conversation_message', $data['message_id'],
$title, $data['message'],
$data['message_date'], $data['user_id'], $data['conversation_id'], $metadata
);
}
protected function _updateIndex(XenForo_Search_Indexer $indexer, array $data, array $fieldUpdates)
{
if (!($this->enabled))
{
return;
}
$indexer->updateIndex('conversation_message', $data['message_id'], $fieldUpdates);
}
protected function _deleteFromIndex(XenForo_Search_Indexer $indexer, array $dataList)
{
if (!($this->enabled))
{
return;
}
$conversationIds = [];
foreach ($dataList AS $data)
{
$conversationIds[] = is_array($data) ? $data['message_id'] : $data;
}
$indexer->deleteFromIndex('conversation_message', $conversationIds);
}
public function rebuildIndex(XenForo_Search_Indexer $indexer, $lastId, $batchSize)
{
if (!($this->enabled))
{
return false;
}
$conversationIds = $this->_getConversationModel()->getConversationMessageIdsInRange($lastId, $batchSize);
if (!$conversationIds)
{
return false;
}
$this->quickIndex($indexer, $conversationIds);
return max($conversationIds);
}
public function quickIndex(XenForo_Search_Indexer $indexer, array $contentIds)
{
if (!($this->enabled))
{
return false;
}
$conversationModel = $this->_getConversationModel();
$messages = $conversationModel->getConversationMessagesByIds(
$contentIds, ['skipUser' => true]
);
$conversationIds = [];
foreach ($messages AS $message)
{
$conversationIds[] = $message['conversation_id'];
}
$conversations = $conversationModel->sv_getConversationsByIds(array_unique($conversationIds));
$recipients = [];
$flattenedRecipients = $conversationModel->getConversationsRecipients($conversationIds, true);
foreach ($flattenedRecipients AS &$recipient)
{
$recipients[$recipient['conversation_id']][$recipient['user_id']] = $recipient;
}
foreach ($conversations AS $conversation_id => &$conversation)
{
$conversation['all_recipients'] = isset($recipients[$conversation_id])
? $recipients[$conversation_id]
: [];
if (empty($conversation['all_recipients']))
{
unset($conversations[$conversation_id]);
}
}
unset($flattenedRecipients);
unset($recipients);
foreach ($messages AS &$message)
{
$conversation = (isset($conversations[$message['conversation_id']]) ? $conversations[$message['conversation_id']] : null);
if (!$conversation)
{
continue;
}
$this->insertIntoIndex($indexer, $message, $conversation);
}
return true;
}
public function getInlineModConfiguration()
{
return [];
}
public function getDataForResults(array $ids, array $viewingUser, array $resultsGrouped)
{
if (!($this->enabled))
{
return [];
}
$conversationModel = $this->_getConversationModel();
$messages = $conversationModel->getConversationMessagesByIds($ids);
$conversationIds = [];
foreach ($messages AS $message)
{
$conversationIds[$message['conversation_id']] = true;
}
$conversationIds = array_keys($conversationIds);
$conversations = $conversationModel->getConversationsForUserByIds($viewingUser['user_id'], $conversationIds);
// unflatten conversation recipients in a single query
$recipients = [];
$flattenedRecipients = $conversationModel->getConversationsRecipients($conversationIds);
foreach ($flattenedRecipients AS &$recipient)
{
$recipients[$recipient['conversation_id']][$recipient['user_id']] = $recipient;
}
// link up all conversations
foreach ($conversations AS $conversation_id => &$conversation)
{
$conversation['all_recipients'] = isset($recipients[$conversation_id])
? $recipients[$conversation_id]
: [];
}
foreach ($messages AS $messageId => &$message)
{
$message['conversation'] = (isset($conversations[$message['conversation_id']]) ? $conversations[$message['conversation_id']] : null);
if (!isset($message['conversation']) || $message['message_id'] == $message['conversation']['first_message_id'] && isset($resultsGrouped['conversation'][$message['conversation_id']]))
{
// matched first message and conversation, skip the message
unset($messages[$messageId]);
}
}
return $messages;
}
public function canViewResult(array $result, array $viewingUser)
{
if (!($this->enabled))
{
return false;
}
return $this->_getConversationModel()->canViewConversation($result['conversation'], $null, $viewingUser);
}
public function prepareResult(array $result, array $viewingUser)
{
if (!($this->enabled))
{
return $result;
}
$result = $this->_getConversationModel()->prepareMessage($result, $result['conversation']);
$result['title'] = XenForo_Helper_String::censorString($result['conversation']['title']);
return $result;
}
public function addInlineModOption(array &$result)
{
return [];
}
public function getResultDate(array $result)
{
return $result['message_date'];
}
public function renderResult(XenForo_View $view, array $result, array $search)
{
return $view->createTemplateObject(
'search_result_conversation_message', [
'conversation' => $result['conversation'],
'conversation_message' => $result,
'search' => $search,
]
);
}
public function getSearchContentTypes()
{
return ['conversation_message', 'conversation'];
}
public function getTypeConstraintsFromInput(XenForo_Input $input)
{
if (!($this->enabled))
{
return [];
}
$constraints = [];
$replyCount = $input->filterSingle('reply_count', XenForo_Input::UINT);
if ($replyCount)
{
$constraints['reply_count'] = $replyCount;
}
$prefixes = $input->filterSingle('prefixes', XenForo_Input::UINT, ['array' => true]);
if ($prefixes && reset($prefixes))
{
$prefixes = array_unique($prefixes);
$constraints['prefix'] = implode(' ', $prefixes);
if (!$constraints['prefix'])
{
unset($constraints['prefix']); // just 0
}
}
$conversationId = $input->filterSingle('conversation_id', XenForo_Input::UINT);
if ($conversationId)
{
$constraints['conversation'] = $conversationId;
// undo things that don't make sense with this
$constraints['titles_only'] = false;
}
$recipients = $input->filterSingle('recipients', XenForo_Input::STRING);
if ($recipients)
{
$usernames = array_unique(explode(',', $recipients));
$users = $this->_getUserModel()->getUsersByNames($usernames, [], $notFound);
$constraints['recipients'] = array_keys($users);
}
return $constraints;
}
/**
* @param XenForo_Search_SourceHandler_Abstract $sourceHandler
* @param array $constraints
* @param array $constraintsGeneral
* @param array|null $viewingUser
* @return array
*/
public function filterConstraintsFromGeneral(/** @noinspection PhpUnusedParameterInspection */XenForo_Search_SourceHandler_Abstract $sourceHandler, array $constraints, array $constraintsGeneral, array $viewingUser = null)
{
return $constraints;
}
public function filterConstraints(XenForo_Search_SourceHandler_Abstract $sourceHandler, array $constraints)
{
$constraints = parent::filterConstraints($sourceHandler, $constraints);
$constraints['require_recipient'] = XenForo_Visitor::getUserId();
return $constraints;
}
public function processConstraint(XenForo_Search_SourceHandler_Abstract $sourceHandler, $constraint, $constraintInfo, array $constraints)
{
if (!($this->enabled))
{
return [];
}
switch ($constraint)
{
case 'reply_count':
$replyCount = intval($constraintInfo);
if ($replyCount > 0)
{
return [
'query' => ['conversation', 'reply_count', '>=', $replyCount]
];
}
break;
case 'prefix':
if ($constraintInfo)
{
return [
'metadata' => ['prefix', preg_split('/\D+/', strval($constraintInfo))],
];
}
break;
case 'conversation':
$conversationId = intval($constraintInfo);
if ($conversationId > 0)
{
return [
'metadata' => ['conversation', $conversationId]
];
}
break;
case 'require_recipient':
if ($constraintInfo)
{
return [
'metadata' => ['recipients', $constraintInfo]
];
}
break;
case 'recipients':
if ($constraintInfo)
{
return [
'metadata' => ['recipients', $constraintInfo]
];
}
break;
}
return false;
}
public function getSearchFormControllerResponse(XenForo_ControllerPublic_Abstract $controller, XenForo_Input $input, array $viewParams)
{
if (!($this->enabled))
{
return null;
}
$params = $input->filterSingle('c', XenForo_Input::ARRAY_SIMPLE);
if (!XenForo_Visitor::getUserId())
{
return null;
}
$viewParams['search']['reply_count'] = empty($params['reply_count']) ? '' : $params['reply_count'];
if (!empty($params['prefix']))
{
$viewParams['search']['prefixes'] = array_fill_keys(explode(' ', $params['prefix']), true);
}
else
{
$viewParams['search']['prefixes'] = [];
}
/** @var $threadPrefixModel XenForo_Model_ThreadPrefix */
$threadPrefixModel = XenForo_Model::create('XenForo_Model_ThreadPrefix');
$viewParams['prefixes'] = $threadPrefixModel->getPrefixesByGroups();
if ($viewParams['prefixes'])
{
$visiblePrefixes = $threadPrefixModel->getVisiblePrefixIds();
foreach ($viewParams['prefixes'] AS $key => &$prefixes)
{
foreach ($prefixes AS $prefixId => $prefix)
{
if (!isset($visiblePrefixes[$prefixId]))
{
unset($prefixes[$prefixId]);
}
}
if (!count($prefixes))
{
unset($viewParams['prefixes'][$key]);
}
}
}
$viewParams['search']['conversation'] = [];
if (!empty($params['conversation']))
{
$conversationModel = $this->_getConversationModel();
$viewingUser = XenForo_Visitor::getInstance()->toArray();
$conversation = $conversationModel->getConversationForUser($params['conversation'], $viewingUser);
if ($conversation)
{
if ($conversationModel->canViewConversation($conversation, $null, $viewingUser))
{
$viewParams['search']['conversation'] = $conversation;
}
}
}
if (!empty($params['recipients']) && is_array($params['recipients']))
{
$users = $this->_getUserModel()->getUsersByIds($params['recipients']);
$usernames = XenForo_Application::arrayColumn($users, 'username');
$viewParams['search']['recipients'] = implode(', ', $usernames);
}
return $controller->responseView(
'XenForo_ViewPublic_Search_Form_Post', 'search_form_conversation_message', $viewParams
);
}
public function getOrderClause($order)
{
if ($order == 'replies')
{
return [
['conversation', 'reply_count', 'desc'],
['search_index', 'item_date', 'desc']
];
}
return false;
}
public function getJoinStructures(array $tables)
{
if (!($this->enabled))
{
return [];
}
$structures = [];
if (isset($tables['conversation']))
{
$structures['conversation'] = [
'table' => 'xf_conversation_master',
'key' => 'conversation_id',
'relationship' => ['search_index', 'discussion_id'],
];
}
return $structures;
}
/**
* Gets the content type that will be used when grouping for this type.
*
* @see XenForo_Search_DataHandler_Abstract::getGroupByType()
*/
public function getGroupByType()
{
return 'conversation';
}
/**
* @return SV_ConversationImprovements_XenForo_Model_Conversation
* @throws XenForo_Exception
*/
protected function _getConversationModel()
{
if ($this->_conversationModel === null)
{
$this->_conversationModel = XenForo_Model::create('XenForo_Model_Conversation');
}
return $this->_conversationModel;
}
/**
* @return XenForo_Model_User
* @throws XenForo_Exception
*/
protected function _getUserModel()
{
if ($this->_userModel === null)
{
$this->_userModel = XenForo_Model::create('XenForo_Model_User');
}
return $this->_userModel;
}
}