| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Controller;
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
|
|
|
9 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
10 |
use Laminas\Log\LoggerInterface;
|
|
|
11 |
use Laminas\View\Model\ViewModel;
|
|
|
12 |
|
|
|
13 |
use LeadersLinked\Library\Functions;
|
|
|
14 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
15 |
|
|
|
16 |
use LeadersLinked\Model\User;
|
|
|
17 |
|
|
|
18 |
use LeadersLinked\Mapper\UserBlockedMapper;
|
|
|
19 |
|
| 700 |
stevensc |
20 |
|
| 307 |
www |
21 |
use LeadersLinked\Mapper\ConversationMapper;
|
|
|
22 |
use LeadersLinked\Mapper\MessageMapper;
|
|
|
23 |
use LeadersLinked\Model\Conversation;
|
| 1 |
efrain |
24 |
use Laminas\View\Model\JsonModel;
|
| 307 |
www |
25 |
use LeadersLinked\Form\InMail\SendForm;
|
|
|
26 |
use LeadersLinked\Model\Message;
|
| 199 |
efrain |
27 |
use LeadersLinked\Mapper\AbuseReportMapper;
|
|
|
28 |
use LeadersLinked\Model\AbuseReport;
|
| 345 |
www |
29 |
use LeadersLinked\Library\Storage;
|
| 1 |
efrain |
30 |
|
|
|
31 |
class InMailController extends AbstractActionController
|
|
|
32 |
{
|
|
|
33 |
/**
|
|
|
34 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
35 |
*/
|
|
|
36 |
private $adapter;
|
| 582 |
ariadna |
37 |
|
| 1 |
efrain |
38 |
/**
|
|
|
39 |
*
|
|
|
40 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
41 |
*/
|
|
|
42 |
private $cache;
|
| 582 |
ariadna |
43 |
|
| 1 |
efrain |
44 |
/**
|
|
|
45 |
* @var \Laminas\Log\LoggerInterface
|
|
|
46 |
*/
|
|
|
47 |
private $logger;
|
| 582 |
ariadna |
48 |
|
| 1 |
efrain |
49 |
/**
|
|
|
50 |
* @var array
|
|
|
51 |
*/
|
|
|
52 |
private $config;
|
| 582 |
ariadna |
53 |
|
| 1 |
efrain |
54 |
/**
|
|
|
55 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
56 |
*/
|
|
|
57 |
private $translator;
|
| 582 |
ariadna |
58 |
|
|
|
59 |
|
| 1 |
efrain |
60 |
/**
|
|
|
61 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
62 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
63 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
64 |
* @param array $config
|
|
|
65 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
66 |
*/
|
|
|
67 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
68 |
{
|
|
|
69 |
$this->adapter = $adapter;
|
|
|
70 |
$this->cache = $cache;
|
|
|
71 |
$this->logger = $logger;
|
|
|
72 |
$this->config = $config;
|
|
|
73 |
$this->translator = $translator;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function indexAction()
|
|
|
77 |
{
|
|
|
78 |
$request = $this->getRequest();
|
| 667 |
stevensc |
79 |
|
|
|
80 |
// Verificar que la petición sea GET
|
|
|
81 |
if (!$request->isGet()) {
|
|
|
82 |
return new JsonModel([
|
|
|
83 |
'success' => false,
|
|
|
84 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
85 |
]);
|
|
|
86 |
}
|
| 307 |
www |
87 |
|
| 667 |
stevensc |
88 |
// Obtener el usuario actual autenticado
|
|
|
89 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
90 |
$currentUser = $currentUserPlugin->getUser();
|
| 307 |
www |
91 |
|
| 667 |
stevensc |
92 |
// Obtener la fecha actual de la base de datos para cálculos de tiempo
|
|
|
93 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
94 |
$now = $userMapper->getDatebaseNow();
|
| 1 |
efrain |
95 |
|
| 667 |
stevensc |
96 |
// Obtener IDs de usuarios que el usuario actual ha bloqueado
|
|
|
97 |
$userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
|
|
|
98 |
$user_blocked_ids = $userBlockedMapper->fetchAllBlockedReturnIds($currentUser->id);
|
| 199 |
efrain |
99 |
|
| 667 |
stevensc |
100 |
// Obtener IDs de usuarios que han bloqueado al usuario actual
|
|
|
101 |
$user_blocked_me_ids = $userBlockedMapper->fetchAllUserBlockMeReturnIds($currentUser->id);
|
| 1 |
efrain |
102 |
|
| 693 |
stevensc |
103 |
$storage = Storage::getInstance($this->config, $this->adapter);
|
| 667 |
stevensc |
104 |
$conversationMapper = ConversationMapper::getInstance($this->adapter);
|
|
|
105 |
$messageMapper = MessageMapper::getInstance($this->adapter);
|
|
|
106 |
$conversations = [];
|
|
|
107 |
$selected_user_id = 0;
|
| 307 |
www |
108 |
|
| 667 |
stevensc |
109 |
$records = $conversationMapper->fetchAllByUserId($currentUser->id);
|
| 681 |
stevensc |
110 |
|
|
|
111 |
$id = $this->params()->fromRoute('id');
|
| 667 |
stevensc |
112 |
|
| 673 |
stevensc |
113 |
foreach ($records as $record) {
|
| 667 |
stevensc |
114 |
// Omitir conversaciones que ya fueron procesadas como seleccionadas
|
|
|
115 |
if ($selected_user_id) {
|
|
|
116 |
if ($record->sender_id == $currentUser->id && $record->receiver_id == $selected_user_id) {
|
|
|
117 |
continue;
|
| 1 |
efrain |
118 |
}
|
| 667 |
stevensc |
119 |
if ($record->receiver_id == $currentUser->id && $record->sender_id == $selected_user_id) {
|
|
|
120 |
continue;
|
| 582 |
ariadna |
121 |
}
|
| 667 |
stevensc |
122 |
}
|
| 1 |
efrain |
123 |
|
| 667 |
stevensc |
124 |
if ($record->sender_id == $currentUser->id) {
|
|
|
125 |
$user = $userMapper->fetchOne($record->receiver_id);
|
|
|
126 |
}
|
|
|
127 |
if ($record->receiver_id == $currentUser->id) {
|
|
|
128 |
$user = $userMapper->fetchOne($record->sender_id);
|
|
|
129 |
|
|
|
130 |
// Omitir usuarios bloqueados
|
|
|
131 |
if (in_array($user->id, $user_blocked_ids) || in_array($user->id, $user_blocked_me_ids)) {
|
|
|
132 |
continue;
|
| 582 |
ariadna |
133 |
}
|
| 667 |
stevensc |
134 |
}
|
| 1 |
efrain |
135 |
|
| 667 |
stevensc |
136 |
$timeElapsedString = '';
|
|
|
137 |
$unread = $messageMapper->fetchCountUnreadMessagesByConversationIdAndReceiverId(
|
|
|
138 |
$record->id,
|
|
|
139 |
$currentUser->id
|
|
|
140 |
);
|
|
|
141 |
$lastMessage = $messageMapper->fetchLastMessagesByConversationIdAndReceiverId(
|
|
|
142 |
$record->id,
|
|
|
143 |
$currentUser->id
|
|
|
144 |
);
|
| 1 |
efrain |
145 |
|
| 667 |
stevensc |
146 |
if ($lastMessage) {
|
|
|
147 |
$timeElapsedString = $this->timeAgo($lastMessage, $now);
|
| 582 |
ariadna |
148 |
}
|
| 1 |
efrain |
149 |
|
| 667 |
stevensc |
150 |
array_push($conversations, [
|
|
|
151 |
'uuid' => $user->uuid,
|
|
|
152 |
'name' => trim($user->first_name . ' ' . $user->last_name),
|
| 692 |
stevensc |
153 |
'image' => $storage->getUserImage($user),
|
| 667 |
stevensc |
154 |
'profile' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
|
|
|
155 |
'last_message' => $timeElapsedString,
|
|
|
156 |
'count_unread' => $unread,
|
| 702 |
stevensc |
157 |
'messages_url' => $this->url()->fromRoute('inmail/messages',['uuid' => $user->uuid]),
|
| 683 |
stevensc |
158 |
'selected' => 0,
|
| 667 |
stevensc |
159 |
]);
|
|
|
160 |
}
|
| 1 |
efrain |
161 |
|
| 667 |
stevensc |
162 |
// Ordenar por: seleccionados primero, luego por fecha del último mensaje
|
|
|
163 |
usort($conversations, function ($a, $b) {
|
|
|
164 |
if ($a['selected'] == $b['selected']) {
|
|
|
165 |
if ($a['last_message'] == $b['last_message']) {
|
|
|
166 |
return 0;
|
| 582 |
ariadna |
167 |
} else {
|
| 667 |
stevensc |
168 |
return $a['last_message'] < $b['last_message'] ? -1 : 1;
|
| 582 |
ariadna |
169 |
}
|
| 667 |
stevensc |
170 |
} else {
|
|
|
171 |
return $a['selected'] < $b['selected'] ? -1 : 1;
|
|
|
172 |
}
|
|
|
173 |
});
|
| 1 |
efrain |
174 |
|
| 667 |
stevensc |
175 |
return new JsonModel([
|
|
|
176 |
'success' => true,
|
|
|
177 |
'data' => $conversations
|
|
|
178 |
]);
|
| 1 |
efrain |
179 |
}
|
| 307 |
www |
180 |
|
| 677 |
stevensc |
181 |
public function getMessagesAction()
|
| 1 |
efrain |
182 |
{
|
|
|
183 |
$request = $this->getRequest();
|
| 666 |
stevensc |
184 |
|
|
|
185 |
if (!$request->isGet()) {
|
|
|
186 |
return new JsonModel([
|
|
|
187 |
'success' => false,
|
|
|
188 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
189 |
]);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
$page = intval($this->params()->fromQuery('page', 0), 10);
|
| 683 |
stevensc |
193 |
$uuid = $this->params()->fromRoute('uuid');
|
| 666 |
stevensc |
194 |
|
| 683 |
stevensc |
195 |
if (!$uuid) {
|
| 666 |
stevensc |
196 |
return new JsonModel([
|
|
|
197 |
'success' => false,
|
|
|
198 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
199 |
]);
|
|
|
200 |
}
|
| 307 |
www |
201 |
|
| 666 |
stevensc |
202 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
203 |
$currentUser = $currentUserPlugin->getUser();
|
| 582 |
ariadna |
204 |
|
| 666 |
stevensc |
205 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
206 |
$now = $userMapper->getDatebaseNow();
|
| 307 |
www |
207 |
|
| 683 |
stevensc |
208 |
$user = $userMapper->fetchOneByUuid($uuid);
|
| 666 |
stevensc |
209 |
if (!$user) {
|
|
|
210 |
return new JsonModel([
|
|
|
211 |
'success' => false,
|
|
|
212 |
'data' => 'ERROR_REQUEST_IS_INVALID'
|
|
|
213 |
]);
|
|
|
214 |
}
|
| 307 |
www |
215 |
|
| 666 |
stevensc |
216 |
$abuseReportMapper = AbuseReportMapper::getInstance($this->adapter);
|
|
|
217 |
$abuse_report_message_ids = $abuseReportMapper->fetchAllDataByUserReportingIdAndTypeReturnIds(
|
|
|
218 |
$currentUser->id,
|
|
|
219 |
AbuseReport::TYPE_INMAIL_MESSAGE
|
|
|
220 |
);
|
| 307 |
www |
221 |
|
| 666 |
stevensc |
222 |
$conversationMapper = ConversationMapper::getInstance($this->adapter);
|
|
|
223 |
$messageMapper = MessageMapper::getInstance($this->adapter);
|
|
|
224 |
|
|
|
225 |
$conversation = $conversationMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
|
|
|
226 |
|
|
|
227 |
$messages = [];
|
|
|
228 |
|
|
|
229 |
if ($conversation) {
|
|
|
230 |
$records = $messageMapper->getAllMessagesPaginatorByConversationId($conversation->id, $page);
|
| 582 |
ariadna |
231 |
|
| 666 |
stevensc |
232 |
foreach ($records as $record) {
|
|
|
233 |
$timeElapsedString = $this->timeAgo($record->added_on, $now);
|
| 307 |
www |
234 |
|
| 666 |
stevensc |
235 |
if ($record->sender_id == $currentUser->id) {
|
|
|
236 |
array_push($messages, [
|
|
|
237 |
'uuid' => $record->uuid,
|
|
|
238 |
'sender_name' => trim($currentUser->first_name . ' ' . $currentUser->last_name),
|
|
|
239 |
'sender_image' => $this->url()->fromRoute('storage', [
|
|
|
240 |
'type' => 'user',
|
|
|
241 |
'filename' => $currentUser->image,
|
|
|
242 |
'code' => $currentUser->uuid
|
|
|
243 |
], ['force_canonical' => true]),
|
|
|
244 |
'sender_profile' => $this->url()->fromRoute('profile/view', ['id' => $currentUser->uuid]),
|
|
|
245 |
'receiver_name' => trim($user->first_name . ' ' . $user->last_name),
|
|
|
246 |
'receiver_image' => $this->url()->fromRoute('storage', [
|
|
|
247 |
'type' => 'user',
|
|
|
248 |
'filename' => $user->image,
|
|
|
249 |
'code' => $user->uuid
|
|
|
250 |
], ['force_canonical' => true]),
|
|
|
251 |
'receiver_profile' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
|
| 697 |
stevensc |
252 |
'side' => 'left',
|
| 666 |
stevensc |
253 |
'message' => $record->message,
|
|
|
254 |
'type' => $record->type,
|
|
|
255 |
'filename' => $record->filename ? $this->url()->fromRoute('storage', [
|
|
|
256 |
'type' => 'message',
|
|
|
257 |
'filename' => $record->filename,
|
|
|
258 |
'code' => $record->uuid
|
|
|
259 |
], ['force_canonical' => true]) : '',
|
|
|
260 |
'date' => $timeElapsedString,
|
|
|
261 |
'url_abuse_report' => '', // No se puede reportar mensajes propios
|
|
|
262 |
]);
|
|
|
263 |
}
|
|
|
264 |
else {
|
|
|
265 |
if (in_array($record->id, $abuse_report_message_ids)) {
|
|
|
266 |
array_push($messages, [
|
|
|
267 |
'uuid' => $record->uuid,
|
|
|
268 |
'sender_name' => trim($user->first_name . ' ' . $user->last_name),
|
|
|
269 |
'sender_image' => $this->url()->fromRoute('storage', [
|
|
|
270 |
'type' => 'user',
|
|
|
271 |
'filename' => $user->image,
|
|
|
272 |
'code' => $user->uuid
|
|
|
273 |
], ['force_canonical' => true]),
|
|
|
274 |
'sender_profile' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
|
|
|
275 |
'receiver_name' => trim($currentUser->first_name . ' ' . $currentUser->last_name),
|
|
|
276 |
'receiver_image' => $this->url()->fromRoute('storage', [
|
|
|
277 |
'type' => 'user',
|
|
|
278 |
'filename' => $currentUser->image,
|
|
|
279 |
'code' => $user->uuid
|
|
|
280 |
], ['force_canonical' => true]),
|
|
|
281 |
'receiver_profile' => $this->url()->fromRoute('profile/view', ['id' => $currentUser->uuid]),
|
| 697 |
stevensc |
282 |
'side' => 'right',
|
| 666 |
stevensc |
283 |
'message' => 'LABEL_ABUSE_CONTENT_REPORTED',
|
|
|
284 |
'type' => Message::TYPE_TEXT,
|
|
|
285 |
'filename' => '',
|
|
|
286 |
'date' => $timeElapsedString,
|
|
|
287 |
'url_abuse_report' => '', // Ya no se puede reportar
|
|
|
288 |
]);
|
|
|
289 |
} else {
|
|
|
290 |
$url_abuse_report = $this->url()->fromRoute('helpers/abuse-report', [
|
|
|
291 |
'type' => 'message',
|
|
|
292 |
'id' => $record->uuid
|
|
|
293 |
], ['force_canonical' => true]);
|
| 582 |
ariadna |
294 |
|
| 307 |
www |
295 |
array_push($messages, [
|
|
|
296 |
'uuid' => $record->uuid,
|
| 666 |
stevensc |
297 |
'sender_name' => trim($user->first_name . ' ' . $user->last_name),
|
|
|
298 |
'sender_image' => $this->url()->fromRoute('storage', [
|
|
|
299 |
'type' => 'user',
|
|
|
300 |
'filename' => $user->image,
|
|
|
301 |
'code' => $user->uuid
|
|
|
302 |
], ['force_canonical' => true]),
|
|
|
303 |
'sender_profile' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
|
|
|
304 |
'receiver_name' => trim($currentUser->first_name . ' ' . $currentUser->last_name),
|
|
|
305 |
'receiver_image' => $this->url()->fromRoute('storage', [
|
|
|
306 |
'type' => 'user',
|
|
|
307 |
'filename' => $currentUser->image,
|
|
|
308 |
'code' => $user->uuid
|
|
|
309 |
], ['force_canonical' => true]),
|
|
|
310 |
'receiver_profile' => $this->url()->fromRoute('profile/view', ['id' => $currentUser->uuid]),
|
| 697 |
stevensc |
311 |
'side' => 'right',
|
| 307 |
www |
312 |
'message' => $record->message,
|
|
|
313 |
'type' => $record->type,
|
| 666 |
stevensc |
314 |
'filename' => $record->filename ? $this->url()->fromRoute('storage', [
|
|
|
315 |
'type' => 'message',
|
|
|
316 |
'filename' => $record->filename,
|
|
|
317 |
'code' => $record->uuid
|
|
|
318 |
], ['force_canonical' => true]) : '',
|
| 307 |
www |
319 |
'date' => $timeElapsedString,
|
| 666 |
stevensc |
320 |
'url_abuse_report' => $url_abuse_report,
|
| 307 |
www |
321 |
]);
|
| 582 |
ariadna |
322 |
|
| 307 |
www |
323 |
$messageMapper->markAsRead($record->id);
|
| 261 |
efrain |
324 |
}
|
| 307 |
www |
325 |
}
|
| 1 |
efrain |
326 |
}
|
| 307 |
www |
327 |
|
|
|
328 |
return new JsonModel([
|
|
|
329 |
'success' => true,
|
|
|
330 |
'data' => $messages,
|
| 666 |
stevensc |
331 |
'pagination' => $records->getPages()
|
| 307 |
www |
332 |
]);
|
| 1 |
efrain |
333 |
}
|
| 666 |
stevensc |
334 |
|
|
|
335 |
return new JsonModel([
|
|
|
336 |
'success' => true,
|
|
|
337 |
'data' => $messages,
|
|
|
338 |
'pagination' => 1
|
|
|
339 |
]);
|
| 1 |
efrain |
340 |
}
|
| 307 |
www |
341 |
|
|
|
342 |
public function sendMessageAction()
|
| 1 |
efrain |
343 |
{
|
| 589 |
ariadna |
344 |
// Verificar que la petición sea POST
|
| 1 |
efrain |
345 |
$request = $this->getRequest();
|
| 589 |
ariadna |
346 |
if (!$request->isPost()) {
|
|
|
347 |
return new JsonModel([
|
|
|
348 |
'success' => false,
|
|
|
349 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
350 |
]);
|
|
|
351 |
}
|
| 307 |
www |
352 |
|
| 589 |
ariadna |
353 |
// Obtener el usuario actual
|
|
|
354 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
355 |
$currentUser = $currentUserPlugin->getUser();
|
| 307 |
www |
356 |
|
| 589 |
ariadna |
357 |
// Obtener la fecha actual de la base de datos
|
|
|
358 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
359 |
$now = $userMapper->getDatebaseNow();
|
| 582 |
ariadna |
360 |
|
| 589 |
ariadna |
361 |
// Validar el ID del destinatario
|
| 684 |
stevensc |
362 |
$uuid = $this->params()->fromRoute('uuid');
|
|
|
363 |
if (!$uuid) {
|
| 589 |
ariadna |
364 |
return new JsonModel([
|
|
|
365 |
'success' => false,
|
|
|
366 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
367 |
]);
|
|
|
368 |
}
|
| 307 |
www |
369 |
|
| 589 |
ariadna |
370 |
// Buscar el usuario destinatario
|
| 684 |
stevensc |
371 |
$user = $userMapper->fetchOneByUuidAndNetworkId($uuid, $currentUser->network_id);
|
| 589 |
ariadna |
372 |
if (!$user) {
|
|
|
373 |
return new JsonModel([
|
|
|
374 |
'success' => false,
|
|
|
375 |
'data' => 'ERROR_REQUEST_IS_INVALID'
|
|
|
376 |
]);
|
|
|
377 |
}
|
| 307 |
www |
378 |
|
| 589 |
ariadna |
379 |
// Combinar datos POST y archivos
|
|
|
380 |
$data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
| 307 |
www |
381 |
|
| 589 |
ariadna |
382 |
// Si se envía un archivo sin error, usar su nombre como mensaje
|
|
|
383 |
if (!empty($data['file']) && empty($data['file']['error'])) {
|
|
|
384 |
$data['message'] = $data['file']['name'];
|
|
|
385 |
}
|
| 307 |
www |
386 |
|
| 589 |
ariadna |
387 |
// Validar los datos del formulario
|
|
|
388 |
$form = new SendForm();
|
|
|
389 |
$form->setData($data);
|
| 582 |
ariadna |
390 |
|
| 589 |
ariadna |
391 |
if (!$form->isValid()) {
|
|
|
392 |
// Si el formulario no es válido, devolver los errores
|
|
|
393 |
$messages = [];
|
|
|
394 |
$form_messages = (array) $form->getMessages();
|
|
|
395 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
396 |
$messages[$fieldname] = array_values($field_messages);
|
| 307 |
www |
397 |
}
|
| 589 |
ariadna |
398 |
return new JsonModel([
|
|
|
399 |
'success' => false,
|
|
|
400 |
'data' => $messages
|
|
|
401 |
]);
|
|
|
402 |
}
|
| 582 |
ariadna |
403 |
|
| 589 |
ariadna |
404 |
// Obtener los datos validados
|
|
|
405 |
$dataPost = (array) $form->getData();
|
| 307 |
www |
406 |
|
| 589 |
ariadna |
407 |
// Buscar o crear la conversación
|
|
|
408 |
$conversationMapper = ConversationMapper::getInstance($this->adapter);
|
|
|
409 |
$conversation = $conversationMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
|
| 307 |
www |
410 |
|
| 589 |
ariadna |
411 |
if ($conversation) {
|
|
|
412 |
// Actualizar estado de la conversación existente
|
|
|
413 |
$conversation->receiver_status = Conversation::STATUS_NORMAL;
|
|
|
414 |
$conversation->sender_status = Conversation::STATUS_NORMAL;
|
| 307 |
www |
415 |
|
| 589 |
ariadna |
416 |
if (!$conversationMapper->update($conversation)) {
|
|
|
417 |
return new JsonModel([
|
|
|
418 |
'success' => false,
|
|
|
419 |
'data' => $conversationMapper->getError()
|
|
|
420 |
]);
|
|
|
421 |
}
|
|
|
422 |
} else {
|
|
|
423 |
// Crear nueva conversación
|
|
|
424 |
$conversation = new Conversation();
|
|
|
425 |
$conversation->sender_id = $currentUser->id;
|
|
|
426 |
$conversation->sender_status = Conversation::STATUS_NORMAL;
|
|
|
427 |
$conversation->receiver_id = $user->id;
|
|
|
428 |
$conversation->receiver_status = Conversation::STATUS_NORMAL;
|
| 582 |
ariadna |
429 |
|
| 589 |
ariadna |
430 |
if (!$conversationMapper->insert($conversation)) {
|
|
|
431 |
return new JsonModel([
|
|
|
432 |
'success' => false,
|
|
|
433 |
'data' => $conversationMapper->getError()
|
|
|
434 |
]);
|
|
|
435 |
}
|
|
|
436 |
}
|
| 307 |
www |
437 |
|
| 589 |
ariadna |
438 |
// Procesar archivo adjunto si existe
|
|
|
439 |
$files = $this->getRequest()->getFiles()->toArray();
|
|
|
440 |
$type = Message::TYPE_TEXT;
|
|
|
441 |
$message_tmp_filename = '';
|
|
|
442 |
$message_filename = '';
|
| 307 |
www |
443 |
|
| 589 |
ariadna |
444 |
if (isset($files['file']) && empty($files['file']['error'])) {
|
|
|
445 |
$message_tmp_filename = $files['file']['tmp_name'];
|
|
|
446 |
$message_filename = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
|
| 307 |
www |
447 |
|
| 589 |
ariadna |
448 |
// Determinar el tipo de archivo
|
|
|
449 |
$mime_type = mime_content_type($message_tmp_filename);
|
|
|
450 |
if ($mime_type == 'image/jpg' || $mime_type == 'image/jpeg' || $mime_type == 'image/png') {
|
|
|
451 |
$type = Storage::FILE_TYPE_IMAGE;
|
|
|
452 |
} else if ($mime_type == 'video/webm' || $mime_type == 'video/mpeg' || $mime_type == 'video/mpg' || $mime_type == 'video/mp4') {
|
|
|
453 |
$type = Storage::FILE_TYPE_VIDEO;
|
|
|
454 |
} else if ($mime_type == 'application/pdf') {
|
|
|
455 |
$type = Storage::FILE_TYPE_DOCUMENT;
|
|
|
456 |
}
|
|
|
457 |
}
|
| 307 |
www |
458 |
|
| 589 |
ariadna |
459 |
// Crear y guardar el mensaje
|
|
|
460 |
$message = new Message();
|
|
|
461 |
$message->conversation_id = $conversation->id;
|
|
|
462 |
$message->read = Message::NO;
|
|
|
463 |
$message->message = $type == Message::TYPE_TEXT ? $dataPost['message'] : '';
|
|
|
464 |
$message->receiver_id = $user->id;
|
|
|
465 |
$message->receiver_status = Message::STATUS_NORMAL;
|
|
|
466 |
$message->sender_id = $currentUser->id;
|
|
|
467 |
$message->sender_status = Message::STATUS_NORMAL;
|
|
|
468 |
$message->type = $type;
|
| 307 |
www |
469 |
|
| 589 |
ariadna |
470 |
$messageMapper = MessageMapper::getInstance($this->adapter);
|
|
|
471 |
if (!$messageMapper->insert($message)) {
|
|
|
472 |
return new JsonModel([
|
|
|
473 |
'success' => false,
|
|
|
474 |
'data' => $messageMapper->getError()
|
|
|
475 |
]);
|
|
|
476 |
}
|
| 307 |
www |
477 |
|
| 589 |
ariadna |
478 |
// Procesar archivo adjunto si existe
|
|
|
479 |
if ($message_filename) {
|
|
|
480 |
$storage = new Storage();
|
|
|
481 |
$storage->setAdapter($this->adapter);
|
|
|
482 |
$storage->setLogger($this->logger);
|
|
|
483 |
$storage->setTranslator($this->translator);
|
| 307 |
www |
484 |
|
| 589 |
ariadna |
485 |
if (!$storage->uploadMessageFile($message_tmp_filename, $message_filename, $message->uuid)) {
|
|
|
486 |
return new JsonModel([
|
|
|
487 |
'success' => false,
|
|
|
488 |
'data' => $storage->getError()
|
|
|
489 |
]);
|
|
|
490 |
}
|
| 307 |
www |
491 |
|
| 589 |
ariadna |
492 |
$message->filename = $message_filename;
|
|
|
493 |
if (!$messageMapper->update($message)) {
|
| 1 |
efrain |
494 |
return new JsonModel([
|
| 589 |
ariadna |
495 |
'success' => false,
|
|
|
496 |
'data' => $messageMapper->getError()
|
| 1 |
efrain |
497 |
]);
|
|
|
498 |
}
|
|
|
499 |
}
|
| 589 |
ariadna |
500 |
|
|
|
501 |
// Preparar respuesta exitosa
|
|
|
502 |
$filename = '';
|
|
|
503 |
if ($message->filename) {
|
|
|
504 |
$filename = $this->url()->fromRoute('storage', [
|
|
|
505 |
'type' => 'message',
|
|
|
506 |
'filename' => $message->filename,
|
|
|
507 |
'code' => $message->uuid
|
|
|
508 |
], ['force_canonical' => true]);
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
// Actualizar última actividad del usuario
|
|
|
512 |
$userMapper->updateLastActivity($currentUser->id);
|
|
|
513 |
|
|
|
514 |
// Devolver respuesta exitosa con datos del mensaje
|
|
|
515 |
return new JsonModel([
|
|
|
516 |
'success' => true,
|
|
|
517 |
'data' => [
|
|
|
518 |
'sender_name' => trim($currentUser->first_name . ' ' . $currentUser->last_name),
|
|
|
519 |
'sender_image' => $this->url()->fromRoute('storage', [
|
|
|
520 |
'type' => 'user',
|
|
|
521 |
'filename' => $currentUser->image,
|
|
|
522 |
'code' => $currentUser->uuid
|
|
|
523 |
], ['force_canonical' => true]),
|
|
|
524 |
'sender_profile' => $this->url()->fromRoute('profile/view', ['id' => $currentUser->uuid]),
|
|
|
525 |
'receiver_name' => trim($user->first_name . ' ' . $user->last_name),
|
|
|
526 |
'receiver_image' => $this->url()->fromRoute('storage', [
|
|
|
527 |
'type' => 'user',
|
|
|
528 |
'filename' => $user->image,
|
|
|
529 |
'code' => $user->uuid
|
|
|
530 |
], ['force_canonical' => true]),
|
|
|
531 |
'receiver_profile' => $this->url()->fromRoute('profile/view', ['id' => $user->uuid]),
|
|
|
532 |
'side' => 'left',
|
|
|
533 |
'message' => $message->message,
|
|
|
534 |
'type' => $message->type,
|
|
|
535 |
'filename' => $filename,
|
|
|
536 |
'date' => $this->timeAgo($now, $now),
|
|
|
537 |
]
|
|
|
538 |
]);
|
| 1 |
efrain |
539 |
}
|
| 307 |
www |
540 |
|
|
|
541 |
private function timeAgo($timestamp, $now = '')
|
|
|
542 |
{
|
| 582 |
ariadna |
543 |
|
| 307 |
www |
544 |
if ($now) {
|
|
|
545 |
$datetime1 = \DateTime::createFromFormat('Y-m-d H:i:s', $now);
|
| 1 |
efrain |
546 |
} else {
|
| 307 |
www |
547 |
$now = date('Y-m-d H:i:s');
|
|
|
548 |
$datetime1 = date_create($now);
|
| 1 |
efrain |
549 |
}
|
| 307 |
www |
550 |
$datetime2 = date_create($timestamp);
|
| 582 |
ariadna |
551 |
|
| 307 |
www |
552 |
$diff = date_diff($datetime1, $datetime2);
|
|
|
553 |
$timemsg = '';
|
|
|
554 |
if ($diff->y > 0) {
|
| 582 |
ariadna |
555 |
$timemsg = $diff->y . ' ' . ($diff->y > 1 ? $this->translator->translate('LABEL_YEARS_SMALL') : $this->translator->translate('LABEL_YEAR_SMALL'));
|
| 307 |
www |
556 |
} else if ($diff->m > 0) {
|
| 582 |
ariadna |
557 |
$timemsg = $diff->m . ' ' . ($diff->m > 1 ? $this->translator->translate('LABEL_MONTHS_SMALL') : $this->translator->translate('LABEL_MONTH_SMALL'));
|
| 307 |
www |
558 |
} else if ($diff->d > 0) {
|
| 582 |
ariadna |
559 |
$timemsg = $diff->d . ' ' . ($diff->d > 1 ? $this->translator->translate('LABEL_DAYS_SMALL') : $this->translator->translate('LABEL_DAY_SMALL'));
|
| 307 |
www |
560 |
} else if ($diff->h > 0) {
|
| 582 |
ariadna |
561 |
$timemsg = $diff->h . ' ' . ($diff->h > 1 ? $this->translator->translate('LABEL_HOURS_SMALL') : $this->translator->translate('LABEL_HOUR_SMALL'));
|
| 307 |
www |
562 |
} else if ($diff->i > 0) {
|
| 582 |
ariadna |
563 |
$timemsg = $diff->i . ' ' . ($diff->i > 1 ? $this->translator->translate('LABEL_MINUTES_SMALL') : $this->translator->translate('LABEL_MINUTE_SMALL'));
|
| 307 |
www |
564 |
} else if ($diff->s > 0) {
|
| 582 |
ariadna |
565 |
$timemsg = $diff->s . ' ' . ($diff->s > 1 ? $this->translator->translate('LABEL_SECONDS_SMALL') : $this->translator->translate('LABEL_SECOND_SMALL'));
|
| 307 |
www |
566 |
}
|
|
|
567 |
if (!$timemsg) {
|
| 582 |
ariadna |
568 |
$timemsg = $this->translator->translate('LABEL_NOW');
|
| 307 |
www |
569 |
} else {
|
| 582 |
ariadna |
570 |
$timemsg = $this->translator->translate('LABEL_AGO_SMALL') . ' ' . $timemsg . '';
|
| 307 |
www |
571 |
}
|
|
|
572 |
return $timemsg;
|
| 1 |
efrain |
573 |
}
|
| 307 |
www |
574 |
|
| 684 |
stevensc |
575 |
public function deleteMessageAction()
|
| 1 |
efrain |
576 |
{
|
|
|
577 |
$request = $this->getRequest();
|
|
|
578 |
if ($request->isPost()) {
|
|
|
579 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
580 |
$currentUser = $currentUserPlugin->getUser();
|
| 582 |
ariadna |
581 |
|
| 684 |
stevensc |
582 |
$uuid = $this->params()->fromRoute('uuid');
|
|
|
583 |
if (!$uuid) {
|
| 1 |
efrain |
584 |
return new JsonModel([
|
|
|
585 |
'success' => false,
|
|
|
586 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
587 |
]);
|
|
|
588 |
}
|
| 582 |
ariadna |
589 |
|
| 307 |
www |
590 |
$userMapper = UserMapper::getInstance($this->adapter);
|
| 684 |
stevensc |
591 |
$user = $userMapper->fetchOneByUuid($uuid);
|
| 1 |
efrain |
592 |
if (!$user) {
|
|
|
593 |
return new JsonModel([
|
|
|
594 |
'success' => false,
|
|
|
595 |
'data' => 'ERROR_REQUEST_IS_INVALID'
|
|
|
596 |
]);
|
|
|
597 |
}
|
| 582 |
ariadna |
598 |
|
| 307 |
www |
599 |
$conversationMapper = ConversationMapper::getInstance($this->adapter);
|
|
|
600 |
$conversation = $conversationMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
|
| 582 |
ariadna |
601 |
|
|
|
602 |
|
| 307 |
www |
603 |
if ($conversation) {
|
| 582 |
ariadna |
604 |
|
| 307 |
www |
605 |
if ($conversation->sender_id == $currentUser->id && $conversation->receiver_id == $user->id) {
|
|
|
606 |
$conversation->sender_status = Conversation::STATUS_DELETED;
|
|
|
607 |
if ($conversationMapper->update($conversation)) {
|
|
|
608 |
$response = [
|
|
|
609 |
'success' => true,
|
|
|
610 |
'data' => 'LABEL_CONVERSATION_WAS_DELETED'
|
|
|
611 |
];
|
|
|
612 |
} else {
|
| 582 |
ariadna |
613 |
|
|
|
614 |
|
| 307 |
www |
615 |
$response = [
|
|
|
616 |
'success' => false,
|
|
|
617 |
'data' => $conversationMapper->getError()
|
|
|
618 |
];
|
| 291 |
www |
619 |
}
|
| 307 |
www |
620 |
}
|
| 582 |
ariadna |
621 |
|
| 307 |
www |
622 |
if ($conversation->receiver_id == $currentUser->id && $conversation->sender_id == $user->id) {
|
|
|
623 |
$conversation->receiver_status = Conversation::STATUS_DELETED;
|
|
|
624 |
if ($conversationMapper->update($conversation)) {
|
|
|
625 |
$response = [
|
|
|
626 |
'success' => true,
|
|
|
627 |
'data' => 'LABEL_CONVERSATION_WAS_DELETED'
|
|
|
628 |
];
|
| 291 |
www |
629 |
} else {
|
| 582 |
ariadna |
630 |
|
|
|
631 |
|
| 307 |
www |
632 |
$response = [
|
|
|
633 |
'success' => false,
|
|
|
634 |
'data' => $conversationMapper->getError()
|
|
|
635 |
];
|
| 1 |
efrain |
636 |
}
|
|
|
637 |
}
|
| 582 |
ariadna |
638 |
|
| 307 |
www |
639 |
return new JsonModel($response);
|
| 1 |
efrain |
640 |
} else {
|
| 307 |
www |
641 |
$response = [
|
|
|
642 |
'success' => false,
|
|
|
643 |
'data' => 'ERROR_CONVERSATION_NOT_FOUND'
|
|
|
644 |
];
|
| 1 |
efrain |
645 |
}
|
| 582 |
ariadna |
646 |
|
| 307 |
www |
647 |
return new JsonModel($response);
|
| 1 |
efrain |
648 |
} else {
|
| 307 |
www |
649 |
$response = [
|
| 1 |
efrain |
650 |
'success' => false,
|
|
|
651 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
| 307 |
www |
652 |
];
|
| 1 |
efrain |
653 |
}
|
| 582 |
ariadna |
654 |
|
| 307 |
www |
655 |
return new JsonModel($response);
|
| 1 |
efrain |
656 |
}
|
|
|
657 |
}
|