fix DM_List.addEvents (#361)

This commit is contained in:
BlowaterNostr 2023-12-22 20:20:34 +08:00 committed by GitHub
parent a7d8f7c5e4
commit 46eb9f3eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 87 deletions

View File

@ -27,7 +27,7 @@ import { Channel } from "https://raw.githubusercontent.com/BlowaterNostr/csp/mas
import { group_GM_events, GroupChatSyncer, GroupMessageController } from "../features/gm.ts";
import { OtherConfig } from "./config-other.ts";
import { ProfileGetter } from "./search.tsx";
import { DirectedMessageController } from "../features/dm.ts";
import { DirectedMessageController, InvalidEvent } from "../features/dm.ts";
import { ConnectionPool } from "../lib/nostr-ts/relay-pool.ts";
import { LamportTime } from "../time.ts";
@ -161,8 +161,13 @@ export class App {
profileSyncer.add(args.ctx.publicKey.hex);
// init conversation list
const all_events = Array.from(args.database.getAllEvents());
const conversationLists = new DM_List(args.ctx);
conversationLists.addEvents(Array.from(args.database.getAllEvents()));
const err = conversationLists.addEvents(all_events);
if (err instanceof InvalidEvent) {
console.error(err);
await args.database.remove(err.event.id);
}
const dmController = new DirectedMessageController(args.ctx);
const groupSyncer = new GroupChatSyncer(args.database, args.pool);
@ -174,7 +179,7 @@ export class App {
(async () => {
// load DMs
for (const e of args.database.getAllEvents()) {
for (const e of all_events) {
if (e.kind == NostrKind.DIRECT_MESSAGE) {
const error = await dmController.addEvent({
...e,

View File

@ -482,7 +482,6 @@ export async function* Database_Update(
}
profileSyncer.add(...changes_events.map((e) => e.pubkey));
// @ts-ignore
convoLists.addEvents(changes_events);
for (let e of changes_events) {
const t = getTags(e).lamport_timestamp;

View File

@ -2,6 +2,7 @@ import { ConversationListRetriever, NewMessageChecker } from "./conversation-lis
import { PublicKey } from "../lib/nostr-ts/key.ts";
import { NostrAccountContext, NostrEvent, NostrKind } from "../lib/nostr-ts/nostr.ts";
import { getTags, Parsed_Event } from "../nostr.ts";
import { InvalidEvent } from "../features/dm.ts";
export interface ConversationSummary {
pubkey: PublicKey;
@ -11,14 +12,15 @@ export interface ConversationSummary {
export class DM_List implements ConversationListRetriever, NewMessageChecker {
readonly convoSummaries = new Map<string, ConversationSummary>();
readonly newMessages = new Map<string, number>();
constructor(
public readonly ctx: NostrAccountContext,
) {}
has(hex: string, isGourpChat: boolean): boolean {
// todo: implement NewMessageChecker
return false;
has(hex: string, isGourpChat: boolean): number {
// return this.newMessages.get(hex) || 0;
return 0;
}
*getStrangers() {
@ -69,28 +71,31 @@ export class DM_List implements ConversationListRetriever, NewMessageChecker {
addEvents(
events: NostrEvent[],
) {
// const t = Date.now();
let i = 0;
for (const event of events) {
switch (event.kind) {
case NostrKind.DIRECT_MESSAGE:
{
if (event.kind != NostrKind.DIRECT_MESSAGE) {
continue;
}
let whoAm_I_TalkingTo = "";
if (event.pubkey == this.ctx.publicKey.hex) {
// I am the sender
whoAm_I_TalkingTo = getTags(event).p[0];
if (whoAm_I_TalkingTo == undefined) {
return new Error(`event ${event.id} does not have p tags`);
return new InvalidEvent(event, `event ${event.id} does not have p tags`);
}
} else if (getTags(event).p[0] == this.ctx.publicKey.hex) {
// I am the receiver
whoAm_I_TalkingTo = event.pubkey;
} else {
// I am neither. Possible because other user has used this device before
break;
continue;
}
this.newMessages.set(whoAm_I_TalkingTo, this.has(whoAm_I_TalkingTo, false) + 1);
const userInfo = this.convoSummaries.get(whoAm_I_TalkingTo);
if (userInfo) {
// userInfo.events.push(event);
if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) {
// talking to myself
if (userInfo.newestEventSendByMe) {
@ -126,7 +131,7 @@ export class DM_List implements ConversationListRetriever, NewMessageChecker {
} else {
const pubkey = PublicKey.FromHex(whoAm_I_TalkingTo);
if (pubkey instanceof Error) {
return pubkey;
return new InvalidEvent(event, pubkey.message);
}
const newUserInfo: ConversationSummary = {
pubkey,
@ -149,9 +154,6 @@ export class DM_List implements ConversationListRetriever, NewMessageChecker {
this.convoSummaries.set(whoAm_I_TalkingTo, newUserInfo);
}
}
break;
}
}
}
}

View File

@ -39,7 +39,7 @@ export type ContactUpdate =
| StartCreateGroupChat;
export interface NewMessageChecker {
has(hex: string, isGourpChat: boolean): boolean;
has(hex: string, isGourpChat: boolean): number;
}
type Props = {
@ -86,6 +86,7 @@ export class ConversationList extends Component<Props, State> {
const contacts = Array.from(props.convoListRetriever.getContacts());
const strangers = Array.from(props.convoListRetriever.getStrangers());
const groups = Array.from(props.groupChatListGetter.getConversationList());
let isGroupChat = false;
switch (this.state.selectedContactGroup) {
case "Contacts":
listToRender = contacts;
@ -95,13 +96,14 @@ export class ConversationList extends Component<Props, State> {
break;
case "Group":
listToRender = groups;
isGroupChat = true;
break;
}
const convoListToRender = [];
for (const conversationSummary of listToRender) {
convoListToRender.push({
conversation: conversationSummary,
newMessageCount: 0,
newMessageCount: props.hasNewMessages.has(conversationSummary.pubkey.hex, isGroupChat),
});
}
@ -390,7 +392,7 @@ function ConversationListItem(props: ListItemProps) {
{props.newMessageCount > 0
? (
<span
class={tw`absolute top-0 right-0 px-1 text-[${PrimaryTextColor}] text-xs rounded-full bg-[${ErrorColor}]`}
class={tw`absolute bottom-0 right-0 px-1 text-[${PrimaryTextColor}] text-xs rounded-full bg-[${ErrorColor}]`}
>
{props.newMessageCount}
</span>

View File

@ -315,8 +315,8 @@ async function parseDM(
}
export class InvalidEvent extends Error {
constructor(kind: NostrKind, message: string) {
super(`invliad event, expecting kind:${kind}, ${message}`);
constructor(public readonly event: NostrEvent, message: string) {
super(`invliad event, expecting kind:${event.kind}, ${message}`);
this.name = "InvalidEvent";
}
}
@ -339,7 +339,7 @@ export function whoIamTalkingTo(event: NostrEvent<NostrKind.DIRECT_MESSAGE>, myP
return {
type: "Other",
error: new InvalidEvent(
NostrKind.DIRECT_MESSAGE,
event,
`No p tag is found - Not a valid DM - id ${event.id}, kind ${event.kind}`,
),
};