blowater/UI/conversation-list.ts

186 lines
7.7 KiB
TypeScript
Raw Normal View History

2023-10-07 20:40:18 +00:00
import { ConversationListRetriever, GroupChatListGetter, NewMessageChecker } from "./conversation-list.tsx";
import { PublicKey } from "../lib/nostr-ts/key.ts";
2023-08-28 17:58:05 +00:00
import { NostrAccountContext, NostrEvent, NostrKind } from "../lib/nostr-ts/nostr.ts";
import { getTags, Parsed_Event, Profile_Nostr_Event } from "../nostr.ts";
2023-10-03 15:15:45 +00:00
import { ProfileSyncer } from "../features/profile.ts";
2023-10-04 15:07:40 +00:00
import { GroupChatCreation } from "../group-chat.ts";
2023-06-30 14:05:57 +00:00
2023-09-23 21:54:13 +00:00
export interface ConversationSummary {
2023-06-30 14:05:57 +00:00
pubkey: PublicKey;
newestEventSendByMe: NostrEvent | undefined;
newestEventReceivedByMe: NostrEvent | undefined;
}
2023-10-07 20:40:18 +00:00
export class ConversationLists implements ConversationListRetriever, GroupChatListGetter, NewMessageChecker {
readonly convoSummaries = new Map<string, ConversationSummary>();
readonly groupChatSummaries = new Map<string, ConversationSummary>();
// private readonly profile = new Map<string, Profile_Nostr_Event>();
2023-09-23 21:54:13 +00:00
constructor(
public readonly ctx: NostrAccountContext,
2023-10-03 15:15:45 +00:00
private readonly profileSyncer: ProfileSyncer,
2023-09-23 21:54:13 +00:00
) {}
2023-10-07 20:40:18 +00:00
has(hex: string, isGourpChat: boolean): boolean {
// todo: implement NewMessageChecker
return false;
}
*getStrangers() {
2023-09-27 20:04:01 +00:00
for (const convoSummary of this.convoSummaries.values()) {
if (
2023-09-27 20:04:01 +00:00
(
convoSummary.newestEventReceivedByMe == undefined ||
convoSummary.newestEventSendByMe == undefined
) &&
!(
convoSummary.newestEventReceivedByMe == undefined &&
convoSummary.newestEventSendByMe == undefined
)
) {
2023-09-27 20:04:01 +00:00
yield convoSummary;
}
}
}
*getContacts() {
for (const userInfo of this.convoSummaries.values()) {
if (
userInfo.newestEventReceivedByMe != undefined &&
userInfo.newestEventSendByMe != undefined
) {
yield userInfo;
}
}
}
*getGroupChat() {
for (const value of this.groupChatSummaries.values()) {
yield value;
}
}
2023-10-04 21:34:43 +00:00
getConversationType(pubkey: PublicKey, isGroupChat: boolean) {
if (isGroupChat) {
return "Group";
}
const contact = this.convoSummaries.get(pubkey.hex);
if (contact == undefined) {
return "Strangers";
}
if (
contact.newestEventReceivedByMe == undefined || contact.newestEventSendByMe == undefined
) {
return "Strangers";
} else {
return "Contacts";
}
}
2023-10-04 15:07:40 +00:00
addGroupCreation(groupChatCreation: GroupChatCreation) {
const publicKey = groupChatCreation.groupKey.publicKey;
2023-10-04 15:07:40 +00:00
this.groupChatSummaries.set(publicKey.hex, {
pubkey: publicKey,
newestEventReceivedByMe: undefined,
newestEventSendByMe: undefined,
});
this.profileSyncer.add(publicKey.hex);
}
addEvents(
events: Parsed_Event[],
2023-10-03 15:15:45 +00:00
) {
// const t = Date.now();
for (const event of events) {
2023-06-30 14:05:57 +00:00
switch (event.kind) {
case NostrKind.DIRECT_MESSAGE:
{
let whoAm_I_TalkingTo = "";
if (event.pubkey == this.ctx.publicKey.hex) {
2023-06-30 14:05:57 +00:00
// I am the sender
whoAm_I_TalkingTo = getTags(event).p[0];
} else if (getTags(event).p[0] == this.ctx.publicKey.hex) {
2023-06-30 14:05:57 +00:00
// I am the receiver
whoAm_I_TalkingTo = event.pubkey;
} else {
// I am neither. Possible because other user has used this device before
break;
}
const userInfo = this.convoSummaries.get(whoAm_I_TalkingTo);
2023-06-30 14:05:57 +00:00
if (userInfo) {
// userInfo.events.push(event);
if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) {
2023-06-30 14:05:57 +00:00
// talking to myself
if (userInfo.newestEventSendByMe) {
if (event.created_at > userInfo.newestEventSendByMe?.created_at) {
userInfo.newestEventSendByMe = event;
userInfo.newestEventReceivedByMe = event;
}
} else {
userInfo.newestEventSendByMe = event;
userInfo.newestEventReceivedByMe = event;
}
} else {
if (this.ctx.publicKey.hex == event.pubkey) {
2023-06-30 14:05:57 +00:00
// I am the sender
if (userInfo.newestEventSendByMe) {
if (event.created_at > userInfo.newestEventSendByMe.created_at) {
userInfo.newestEventSendByMe = event;
}
} else {
userInfo.newestEventSendByMe = event;
}
} else {
// I am the receiver
if (userInfo.newestEventReceivedByMe) {
if (event.created_at > userInfo.newestEventReceivedByMe.created_at) {
userInfo.newestEventReceivedByMe = event;
}
} else {
userInfo.newestEventReceivedByMe = event;
}
}
}
} else {
2023-09-23 21:54:13 +00:00
const newUserInfo: ConversationSummary = {
2023-06-30 14:05:57 +00:00
pubkey: PublicKey.FromHex(whoAm_I_TalkingTo) as PublicKey,
newestEventReceivedByMe: undefined,
newestEventSendByMe: undefined,
};
if (whoAm_I_TalkingTo == this.ctx.publicKey.hex) {
2023-06-30 14:05:57 +00:00
// talking to myself
newUserInfo.newestEventSendByMe = event;
newUserInfo.newestEventReceivedByMe = event;
} else {
if (this.ctx.publicKey.hex == event.pubkey) {
2023-06-30 14:05:57 +00:00
// I am the sender
newUserInfo.newestEventSendByMe = event;
} else {
// I am the receiver
newUserInfo.newestEventReceivedByMe = event;
}
}
this.convoSummaries.set(whoAm_I_TalkingTo, newUserInfo);
2023-06-30 14:05:57 +00:00
}
}
break;
}
}
}
}
2023-09-23 21:54:13 +00:00
export const sortUserInfo = (a: ConversationSummary, b: ConversationSummary) => {
2023-06-30 14:05:57 +00:00
return sortScore(b) - sortScore(a);
};
2023-09-23 21:54:13 +00:00
function sortScore(contact: ConversationSummary) {
2023-06-30 14:05:57 +00:00
let score = 0;
if (contact.newestEventSendByMe !== undefined) {
score += contact.newestEventSendByMe.created_at;
}
if (contact.newestEventReceivedByMe !== undefined) {
score += contact.newestEventReceivedByMe.created_at;
}
return score;
}