From 17bee78e70e78d48c9d1063b4b7842e6114e0c0a Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 30 Jun 2023 11:21:39 +0200 Subject: [PATCH] Add the Hide Topics storage logic. --- source/storage/enums.ts | 1 + source/storage/exports.ts | 3 ++ source/storage/hide-topics.ts | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 source/storage/hide-topics.ts diff --git a/source/storage/enums.ts b/source/storage/enums.ts index 8492cc2..c5021d5 100644 --- a/source/storage/enums.ts +++ b/source/storage/enums.ts @@ -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", diff --git a/source/storage/exports.ts b/source/storage/exports.ts index 847e4ce..e9ff4eb 100644 --- a/source/storage/exports.ts +++ b/source/storage/exports.ts @@ -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), diff --git a/source/storage/hide-topics.ts b/source/storage/hide-topics.ts new file mode 100644 index 0000000..23b9c70 --- /dev/null +++ b/source/storage/hide-topics.ts @@ -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>; + +/** + * Create a {@link Value}-wrapped {@link HideTopicPredicate}. + */ +export async function createValueHideTopicPredicate( + predicate: HideTopicPredicate, +): Promise { + return createValue({ + 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 { + 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; +}