f7cloud_client/apps/mail/lib/Controller/ThreadController.php
root 8b6a0139db f7cloud_client
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-17 22:59:26 +00:00

214 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 F7cloud GmbH and F7cloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Mail\Controller;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AiIntegrations\AiIntegrationsService;
use OCA\Mail\Service\MailboxShareService;
use OCA\Mail\Service\SnoozeService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ThreadController extends Controller {
private string $currentUserId;
private AccountService $accountService;
private IMailManager $mailManager;
private SnoozeService $snoozeService;
private AiIntegrationsService $aiIntergrationsService;
private MailboxShareService $mailboxShareService;
private LoggerInterface $logger;
public function __construct(string $appName,
IRequest $request,
string $UserId,
AccountService $accountService,
IMailManager $mailManager,
SnoozeService $snoozeService,
AiIntegrationsService $aiIntergrationsService,
MailboxShareService $mailboxShareService,
LoggerInterface $logger) {
parent::__construct($appName, $request);
$this->currentUserId = $UserId;
$this->accountService = $accountService;
$this->mailManager = $mailManager;
$this->snoozeService = $snoozeService;
$this->aiIntergrationsService = $aiIntergrationsService;
$this->mailboxShareService = $mailboxShareService;
$this->logger = $logger;
}
/**
* @NoAdminRequired
*
* @param int $id
* @param int $destMailboxId
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
#[TrapError]
public function move(int $id, int $destMailboxId, ?int $shareId = null): JSONResponse {
$resolvedMsg = $this->mailboxShareService->resolveMessageAccess($this->currentUserId, $id, $shareId);
$resolvedDst = $this->mailboxShareService->resolveMailboxForAccess($this->currentUserId, $destMailboxId, $shareId);
if ($resolvedMsg === null || $resolvedDst === null) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
if ($resolvedMsg['account']->getId() !== $resolvedDst['account']->getId()) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->moveThread(
$resolvedMsg['account'],
$resolvedMsg['mailbox'],
$resolvedDst['account'],
$resolvedDst['mailbox'],
$resolvedMsg['message']->getThreadRootId()
);
return new JSONResponse();
}
/**
* @NoAdminRequired
*
* @param int $id
* @param int $unixTimestamp
* @param int $destMailboxId
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
#[TrapError]
public function snooze(int $id, int $unixTimestamp, int $destMailboxId, ?int $shareId = null): JSONResponse {
$resolvedMsg = $this->mailboxShareService->resolveMessageAccess($this->currentUserId, $id, $shareId);
$resolvedDst = $this->mailboxShareService->resolveMailboxForAccess($this->currentUserId, $destMailboxId, $shareId);
if ($resolvedMsg === null || $resolvedDst === null) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
if ($resolvedMsg['account']->getId() !== $resolvedDst['account']->getId()) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->snoozeService->snoozeThread($resolvedMsg['message'], $unixTimestamp, $resolvedMsg['account'], $resolvedMsg['mailbox'], $resolvedDst['account'], $resolvedDst['mailbox']);
return new JSONResponse();
}
/**
* @NoAdminRequired
*
* @param int $id
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
#[TrapError]
public function unSnooze(int $id, ?int $shareId = null): JSONResponse {
$resolved = $this->mailboxShareService->resolveMessageAccess($this->currentUserId, $id, $shareId);
if ($resolved === null) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->snoozeService->unSnoozeThread($resolved['message'], $this->currentUserId);
return new JSONResponse();
}
/**
* @NoAdminRequired
*
* @param int $id
*
* @return JSONResponse
*/
public function summarize(int $id, ?int $shareId = null): JSONResponse {
$resolved = $this->mailboxShareService->resolveMessageAccess($this->currentUserId, $id, $shareId);
if ($resolved === null) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$message = $resolved['message'];
$account = $resolved['account'];
if (empty($message->getThreadRootId())) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
$thread = $this->mailManager->getThread($account, $message->getThreadRootId());
try {
$summary = $this->aiIntergrationsService->summarizeThread(
$account,
$message->getThreadRootId(),
$thread,
$this->currentUserId,
);
} catch (\Throwable $e) {
$this->logger->error('Summarizing thread failed: ' . $e->getMessage(), [
'exception' => $e,
]);
return new JSONResponse([], Http::STATUS_NO_CONTENT);
}
return new JSONResponse(['data' => $summary]);
}
/**
* @NoAdminRequired
*/
public function generateEventData(int $id, ?int $shareId = null): JSONResponse {
$resolved = $this->mailboxShareService->resolveMessageAccess($this->currentUserId, $id, $shareId);
if ($resolved === null) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$message = $resolved['message'];
$account = $resolved['account'];
if (empty($message->getThreadRootId())) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
$thread = $this->mailManager->getThread($account, $message->getThreadRootId());
$data = $this->aiIntergrationsService->generateEventData(
$account,
$message->getThreadRootId(),
$thread,
$this->currentUserId,
);
return new JSONResponse(['data' => $data]);
}
/**
* @NoAdminRequired
*
* @param int $id
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
#[TrapError]
public function delete(int $id, ?int $shareId = null): JSONResponse {
$resolved = $this->mailboxShareService->resolveMessageAccess($this->currentUserId, $id, $shareId);
if ($resolved === null) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->deleteThread(
$resolved['account'],
$resolved['mailbox'],
$resolved['message']->getThreadRootId()
);
return new JSONResponse();
}
}