snappymail/dev/Stores/User/Contact.js

67 lines
2 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 { koComputable, 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
2022-04-16 17:01:24 +08:00
syncMode: 0,
syncUrl: '',
syncUser: '',
syncPass: ''
});
2016-06-30 08:02:45 +08:00
// Also used by Selector
ContactUserStore.hasChecked = koComputable(
// Issue: not all are observed?
() => !!ContactUserStore.find(item => item.checked())
);
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);
2022-09-02 17:52:07 +08:00
fResultFunc?.(line.ErrorCode, line);
2022-05-20 14:34:11 +08:00
}
} catch (e) {
ContactUserStore.syncing(false);
console.error(e);
2022-09-02 17:52:07 +08:00
fResultFunc?.(Notification.UnknownError);
2022-05-20 14:34:11 +08:00
}
}, '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);
}
};