Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Mailbox.php « Db « lib - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a0b78e2e9b2b24b88d3d6d52bc98bb92c4d7dd36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

declare(strict_types=1);

/**
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace OCA\Mail\Db;

use JsonSerializable;
use OCA\Mail\IMAP\MailboxStats;
use OCP\AppFramework\Db\Entity;
use ReturnTypeWillChange;
use function base64_encode;
use function in_array;
use function json_decode;
use function ltrim;
use function strtolower;

/**
 * @method string getName()
 * @method void setName(string $name)
 * @method int getAccountId()
 * @method void setAccountId(int $accountId)
 * @method string|null getSyncNewToken()
 * @method void setSyncNewToken(string|null $syncNewToken)
 * @method string|null getSyncChangedToken()
 * @method void setSyncChangedToken(string|null $syncNewToken)
 * @method string|null getSyncVanishedToken()
 * @method void setSyncVanishedToken(string|null $syncNewToken)
 * @method int|null getSyncNewLock()
 * @method void setSyncNewLock(int|null $ts)
 * @method int|null getSyncChangedLock()
 * @method void setSyncChangedLock(int|null $ts)
 * @method int|null getSyncVanishedLock()
 * @method void setSyncVanishedLock(int|null $ts)
 * @method string getAttributes()
 * @method void setAttributes(string $attributes)
 * @method string|null getDelimiter()
 * @method void setDelimiter(string|null $delimiter)
 * @method int getMessages()
 * @method void setMessages(int $messages)
 * @method int getUnseen()
 * @method void setUnseen(int $unseen)
 * @method bool|null getSelectable()
 * @method void setSelectable(bool $selectable)
 * @method string getSpecialUse()
 * @method void setSpecialUse(string $specialUse)
 * @method bool|null getSyncInBackground()
 * @method void setSyncInBackground(bool $sync)
 */
class Mailbox extends Entity implements JsonSerializable {
	protected $name;
	protected $accountId;
	protected $syncNewToken;
	protected $syncChangedToken;
	protected $syncVanishedToken;
	protected $syncNewLock;
	protected $syncChangedLock;
	protected $syncVanishedLock;
	protected $attributes;
	protected $delimiter;
	protected $messages;
	protected $unseen;
	protected $selectable;
	protected $specialUse;
	protected $syncInBackground;

	/**
	 * @var int
	 * Lock timeout for sync (5 minutes)
	 */
	public const LOCK_TIMEOUT = 300;

	public function __construct() {
		$this->addType('accountId', 'integer');
		$this->addType('messages', 'integer');
		$this->addType('unseen', 'integer');
		$this->addType('syncNewLock', 'integer');
		$this->addType('syncChangedLock', 'integer');
		$this->addType('syncVanishedLock', 'integer');
		$this->addType('selectable', 'boolean');
		$this->addType('syncInBackground', 'boolean');
	}

	public function isInbox(): bool {
		// https://tools.ietf.org/html/rfc3501#section-5.1
		return strtolower($this->getName()) === 'inbox';
	}

	private function getSpecialUseParsed(): array {
		return json_decode($this->getSpecialUse() ?? '[]', true) ?? [];
	}

	public function isSpecialUse(string $specialUse): bool {
		return in_array(
			ltrim(
				strtolower($specialUse),
				'\\'
			),
			array_map("strtolower", $this->getSpecialUseParsed()),
			true
		);
	}

	public function isCached(): bool {
		return $this->getSyncNewToken() !== null
			&& $this->getSyncChangedToken() !== null
			&& $this->getSyncVanishedToken() !== null;
	}

	public function hasLocks(int $now): bool {
		if ($this->getSyncNewLock() !== null || $this->getSyncNewLock() > ($now - self::LOCK_TIMEOUT)) {
			return true;
		}
		if ($this->getSyncChangedLock() !== null || $this->getSyncChangedLock() > ($now - self::LOCK_TIMEOUT)) {
			return true;
		}
		if ($this->getSyncVanishedLock() !== null || $this->getSyncVanishedLock() > ($now - self::LOCK_TIMEOUT)) {
			return true;
		}
		return false;
	}

	/**
	 * @return MailboxStats
	 */
	public function getStats(): MailboxStats {
		return new MailboxStats($this->getMessages(), $this->getUnseen());
	}

	#[ReturnTypeWillChange]
	public function jsonSerialize() {
		$specialUse = $this->getSpecialUseParsed();
		return [
			'databaseId' => $this->getId(),
			'id' => base64_encode($this->getName()),
			'name' => $this->getName(),
			'accountId' => $this->accountId,
			'displayName' => $this->getName(),
			'attributes' => json_decode($this->attributes ?? '[]', true) ?? [],
			'delimiter' => $this->delimiter,
			'specialUse' => $specialUse,
			'specialRole' => $specialUse[0] ?? 0,
			'mailboxes' => [],
			'syncInBackground' => ($this->getSyncInBackground() === true),
			'unread' => $this->unseen,
		];
	}
}