Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/background/set_default_settings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Settings } from "../types/settings"
import { settings_item } from "../storage/items"
import { poll_feed } from "./pull_feed"

// set_setting was moved to ./set_setting so content-script entrypoints
// can import it without pulling in poll_feed / fast-xml-parser.
export { set_setting } from "./set_setting"

export async function init_settings(): Promise<void> {
let current_settings = await chrome.storage.sync.get("settings")
const current_settings = await settings_item.getValue()

if (current_settings.settings) {
if (current_settings) {
// settings already exist, no need to set defaults
await poll_feed()
return
Expand All @@ -32,7 +33,7 @@ export async function init_settings(): Promise<void> {
record_post_history: true,
record_setting_active: false,
}
await chrome.storage.sync.set({ settings: initial_settings })
await settings_item.setValue(initial_settings)

await poll_feed()
}
12 changes: 6 additions & 6 deletions src/background/set_setting.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Settings } from "../types/settings"
import { settings_item } from "../storage/items"

export async function set_setting<K extends keyof Settings>(
key: K,
value: Settings[K]
): Promise<void> {
const current_settings = await chrome.storage.sync.get("settings")
if (!current_settings.settings) {
const current_settings = await settings_item.getValue()
if (!current_settings) {
console.error("Settings not initialized yet.")
return
}

const new_settings = {
...current_settings.settings,
await settings_item.setValue({
...current_settings,
[key]: value,
}
await chrome.storage.sync.set({ settings: new_settings })
})
}
44 changes: 17 additions & 27 deletions src/storage/get.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
import { ItemRecord } from "../types/item_record"
import { get_news_channel_items, list_news_channel_names } from "./items"

export async function get_all_news_channels(): Promise<string[]> {
const news_channels = await chrome.storage.local.getKeys()
return news_channels
.filter(key => key.startsWith("news_channel_"))
.map(key => key.replace("news_channel_", ""))
return await list_news_channel_names()
}

export async function get_news_channel(channel_name: string): Promise<ItemRecord[]> {
const news_channel = await chrome.storage.local.get(`news_channel_${channel_name}`)
return news_channel[`news_channel_${channel_name}`] as ItemRecord[] || []
return (await get_news_channel_items(channel_name)) ?? []
}

export async function get_news_item(
channel_name: string,
item_guid: string
): Promise<ItemRecord | undefined> {
let channel_items = await get_news_channel(channel_name)
if (!channel_items) return undefined

let this_item = channel_items.find(item => item.guid === item_guid)
if (!this_item) return undefined

return this_item
const channel_items = await get_news_channel(channel_name)
return channel_items.find(item => item.guid === item_guid)
}

export interface itemWithContext {
item: ItemRecord
parent_channel: string
}

export async function search_for_news_item(filter_function: (item: ItemRecord) => boolean): Promise<itemWithContext[]> {
let all_channels = await get_all_news_channels()
let matching_items: itemWithContext[] = []

for (let channel of all_channels) {
let items = await get_news_channel(channel)
let filtered_items = items.filter(filter_function)
let filtered_items_with_channel = filtered_items.map(i => {
return {
item: i,
parent_channel: channel,
}
})
matching_items = matching_items.concat(filtered_items_with_channel)
export async function search_for_news_item(
filter_function: (item: ItemRecord) => boolean
): Promise<itemWithContext[]> {
const all_channels = await get_all_news_channels()
const matching_items: itemWithContext[] = []

for (const channel of all_channels) {
const items = await get_news_channel(channel)
const filtered = items.filter(filter_function)
for (const item of filtered) {
matching_items.push({ item, parent_channel: channel })
}
}

return matching_items
Expand Down
55 changes: 55 additions & 0 deletions src/storage/items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ItemRecord } from "../types/item_record"
import { RecentsEntry } from "../types/recents"
import { RevisionHistoryEntry } from "../types/rev_history"
import { Settings } from "../types/settings"

// Typed storage singletons. Storage area prefix matches the area the legacy
// chrome.storage.* calls used: sync for user-editable settings, local for the
// per-channel item caches and the revision history.

export const settings_item = storage.defineItem<Settings | null>("sync:settings", {
fallback: null,
})

export const recents_item = storage.defineItem<RecentsEntry[]>("local:recent_pages", {
fallback: [],
})

// Dynamic per-channel keys. defineItem can't be used at the module level for
// these since the channel name is only known at runtime, so wrap getItem /
// setItem in a small factory that captures the prefixed key and typed payload.

const NEWS_CHANNEL_PREFIX = "news_channel_"
const REVISION_PREFIX = "news_recent_"

export async function get_news_channel_items(channel_name: string): Promise<ItemRecord[] | null> {
return await storage.getItem<ItemRecord[]>(`local:${NEWS_CHANNEL_PREFIX}${channel_name}`)
}

export async function set_news_channel_items(
channel_name: string,
items: ItemRecord[]
): Promise<void> {
await storage.setItem<ItemRecord[]>(`local:${NEWS_CHANNEL_PREFIX}${channel_name}`, items)
}

export async function get_revision_entry(rev_id: string): Promise<RevisionHistoryEntry | null> {
return await storage.getItem<RevisionHistoryEntry>(`local:${REVISION_PREFIX}${rev_id}`)
}

export async function set_revision_entry(
rev_id: string,
entry: RevisionHistoryEntry
): Promise<void> {
await storage.setItem<RevisionHistoryEntry>(`local:${REVISION_PREFIX}${rev_id}`, entry)
}

// Enumerate all news channel names currently in local storage. WXT's helper
// exposes `snapshot(area)` which loads every value in the area;
// chrome.storage.local.getKeys() is lighter (keys-only) so we keep it here.
export async function list_news_channel_names(): Promise<string[]> {
const keys = await chrome.storage.local.getKeys()
return keys
.filter(key => key.startsWith(NEWS_CHANNEL_PREFIX))
.map(key => key.slice(NEWS_CHANNEL_PREFIX.length))
}
6 changes: 0 additions & 6 deletions src/storage/revisions/common.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/storage/revisions/revision_get.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { RevisionHistoryEntry } from "../../types/rev_history"
import { get_storage_key } from "./common"
import { get_revision_entry } from "../items"

export async function get_revision(rev_id: string): Promise<RevisionHistoryEntry | null> {
const item_name = get_storage_key(rev_id)
const result = await chrome.storage.local.get([item_name])
return result[item_name] as RevisionHistoryEntry || null
return await get_revision_entry(rev_id)
}
15 changes: 6 additions & 9 deletions src/storage/revisions/revision_set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RevisionData, RevisionHistoryEntry } from "../../types/rev_history"
import { get_storage_key } from "./common"
import { get_revision_entry, set_revision_entry } from "../items"
import * as get_storage from "../get"
import * as set_storage from "../set"

Expand All @@ -12,7 +12,6 @@ export async function add_revision_to_history(
diff_delete: number
): Promise<RevisionHistoryEntry> {
const uuid = crypto.randomUUID()
const item_name = get_storage_key(uuid)

const data_object: RevisionHistoryEntry = {
rev_id: uuid,
Expand All @@ -22,9 +21,9 @@ export async function add_revision_to_history(

diff_new_size: diff_add,
diff_modified_size: diff_modify,
diff_delete_size: diff_delete
diff_delete_size: diff_delete,
}
await chrome.storage.local.set({ [item_name]: data_object })
await set_revision_entry(uuid, data_object)

return data_object
}
Expand All @@ -37,17 +36,15 @@ export async function remove_null_revisions(item_guid: string): Promise<boolean>

const filtered_uuids: string[] = []
for (const uuid of item.rev_history_uuids ?? []) {
const rev_key = get_storage_key(uuid)
const rev_data = await chrome.storage.local.get(rev_key)

if (rev_data[rev_key] !== null && rev_data[rev_key] !== undefined) {
const rev_data = await get_revision_entry(uuid)
if (rev_data) {
filtered_uuids.push(uuid)
}
}
item.rev_history_uuids = filtered_uuids

await set_storage.update_item_properties("news", item_guid, {
rev_history_uuids: item.rev_history_uuids
rev_history_uuids: item.rev_history_uuids,
})
return true
}
Loading
Loading