snappymail/dev/Stores/User/Contact.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2021-03-10 18:44:48 +08:00
import { SettingsGet } from 'Common/Globals';
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 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);
setInterval(ContactUserStore.sync, config.Interval * 60000 + 5000);
2021-09-23 20:24:06 +08:00
}
};