Remove LEGACY Nostr client services and related components to streamline the codebase
- Delete Nostr client hub and associated files, including the Nostr store, to eliminate unused functionality. - Update service tokens and dependency injections to reflect the removal of Nostr client services. - Adjust notification settings in NotificationSettings.vue to prepare for future implementation of notifications.
This commit is contained in:
parent
92c33aa0a3
commit
7e4b64b831
8 changed files with 7 additions and 661 deletions
|
|
@ -1,188 +0,0 @@
|
|||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { relayHub } from '@/lib/nostr/relayHub'
|
||||
import { config } from '@/lib/config'
|
||||
import { pushService, type PushSubscriptionData } from '@/lib/notifications/push'
|
||||
|
||||
// Define an interface for the account object
|
||||
interface NostrAccount {
|
||||
privkey: string
|
||||
pubkey: string
|
||||
}
|
||||
|
||||
export const useNostrStore = defineStore('nostr', () => {
|
||||
// Connection state
|
||||
const isConnected = ref(false)
|
||||
const isConnecting = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
|
||||
// Configuration
|
||||
const relayUrls = ref<string[]>(config.nostr.relays)
|
||||
const account = ref<NostrAccount | null>(null)
|
||||
|
||||
// Push notifications
|
||||
const pushSubscription = ref<PushSubscriptionData | null>(null)
|
||||
const notificationsEnabled = ref(false)
|
||||
|
||||
// Connection management
|
||||
async function connect(): Promise<void> {
|
||||
try {
|
||||
error.value = null
|
||||
isConnecting.value = true
|
||||
|
||||
// Initialize and connect using the centralized relay hub
|
||||
await relayHub.initialize(relayUrls.value)
|
||||
await relayHub.connect()
|
||||
|
||||
isConnected.value = relayHub.isConnected
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err : new Error('Failed to connect')
|
||||
isConnected.value = false
|
||||
throw err
|
||||
} finally {
|
||||
isConnecting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function disconnect(): void {
|
||||
// Don't disconnect the relay hub as it's managed centrally
|
||||
// Just update our local state
|
||||
isConnected.value = false
|
||||
isConnecting.value = false
|
||||
error.value = null
|
||||
}
|
||||
|
||||
// Configuration setters
|
||||
function setConnected(value: boolean) {
|
||||
isConnected.value = value
|
||||
}
|
||||
|
||||
function setRelayUrls(urls: string[]) {
|
||||
relayUrls.value = urls
|
||||
// The relay hub will handle reconnection with new relays if needed
|
||||
}
|
||||
|
||||
function setAccount(nostrAccount: NostrAccount | null) {
|
||||
account.value = nostrAccount
|
||||
}
|
||||
|
||||
// Push notification management
|
||||
async function enablePushNotifications(): Promise<PushSubscriptionData> {
|
||||
try {
|
||||
const subscription = await pushService.subscribe()
|
||||
pushSubscription.value = subscription
|
||||
notificationsEnabled.value = true
|
||||
|
||||
// Store subscription in localStorage for persistence
|
||||
localStorage.setItem('push-subscription', JSON.stringify(subscription))
|
||||
localStorage.setItem('notifications-enabled', 'true')
|
||||
|
||||
return subscription
|
||||
} catch (error) {
|
||||
console.error('Failed to enable push notifications:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function disablePushNotifications(): Promise<void> {
|
||||
try {
|
||||
await pushService.unsubscribe()
|
||||
pushSubscription.value = null
|
||||
notificationsEnabled.value = false
|
||||
|
||||
// Remove from localStorage
|
||||
localStorage.removeItem('push-subscription')
|
||||
localStorage.removeItem('notifications-enabled')
|
||||
} catch (error) {
|
||||
console.error('Failed to disable push notifications:', error)
|
||||
}
|
||||
}
|
||||
|
||||
async function checkPushNotificationStatus(): Promise<void> {
|
||||
try {
|
||||
// Check localStorage first
|
||||
const storedEnabled = localStorage.getItem('notifications-enabled') === 'true'
|
||||
const storedSubscription = localStorage.getItem('push-subscription')
|
||||
|
||||
if (storedEnabled && storedSubscription) {
|
||||
pushSubscription.value = JSON.parse(storedSubscription)
|
||||
notificationsEnabled.value = true
|
||||
}
|
||||
|
||||
// Verify with push service
|
||||
const currentSubscription = await pushService.getSubscription()
|
||||
if (currentSubscription) {
|
||||
pushSubscription.value = currentSubscription
|
||||
notificationsEnabled.value = true
|
||||
} else if (storedEnabled) {
|
||||
// Stored state says enabled but no actual subscription - clear stored state
|
||||
await disablePushNotifications()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to check push notification status:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// Send test notification
|
||||
async function sendTestNotification(): Promise<void> {
|
||||
await pushService.showLocalNotification({
|
||||
title: '🚨 Test Admin Announcement',
|
||||
body: 'This is a test notification to verify push notifications are working correctly.',
|
||||
icon: '/pwa-192x192.png',
|
||||
tag: 'test-notification',
|
||||
data: {
|
||||
url: '/',
|
||||
type: 'admin-announcement'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Setup relay hub event listeners to keep store state in sync
|
||||
function setupRelayHubListeners(): void {
|
||||
relayHub.on('connected', () => {
|
||||
isConnected.value = true
|
||||
isConnecting.value = false
|
||||
error.value = null
|
||||
})
|
||||
|
||||
relayHub.on('disconnected', () => {
|
||||
isConnected.value = false
|
||||
isConnecting.value = false
|
||||
})
|
||||
|
||||
relayHub.on('error', (err: Error) => {
|
||||
error.value = err
|
||||
isConnected.value = false
|
||||
isConnecting.value = false
|
||||
})
|
||||
|
||||
relayHub.on('connecting', () => {
|
||||
isConnecting.value = true
|
||||
})
|
||||
}
|
||||
|
||||
// Initialize relay hub listeners
|
||||
setupRelayHubListeners()
|
||||
|
||||
return {
|
||||
// State
|
||||
isConnected,
|
||||
isConnecting,
|
||||
error,
|
||||
relayUrls,
|
||||
account,
|
||||
pushSubscription,
|
||||
notificationsEnabled,
|
||||
|
||||
// Actions
|
||||
connect,
|
||||
disconnect,
|
||||
setConnected,
|
||||
setRelayUrls,
|
||||
setAccount,
|
||||
enablePushNotifications,
|
||||
disablePushNotifications,
|
||||
checkPushNotificationStatus,
|
||||
sendTestNotification,
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue