snappymail/dev/Stores/User/Contact.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2021-03-10 18:44:48 +08:00
import { SettingsGet } from 'Common/Globals';
2022-02-17 16:36:29 +08:00
import { pInt } from 'Common/Utils';
import { addObservablesTo, koArrayWithDestroy } from 'External/ko';
2021-09-23 20:24:06 +08:00
import Remote from 'Remote/User/Fetch';
2016-06-30 08:02:45 +08:00
export const ContactUserStore = koArrayWithDestroy();
2016-06-30 08:02:45 +08:00
ContactUserStore.loading = ko.observable(false).extend({ debounce: 200 });
ContactUserStore.importing = ko.observable(false).extend({ debounce: 200 });
ContactUserStore.syncing = ko.observable(false).extend({ debounce: 200 });
2016-06-30 08:02:45 +08:00
addObservablesTo(ContactUserStore, {
2021-09-23 20:24:06 +08:00
allowSync: false, // Admin setting
enableSync: false,
syncUrl: '',
syncUser: '',
syncPass: ''
});
2016-06-30 08:02:45 +08:00
2021-09-23 20:24:06 +08:00
/**
* @param {Function} fResultFunc
* @returns {void}
*/
ContactUserStore.sync = fResultFunc => {
if (ContactUserStore.enableSync()
&& !ContactUserStore.importing()
&& !ContactUserStore.syncing()
) {
ContactUserStore.syncing(true);
Remote.request('ContactsSync', (iError, oData) => {
2021-09-23 20:24:06 +08:00
ContactUserStore.syncing(false);
2021-11-30 17:19:43 +08:00
fResultFunc && fResultFunc(iError, oData);
}, null, 200000);
2021-09-23 20:24:06 +08:00
}
};
2016-06-30 08:02:45 +08:00
2021-09-23 20:24:06 +08:00
ContactUserStore.init = () => {
let value = !!SettingsGet('ContactsSyncIsAllowed');
ContactUserStore.allowSync(value);
if (value) {
ContactUserStore.enableSync(!!SettingsGet('EnableContactsSync'));
ContactUserStore.syncUrl(SettingsGet('ContactsSyncUrl'));
ContactUserStore.syncUser(SettingsGet('ContactsSyncUser'));
ContactUserStore.syncPass(SettingsGet('ContactsSyncPassword'));
setTimeout(ContactUserStore.sync, 10000);
value = pInt(SettingsGet('ContactsSyncInterval'));
2021-11-30 17:19:43 +08:00
value = 5 <= value ? (320 >= value ? value : 320) : 20;
2021-09-23 20:24:06 +08:00
setInterval(ContactUserStore.sync, value * 60000 + 5000);
}
};