Files
kiss-translator/src/libs/sync.js

129 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-07-31 15:08:51 +08:00
import {
KV_SETTING_KEY,
KV_RULES_KEY,
2023-08-20 23:30:08 +08:00
KV_RULES_SHARE_KEY,
KV_SALT_SHARE,
2023-07-31 15:08:51 +08:00
} from "../config";
2023-08-31 00:18:57 +08:00
import {
getSyncWithDefault,
updateSync,
getSettingWithDefault,
getRulesWithDefault,
setSetting,
setRules,
} from "./storage";
2023-07-31 15:08:51 +08:00
import { apiSyncData } from "../apis";
2023-08-20 23:30:08 +08:00
import { sha256 } from "./utils";
2023-07-31 15:08:51 +08:00
/**
* 同步设置
* @returns
*/
2023-08-30 18:05:37 +08:00
const syncSetting = async (isBg = false) => {
const { syncUrl, syncKey, settingUpdateAt } = await getSyncWithDefault();
2023-08-26 14:31:13 +08:00
if (!syncUrl || !syncKey) {
return;
}
2023-07-31 15:08:51 +08:00
2023-08-31 00:18:57 +08:00
const setting = await getSettingWithDefault();
2023-08-26 14:31:13 +08:00
const res = await apiSyncData(
syncUrl,
syncKey,
{
key: KV_SETTING_KEY,
value: setting,
updateAt: settingUpdateAt,
},
isBg
);
2023-08-17 13:27:22 +08:00
2023-08-26 14:31:13 +08:00
if (res && res.updateAt > settingUpdateAt) {
2023-08-30 18:05:37 +08:00
await updateSync({
2023-08-26 14:31:13 +08:00
settingUpdateAt: res.updateAt,
settingSyncAt: res.updateAt,
});
2023-08-31 00:18:57 +08:00
await setSetting(res.value);
2023-08-26 14:31:13 +08:00
} else {
2023-08-30 18:05:37 +08:00
await updateSync({ settingSyncAt: res.updateAt });
2023-08-26 14:31:13 +08:00
}
};
export const trySyncSetting = async (isBg = false) => {
try {
await syncSetting(isBg);
2023-08-17 13:27:22 +08:00
} catch (err) {
console.log("[sync setting]", err);
2023-07-31 15:08:51 +08:00
}
};
/**
* 同步规则
* @returns
*/
2023-08-30 18:05:37 +08:00
const syncRules = async (isBg = false) => {
const { syncUrl, syncKey, rulesUpdateAt } = await getSyncWithDefault();
2023-08-26 14:31:13 +08:00
if (!syncUrl || !syncKey) {
return;
}
2023-08-31 00:18:57 +08:00
const rules = await getRulesWithDefault();
2023-08-26 14:31:13 +08:00
const res = await apiSyncData(
syncUrl,
syncKey,
{
key: KV_RULES_KEY,
value: rules,
updateAt: rulesUpdateAt,
},
isBg
);
2023-07-31 15:08:51 +08:00
2023-08-26 14:31:13 +08:00
if (res && res.updateAt > rulesUpdateAt) {
2023-08-30 18:05:37 +08:00
await updateSync({
2023-08-26 14:31:13 +08:00
rulesUpdateAt: res.updateAt,
rulesSyncAt: res.updateAt,
});
2023-08-31 00:18:57 +08:00
await setRules(res.value);
2023-08-26 14:31:13 +08:00
} else {
2023-08-30 18:05:37 +08:00
await updateSync({ rulesSyncAt: res.updateAt });
2023-08-26 14:31:13 +08:00
}
};
2023-08-17 13:27:22 +08:00
2023-08-26 14:31:13 +08:00
export const trySyncRules = async (isBg = false) => {
try {
await syncRules(isBg);
2023-08-17 13:27:22 +08:00
} catch (err) {
2023-08-21 21:31:20 +08:00
console.log("[sync user rules]", err);
2023-07-31 15:08:51 +08:00
}
};
/**
* 同步分享规则
* @param {*} param0
* @returns
*/
2023-08-20 23:30:08 +08:00
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
await apiSyncData(syncUrl, syncKey, {
key: KV_RULES_SHARE_KEY,
value: rules,
updateAt: Date.now(),
});
const psk = await sha256(syncKey, KV_SALT_SHARE);
const shareUrl = `${syncUrl}?psk=${psk}`;
return shareUrl;
};
/**
* 同步个人设置和规则
* @returns
*/
export const syncAll = async (isBg = false) => {
await syncSetting(isBg);
await syncRules(isBg);
2023-07-31 15:08:51 +08:00
};
2023-08-26 14:31:13 +08:00
export const trySyncAll = async (isBg = false) => {
await trySyncSetting(isBg);
await trySyncRules(isBg);
};