1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Christoph Wurst <[email protected]> |
5
|
|
|
* @author Thomas Müller <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Mail |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
namespace OCA\Mail\Service; |
23
|
|
|
|
24
|
|
|
use OCP\IL10N; |
25
|
|
|
use OCA\Mail\Model\IMessage; |
26
|
|
|
|
27
|
|
|
class UnifiedAccount implements IAccount { |
28
|
|
|
|
29
|
|
|
const ID = -1; |
30
|
|
|
const INBOX_ID = 'all-inboxes'; |
31
|
|
|
|
32
|
|
|
/** @var AccountService */ |
33
|
|
|
private $accountService; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $userId; |
37
|
|
|
|
38
|
|
|
/** @var IL10N */ |
39
|
|
|
private $l10n; |
40
|
|
|
|
41
|
|
|
/** @var Horde_Mail_Rfc822_List */ |
42
|
|
|
private $email; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param AccountService $accountService |
46
|
|
|
* @param string $userId |
47
|
|
|
* @param IL10N $l10n |
48
|
|
|
*/ |
49
|
1 |
|
public function __construct(AccountService $accountService, $userId, IL10N $l10n) { |
50
|
1 |
|
$this->accountService = $accountService; |
51
|
1 |
|
$this->userId = $userId; |
52
|
1 |
|
$this->l10n = $l10n; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getConfiguration() { |
59
|
|
|
return [ |
60
|
|
|
'accountId' => UnifiedAccount::ID, |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
* TODO: function name is :hankey: |
67
|
|
|
*/ |
68
|
|
|
public function getListArray() { |
69
|
|
|
$inbox = $this->buildInbox(); |
70
|
|
|
return [ |
71
|
|
|
'id' => UnifiedAccount::ID, |
72
|
|
|
'email' => '', |
73
|
|
|
'folders' => [$inbox], |
74
|
|
|
'specialFolders' => [], |
75
|
|
|
'delimiter' => '.', |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function buildInbox() { |
80
|
|
|
$displayName = (string)$this->l10n->t('All inboxes'); |
81
|
|
|
|
82
|
|
|
$allAccounts = $this->accountService->findByUserId($this->userId); |
83
|
|
|
|
84
|
|
|
$uidValidity = []; |
85
|
|
|
$uidNext = []; |
86
|
|
|
$unseen = 0; |
87
|
|
|
|
88
|
|
|
foreach($allAccounts as $account) { |
89
|
|
|
/** @var IAccount $account */ |
90
|
|
|
$inbox = $account->getInbox(); |
91
|
|
|
if (is_null($inbox)) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$status = $inbox->getStatus(); |
96
|
|
|
$unseen += isset($status['unseen']) ? $status['unseen'] : 0; |
97
|
|
|
$uidValidity[$account->getId()] = isset($status['uidvalidity']) ? $status['uidvalidity'] : 0; |
98
|
|
|
$uidNext[$account->getId()] = isset($status['uidnext']) ? $status['uidnext'] : 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return [ |
102
|
|
|
'id' => base64_encode(self::INBOX_ID), |
103
|
|
|
'parent' => null, |
104
|
|
|
'name' => $displayName, |
105
|
|
|
'specialRole' => 'inbox', |
106
|
|
|
'unseen' => $unseen, |
107
|
|
|
'total' => 100, |
108
|
|
|
'isEmpty' => false, |
109
|
|
|
'accountId' => UnifiedAccount::ID, |
110
|
|
|
'noSelect' => false, |
111
|
|
|
'uidvalidity' => $uidValidity, |
112
|
|
|
'uidnext' => $uidNext, |
113
|
|
|
'delimiter' => '.' |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $folderId |
119
|
|
|
* @return IMailBox |
120
|
|
|
*/ |
121
|
|
|
public function getMailbox($folderId) { |
122
|
|
|
return new UnifiedMailbox($this->accountService, $this->userId); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getEmail() { |
129
|
|
|
if ($this->email === null) { |
130
|
|
|
$allAccounts = $this->accountService->findByUserId($this->userId); |
131
|
|
|
$addressesList = new \Horde_Mail_Rfc822_List(); |
132
|
|
|
foreach ($allAccounts as $account) { |
133
|
|
|
$inbox = $account->getInbox(); |
134
|
|
|
if (is_null($inbox)) { |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
$addressesList->add($account->getEmail()); |
138
|
|
|
} |
139
|
|
|
$this->email = $addressesList; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
return $this->email; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param IMessage $message |
146
|
|
|
* @param int|null $draftUID |
147
|
|
|
*/ |
148
|
|
|
public function sendMessage(IMessage $message, $draftUID) { |
149
|
|
|
throw new Exception('Not implemented'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param IMessage $message |
154
|
|
|
* @param int|null $previousUID |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public function saveDraft(IMessage $message, $previousUID) { |
158
|
|
|
throw new Exception('Not implemented'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $folderId |
163
|
|
|
* @param string $messageId |
164
|
|
|
*/ |
165
|
|
|
public function deleteMessage($folderId, $messageId) { |
166
|
|
|
$data = json_decode(base64_decode($messageId), true); |
167
|
|
|
$account = $this->accountService->find($this->userId, $data[0]); |
168
|
|
|
$inbox = $account->getInbox(); |
169
|
|
|
$messageId = $data[1]; |
170
|
|
|
|
171
|
|
|
$account->deleteMessage($inbox->getFolderId(), $messageId); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string[] $query |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function getChangedMailboxes($query) { |
179
|
|
|
$accounts = $this->accountService->findByUserId($this->userId); |
180
|
|
|
$changedBoxes = []; |
181
|
|
|
|
182
|
|
|
foreach($accounts as $account) { |
183
|
|
|
/** @var IAccount $account */ |
184
|
|
|
if ($account->getId() === UnifiedAccount::ID) { |
185
|
|
|
continue; |
186
|
|
|
} |
187
|
|
|
$inbox = $account->getInbox(); |
188
|
|
|
$inboxName = $inbox->getFolderId(); |
189
|
|
|
$changes = $account->getChangedMailboxes([$inboxName => [ |
|
|
|
|
190
|
|
|
'uidvalidity' => $query[self::INBOX_ID]['uidvalidity'][$account->getId()], |
191
|
|
|
'uidnext' => $query[self::INBOX_ID]['uidnext'][$account->getId()], |
192
|
|
|
]]); |
193
|
|
|
if (!isset($changes[$inboxName])) { |
194
|
|
|
continue; |
195
|
|
|
} |
196
|
|
|
if (!isset($changedBoxes[self::INBOX_ID])) { |
197
|
|
|
$changedBoxes[self::INBOX_ID] = $this->buildInbox(); |
198
|
|
|
$changedBoxes[self::INBOX_ID]['messages'] = []; |
199
|
|
|
$changedBoxes[self::INBOX_ID]['newUnReadCounter'] = 0; |
200
|
|
|
} |
201
|
|
|
// Create special unified inbox message IDs |
202
|
|
|
foreach ($changes[$inboxName]['messages'] as &$message) { |
203
|
|
|
$id = base64_encode(json_encode([$account->getId(), $message['id']])); |
204
|
|
|
$message['id'] = $id; |
205
|
|
|
$message['accountMail'] = $account->getEmail(); |
206
|
|
|
} |
207
|
|
|
$changedBoxes[self::INBOX_ID]['messages'] = array_merge($changedBoxes[self::INBOX_ID]['messages'], $changes[$inboxName]['messages']); |
208
|
|
|
$changedBoxes[self::INBOX_ID]['newUnReadCounter'] += $changes[$inboxName]['newUnReadCounter']; |
209
|
|
|
} |
210
|
|
|
return $changedBoxes; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return IMailBox |
215
|
|
|
*/ |
216
|
|
|
public function getInbox() { |
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return int |
222
|
|
|
*/ |
223
|
|
|
public function getId() { |
224
|
|
|
return UnifiedAccount::ID; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $messageId |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function resolve($messageId) { |
|
|
|
|
232
|
|
|
$data = json_decode(base64_decode($messageId), true); |
233
|
|
|
$account = $this->accountService->find($this->userId, $data[0]); |
234
|
|
|
$inbox = $account->getInbox(); |
235
|
|
|
$messageId = $data[1]; |
236
|
|
|
|
237
|
|
|
return [$account, base64_encode($inbox->getFolderId()), $messageId]; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..