SQL CaseWhen/Sum + ContactRequest API fix

This commit is contained in:
2021-04-10 01:33:40 +02:00
parent 7506a81514
commit 896bbe76b4
8 changed files with 121 additions and 38 deletions

View File

@@ -11,6 +11,8 @@ namespace Api {
public function __construct(User $user, bool $externalCall, array $params) {
parent::__construct($user, $externalCall, $params);
$this->messageId = null;
$this->csrfTokenRequired = false;
}
protected function sendMail(string $name, ?string $fromEmail, string $subject, string $message, ?string $to = null): bool {
@@ -23,6 +25,7 @@ namespace Api {
"to" => $to
));
$this->lastError = $request->getLastError();
if ($this->success) {
$this->messageId = $request->getResult()["messageId"];
}
@@ -39,6 +42,9 @@ namespace Api\Contact {
use Api\Parameter\StringType;
use Api\VerifyCaptcha;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondNot;
use Driver\SQL\Expression\CaseWhen;
use Driver\SQL\Expression\Sum;
use Objects\User;
class Request extends ContactAPI {
@@ -219,9 +225,10 @@ namespace Api\Contact {
}
$sql = $this->user->getSQL();
$res = $sql->select("ContactRequest.uid", "from_name", "from_email", "from_name", $sql->sum("read"))
$res = $sql->select("ContactRequest.uid", "from_name", "from_email", "from_name",
new Sum(new CaseWhen(new CondNot("ContactMessage.read"), 1, 0), "unread"))
->from("ContactRequest")
->groupBy("uid")
->groupBy("ContactRequest.uid")
->leftJoin("ContactMessage", "ContactRequest.uid", "ContactMessage.request_id")
->execute();

View File

@@ -187,7 +187,7 @@ namespace Api\Mail {
$sql = $this->user->getSQL();
$query = $sql->insert("ContactMessage", ["request_id", "user_id", "message", "messageId", "created_at"])
->onDuplicateKeyStrategy(new UpdateStrategy(["message_id"], ["message" => new Column("message")]));
->onDuplicateKeyStrategy(new UpdateStrategy(["messageId"], ["message" => new Column("message")]));
$entityIds = [];
foreach ($messages as $message) {
@@ -279,6 +279,7 @@ namespace Api\Mail {
}
private function runSearch($mbox, string $searchCriteria, ?\DateTime $lastSyncDateTime, array $messageIds, array &$messages) {
$result = @imap_search($mbox, $searchCriteria);
if ($result === false) {
$err = imap_last_error(); // might return false, if not messages were found, so we can just abort without throwing an error
@@ -287,7 +288,7 @@ namespace Api\Mail {
foreach ($result as $msgNo) {
$header = imap_headerinfo($mbox, $msgNo);
$date = $this->parseDate($header->date);
$date = $this->parseDate($header->date);
if ($date === false) {
return false;
}
@@ -309,7 +310,9 @@ namespace Api\Mail {
foreach ($structure->parts as $part) {
$disposition = (property_exists($part, "disposition") ? $part->disposition : null);
if ($disposition === "attachment") {
$fileName = array_filter($part->dparameters, function($param) { return $param->attribute === "filename"; });
$fileName = array_filter($part->dparameters, function ($param) {
return $param->attribute === "filename";
});
if (count($fileName) > 0) {
$attachments[] = $fileName[0]->value;
}
@@ -320,14 +323,16 @@ namespace Api\Mail {
$body = imap_fetchbody($mbox, $msgNo, "1");
$body = $this->parseBody($body);
$messages[] = [
"messageId" => $messageId,
"requestId" => $requestId,
"timestamp" => $date->getTimestamp(),
"from" => $senderAddress,
"body" => $body,
"attachments" => $attachments
];
if (!isset($messageId[$messageId])) {
$messages[$messageId] = [
"messageId" => $messageId,
"requestId" => $requestId,
"timestamp" => $date->getTimestamp(),
"from" => $senderAddress,
"body" => $body,
"attachments" => $attachments
];
}
}
}
}
@@ -381,7 +386,7 @@ namespace Api\Mail {
$messages = [];
foreach ($boxes as $box) {
imap_reopen($mbox, $box);
if (!$this->runSearch($mbox, $searchCriteria, $lastSyncDateTime, $messageIds,$messages)) {
if (!$this->runSearch($mbox, $searchCriteria, $lastSyncDateTime, $messageIds, $messages)) {
return false;
}
}