Add prettier formatting (#214)
* chore: add prettier * chore: format codebase
This commit is contained in:
@ -6,7 +6,15 @@ import { TaggedRawEvent, HexKey, Lists } from "Nostr";
|
||||
import EventKind from "Nostr/EventKind";
|
||||
import Event from "Nostr/Event";
|
||||
import { Subscriptions } from "Nostr/Subscriptions";
|
||||
import { addDirectMessage, setFollows, setRelays, setMuted, setBlocked, sendNotification, setLatestNotifications } from "State/Login";
|
||||
import {
|
||||
addDirectMessage,
|
||||
setFollows,
|
||||
setRelays,
|
||||
setMuted,
|
||||
setBlocked,
|
||||
sendNotification,
|
||||
setLatestNotifications,
|
||||
} from "State/Login";
|
||||
import { RootState } from "State/Store";
|
||||
import { mapEventToProfile, MetadataCache } from "State/Users";
|
||||
import { useDb } from "State/Users/Db";
|
||||
@ -20,7 +28,12 @@ import useModeration from "Hooks/useModeration";
|
||||
*/
|
||||
export default function useLoginFeed() {
|
||||
const dispatch = useDispatch();
|
||||
const { publicKey: pubKey, privateKey: privKey, latestMuted, readNotifications } = useSelector((s: RootState) => s.login);
|
||||
const {
|
||||
publicKey: pubKey,
|
||||
privateKey: privKey,
|
||||
latestMuted,
|
||||
readNotifications,
|
||||
} = useSelector((s: RootState) => s.login);
|
||||
const { isMuted } = useModeration();
|
||||
const db = useDb();
|
||||
|
||||
@ -31,7 +44,7 @@ export default function useLoginFeed() {
|
||||
sub.Id = `login:meta`;
|
||||
sub.Authors = new Set([pubKey]);
|
||||
sub.Kinds = new Set([EventKind.ContactList, EventKind.SetMetadata]);
|
||||
sub.Limit = 2
|
||||
sub.Limit = 2;
|
||||
|
||||
return sub;
|
||||
}, [pubKey]);
|
||||
@ -77,35 +90,49 @@ export default function useLoginFeed() {
|
||||
return dms;
|
||||
}, [pubKey]);
|
||||
|
||||
const metadataFeed = useSubscription(subMetadata, { leaveOpen: true, cache: true });
|
||||
const notificationFeed = useSubscription(subNotification, { leaveOpen: true, cache: true });
|
||||
const metadataFeed = useSubscription(subMetadata, {
|
||||
leaveOpen: true,
|
||||
cache: true,
|
||||
});
|
||||
const notificationFeed = useSubscription(subNotification, {
|
||||
leaveOpen: true,
|
||||
cache: true,
|
||||
});
|
||||
const dmsFeed = useSubscription(subDms, { leaveOpen: true, cache: true });
|
||||
const mutedFeed = useSubscription(subMuted, { leaveOpen: true, cache: true });
|
||||
|
||||
useEffect(() => {
|
||||
let contactList = metadataFeed.store.notes.filter(a => a.kind === EventKind.ContactList);
|
||||
let metadata = metadataFeed.store.notes.filter(a => a.kind === EventKind.SetMetadata);
|
||||
let profiles = metadata.map(a => mapEventToProfile(a))
|
||||
.filter(a => a !== undefined)
|
||||
.map(a => a!);
|
||||
let contactList = metadataFeed.store.notes.filter(
|
||||
(a) => a.kind === EventKind.ContactList
|
||||
);
|
||||
let metadata = metadataFeed.store.notes.filter(
|
||||
(a) => a.kind === EventKind.SetMetadata
|
||||
);
|
||||
let profiles = metadata
|
||||
.map((a) => mapEventToProfile(a))
|
||||
.filter((a) => a !== undefined)
|
||||
.map((a) => a!);
|
||||
|
||||
for (let cl of contactList) {
|
||||
if (cl.content !== "" && cl.content !== "{}") {
|
||||
let relays = JSON.parse(cl.content);
|
||||
dispatch(setRelays({ relays, createdAt: cl.created_at }));
|
||||
}
|
||||
let pTags = cl.tags.filter(a => a[0] === "p").map(a => a[1]);
|
||||
let pTags = cl.tags.filter((a) => a[0] === "p").map((a) => a[1]);
|
||||
dispatch(setFollows({ keys: pTags, createdAt: cl.created_at }));
|
||||
}
|
||||
|
||||
(async () => {
|
||||
let maxProfile = profiles.reduce((acc, v) => {
|
||||
if (v.created > acc.created) {
|
||||
acc.profile = v;
|
||||
acc.created = v.created;
|
||||
}
|
||||
return acc;
|
||||
}, { created: 0, profile: null as MetadataCache | null });
|
||||
let maxProfile = profiles.reduce(
|
||||
(acc, v) => {
|
||||
if (v.created > acc.created) {
|
||||
acc.profile = v;
|
||||
acc.created = v.created;
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{ created: 0, profile: null as MetadataCache | null }
|
||||
);
|
||||
if (maxProfile.profile) {
|
||||
let existing = await db.find(maxProfile.profile.pubkey);
|
||||
if ((existing?.created ?? 0) < maxProfile.created) {
|
||||
@ -116,52 +143,74 @@ export default function useLoginFeed() {
|
||||
}, [dispatch, metadataFeed.store, db]);
|
||||
|
||||
useEffect(() => {
|
||||
const replies = notificationFeed.store.notes.
|
||||
filter(a => a.kind === EventKind.TextNote && !isMuted(a.pubkey) && a.created_at > readNotifications)
|
||||
replies.forEach(nx => {
|
||||
const replies = notificationFeed.store.notes.filter(
|
||||
(a) =>
|
||||
a.kind === EventKind.TextNote &&
|
||||
!isMuted(a.pubkey) &&
|
||||
a.created_at > readNotifications
|
||||
);
|
||||
replies.forEach((nx) => {
|
||||
dispatch(setLatestNotifications(nx.created_at));
|
||||
makeNotification(db, nx).then(notification => {
|
||||
makeNotification(db, nx).then((notification) => {
|
||||
if (notification) {
|
||||
// @ts-ignore
|
||||
dispatch(sendNotification(notification))
|
||||
dispatch(sendNotification(notification));
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
}, [dispatch, notificationFeed.store, db, readNotifications]);
|
||||
|
||||
useEffect(() => {
|
||||
const muted = getMutedKeys(mutedFeed.store.notes)
|
||||
dispatch(setMuted(muted))
|
||||
const muted = getMutedKeys(mutedFeed.store.notes);
|
||||
dispatch(setMuted(muted));
|
||||
|
||||
const newest = getNewest(mutedFeed.store.notes)
|
||||
if (newest && newest.content.length > 0 && pubKey && newest.created_at > latestMuted) {
|
||||
decryptBlocked(newest, pubKey, privKey).then((plaintext) => {
|
||||
try {
|
||||
const blocked = JSON.parse(plaintext)
|
||||
const keys = blocked.filter((p: any) => p && p.length === 2 && p[0] === "p").map((p: any) => p[1])
|
||||
dispatch(setBlocked({
|
||||
keys,
|
||||
createdAt: newest.created_at,
|
||||
}))
|
||||
} catch (error) {
|
||||
console.debug("Couldn't parse JSON")
|
||||
}
|
||||
}).catch((error) => console.warn(error))
|
||||
const newest = getNewest(mutedFeed.store.notes);
|
||||
if (
|
||||
newest &&
|
||||
newest.content.length > 0 &&
|
||||
pubKey &&
|
||||
newest.created_at > latestMuted
|
||||
) {
|
||||
decryptBlocked(newest, pubKey, privKey)
|
||||
.then((plaintext) => {
|
||||
try {
|
||||
const blocked = JSON.parse(plaintext);
|
||||
const keys = blocked
|
||||
.filter((p: any) => p && p.length === 2 && p[0] === "p")
|
||||
.map((p: any) => p[1]);
|
||||
dispatch(
|
||||
setBlocked({
|
||||
keys,
|
||||
createdAt: newest.created_at,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
console.debug("Couldn't parse JSON");
|
||||
}
|
||||
})
|
||||
.catch((error) => console.warn(error));
|
||||
}
|
||||
}, [dispatch, mutedFeed.store])
|
||||
}, [dispatch, mutedFeed.store]);
|
||||
|
||||
useEffect(() => {
|
||||
let dms = dmsFeed.store.notes.filter(a => a.kind === EventKind.DirectMessage);
|
||||
let dms = dmsFeed.store.notes.filter(
|
||||
(a) => a.kind === EventKind.DirectMessage
|
||||
);
|
||||
dispatch(addDirectMessage(dms));
|
||||
}, [dispatch, dmsFeed.store]);
|
||||
}
|
||||
|
||||
|
||||
async function decryptBlocked(raw: TaggedRawEvent, pubKey: HexKey, privKey?: HexKey) {
|
||||
const ev = new Event(raw)
|
||||
async function decryptBlocked(
|
||||
raw: TaggedRawEvent,
|
||||
pubKey: HexKey,
|
||||
privKey?: HexKey
|
||||
) {
|
||||
const ev = new Event(raw);
|
||||
if (pubKey && privKey) {
|
||||
return await ev.DecryptData(raw.content, privKey, pubKey)
|
||||
return await ev.DecryptData(raw.content, privKey, pubKey);
|
||||
} else {
|
||||
return await barierNip07(() => window.nostr.nip04.decrypt(pubKey, raw.content));
|
||||
return await barierNip07(() =>
|
||||
window.nostr.nip04.decrypt(pubKey, raw.content)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user