address review comments and add private blocking

This commit is contained in:
Alejandro Gomez
2023-01-27 22:10:14 +01:00
parent 592a8b04c0
commit 456aa5fb79
23 changed files with 385 additions and 218 deletions

View File

@ -3,12 +3,20 @@ import * as secp from '@noble/secp256k1';
import { DefaultRelays } from 'Const';
import { HexKey, TaggedRawEvent } from 'Nostr';
import { RelaySettings } from 'Nostr/Connection';
import type { AppDispatch, RootState } from "State/Store";
const PrivateKeyItem = "secret";
const PublicKeyItem = "pubkey";
const NotificationsReadItem = "notifications-read";
const UserPreferencesKey = "preferences";
export interface NotificationRequest {
title: string
body: string
icon: string
timestamp: number
}
export interface UserPreferences {
/**
* Enable reactions / reposts / zaps
@ -87,6 +95,11 @@ export interface LoginStore {
*/
latestMuted: number,
/**
* A list of pubkeys this user has muted privately
*/
blocked: HexKey[],
/**
* Notifications for this login session
*/
@ -122,6 +135,7 @@ const InitState = {
follows: [],
latestFollows: 0,
muted: [],
blocked: [],
latestMuted: 0,
notifications: [],
readNotifications: new Date().getTime(),
@ -238,49 +252,26 @@ const LoginSlice = createSlice({
},
setMuted(state, action: PayloadAction<{createdAt: number, keys: HexKey[]}>) {
const { createdAt, keys } = action.payload
if (createdAt > state.latestMuted) {
if (createdAt >= state.latestMuted) {
const muted = new Set([...keys])
state.muted = Array.from(muted)
state.latestMuted = createdAt
}
},
addNotifications: (state, action: PayloadAction<TaggedRawEvent | TaggedRawEvent[]>) => {
let n = action.payload;
if (!Array.isArray(n)) {
n = [n];
}
let didChange = false;
for (let x of n) {
if (!state.notifications.some(a => a.id === x.id)) {
state.notifications.push(x);
didChange = true;
}
}
if (didChange) {
state.notifications = [
...state.notifications
];
}
setBlocked(state, action: PayloadAction<{createdAt: number, keys: HexKey[]}>) {
const { createdAt, keys } = action.payload
if (createdAt >= state.latestMuted) {
const blocked = new Set([...keys])
state.blocked = Array.from(blocked)
state.latestMuted = createdAt
}
},
addDirectMessage: (state, action: PayloadAction<TaggedRawEvent | Array<TaggedRawEvent>>) => {
let n = action.payload;
if (!Array.isArray(n)) {
n = [n];
}
let didChange = false;
for (let x of n) {
if (!state.dms.some(a => a.id === x.id)) {
state.dms.push(x);
didChange = true;
}
}
if (didChange) {
state.dms = [
...state.dms
];
}
state.dms = n;
},
incDmInteraction: (state) => {
state.dmInteraction += 1;
@ -309,12 +300,36 @@ export const {
setRelays,
removeRelay,
setFollows,
addNotifications,
setMuted,
setBlocked,
addDirectMessage,
incDmInteraction,
logout,
markNotificationsRead,
setPreferences,
} = LoginSlice.actions;
export function sendNotification({ title, body, icon, timestamp }: NotificationRequest) {
return async (dispatch: AppDispatch, getState: () => RootState) => {
const state = getState()
const { readNotifications } = state.login
const hasPermission = "Notification" in window && Notification.permission === "granted"
const shouldShowNotification = hasPermission && timestamp > readNotifications
if (shouldShowNotification) {
try {
let worker = await navigator.serviceWorker.ready;
worker.showNotification(title, {
tag: "notification",
vibrate: [500],
body,
icon,
timestamp,
});
} catch (error) {
console.warn(error)
}
}
}
}
export const reducer = LoginSlice.reducer;