2023-07-31 15:08:51 +08:00
|
|
|
import {
|
|
|
|
|
STOKEY_SYNC,
|
|
|
|
|
DEFAULT_SYNC,
|
|
|
|
|
KV_SETTING_KEY,
|
|
|
|
|
KV_RULES_KEY,
|
2023-08-20 23:30:08 +08:00
|
|
|
KV_RULES_SHARE_KEY,
|
2023-07-31 15:08:51 +08:00
|
|
|
STOKEY_SETTING,
|
|
|
|
|
STOKEY_RULES,
|
2023-08-20 23:30:08 +08:00
|
|
|
KV_SALT_SHARE,
|
2023-07-31 15:08:51 +08:00
|
|
|
} from "../config";
|
|
|
|
|
import storage from "../libs/storage";
|
|
|
|
|
import { getSetting, getRules } from ".";
|
|
|
|
|
import { apiSyncData } from "../apis";
|
2023-08-20 23:30:08 +08:00
|
|
|
import { sha256 } from "./utils";
|
2023-07-31 15:08:51 +08:00
|
|
|
|
2023-08-22 16:27:09 +08:00
|
|
|
/**
|
|
|
|
|
* 同步相关数据
|
|
|
|
|
*/
|
|
|
|
|
export const syncOpt = {
|
|
|
|
|
load: async () => (await storage.getObj(STOKEY_SYNC)) || DEFAULT_SYNC,
|
|
|
|
|
update: async (obj) => {
|
|
|
|
|
await storage.putObj(STOKEY_SYNC, obj);
|
|
|
|
|
},
|
|
|
|
|
};
|
2023-07-31 15:08:51 +08:00
|
|
|
|
2023-08-22 16:27:09 +08:00
|
|
|
/**
|
|
|
|
|
* 同步设置
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const syncSetting = async (isBg = false) => {
|
2023-08-26 14:31:13 +08:00
|
|
|
const { syncUrl, syncKey, settingUpdateAt } = await syncOpt.load();
|
|
|
|
|
if (!syncUrl || !syncKey) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-31 15:08:51 +08:00
|
|
|
|
2023-08-26 14:31:13 +08:00
|
|
|
const setting = await getSetting();
|
|
|
|
|
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) {
|
|
|
|
|
await syncOpt.update({
|
|
|
|
|
settingUpdateAt: res.updateAt,
|
|
|
|
|
settingSyncAt: res.updateAt,
|
|
|
|
|
});
|
|
|
|
|
await storage.setObj(STOKEY_SETTING, res.value);
|
|
|
|
|
} else {
|
|
|
|
|
await syncOpt.update({ settingSyncAt: res.updateAt });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-22 16:27:09 +08:00
|
|
|
/**
|
|
|
|
|
* 同步规则
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const syncRules = async (isBg = false) => {
|
2023-08-26 14:31:13 +08:00
|
|
|
const { syncUrl, syncKey, rulesUpdateAt } = await syncOpt.load();
|
|
|
|
|
if (!syncUrl || !syncKey) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rules = await getRules();
|
|
|
|
|
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) {
|
|
|
|
|
await syncOpt.update({
|
|
|
|
|
rulesUpdateAt: res.updateAt,
|
|
|
|
|
rulesSyncAt: res.updateAt,
|
|
|
|
|
});
|
|
|
|
|
await storage.setObj(STOKEY_RULES, res.value);
|
|
|
|
|
} else {
|
|
|
|
|
await syncOpt.update({ rulesSyncAt: res.updateAt });
|
|
|
|
|
}
|
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-22 16:27:09 +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;
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-22 16:27:09 +08:00
|
|
|
/**
|
|
|
|
|
* 同步个人设置和规则
|
|
|
|
|
* @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);
|
|
|
|
|
};
|