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