1
Fork 0

Add the Hide Topics storage logic.

This commit is contained in:
Bauke 2023-06-30 11:21:39 +02:00
parent 0fff485473
commit 17bee78e70
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 68 additions and 0 deletions

View File

@ -6,6 +6,7 @@ export enum Feature {
Autocomplete = "autocomplete",
BackToTop = "back-to-top",
Debug = "debug",
HideTopics = "hide-topics",
HideVotes = "hide-votes",
JumpToNewComment = "jump-to-new-comment",
MarkdownToolbar = "markdown-toolbar",

View File

@ -1,11 +1,13 @@
import {createValue} from "@holllo/webextension-storage";
import browser from "webextension-polyfill";
import {Data, Feature} from "./enums.js";
import {collectHideTopicsData} from "./hide-topics.js";
import {defaultKnownGroups} from "./known-groups.js";
import {collectUsernameColors} from "./username-color.js";
import {collectUserLabels} from "./user-label.js";
export * from "./enums.js";
export * from "./hide-topics.js";
export * from "./username-color.js";
export * from "./user-label.js";
@ -56,6 +58,7 @@ export const storageValues = {
value: "2.0.0",
storage: browser.storage.sync,
}),
[Feature.HideTopics]: collectHideTopicsData(),
[Feature.HideVotes]: createValue({
deserialize: (input) => JSON.parse(input) as HideVotesData,
serialize: (input) => JSON.stringify(input),

View File

@ -0,0 +1,64 @@
import browser from "webextension-polyfill";
import {createValue, type Value} from "@holllo/webextension-storage";
import {Feature} from "./enums.js";
/**
* The different matchers for {@link HideTopicPredicate}.
*/
export enum HideTopicMatcher {
DomainIncludes = "domain-includes",
TildesUsernameEquals = "tildes-username-equals",
TitleIncludes = "title-includes",
UserLabelEquals = "user-label-equals",
}
/**
* The predicate for whether a topic should be hidden or not.
*/
export type HideTopicPredicate = {
id: number;
matcher: HideTopicMatcher;
value: string;
};
/**
* Shorthand for an array of {@link Value}-wrapped {@link HideTopicPredicate}s.
*/
export type HideTopicsData = Array<Value<HideTopicPredicate>>;
/**
* Create a {@link Value}-wrapped {@link HideTopicPredicate}.
*/
export async function createValueHideTopicPredicate(
predicate: HideTopicPredicate,
): Promise<HideTopicsData[number]> {
return createValue<HideTopicPredicate>({
deserialize: (input) => JSON.parse(input) as HideTopicPredicate,
serialize: (input) => JSON.stringify(input),
key: `${Feature.HideTopics}-${predicate.id}`,
value: predicate,
storage: browser.storage.sync,
});
}
/**
* Get all hide topic predicates from storage and combine them into a single
* array.
*/
export async function collectHideTopicsData(): Promise<HideTopicsData> {
const storage = await browser.storage.sync.get();
const predicates = [];
for (const [key, value] of Object.entries(storage)) {
if (!key.startsWith(Feature.HideTopics)) {
continue;
}
predicates.push(
await createValueHideTopicPredicate(
JSON.parse(value as string) as HideTopicPredicate,
),
);
}
return predicates;
}