Various updates
This commit is contained in:
@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Exception\Request\MissingArgument;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Common\Service\Jobs as Service;
|
||||
use function Safe\json_decode;
|
||||
|
||||
class Jobs
|
||||
{
|
||||
@ -15,7 +16,7 @@ class Jobs
|
||||
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Messages $messagesService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$json = json_decode($body->getContents());
|
||||
if (!isset($json->messages)) {
|
||||
throw new MissingArgument('messages', 'array', 'messages ids');
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Exception\Request\MissingArgument;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Common\Service\Messages as Service;
|
||||
use ProVM\Emails\Model\Message;
|
||||
use function Safe\json_decode;
|
||||
|
||||
class Messages
|
||||
{
|
||||
@ -19,6 +19,17 @@ class Messages
|
||||
$messages = array_map(function(Message $message) {
|
||||
return $message->toArray();
|
||||
}, $service->getAll($mailbox->getName()));
|
||||
usort($messages, function($a, $b) {
|
||||
$d = $a['date_time'] - $b['date_time'];
|
||||
if ($d->days === 0) {
|
||||
$f = strcmp($a['from'], $b['from']);
|
||||
if ($f === 0) {
|
||||
return strcmp($a['subject'], $b['subject']);
|
||||
}
|
||||
return $f;
|
||||
}
|
||||
return $d->format('%r%a');
|
||||
});
|
||||
$output = [
|
||||
'mailbox' => $mailbox->toArray(),
|
||||
'total' => count($messages),
|
||||
@ -34,6 +45,17 @@ class Messages
|
||||
}, $service->getValid($mailbox->getName())), function($message) {
|
||||
return $message !== null;
|
||||
}));
|
||||
usort($messages, function($a, $b) {
|
||||
$d = strcmp($a['date_time'], $b['date_time']);
|
||||
if ($d === 0) {
|
||||
$f = strcmp($a['from'], $b['from']);
|
||||
if ($f === 0) {
|
||||
return strcmp($a['subject'], $b['subject']);
|
||||
}
|
||||
return $f;
|
||||
}
|
||||
return $d;
|
||||
});
|
||||
$output = [
|
||||
'mailbox' => $mailbox->toArray(),
|
||||
'total' => count($messages),
|
||||
@ -49,7 +71,7 @@ class Messages
|
||||
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachmentsService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$json = json_decode($body->getContents());
|
||||
if (!isset($json->mailboxes)) {
|
||||
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
|
||||
}
|
||||
@ -70,4 +92,4 @@ class Messages
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
interface Model
|
||||
use JsonSerializable;
|
||||
|
||||
interface Model extends JsonSerializable
|
||||
{
|
||||
}
|
||||
}
|
||||
|
28
api/common/Define/Repository.php
Normal file
28
api/common/Define/Repository.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
use PDO;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
interface Repository
|
||||
{
|
||||
public function getConnection(): PDO;
|
||||
public function getTable(): string;
|
||||
public function getLogger(): LoggerInterface;
|
||||
|
||||
public function setConnection(PDO $pdo): Repository;
|
||||
public function setTable(string $table): Repository;
|
||||
public function setLogger(LoggerInterface $logger): Repository;
|
||||
|
||||
public function isInstalled(): bool;
|
||||
public function install();
|
||||
public function resetIndex(): void;
|
||||
|
||||
public function create(array $data): Model;
|
||||
public function save(Model &$model): void;
|
||||
public function fetchAll(): array;
|
||||
public function fetchById(int $id): Model;
|
||||
public function update(Model $model, Model $old): void;
|
||||
public function optimize(): void;
|
||||
public function delete(Model $model): void;
|
||||
}
|
@ -55,4 +55,4 @@ class Model
|
||||
}
|
||||
return $this->getContainer()->get($repository_class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,11 @@ namespace ProVM\Common\Implement;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Define\Model as ModelInterface;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Define;
|
||||
|
||||
abstract class Repository
|
||||
abstract class Repository implements Define\Repository
|
||||
{
|
||||
public function __construct(PDO $connection, LoggerInterface $logger)
|
||||
{
|
||||
@ -32,33 +32,32 @@ abstract class Repository
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function setConnection(PDO $pdo): Repository
|
||||
public function setConnection(PDO $pdo): Define\Repository
|
||||
{
|
||||
$this->connection = $pdo;
|
||||
return $this;
|
||||
}
|
||||
public function setTable(string $table): Repository
|
||||
public function setTable(string $table): Define\Repository
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function setLogger(LoggerInterface $logger): Repository
|
||||
public function setLogger(LoggerInterface $logger): Define\Repository
|
||||
{
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
|
||||
abstract protected function fieldsForUpdate(): array;
|
||||
abstract protected function valuesForUpdate(ModelInterface $model): array;
|
||||
protected function idProperty(): string
|
||||
public function isInstalled(): bool
|
||||
{
|
||||
return 'getId';
|
||||
$query = "SHOW TABLES LIKE '{$this->getTable()}'";
|
||||
$st = $this->getConnection()->query($query);
|
||||
if ($st === false) {
|
||||
throw new PDOException("Could not run query {$query}");
|
||||
}
|
||||
return $st->rowCount() > 0;
|
||||
}
|
||||
protected function idField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
public function update(ModelInterface $model, ModelInterface $old): void
|
||||
public function update(Define\Model $model, Define\Model $old): void
|
||||
{
|
||||
$query = "UPDATE `{$this->getTable()}` SET ";
|
||||
$model_values = $this->valuesForUpdate($model);
|
||||
@ -79,22 +78,7 @@ abstract class Repository
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
$st->execute($values);
|
||||
}
|
||||
abstract protected function fieldsForInsert(): array;
|
||||
abstract protected function valuesForInsert(ModelInterface $model): array;
|
||||
protected function insert(ModelInterface $model): void
|
||||
{
|
||||
$fields = $this->fieldsForInsert();
|
||||
$fields_string = implode(', ', array_map(function($field) {
|
||||
return "`{$field}`";
|
||||
}, $fields));
|
||||
$fields_questions = implode(', ', array_fill(0, count($fields), '?'));
|
||||
$query = "INSERT INTO `{$this->getTable()}` ({$fields_string}) VALUES ({$fields_questions})";
|
||||
$values = $this->valuesForInsert($model);
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
$st->execute($values);
|
||||
}
|
||||
abstract protected function defaultFind(ModelInterface $model): ModelInterface;
|
||||
public function save(ModelInterface &$model): void
|
||||
public function save(Define\Model &$model): void
|
||||
{
|
||||
try {
|
||||
$old = $this->defaultFind($model);
|
||||
@ -107,12 +91,7 @@ abstract class Repository
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
abstract public function load(array $row): ModelInterface;
|
||||
|
||||
abstract protected function fieldsForCreate(): array;
|
||||
abstract protected function valuesForCreate(array $data): array;
|
||||
abstract protected function defaultSearch(array $data): ModelInterface;
|
||||
public function create(array $data): ModelInterface
|
||||
public function create(array $data): Define\Model
|
||||
{
|
||||
try {
|
||||
return $this->defaultSearch($data);
|
||||
@ -121,11 +100,6 @@ abstract class Repository
|
||||
return $this->load($data);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getId(ModelInterface $model): int
|
||||
{
|
||||
return $model->getId();
|
||||
}
|
||||
public function resetIndex(): void
|
||||
{
|
||||
$query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1";
|
||||
@ -136,7 +110,7 @@ abstract class Repository
|
||||
$query = "OPTIMIZE TABLE `{$this->getTable()}`";
|
||||
$this->getConnection()->query($query);
|
||||
}
|
||||
public function delete(ModelInterface $model): void
|
||||
public function delete(Define\Model $model): void
|
||||
{
|
||||
$query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
@ -144,8 +118,42 @@ abstract class Repository
|
||||
$this->resetIndex();
|
||||
$this->optimize();
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}`";
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
public function fetchById(int $id): Define\Model
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
|
||||
protected function fetchOne(string $query, ?array $values = null): ModelInterface
|
||||
protected function idProperty(): string
|
||||
{
|
||||
return 'getId';
|
||||
}
|
||||
protected function idField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
protected function insert(Define\Model $model): void
|
||||
{
|
||||
$fields = $this->fieldsForInsert();
|
||||
$fields_string = implode(', ', array_map(function($field) {
|
||||
return "`{$field}`";
|
||||
}, $fields));
|
||||
$fields_questions = implode(', ', array_fill(0, count($fields), '?'));
|
||||
$query = "INSERT INTO `{$this->getTable()}` ({$fields_string}) VALUES ({$fields_questions})";
|
||||
$values = $this->valuesForInsert($model);
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
$st->execute($values);
|
||||
}
|
||||
protected function getId(Define\Model $model): int
|
||||
{
|
||||
return $model->getId();
|
||||
}
|
||||
protected function fetchOne(string $query, ?array $values = null): Define\Model
|
||||
{
|
||||
if ($values !== null) {
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
@ -174,14 +182,14 @@ abstract class Repository
|
||||
return array_map([$this, 'load'], $rows);
|
||||
}
|
||||
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}`";
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
public function fetchById(int $id): ModelInterface
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
}
|
||||
abstract public function install(): void;
|
||||
abstract public function load(array $row): Define\Model;
|
||||
abstract protected function fieldsForUpdate(): array;
|
||||
abstract protected function valuesForUpdate(Define\Model $model): array;
|
||||
abstract protected function fieldsForInsert(): array;
|
||||
abstract protected function valuesForInsert(Define\Model $model): array;
|
||||
abstract protected function defaultFind(Define\Model $model): Define\Model;
|
||||
abstract protected function fieldsForCreate(): array;
|
||||
abstract protected function valuesForCreate(array $data): array;
|
||||
abstract protected function defaultSearch(array $data): Define\Model;
|
||||
}
|
||||
|
25
api/common/Middleware/Attachments.php
Normal file
25
api/common/Middleware/Attachments.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Service;
|
||||
|
||||
class Attachments
|
||||
{
|
||||
public function __construct(protected Service\Attachments $service, protected LoggerInterface $logger) {}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
$this->service->checkDownloaded();
|
||||
$this->service->checkEncryption();
|
||||
} catch (BlankResult $e) {
|
||||
$this->logger->notice($e);
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
20
api/common/Middleware/Install.php
Normal file
20
api/common/Middleware/Install.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use ProVM\Common\Service\Install as Service;
|
||||
|
||||
class Install
|
||||
{
|
||||
public function __construct(protected Service $service) {}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if (!$this->service->check()) {
|
||||
$this->service->install();
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
@ -5,34 +5,21 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use function Safe\json_encode;
|
||||
|
||||
class Logging
|
||||
{
|
||||
public function __construct(LoggerInterface $logger) {
|
||||
$this->setLogger($logger);
|
||||
}
|
||||
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function getLogger(): LoggerInterface
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function setLogger(LoggerInterface $logger): Logging
|
||||
{
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
public function __construct(protected LoggerInterface $logger) {}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$response = $handler->handle($request);
|
||||
$output = [
|
||||
'uri' => var_export($request->getUri(), true),
|
||||
'body' => $request->getBody()->getContents()
|
||||
'body' => $request->getBody()->getContents(),
|
||||
'response' => (clone $response)->getBody()->getContents()
|
||||
];
|
||||
$this->getLogger()->info(\Safe\json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
$this->logger->info(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
24
api/common/Middleware/Mailboxes.php
Normal file
24
api/common/Middleware/Mailboxes.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Service;
|
||||
|
||||
class Mailboxes
|
||||
{
|
||||
public function __construct(protected Service\Messages $service, protected LoggerInterface $logger) {}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
$this->service->checkUpdate();
|
||||
} catch (BlankResult $e) {
|
||||
$this->logger->notice($e);
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
24
api/common/Middleware/Messages.php
Normal file
24
api/common/Middleware/Messages.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Service;
|
||||
|
||||
class Messages
|
||||
{
|
||||
public function __construct(protected Service\Messages $service, protected LoggerInterface $logger) {}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
$this->service->checkSchedule();
|
||||
} catch (BlankResult $e) {
|
||||
$this->logger->notice($e);
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\Message\AttachmentInterface;
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
use Nyholm\Psr7\Stream;
|
||||
use Safe\Exceptions\FilesystemException;
|
||||
use ProVM\Common\Exception\Message\NoAttachments;
|
||||
use ProVM\Emails\Model\Message;
|
||||
use ProVM\Emails\Repository\Attachment;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Safe\Exceptions\FilesystemException;
|
||||
use function Safe\{file_get_contents,file_put_contents};
|
||||
|
||||
class Attachments extends Base
|
||||
{
|
||||
@ -100,13 +100,33 @@ class Attachments extends Base
|
||||
$attachment->getFullFilename()
|
||||
]);
|
||||
}
|
||||
return \Safe\file_get_contents($filename);
|
||||
return file_get_contents($filename);
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->getRepository()->fetchAll();
|
||||
}
|
||||
public function getDownloadedFiles(): array
|
||||
{
|
||||
$downloaded = [];
|
||||
$folder = $this->getFolder();
|
||||
$files = new \FilesystemIterator($folder);
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
$name = $file->getBasename(".{$file->getExtension()}");
|
||||
list($subject, $date, $filename) = explode(' - ', $name);
|
||||
try {
|
||||
$message = $this->getMessages()->find($subject, $date)[0];
|
||||
$filename = "{$filename}.{$file->getExtension()}";
|
||||
$downloaded []= compact('message', 'filename');
|
||||
} catch (BlankResult $e) {
|
||||
}
|
||||
}
|
||||
return $downloaded;
|
||||
}
|
||||
public function create(int $message_id): array
|
||||
{
|
||||
$message = $this->getMessages()->getRepository()->fetchById($message_id);
|
||||
@ -183,7 +203,7 @@ class Attachments extends Base
|
||||
$attachment->getFullFilename()
|
||||
]);
|
||||
try {
|
||||
\Safe\file_put_contents($destination, $remote_attachment->getDecodedContent());
|
||||
file_put_contents($destination, $remote_attachment->getDecodedContent());
|
||||
return true;
|
||||
} catch (FilesystemException $e) {
|
||||
$this->getLogger()->error($e);
|
||||
@ -236,6 +256,9 @@ class Attachments extends Base
|
||||
if (!$message->hasValidAttachments()) {
|
||||
return false;
|
||||
}
|
||||
if ($message->hasDownloadedAttachments()) {
|
||||
return true;
|
||||
}
|
||||
foreach ($remote_message->getAttachments() as $attachment) {
|
||||
if (!str_contains($attachment->getFilename(), '.pdf')) {
|
||||
continue;
|
||||
@ -247,4 +270,59 @@ class Attachments extends Base
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public function find(Message $message, string $filename): \ProVM\Emails\Model\Attachment
|
||||
{
|
||||
return $this->getRepository()->fetchByMessageAndFilename($message->getId(), $filename);
|
||||
}
|
||||
public function exists(Message $message, string $filename): bool
|
||||
{
|
||||
try {
|
||||
$this->find($message, $filename);
|
||||
return true;
|
||||
} catch (BlankResult $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function add(Message $message, string $filename): bool
|
||||
{
|
||||
$data = [
|
||||
'message_id' => $message->getId(),
|
||||
'filename' => $filename
|
||||
];
|
||||
try {
|
||||
$attachment = $this->getRepository()->create($data);
|
||||
$attachment->itIsDownloaded();
|
||||
$this->getRepository()->save($attachment);
|
||||
$message->doesHaveDownloadedAttachments();
|
||||
$this->getMessages()->getRepository()->save($message);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
$this->getLogger()->error($e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function checkDownloaded(): void
|
||||
{
|
||||
$data = $this->getDownloadedFiles();
|
||||
foreach ($data as $info) {
|
||||
if (!$this->exists($info['message'], $info['filename'])) {
|
||||
$this->logger->info("Updating attachment {$info['filename']} for message {$info['message']->getSubject()}");
|
||||
$this->add($info['message'], $info['filename']);
|
||||
}
|
||||
}
|
||||
}
|
||||
public function getDownloaded(): array
|
||||
{
|
||||
return $this->getRepository()->fetchDownloaded();
|
||||
}
|
||||
public function checkEncryption(): void
|
||||
{
|
||||
$attachments = $this->getDownloaded();
|
||||
foreach ($attachments as $attachment) {
|
||||
if ($attachment->isEncrypted() and !$attachment->isDecrypted()) {
|
||||
$this->logger->notice("Schedule decrypt for {$attachment->getFullFilename()}");
|
||||
$this->decrypt($attachment->getMessage(), $attachment->getFilename());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
40
api/common/Service/Install.php
Normal file
40
api/common/Service/Install.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use ProVM\Common\Factory\Model;
|
||||
|
||||
class Install
|
||||
{
|
||||
public function __construct(protected Model $factory, protected array $model_list) {}
|
||||
|
||||
public function check(): bool
|
||||
{
|
||||
foreach ($this->model_list as $model_class) {
|
||||
$repository = $this->factory->find($model_class);
|
||||
if (!$repository->isInstalled()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public function install(): void
|
||||
{
|
||||
$check = true;
|
||||
$repository = null;
|
||||
foreach ($this->model_list as $model_class) {
|
||||
$repository = $this->factory->find($model_class);
|
||||
if ($check) {
|
||||
$query = "SET FOREIGN_KEY_CHECKS = 0";
|
||||
$repository->getConnection()->query($query);
|
||||
$check = false;
|
||||
}
|
||||
if (!$repository->isInstalled()) {
|
||||
$repository->install();
|
||||
}
|
||||
}
|
||||
if (!$check) {
|
||||
$query = "SET FOREIGN_KEY_CHECKS = 1";
|
||||
$repository->getConnection()->query($query);
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ use ProVM\Emails\Repository\State;
|
||||
|
||||
class Mailboxes extends Base
|
||||
{
|
||||
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger)
|
||||
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger, protected int $max_update_days)
|
||||
{
|
||||
$this->setRepository($repository)
|
||||
->setRemoteService($remoteService)
|
||||
@ -35,7 +35,6 @@ class Mailboxes extends Base
|
||||
{
|
||||
return $this->statesRepository;
|
||||
}
|
||||
|
||||
public function setRepository(Mailbox $repository): Mailboxes
|
||||
{
|
||||
$this->repository = $repository;
|
||||
@ -110,6 +109,7 @@ class Mailboxes extends Base
|
||||
$this->getStatesRepository()->save($state);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
$this->getLogger()->error($e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -141,4 +141,13 @@ class Mailboxes extends Base
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public function isUpdated(\ProVM\Emails\Model\Mailbox $mailbox): bool
|
||||
{
|
||||
$states = $mailbox->getStates();
|
||||
if (count($states) === 0) {
|
||||
return false;
|
||||
}
|
||||
$last = $states[count($states) - 1];
|
||||
return abs((int) $last->getDateTime()->diff(new \DateTimeImmutable())->format('%r%a')) < $this->max_update_days;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ namespace ProVM\Common\Service;
|
||||
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use PDOException;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Exception\Mailbox\Stateless;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
@ -14,17 +15,19 @@ use Safe\DateTimeImmutable;
|
||||
|
||||
class Messages extends Base
|
||||
{
|
||||
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, LoggerInterface $logger)
|
||||
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, Jobs $jobsService, LoggerInterface $logger)
|
||||
{
|
||||
$this->setMailboxes($mailboxes)
|
||||
->setRepository($repository)
|
||||
->setRemoteService($remoteService)
|
||||
->setJobsService($jobsService)
|
||||
->setLogger($logger);
|
||||
}
|
||||
|
||||
protected Mailboxes $mailboxes;
|
||||
protected Message $repository;
|
||||
protected Remote\Messages $remoteService;
|
||||
protected Jobs $jobsService;
|
||||
|
||||
public function getMailboxes(): Mailboxes
|
||||
{
|
||||
@ -38,6 +41,10 @@ class Messages extends Base
|
||||
{
|
||||
return $this->remoteService;
|
||||
}
|
||||
public function getJobsService(): Jobs
|
||||
{
|
||||
return $this->jobsService;
|
||||
}
|
||||
|
||||
public function setMailboxes(Mailboxes $mailboxes): Messages
|
||||
{
|
||||
@ -54,6 +61,11 @@ class Messages extends Base
|
||||
$this->remoteService = $service;
|
||||
return $this;
|
||||
}
|
||||
public function setJobsService(Jobs $service): Messages
|
||||
{
|
||||
$this->jobsService = $service;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message
|
||||
{
|
||||
@ -142,6 +154,7 @@ class Messages extends Base
|
||||
$message->doesHaveValidAttachments();
|
||||
}
|
||||
}
|
||||
error_log(json_encode(compact('message')).PHP_EOL,3,'/logs/debug');
|
||||
$this->getRepository()->save($message);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
@ -174,4 +187,30 @@ class Messages extends Base
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function find(string $subject, string $date): array
|
||||
{
|
||||
return $this->repository->fetchAllBySubjectAndDate($subject, new DateTimeImmutable($date));
|
||||
}
|
||||
public function checkUpdate(): void
|
||||
{
|
||||
$registered = $this->getMailboxes()->getRegistered();
|
||||
foreach ($registered as $mailbox) {
|
||||
if (!$this->getMailboxes()->isUpdated($mailbox)) {
|
||||
$this->logger->info("Updating messages from {$mailbox->getName()}");
|
||||
$this->grab($mailbox->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
public function checkSchedule(): void
|
||||
{
|
||||
$messages = $this->getRepository()->fetchAll();
|
||||
foreach ($messages as $message) {
|
||||
if ($message->hasAttachments() and $message->hasValidAttachments() and !$message->hasDownloadedAttachments() and !$message->hasScheduledDownloads()) {
|
||||
if ($this->getJobsService()->schedule($message->getId())) {
|
||||
$message->doesHaveDownloadedAttachments();
|
||||
$this->getRepository()->save($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user