14 private $mailCheckInterval;
18 $this->userId = $userId;
19 $this->mailCheckInterval = COption::getOptionString(
'intranet',
'mail_check_period', 10) * 60;
24 if (Loader::includeModule(
'crm'))
26 return (
bool)MailFilterTable::getCount([
27 '=MAILBOX_ID' => $mailboxId,
28 '=ACTION_TYPE' =>
'crm_imap',
40 foreach ($mailboxesSyncInfo as $mailboxId => $lastMailCheckData)
42 if (!$lastMailCheckData[
'isSuccess'])
44 $mailboxes[$mailboxId] = $lastMailCheckData;
52 $mailboxesToSync = [];
55 foreach ($mailboxesSyncInfo as $mailboxId => $lastMailCheckData)
57 if ($lastMailCheckData[
'isSuccess'])
59 $mailboxesToSync[$mailboxId] = $lastMailCheckData;
62 return $mailboxesToSync;
79 $mailboxesToSync = [];
80 foreach ($mailboxesSyncData as $mailboxId => $lastMailCheckData)
82 if ($lastMailCheckData[
'timeStarted'] >= 0 && (time() - intval($lastMailCheckData[
'timeStarted']) >= $this->mailCheckInterval))
84 $mailboxesToSync[$mailboxId] = $lastMailCheckData;
87 return $mailboxesToSync;
92 return $this->mailCheckInterval;
98 '=MAILBOX_ID' => $mailboxId,
99 '=ENTITY_TYPE' =>
'MAILBOX',
100 '=ENTITY_ID' => $mailboxId,
101 '=PROPERTY_NAME' =>
'SYNC_STATUS',
104 return MailEntityOptionsTable::deleteList($filter);
109 $this->saveSyncStatus($mailboxId,
true, 0);
112 private function buildTimeForSyncStatus($time): int
114 if($time !==
null && (
int)$time >= 0)
124 $this->saveSyncStatus($mailboxId,
true, $this->buildTimeForSyncStatus($time));
129 $this->saveSyncStatus($mailboxId, $isSuccess, $this->buildTimeForSyncStatus($time));
132 private function saveSyncStatus($mailboxID, $status, $date)
135 '=MAILBOX_ID' => $mailboxID,
136 '=ENTITY_TYPE' =>
'MAILBOX',
137 '=ENTITY_ID' => $mailboxID,
138 '=PROPERTY_NAME' =>
'SYNC_STATUS',
142 'MAILBOX_ID' => $mailboxID,
143 'ENTITY_TYPE' =>
'MAILBOX',
144 'ENTITY_ID' => $mailboxID,
145 'PROPERTY_NAME' =>
'SYNC_STATUS',
150 $fields[
'VALUE'] = $status;
153 if(MailEntityOptionsTable::getCount($filter))
155 MailEntityOptionsTable::update(
158 'DATE_INSERT' => $fields[
'DATE_INSERT'],
159 'VALUE' => $fields[
'VALUE'],
165 MailEntityOptionsTable::add(
173 $dateLastOpening = \Bitrix\Mail\Internals\MailEntityOptionsTable::getList(
180 '=MAILBOX_ID' => $mailboxID,
181 '=ENTITY_TYPE' =>
'MAILBOX',
182 '=ENTITY_ID' => $mailboxID,
183 '=PROPERTY_NAME' =>
'SYNC_STATUS',
189 if(isset($dateLastOpening[
'VALUE']))
192 'isSuccess' => (bool)$dateLastOpening[
'VALUE'],
193 'timeStarted' => $dateLastOpening[
'DATE_INSERT']->getTimestamp(),
205 $mailboxesSyncInfo = [];
208 foreach ($userMailboxIds as $id)
212 if($mailboxSyncInfo !==
false)
214 $mailboxesSyncInfo[$id] = $mailboxSyncInfo;
217 return $mailboxesSyncInfo;
225 return intval($lastMailCheckData[
'timeStarted']) + $this->mailCheckInterval - time();
235 $timeBeforeNextSyncMailboxes = [];
237 foreach ($mailboxesSuccessSynced as $mailboxId => $lastMailCheckData)
239 $timeBeforeNextSyncMailboxes[] = intval($lastMailCheckData[
'timeStarted']) + $this->mailCheckInterval - time();
242 return !empty($timeBeforeNextSyncMailboxes) && min($timeBeforeNextSyncMailboxes) > 0 ? min($timeBeforeNextSyncMailboxes) : 0;
251 return !empty($mailboxesIdsFailedToSync) && count($mailboxesIdsFailedToSync) > 0
252 ? (int)$mailboxesIdsFailedToSync[0]
259 if (!(isset($mailboxesOptions[$mailboxId]) && array_key_exists(
'isSuccess', $mailboxesOptions[$mailboxId])))
263 return $mailboxesOptions[$mailboxId][
'isSuccess'];
269 if (!(isset($mailboxesOptions[$mailboxId]) && array_key_exists(
'timeStarted', $mailboxesOptions[$mailboxId])))
273 return $mailboxesOptions[$mailboxId][
'timeStarted'];
setDefaultSyncData($mailboxId)
getSuccessSyncedMailboxes()
getMailboxSyncInfo($mailboxID)
getFailedToSyncMailboxes()
getLastMailboxSyncIsSuccessStatus($mailboxId)
getNextTimeToSync($lastMailCheckData)
static checkSyncWithCrm(int $mailboxId)
getNeedToBeSyncedMailboxes()
getLastMailboxSyncTime($mailboxId)
setSyncStatus($mailboxId, $isSuccess, $time=null)
deleteSyncData($mailboxId)
setSyncStartedData($mailboxId, $time=null)
getFirstFailedToSyncMailboxId()
static getUserMailboxes($userId=null)
static createFromTimestamp($timestamp)