diff options
author | vnugent <public@vaughnnugent.com> | 2023-11-19 14:50:46 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-11-19 14:50:46 -0500 |
commit | bc7b86a242673d7831f6105d000995d9f4d63e09 (patch) | |
tree | 8da5c92047e92174b80ff6f460f8c3148e1e00ca /extension/src/entries/store | |
parent | 0b609c17199e937518c42365b360288acfa872be (diff) |
hasty not working update to get my workspace clean
Diffstat (limited to 'extension/src/entries/store')
-rw-r--r-- | extension/src/entries/store/allowedOrigins.ts | 43 | ||||
-rw-r--r-- | extension/src/entries/store/features.ts | 116 | ||||
-rw-r--r-- | extension/src/entries/store/identity.ts | 43 | ||||
-rw-r--r-- | extension/src/entries/store/index.ts | 60 | ||||
-rw-r--r-- | extension/src/entries/store/types.ts | 9 |
5 files changed, 271 insertions, 0 deletions
diff --git a/extension/src/entries/store/allowedOrigins.ts b/extension/src/entries/store/allowedOrigins.ts new file mode 100644 index 0000000..7fc5e15 --- /dev/null +++ b/extension/src/entries/store/allowedOrigins.ts @@ -0,0 +1,43 @@ + +import 'pinia' +import { } from 'lodash' +import { PiniaPluginContext } from 'pinia' +import { computed, ref } from 'vue'; +import { onWatchableChange } from '../../features/types'; +import { type AllowedOriginStatus } from '../../features/nip07allow-api'; + +declare module 'pinia' { + export interface PiniaCustomProperties { + isTabAllowed: boolean; + currentOrigin: string | undefined; + allowedOrigins: Array<string>; + isOriginProtectionOn: boolean; + allowOrigin(origin?:string): Promise<void>; + dissallowOrigin(origin?:string): Promise<void>; + disableOriginProtection(): Promise<void>; + setOriginProtection(value: boolean): Promise<void>; + } +} + +export const originPlugin = ({ store }: PiniaPluginContext) => { + + const { plugins } = store + const status = ref<AllowedOriginStatus>() + + onWatchableChange(plugins.allowedOrigins, async () => { + //Update the status + status.value = await plugins.allowedOrigins.getStatus() + }, { immediate: true }) + + return { + allowedOrigins: computed(() => status.value?.allowedOrigins || []), + isTabAllowed: computed(() => status.value?.isAllowed == true), + currentOrigin: computed(() => status.value?.currentOrigin), + isOriginProtectionOn: computed(() => status.value?.enabled == true), + //Push to the allow list, will trigger a change if needed + allowOrigin: plugins.allowedOrigins.addOrigin, + //Remove from allow list, will trigger a change if needed + dissallowOrigin: plugins.allowedOrigins.removeOrigin, + setOriginProtection: plugins.allowedOrigins.enable + } +} diff --git a/extension/src/entries/store/features.ts b/extension/src/entries/store/features.ts new file mode 100644 index 0000000..9bf3052 --- /dev/null +++ b/extension/src/entries/store/features.ts @@ -0,0 +1,116 @@ + +import 'pinia' +import { } from 'lodash' +import { PiniaPluginContext } from 'pinia' +import { type Tabs, tabs } from 'webextension-polyfill' + +import { + SendMessageHandler, + useAuthApi, + useHistoryApi, + useIdentityApi, + useNostrApi, + useLocalPki, + usePkiApi, + useSettingsApi, + useForegoundFeatures, + useEventTagFilterApi, + useInjectAllowList +} from "../../features" + +import { RuntimeContext, createPort } from '../../webext-bridge' +import { ref } from 'vue' +import { onWatchableChange } from '../../features/types' + +export type BgPlugins = ReturnType<typeof usePlugins> +export type BgPluginState<T> = { plugins: BgPlugins } & T + +declare module 'pinia' { + export interface PiniaCustomProperties { + plugins: BgPlugins + currentTab: Tabs.Tab | undefined + } +} + +const usePlugins = (sendMessage: SendMessageHandler) => { + //Create plugin wrapping function + const { use } = useForegoundFeatures(sendMessage) + + return { + settings: use(useSettingsApi), + user: use(useAuthApi), + identity: use(useIdentityApi), + nostr: use(useNostrApi), + history: use(useHistoryApi), + localPki: use(useLocalPki), + pki: use(usePkiApi), + tagFilter: use(useEventTagFilterApi), + allowedOrigins: use(useInjectAllowList) + } +} + +export const useBackgroundPiniaPlugin = (context: RuntimeContext) => { + //Create port for context + const { sendMessage } = createPort(context) + const plugins = usePlugins(sendMessage) + const { user } = plugins; + + const currentTab = ref<Tabs.Tab | undefined>(undefined) + + //Plugin store + return ({ store }: PiniaPluginContext) => { + + //watch for status changes + onWatchableChange(user, async () => { + //Get status update and set the values + const { loggedIn, userName } = await user.getStatus(); + store.loggedIn = loggedIn; + store.userName = userName; + + }, { immediate: true }) + + //Wait for settings changes + onWatchableChange(plugins.settings, async () => { + + //Update settings and dark mode on change + store.settings = await plugins.settings.getSiteConfig(); + store.darkMode = await plugins.settings.getDarkMode(); + console.log("Settings changed") + }, { immediate: true }) + + + const initTab = async () => { + + if(!tabs){ + return; + } + + //Get the current tab + const [active] = await tabs.query({ active: true, currentWindow: true }) + currentTab.value = active + + //Watch for changes to the current tab + tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { + //If the url changed, update the current tab + if (changeInfo.url) { + currentTab.value = tab + } + }) + + tabs.onActivated.addListener(async ({ tabId }) => { + //Get the tab + const tab = await tabs.get(tabId) + //Update the current tab + currentTab.value = tab + }) + } + + + initTab() + + return{ + plugins, + currentTab, + } + } +}
\ No newline at end of file diff --git a/extension/src/entries/store/identity.ts b/extension/src/entries/store/identity.ts new file mode 100644 index 0000000..58a6b67 --- /dev/null +++ b/extension/src/entries/store/identity.ts @@ -0,0 +1,43 @@ + +import 'pinia' +import { } from 'lodash' +import { PiniaPluginContext } from 'pinia' +import { NostrPubKey } from '../../features' +import { ref } from 'vue'; +import { onWatchableChange } from '../../features/types'; + +declare module 'pinia' { + export interface PiniaCustomStateProperties { + allKeys: NostrPubKey[]; + selectedKey: NostrPubKey | undefined; + deleteIdentity(key: Partial<NostrPubKey>): Promise<void>; + createIdentity(id: Partial<NostrPubKey>): Promise<NostrPubKey>; + updateIdentity(id: NostrPubKey): Promise<NostrPubKey>; + selectKey(key: NostrPubKey): Promise<void>; + } +} + + +export const identityPlugin = ({ store }: PiniaPluginContext) => { + + const { identity } = store.plugins + + const allKeys = ref<NostrPubKey[]>([]) + const selectedKey = ref<NostrPubKey | undefined>(undefined) + + onWatchableChange(identity, async () => { + allKeys.value = await identity.getAllKeys(); + //Get the current key + selectedKey.value = await identity.getPublicKey(); + console.log('Selected key is now', selectedKey.value) + }, { immediate:true }) + + return { + selectedKey, + allKeys, + selectKey: identity.selectKey, + deleteIdentity: identity.deleteIdentity, + createIdentity: identity.createIdentity, + updateIdentity: identity.updateIdentity + } +}
\ No newline at end of file diff --git a/extension/src/entries/store/index.ts b/extension/src/entries/store/index.ts new file mode 100644 index 0000000..07fce6d --- /dev/null +++ b/extension/src/entries/store/index.ts @@ -0,0 +1,60 @@ +// Copyright (C) 2023 Vaughn Nugent +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +import 'pinia' +import { } from 'lodash' +import { defineStore } from 'pinia' +import { PluginConfig } from '../../features/' +import { NostrStoreState } from './types' + +export type * from './types' +export * from './allowedOrigins' +export * from './features' +export * from './identity' + +export const useStore = defineStore({ + id: 'main', + state: (): NostrStoreState =>({ + loggedIn: false, + userName: '', + settings: undefined as any, + darkMode: false + }), + actions: { + + async login (token: string) { + await this.plugins.user.login(token); + }, + + async logout () { + await this.plugins.user.logout(); + }, + + saveSiteConfig(config: PluginConfig) { + return this.plugins.settings.setSiteConfig(config) + }, + + async toggleDarkMode(){ + await this.plugins.settings.setDarkMode(this.darkMode === false) + }, + + checkIsCurrentOriginAllowed() { + + } + }, + getters:{ + + }, +})
\ No newline at end of file diff --git a/extension/src/entries/store/types.ts b/extension/src/entries/store/types.ts new file mode 100644 index 0000000..7addda4 --- /dev/null +++ b/extension/src/entries/store/types.ts @@ -0,0 +1,9 @@ +import { } from "webextension-polyfill"; +import { PluginConfig } from "../../features"; + +export interface NostrStoreState { + loggedIn: boolean; + userName: string | null; + settings: PluginConfig; + darkMode: boolean; +}
\ No newline at end of file |