mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-08 04:54:32 +00:00
refactor(frontend): cond -> scope
This commit is contained in:
parent
30005ba959
commit
010ec113c2
2 changed files with 36 additions and 36 deletions
|
@ -7,7 +7,7 @@ import { v4 as uuid } from 'uuid';
|
||||||
import type { PreferencesProfile, StorageProvider } from '@/preferences/manager.js';
|
import type { PreferencesProfile, StorageProvider } from '@/preferences/manager.js';
|
||||||
import { cloudBackup } from '@/preferences/utility.js';
|
import { cloudBackup } from '@/preferences/utility.js';
|
||||||
import { miLocalStorage } from '@/local-storage.js';
|
import { miLocalStorage } from '@/local-storage.js';
|
||||||
import { isSameCond, PreferencesManager } from '@/preferences/manager.js';
|
import { isSameScope, PreferencesManager } from '@/preferences/manager.js';
|
||||||
import { store } from '@/store.js';
|
import { store } from '@/store.js';
|
||||||
import { $i } from '@/account.js';
|
import { $i } from '@/account.js';
|
||||||
import { misskeyApi } from '@/utility/misskey-api.js';
|
import { misskeyApi } from '@/utility/misskey-api.js';
|
||||||
|
@ -44,7 +44,7 @@ const storageProvider: StorageProvider = {
|
||||||
scope: ['client', 'preferences', 'sync'],
|
scope: ['client', 'preferences', 'sync'],
|
||||||
key: syncGroup + ':' + ctx.key,
|
key: syncGroup + ':' + ctx.key,
|
||||||
}) as [any, any][];
|
}) as [any, any][];
|
||||||
const target = cloudData.find(([cond]) => isSameCond(cond, ctx.cond));
|
const target = cloudData.find(([scope]) => isSameScope(scope, ctx.scope));
|
||||||
if (target == null) return null;
|
if (target == null) return null;
|
||||||
return {
|
return {
|
||||||
value: target[1],
|
value: target[1],
|
||||||
|
@ -73,12 +73,12 @@ const storageProvider: StorageProvider = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const i = cloudData.findIndex(([cond]) => isSameCond(cond, ctx.cond));
|
const i = cloudData.findIndex(([scope]) => isSameScope(scope, ctx.scope));
|
||||||
|
|
||||||
if (i === -1) {
|
if (i === -1) {
|
||||||
cloudData.push([ctx.cond, ctx.value]);
|
cloudData.push([ctx.scope, ctx.value]);
|
||||||
} else {
|
} else {
|
||||||
cloudData[i] = [ctx.cond, ctx.value];
|
cloudData[i] = [ctx.scope, ctx.value];
|
||||||
}
|
}
|
||||||
|
|
||||||
await misskeyApi('i/registry/set', {
|
await misskeyApi('i/registry/set', {
|
||||||
|
|
|
@ -25,7 +25,7 @@ type PREF = typeof PREF_DEF;
|
||||||
type ValueOf<K extends keyof PREF> = PREF[K]['default'];
|
type ValueOf<K extends keyof PREF> = PREF[K]['default'];
|
||||||
type Account = string; // <host>/<userId>
|
type Account = string; // <host>/<userId>
|
||||||
|
|
||||||
type Cond = Partial<{
|
type Scope = Partial<{
|
||||||
server: string | null; // 将来のため
|
server: string | null; // 将来のため
|
||||||
account: Account | null;
|
account: Account | null;
|
||||||
device: string | null; // 将来のため
|
device: string | null; // 将来のため
|
||||||
|
@ -35,33 +35,33 @@ type ValueMeta = Partial<{
|
||||||
sync: boolean;
|
sync: boolean;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
type PrefRecord<K extends keyof PREF> = [cond: Cond, value: ValueOf<K>, meta: ValueMeta];
|
type PrefRecord<K extends keyof PREF> = [scope: Scope, value: ValueOf<K>, meta: ValueMeta];
|
||||||
|
|
||||||
function parseCond(cond: Cond): {
|
function parseScope(scope: Scope): {
|
||||||
server: string | null;
|
server: string | null;
|
||||||
account: Account | null;
|
account: Account | null;
|
||||||
device: string | null;
|
device: string | null;
|
||||||
} {
|
} {
|
||||||
return {
|
return {
|
||||||
server: cond.server ?? null,
|
server: scope.server ?? null,
|
||||||
account: cond.account ?? null,
|
account: scope.account ?? null,
|
||||||
device: cond.device ?? null,
|
device: scope.device ?? null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeCond(cond: Partial<{
|
function makeScope(scope: Partial<{
|
||||||
server: string | null;
|
server: string | null;
|
||||||
account: Account | null;
|
account: Account | null;
|
||||||
device: string | null;
|
device: string | null;
|
||||||
}>): Cond {
|
}>): Scope {
|
||||||
const c = {} as Cond;
|
const c = {} as Scope;
|
||||||
if (cond.server != null) c.server = cond.server;
|
if (scope.server != null) c.server = scope.server;
|
||||||
if (cond.account != null) c.account = cond.account;
|
if (scope.account != null) c.account = scope.account;
|
||||||
if (cond.device != null) c.device = cond.device;
|
if (scope.device != null) c.device = scope.device;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isSameCond(a: Cond, b: Cond): boolean {
|
export function isSameScope(a: Scope, b: Scope): boolean {
|
||||||
// null と undefined (キー無し) は区別したくないので == で比較
|
// null と undefined (キー無し) は区別したくないので == で比較
|
||||||
// eslint-disable-next-line eqeqeq
|
// eslint-disable-next-line eqeqeq
|
||||||
return a.server == b.server && a.account == b.account && a.device == b.device;
|
return a.server == b.server && a.account == b.account && a.device == b.device;
|
||||||
|
@ -80,9 +80,9 @@ export type PreferencesProfile = {
|
||||||
|
|
||||||
export type StorageProvider = {
|
export type StorageProvider = {
|
||||||
save: (ctx: { profile: PreferencesProfile; }) => void;
|
save: (ctx: { profile: PreferencesProfile; }) => void;
|
||||||
cloudGets: <K extends keyof PREF>(ctx: { needs: { key: K; cond: Cond; }[] }) => Promise<Partial<Record<K, ValueOf<K>>>>;
|
cloudGets: <K extends keyof PREF>(ctx: { needs: { key: K; scope: Scope; }[] }) => Promise<Partial<Record<K, ValueOf<K>>>>;
|
||||||
cloudGet: <K extends keyof PREF>(ctx: { key: K; cond: Cond; }) => Promise<{ value: ValueOf<K>; } | null>;
|
cloudGet: <K extends keyof PREF>(ctx: { key: K; scope: Scope; }) => Promise<{ value: ValueOf<K>; } | null>;
|
||||||
cloudSet: <K extends keyof PREF>(ctx: { key: K; cond: Cond; value: ValueOf<K>; }) => Promise<void>;
|
cloudSet: <K extends keyof PREF>(ctx: { key: K; scope: Scope; value: ValueOf<K>; }) => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PreferencesDefinition = Record<string, {
|
export type PreferencesDefinition = Record<string, {
|
||||||
|
@ -141,8 +141,8 @@ export class PreferencesManager {
|
||||||
this.rewriteRawState(key, value);
|
this.rewriteRawState(key, value);
|
||||||
|
|
||||||
const record = this.getMatchedRecordOf(key);
|
const record = this.getMatchedRecordOf(key);
|
||||||
if (parseCond(record[0]).account == null && this.isAccountDependentKey(key)) {
|
if (parseScope(record[0]).account == null && this.isAccountDependentKey(key)) {
|
||||||
this.profile.preferences[key].push([makeCond({
|
this.profile.preferences[key].push([makeScope({
|
||||||
account: `${host}/${$i!.id}`,
|
account: `${host}/${$i!.id}`,
|
||||||
}), value, {}]);
|
}), value, {}]);
|
||||||
this.save();
|
this.save();
|
||||||
|
@ -155,7 +155,7 @@ export class PreferencesManager {
|
||||||
if (record[2].sync) {
|
if (record[2].sync) {
|
||||||
// awaitの必要なし
|
// awaitの必要なし
|
||||||
// TODO: リクエストを間引く
|
// TODO: リクエストを間引く
|
||||||
this.storageProvider.cloudSet({ key, cond: record[0], value: record[1] });
|
this.storageProvider.cloudSet({ key, scope: record[0], value: record[1] });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,14 +208,14 @@ export class PreferencesManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async fetchCloudValues() {
|
private async fetchCloudValues() {
|
||||||
const needs = [] as { key: keyof PREF; cond: Cond; }[];
|
const needs = [] as { key: keyof PREF; scope: Scope; }[];
|
||||||
for (const _key in PREF_DEF) {
|
for (const _key in PREF_DEF) {
|
||||||
const key = _key as keyof PREF;
|
const key = _key as keyof PREF;
|
||||||
const record = this.getMatchedRecordOf(key);
|
const record = this.getMatchedRecordOf(key);
|
||||||
if (record[2].sync) {
|
if (record[2].sync) {
|
||||||
needs.push({
|
needs.push({
|
||||||
key,
|
key,
|
||||||
cond: record[0],
|
scope: record[0],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,7 +242,7 @@ export class PreferencesManager {
|
||||||
public static newProfile(): PreferencesProfile {
|
public static newProfile(): PreferencesProfile {
|
||||||
const data = {} as PreferencesProfile['preferences'];
|
const data = {} as PreferencesProfile['preferences'];
|
||||||
for (const key in PREF_DEF) {
|
for (const key in PREF_DEF) {
|
||||||
data[key] = [[makeCond({}), PREF_DEF[key].default, {}]];
|
data[key] = [[makeScope({}), PREF_DEF[key].default, {}]];
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
id: uuid(),
|
id: uuid(),
|
||||||
|
@ -259,7 +259,7 @@ export class PreferencesManager {
|
||||||
for (const key in PREF_DEF) {
|
for (const key in PREF_DEF) {
|
||||||
const records = profileLike.preferences[key];
|
const records = profileLike.preferences[key];
|
||||||
if (records == null || records.length === 0) {
|
if (records == null || records.length === 0) {
|
||||||
data[key] = [[makeCond({}), PREF_DEF[key].default, {}]];
|
data[key] = [[makeScope({}), PREF_DEF[key].default, {}]];
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
data[key] = records;
|
data[key] = records;
|
||||||
|
@ -289,18 +289,18 @@ export class PreferencesManager {
|
||||||
public getMatchedRecordOf<K extends keyof PREF>(key: K): PrefRecord<K> {
|
public getMatchedRecordOf<K extends keyof PREF>(key: K): PrefRecord<K> {
|
||||||
const records = this.profile.preferences[key];
|
const records = this.profile.preferences[key];
|
||||||
|
|
||||||
if ($i == null) return records.find(([cond, v]) => parseCond(cond).account == null)!;
|
if ($i == null) return records.find(([scope, v]) => parseScope(scope).account == null)!;
|
||||||
|
|
||||||
const accountOverrideRecord = records.find(([cond, v]) => parseCond(cond).account === `${host}/${$i!.id}`);
|
const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).account === `${host}/${$i!.id}`);
|
||||||
if (accountOverrideRecord) return accountOverrideRecord;
|
if (accountOverrideRecord) return accountOverrideRecord;
|
||||||
|
|
||||||
const record = records.find(([cond, v]) => parseCond(cond).account == null);
|
const record = records.find(([scope, v]) => parseScope(scope).account == null);
|
||||||
return record!;
|
return record!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public isAccountOverrided<K extends keyof PREF>(key: K): boolean {
|
public isAccountOverrided<K extends keyof PREF>(key: K): boolean {
|
||||||
if ($i == null) return false;
|
if ($i == null) return false;
|
||||||
return this.profile.preferences[key].some(([cond, v]) => parseCond(cond).account === `${host}/${$i!.id}`) ?? false;
|
return this.profile.preferences[key].some(([scope, v]) => parseScope(scope).account === `${host}/${$i!.id}`) ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setAccountOverride<K extends keyof PREF>(key: K) {
|
public setAccountOverride<K extends keyof PREF>(key: K) {
|
||||||
|
@ -309,7 +309,7 @@ export class PreferencesManager {
|
||||||
if (this.isAccountOverrided(key)) return;
|
if (this.isAccountOverrided(key)) return;
|
||||||
|
|
||||||
const records = this.profile.preferences[key];
|
const records = this.profile.preferences[key];
|
||||||
records.push([makeCond({
|
records.push([makeScope({
|
||||||
account: `${host}/${$i!.id}`,
|
account: `${host}/${$i!.id}`,
|
||||||
}), this.s[key], {}]);
|
}), this.s[key], {}]);
|
||||||
|
|
||||||
|
@ -322,7 +322,7 @@ export class PreferencesManager {
|
||||||
|
|
||||||
const records = this.profile.preferences[key];
|
const records = this.profile.preferences[key];
|
||||||
|
|
||||||
const index = records.findIndex(([cond, v]) => parseCond(cond).account === `${host}/${$i!.id}`);
|
const index = records.findIndex(([scope, v]) => parseScope(scope).account === `${host}/${$i!.id}`);
|
||||||
if (index === -1) return;
|
if (index === -1) return;
|
||||||
|
|
||||||
records.splice(index, 1);
|
records.splice(index, 1);
|
||||||
|
@ -341,7 +341,7 @@ export class PreferencesManager {
|
||||||
|
|
||||||
const record = this.getMatchedRecordOf(key);
|
const record = this.getMatchedRecordOf(key);
|
||||||
|
|
||||||
const existing = await this.storageProvider.cloudGet({ key, cond: record[0] });
|
const existing = await this.storageProvider.cloudGet({ key, scope: record[0] });
|
||||||
if (existing != null && !deepEqual(existing.value, record[1])) {
|
if (existing != null && !deepEqual(existing.value, record[1])) {
|
||||||
const { canceled, result } = await os.select({
|
const { canceled, result } = await os.select({
|
||||||
title: i18n.ts.preferenceSyncConflictTitle,
|
title: i18n.ts.preferenceSyncConflictTitle,
|
||||||
|
@ -371,7 +371,7 @@ export class PreferencesManager {
|
||||||
this.save();
|
this.save();
|
||||||
|
|
||||||
// awaitの必要性は無い
|
// awaitの必要性は無い
|
||||||
this.storageProvider.cloudSet({ key, cond: record[0], value: this.s[key] });
|
this.storageProvider.cloudSet({ key, scope: record[0], value: this.s[key] });
|
||||||
|
|
||||||
return { enabled: true };
|
return { enabled: true };
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue