2016-08-17 06:01:20 +08:00
|
|
|
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';
|
2022-02-17 19:48:57 +08:00
|
|
|
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
|
|
|
|
2022-02-17 19:48:57 +08:00
|
|
|
export const ContactUserStore = koArrayWithDestroy();
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-03-11 05:41:35 +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
|
|
|
|
2021-03-16 17:59:47 +08:00
|
|
|
addObservablesTo(ContactUserStore, {
|
2021-09-23 20:24:06 +08:00
|
|
|
allowSync: false, // Admin setting
|
2022-04-16 17:01:24 +08:00
|
|
|
syncMode: 0,
|
2021-03-11 05:41:35 +08:00
|
|
|
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 => {
|
2022-04-16 17:01:24 +08:00
|
|
|
if (ContactUserStore.syncMode()
|
2021-09-23 20:24:06 +08:00
|
|
|
&& !ContactUserStore.importing()
|
|
|
|
&& !ContactUserStore.syncing()
|
|
|
|
) {
|
|
|
|
ContactUserStore.syncing(true);
|
2022-05-20 14:34:11 +08:00
|
|
|
Remote.streamPerLine(line => {
|
|
|
|
try {
|
|
|
|
line = JSON.parse(line);
|
|
|
|
if ('ContactsSync' === line.Action) {
|
|
|
|
ContactUserStore.syncing(false);
|
|
|
|
fResultFunc && fResultFunc(line.ErrorCode, line);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
ContactUserStore.syncing(false);
|
|
|
|
console.error(e);
|
|
|
|
fResultFunc && fResultFunc(Notification.UnknownError);
|
|
|
|
}
|
|
|
|
}, 'ContactsSync');
|
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) {
|
2022-04-16 17:01:24 +08:00
|
|
|
ContactUserStore.syncMode(SettingsGet('ContactsSyncMode'));
|
2021-09-23 20:24:06 +08:00
|
|
|
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);
|
|
|
|
}
|
2021-03-11 05:41:35 +08:00
|
|
|
};
|