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-09-09 16:02:40 +08:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2022-09-09 16:02:40 +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 = () => {
|
2022-11-18 16:54:53 +08:00
|
|
|
let config = SettingsGet('ContactsSync');
|
|
|
|
ContactUserStore.allowSync(!!config);
|
|
|
|
if (config) {
|
|
|
|
ContactUserStore.syncMode(config.Mode);
|
|
|
|
ContactUserStore.syncUrl(config.Url);
|
|
|
|
ContactUserStore.syncUser(config.User);
|
|
|
|
ContactUserStore.syncPass(config.Password);
|
2021-09-23 20:24:06 +08:00
|
|
|
setTimeout(ContactUserStore.sync, 10000);
|
2022-11-18 16:54:53 +08:00
|
|
|
setInterval(ContactUserStore.sync, config.Interval * 60000 + 5000);
|
2021-09-23 20:24:06 +08:00
|
|
|
}
|
2021-03-11 05:41:35 +08:00
|
|
|
};
|