add Profile_Nostr_Event (#78)

This commit is contained in:
BlowaterNostr 2023-07-14 18:59:25 +08:00 committed by GitHub
parent a37ea9f72b
commit 1099d342fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 85 additions and 51 deletions

View File

@ -184,7 +184,7 @@ export class App {
this.model.social.threads = getSocialPosts(this.database, this.model.allUsersInfo);
/* my profile */
this.model.myProfile = this.model.allUsersInfo.get(accountContext.publicKey.hex)?.profile?.content;
this.model.myProfile = this.model.allUsersInfo.get(accountContext.publicKey.hex)?.profile?.profile;
/* contacts */
for (const contact of this.model.allUsersInfo.values()) {
@ -198,8 +198,8 @@ export class App {
contact.pubkey.hex,
new_DM_EditorModel({
pubkey,
name: contact.profile?.content.name,
picture: contact.profile?.content.picture,
name: contact.profile?.profile.name,
picture: contact.profile?.profile.picture,
}),
);
}
@ -438,7 +438,7 @@ export function getFocusedContent(
return;
}
if (focusedContent instanceof PublicKey) {
const profileData = allUserInfo.get(focusedContent.hex)?.profile?.content;
const profileData = allUserInfo.get(focusedContent.hex)?.profile?.profile;
return {
type: "ProfileData" as "ProfileData",
data: profileData,

View File

@ -165,7 +165,7 @@ export async function* UI_Interaction_Update(
const profile = getProfileEvent(model.app.database, pubkey);
model.dm.search.searchResults = [{
pubkey: pubkey,
profile: profile?.content,
profile: profile?.profile,
}];
} else {
const profiles = getProfilesByName(model.app.database, event.text);
@ -176,7 +176,7 @@ export async function* UI_Interaction_Update(
}
return {
pubkey: pubkey,
profile: p.content,
profile: p.profile,
};
});
}
@ -499,7 +499,7 @@ export async function* Database_Update(
const profileEvent = getProfileEvent(database, profile.pubkey);
return {
pubkey: profile.pubkey,
profile: profileEvent?.content,
profile: profileEvent?.profile,
};
});
}
@ -509,7 +509,7 @@ export async function* Database_Update(
if (newProfile == undefined) {
throw new Error("impossible");
}
model.myProfile = newProfile.content;
model.myProfile = newProfile.profile;
}
} else if (e.kind == NostrKind.DIRECT_MESSAGE) {
const pubkey = PublicKey.FromHex(e.pubkey);
@ -520,9 +520,9 @@ export async function* Database_Update(
const author = getProfileEvent(database, pubkey);
if (e.pubkey != ctx.publicKey.hex) {
notify(
author?.content.name ? author.content.name : "",
author?.profile.name ? author.profile.name : "",
"new message",
author?.content.picture ? author.content.picture : "",
author?.profile.picture ? author.profile.picture : "",
() => {
const k = PublicKey.FromHex(e.pubkey);
if (k instanceof Error) {

View File

@ -1,5 +1,5 @@
import { Database_Contextual_View } from "../database.ts";
import { ProfileEvent, ProfileFromNostrEvent, profilesStream } from "../features/profile.ts";
import { ProfileFromNostrEvent, profilesStream } from "../features/profile.ts";
import { Channel } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
import { ContactGroup } from "./contact-list.tsx";
@ -13,12 +13,12 @@ import {
ConnectionPool,
newSubID,
} from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/relay.ts";
import { CustomAppData, getTags } from "../nostr.ts";
import { CustomAppData, getTags, Profile_Nostr_Event } from "../nostr.ts";
import { assertEquals } from "https://deno.land/std@0.176.0/testing/asserts.ts";
export interface UserInfo {
pubkey: PublicKey;
profile: ProfileEvent | undefined; // todo: maybe change it to ProfileEvent
profile: Profile_Nostr_Event | undefined; // todo: maybe change it to ProfileEvent
newestEventSendByMe: NostrEvent | undefined;
newestEventReceivedByMe: NostrEvent | undefined;
pinEvent: {
@ -109,7 +109,16 @@ export function getAllUsersInformation(
case NostrKind.META_DATA:
{
const userInfo = res.get(event.pubkey);
const profileEvent = ProfileFromNostrEvent(event);
const profileEvent = ProfileFromNostrEvent({
content: event.content,
created_at: event.created_at,
id: event.id,
kind: event.kind,
parsedTags: event.parsedTags,
pubkey: event.pubkey,
sig: event.sig,
tags: event.tags,
});
if (userInfo) {
if (userInfo.profile) {
if (profileEvent.created_at > userInfo.profile?.created_at) {

View File

@ -285,13 +285,13 @@ function ConversationListItem(props: ListItemProps) {
<Fragment>
<Avatar
class={tw`w-8 h-8 mr-2`}
picture={props.userInfo.profile?.content.picture}
picture={props.userInfo.profile?.profile.picture}
/>
<div
class={tw`flex-1 overflow-hidden relative`}
>
<p class={tw`truncate w-full`}>
{props.userInfo.profile?.content.name || props.userInfo.pubkey.bech32()}
{props.userInfo.profile?.profile.name || props.userInfo.pubkey.bech32()}
</p>
{props.userInfo.newestEventReceivedByMe !== undefined
? (

View File

@ -45,8 +45,8 @@ export function convertEventsToChatMessages(
created_at: new Date(textEvents[i].created_at * 1000),
author: {
pubkey,
name: author?.profile?.content.name,
picture: author?.profile?.content.picture,
name: author?.profile?.profile.name,
picture: author?.profile?.profile.picture,
},
lamport: getTags(textEvents[i]).lamport_timestamp,
});
@ -70,8 +70,8 @@ export function convertEventsToChatMessages(
created_at: new Date(imageEvents[0].created_at * 1000),
author: {
pubkey: pubkey,
name: author?.profile?.content.name,
picture: author?.profile?.content.picture,
name: author?.profile?.profile.name,
picture: author?.profile?.profile.picture,
},
lamport: getTags(imageEvents[0]).lamport_timestamp,
});

View File

@ -50,8 +50,8 @@ export function DirectMessageContainer(props: DirectMessageContainerProps) {
const profile = getProfileEvent(props.db, currentConversation);
currentEditorModel.target.receiver = {
pubkey: currentConversation,
name: profile?.content.name,
picture: profile?.content.picture,
name: profile?.profile.name,
picture: profile?.profile.picture,
};
}
}

View File

@ -476,7 +476,7 @@ export function ParseMessageContent(
}
const profile = getProfileEvent(db, pubkey);
if (profile) {
vnode.push(ProfileCard(profile.content, pubkey, eventEmitter));
vnode.push(ProfileCard(profile.profile, pubkey, eventEmitter));
} else {
profilesSyncer.add(pubkey.hex);
}

View File

@ -1,5 +1,5 @@
import { PublicKey } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/key.ts";
import { ProfileData, ProfileEvent } from "../features/profile.ts";
import { ProfileData } from "../features/profile.ts";
export type SearchUpdate = Cancel | SearchPublicKey | SelectProfile | Start;
export type Start = {

View File

@ -15,7 +15,7 @@ import {
NostrKind,
prepareNormalNostrEvent,
} from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts";
import { Tag } from "../nostr.ts";
import { ParsedTag_Nostr_Event, PlainText_Nostr_Event, Profile_Nostr_Event, Tag } from "../nostr.ts";
// nip01 meta data
// https://github.com/nostr-protocol/nips/blob/master/05.md
@ -71,10 +71,25 @@ export async function saveProfile(
pool.sendEvent(event);
}
export function getProfileEvent(db: Database_Contextual_View, pubkey: PublicKey): ProfileEvent | undefined {
const events = Array.from(db.filterEvents((e) => {
return e.kind === NostrKind.META_DATA && e.pubkey === pubkey.hex;
}));
export function getProfileEvent(
db: Database_Contextual_View,
pubkey: PublicKey,
): Profile_Nostr_Event | undefined {
const events: ParsedTag_Nostr_Event<NostrKind.META_DATA>[] = [];
for (const e of db.events) {
if (e.kind === NostrKind.META_DATA && e.pubkey === pubkey.hex) {
events.push({
content: e.content,
kind: e.kind,
created_at: e.created_at,
id: e.id,
parsedTags: e.parsedTags,
pubkey: e.pubkey,
sig: e.sig,
tags: e.tags,
});
}
}
if (events.length == 0) {
return undefined;
}
@ -83,10 +98,22 @@ export function getProfileEvent(db: Database_Contextual_View, pubkey: PublicKey)
return ProfileFromNostrEvent(newest);
}
export function getProfilesByName(db: Database_Contextual_View, name: string): ProfileEvent[] {
const events = Array.from(db.filterEvents((e) => {
return e.kind === NostrKind.META_DATA;
}));
export function getProfilesByName(db: Database_Contextual_View, name: string): Profile_Nostr_Event[] {
const events: ParsedTag_Nostr_Event<NostrKind.META_DATA>[] = [];
for (const e of db.events) {
if (e.kind === NostrKind.META_DATA) {
events.push({
content: e.content,
kind: e.kind,
created_at: e.created_at,
id: e.id,
parsedTags: e.parsedTags,
pubkey: e.pubkey,
sig: e.sig,
tags: e.tags,
});
}
}
if (events.length == 0) {
return [];
}
@ -96,7 +123,7 @@ export function getProfilesByName(db: Database_Contextual_View, name: string): P
for (const events of profilesPerUser.values()) {
events.sort((e1, e2) => e2.created_at - e1.created_at);
const p = ProfileFromNostrEvent(events[0]);
if (p.content.name && p.content.name?.toLocaleLowerCase().indexOf(name.toLowerCase()) != -1) {
if (p.profile.name && p.profile.name?.toLocaleLowerCase().indexOf(name.toLowerCase()) != -1) {
result.push(p);
}
}
@ -106,8 +133,8 @@ export function getProfilesByName(db: Database_Contextual_View, name: string): P
export function getProfiles(
db: Database_Contextual_View,
pubkeys: Set<string>,
): Map<string, /*pubkey*/ ProfileEvent | undefined> {
const contacts: Map<string, ProfileEvent | undefined> = new Map();
): Map<string, /*pubkey*/ Profile_Nostr_Event | undefined> {
const contacts: Map<string, Profile_Nostr_Event | undefined> = new Map();
for (const key of pubkeys) {
const event = getProfileEvent(db, PublicKey.FromHex(key) as PublicKey);
contacts.set(key, event);
@ -125,17 +152,9 @@ export interface ProfileData {
[key: string]: string | undefined;
}
export interface ProfileEvent {
kind: NostrKind;
id: string;
sig: string;
created_at: number;
pubkey: string;
tags: Tag[];
content: ProfileData;
}
export function ProfileFromNostrEvent(event: NostrEvent): ProfileEvent {
export function ProfileFromNostrEvent(
event: ParsedTag_Nostr_Event<NostrKind.META_DATA>,
): Profile_Nostr_Event {
let profileData: ProfileData = {};
try {
profileData = JSON.parse(event.content);
@ -149,6 +168,8 @@ export function ProfileFromNostrEvent(event: NostrEvent): ProfileEvent {
created_at: event.created_at,
pubkey: event.pubkey,
tags: event.tags,
content: profileData,
content: event.content,
parsedTags: event.parsedTags,
profile: profileData,
};
}

View File

@ -30,8 +30,8 @@ export function getSocialPosts(
event: event,
author: {
pubkey: pubkey,
name: userInfo?.profile?.content.name,
picture: userInfo?.profile?.content.picture,
name: userInfo?.profile?.profile.name,
picture: userInfo?.profile?.profile.picture,
},
content: event.content,
created_at: new Date(event.created_at * 1000),

View File

@ -10,6 +10,7 @@ import {
prepareNormalNostrEvent,
TagPubKey,
} from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nostr.ts";
import { ProfileData } from "./features/profile.ts";
type TotolChunks = string;
type ChunkIndex = string; // 0-indexed
@ -283,8 +284,11 @@ export type Decrypted_Nostr_Event = ParsedTag_Nostr_Event<NostrKind.CustomAppDat
export type Decryptable_Nostr_Event = nostr.NostrEvent<NostrKind.CustomAppData>;
export type PlainText_Nostr_Event = ParsedTag_Nostr_Event<
Exclude<NostrKind, NostrKind.CustomAppData>
Exclude<NostrKind, NostrKind.CustomAppData> // todo: exclude DM as well
>;
export type Profile_Nostr_Event = ParsedTag_Nostr_Event<NostrKind.META_DATA> & {
profile: ProfileData;
};
export type CustomAppData = PinContact | UnpinContact | UserLogin;
export type PinContact = {