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