sync by worker
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
KV_RULES_KEY,
|
||||
KV_RULES_SHARE_KEY,
|
||||
KV_SALT_SHARE,
|
||||
OPT_SYNCTYPE_WEBDAV,
|
||||
} from "../config";
|
||||
import {
|
||||
getSyncWithDefault,
|
||||
@@ -15,36 +16,59 @@ import {
|
||||
import { apiSyncData } from "../apis";
|
||||
import { sha256 } from "./utils";
|
||||
|
||||
const syncByWorker = async ({
|
||||
key,
|
||||
value,
|
||||
syncUrl,
|
||||
syncKey,
|
||||
updateAt = 0,
|
||||
syncAt = 0,
|
||||
isBg = false,
|
||||
isForce = false,
|
||||
}) => {
|
||||
if (isForce) {
|
||||
updateAt = Date.now();
|
||||
}
|
||||
return await apiSyncData(
|
||||
`${syncUrl}/sync`,
|
||||
syncKey,
|
||||
{
|
||||
key,
|
||||
value,
|
||||
updateAt: syncAt === 0 ? 0 : updateAt,
|
||||
},
|
||||
isBg
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 同步设置
|
||||
* @returns
|
||||
*/
|
||||
const syncSetting = async (isBg = false, isForce = false) => {
|
||||
let {
|
||||
const {
|
||||
syncType,
|
||||
syncUrl,
|
||||
syncUser,
|
||||
syncKey,
|
||||
settingUpdateAt = 0,
|
||||
settingSyncAt = 0,
|
||||
} = await getSyncWithDefault();
|
||||
if (!syncUrl || !syncKey) {
|
||||
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isForce) {
|
||||
settingUpdateAt = Date.now();
|
||||
}
|
||||
|
||||
const setting = await getSettingWithDefault();
|
||||
const res = await apiSyncData(
|
||||
`${syncUrl}/sync`,
|
||||
const res = await syncByWorker({
|
||||
key: KV_SETTING_KEY,
|
||||
value: setting,
|
||||
syncUrl,
|
||||
syncKey,
|
||||
{
|
||||
key: KV_SETTING_KEY,
|
||||
value: setting,
|
||||
updateAt: settingSyncAt === 0 ? 0 : settingUpdateAt,
|
||||
},
|
||||
isBg
|
||||
);
|
||||
updateAt: settingUpdateAt,
|
||||
syncAt: settingSyncAt,
|
||||
isBg,
|
||||
isForce,
|
||||
});
|
||||
|
||||
if (res.updateAt > settingUpdateAt) {
|
||||
await setSetting(res.value);
|
||||
@@ -70,31 +94,29 @@ export const trySyncSetting = async (isBg = false, isForce = false) => {
|
||||
* @returns
|
||||
*/
|
||||
const syncRules = async (isBg = false, isForce = false) => {
|
||||
let {
|
||||
const {
|
||||
syncType,
|
||||
syncUrl,
|
||||
syncUser,
|
||||
syncKey,
|
||||
rulesUpdateAt = 0,
|
||||
rulesSyncAt = 0,
|
||||
} = await getSyncWithDefault();
|
||||
if (!syncUrl || !syncKey) {
|
||||
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isForce) {
|
||||
rulesUpdateAt = Date.now();
|
||||
}
|
||||
|
||||
const rules = await getRulesWithDefault();
|
||||
const res = await apiSyncData(
|
||||
`${syncUrl}/sync`,
|
||||
const res = await syncByWorker({
|
||||
key: KV_RULES_KEY,
|
||||
value: rules,
|
||||
syncUrl,
|
||||
syncKey,
|
||||
{
|
||||
key: KV_RULES_KEY,
|
||||
value: rules,
|
||||
updateAt: rulesSyncAt === 0 ? 0 : rulesUpdateAt,
|
||||
},
|
||||
isBg
|
||||
);
|
||||
updateAt: rulesUpdateAt,
|
||||
syncAt: rulesSyncAt,
|
||||
isBg,
|
||||
isForce,
|
||||
});
|
||||
|
||||
if (res.updateAt > rulesUpdateAt) {
|
||||
await setRules(res.value);
|
||||
@@ -121,10 +143,13 @@ export const trySyncRules = async (isBg = false, isForce = false) => {
|
||||
* @returns
|
||||
*/
|
||||
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
|
||||
await apiSyncData(`${syncUrl}/sync`, syncKey, {
|
||||
await syncByWorker({
|
||||
key: KV_RULES_SHARE_KEY,
|
||||
value: rules,
|
||||
syncUrl,
|
||||
syncKey,
|
||||
updateAt: Date.now(),
|
||||
syncAt: Date.now(),
|
||||
});
|
||||
const psk = await sha256(syncKey, KV_SALT_SHARE);
|
||||
const shareUrl = `${syncUrl}/rules?psk=${psk}`;
|
||||
|
||||
Reference in New Issue
Block a user