1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OCA\Mail\Service; |
4
|
|
|
|
5
|
|
|
use OCP\IL10N; |
6
|
|
|
use OCA\Mail\Model\IMessage; |
7
|
|
|
|
8
|
|
|
class UnifiedAccount implements IAccount { |
9
|
|
|
|
10
|
|
|
const ID = -1; |
11
|
|
|
const INBOX_ID = 'all-inboxes'; |
12
|
|
|
|
13
|
|
|
/** @var AccountService */ |
14
|
|
|
private $accountService; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $userId; |
18
|
|
|
|
19
|
|
|
/** @var IL10N */ |
20
|
|
|
private $l10n; |
21
|
|
|
|
22
|
|
|
/** @var Horde_Mail_Rfc822_List */ |
23
|
|
|
private $email; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param AccountService $accountService |
27
|
|
|
* @param string $userId |
28
|
|
|
* @param IL10N $l10n |
29
|
|
|
*/ |
30
|
1 |
|
public function __construct(AccountService $accountService, $userId, IL10N $l10n) { |
31
|
1 |
|
$this->accountService = $accountService; |
32
|
1 |
|
$this->userId = $userId; |
33
|
1 |
|
$this->l10n = $l10n; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getConfiguration() { |
40
|
|
|
return [ |
41
|
|
|
'accountId' => UnifiedAccount::ID, |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
* TODO: function name is :hankey: |
48
|
|
|
*/ |
49
|
|
|
public function getListArray() { |
50
|
|
|
$inbox = $this->buildInbox(); |
51
|
|
|
return [ |
52
|
|
|
'id' => UnifiedAccount::ID, |
53
|
|
|
'email' => '', |
54
|
|
|
'folders' => [$inbox], |
55
|
|
|
'specialFolders' => [], |
56
|
|
|
'delimiter' => '.', |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function buildInbox() { |
61
|
|
|
$displayName = (string)$this->l10n->t('All inboxes'); |
62
|
|
|
|
63
|
|
|
$allAccounts = $this->accountService->findByUserId($this->userId); |
64
|
|
|
|
65
|
|
|
$uidValidity = []; |
66
|
|
|
$uidNext = []; |
67
|
|
|
$unseen = 0; |
68
|
|
|
|
69
|
|
|
foreach($allAccounts as $account) { |
70
|
|
|
/** @var IAccount $account */ |
71
|
|
|
$inbox = $account->getInbox(); |
72
|
|
|
if (is_null($inbox)) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$status = $inbox->getStatus(); |
77
|
|
|
$unseen += isset($status['unseen']) ? $status['unseen'] : 0; |
78
|
|
|
$uidValidity[$account->getId()] = isset($status['uidvalidity']) ? $status['uidvalidity'] : 0; |
79
|
|
|
$uidNext[$account->getId()] = isset($status['uidnext']) ? $status['uidnext'] : 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return [ |
83
|
|
|
'id' => base64_encode(self::INBOX_ID), |
84
|
|
|
'parent' => null, |
85
|
|
|
'name' => $displayName, |
86
|
|
|
'specialRole' => 'inbox', |
87
|
|
|
'unseen' => $unseen, |
88
|
|
|
'total' => 100, |
89
|
|
|
'isEmpty' => false, |
90
|
|
|
'accountId' => UnifiedAccount::ID, |
91
|
|
|
'noSelect' => false, |
92
|
|
|
'uidvalidity' => $uidValidity, |
93
|
|
|
'uidnext' => $uidNext, |
94
|
|
|
'delimiter' => '.' |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $folderId |
100
|
|
|
* @return IMailBox |
101
|
|
|
*/ |
102
|
|
|
public function getMailbox($folderId) { |
103
|
|
|
return new UnifiedMailbox($this->accountService, $this->userId); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getEmail() { |
110
|
|
|
if ($this->email === null) { |
111
|
|
|
$allAccounts = $this->accountService->findByUserId($this->userId); |
112
|
|
|
$addressesList = new \Horde_Mail_Rfc822_List(); |
113
|
|
|
foreach ($allAccounts as $account) { |
114
|
|
|
$inbox = $account->getInbox(); |
115
|
|
|
if (is_null($inbox)) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
$addressesList->add($account->getEmail()); |
119
|
|
|
} |
120
|
|
|
$this->email = $addressesList; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
return $this->email; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param IMessage $message |
127
|
|
|
* @param int|null $draftUID |
128
|
|
|
*/ |
129
|
|
|
public function sendMessage(IMessage $message, $draftUID) { |
130
|
|
|
throw new Exception('Not implemented'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param IMessage $message |
135
|
|
|
* @param int|null $previousUID |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
|
|
public function saveDraft(IMessage $message, $previousUID) { |
139
|
|
|
throw new Exception('Not implemented'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $folderId |
144
|
|
|
* @param string $messageId |
145
|
|
|
*/ |
146
|
|
|
public function deleteMessage($folderId, $messageId) { |
147
|
|
|
$data = json_decode(base64_decode($messageId), true); |
148
|
|
|
$account = $this->accountService->find($this->userId, $data[0]); |
149
|
|
|
$inbox = $account->getInbox(); |
150
|
|
|
$messageId = $data[1]; |
151
|
|
|
|
152
|
|
|
$account->deleteMessage($inbox->getFolderId(), $messageId); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string[] $query |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function getChangedMailboxes($query) { |
160
|
|
|
$accounts = $this->accountService->findByUserId($this->userId); |
161
|
|
|
$changedBoxes = []; |
162
|
|
|
|
163
|
|
|
foreach($accounts as $account) { |
164
|
|
|
/** @var IAccount $account */ |
165
|
|
|
if ($account->getId() === UnifiedAccount::ID) { |
166
|
|
|
continue; |
167
|
|
|
} |
168
|
|
|
$inbox = $account->getInbox(); |
169
|
|
|
$inboxName = $inbox->getFolderId(); |
170
|
|
|
$changes = $account->getChangedMailboxes([$inboxName => [ |
|
|
|
|
171
|
|
|
'uidvalidity' => $query[self::INBOX_ID]['uidvalidity'][$account->getId()], |
172
|
|
|
'uidnext' => $query[self::INBOX_ID]['uidnext'][$account->getId()], |
173
|
|
|
]]); |
174
|
|
|
if (!isset($changes[$inboxName])) { |
175
|
|
|
continue; |
176
|
|
|
} |
177
|
|
|
if (!isset($changedBoxes[self::INBOX_ID])) { |
178
|
|
|
$changedBoxes[self::INBOX_ID] = $this->buildInbox(); |
179
|
|
|
$changedBoxes[self::INBOX_ID]['messages'] = []; |
180
|
|
|
$changedBoxes[self::INBOX_ID]['newUnReadCounter'] = 0; |
181
|
|
|
} |
182
|
|
|
// Create special unified inbox message IDs |
183
|
|
|
foreach ($changes[$inboxName]['messages'] as &$message) { |
184
|
|
|
$id = base64_encode(json_encode([$account->getId(), $message['id']])); |
185
|
|
|
$message['id'] = $id; |
186
|
|
|
} |
187
|
|
|
$changedBoxes[self::INBOX_ID]['messages'] = array_merge($changedBoxes[self::INBOX_ID]['messages'], $changes[$inboxName]['messages']); |
188
|
|
|
$changedBoxes[self::INBOX_ID]['newUnReadCounter'] += $changes[$inboxName]['newUnReadCounter']; |
189
|
|
|
} |
190
|
|
|
return $changedBoxes; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return IMailBox |
195
|
|
|
*/ |
196
|
|
|
public function getInbox() { |
197
|
|
|
return null; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return int |
202
|
|
|
*/ |
203
|
|
|
public function getId() { |
204
|
|
|
return UnifiedAccount::ID; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param $messageId |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function resolve($messageId) { |
|
|
|
|
212
|
|
|
$data = json_decode(base64_decode($messageId), true); |
213
|
|
|
$account = $this->accountService->find($this->userId, $data[0]); |
214
|
|
|
$inbox = $account->getInbox(); |
215
|
|
|
$messageId = $data[1]; |
216
|
|
|
|
217
|
|
|
return [$account, base64_encode($inbox->getFolderId()), $messageId]; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
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..