blowater/features/dm.ts

256 lines
7.5 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
import * as csp from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
2023-07-11 09:49:58 +00:00
import { Database_Contextual_View, NotFound, whoIamTalkingTo } from "../database.ts";
2023-06-30 14:05:57 +00:00
import {
DecryptionFailure,
decryptNostrEvent,
NostrAccountContext,
NostrEvent,
NostrKind,
RelayResponse_Event,
2023-08-28 17:58:05 +00:00
} from "../lib/nostr-ts/nostr.ts";
import { ConnectionPool } from "../lib/nostr-ts/relay.ts";
2023-09-11 20:40:56 +00:00
import { prepareNostrImageEvent, Tag } from "../nostr.ts";
2023-08-28 17:58:05 +00:00
import { PublicKey } from "../lib/nostr-ts/key.ts";
import { prepareEncryptedNostrEvent, prepareNormalNostrEvent } from "../lib/nostr-ts/event.ts";
2023-06-30 14:05:57 +00:00
export async function sendDMandImages(args: {
sender: NostrAccountContext;
2023-07-11 09:19:57 +00:00
receiverPublicKey: PublicKey;
2023-06-30 14:05:57 +00:00
message: string;
files: Blob[];
kind: NostrKind;
lamport_timestamp: number;
pool: ConnectionPool;
tags: Tag[];
}) {
const { tags, sender, receiverPublicKey, message, files, kind, lamport_timestamp, pool } = args;
2023-06-30 14:05:57 +00:00
console.log("sendDMandImages", message, files);
const eventsToSend: NostrEvent[] = [];
if (message.trim().length !== 0) {
// build the nostr event
const nostrEvent = await prepareEncryptedNostrEvent(
sender,
2023-09-19 19:38:38 +00:00
{
encryptKey: receiverPublicKey,
kind,
tags: [
["p", receiverPublicKey.hex],
["lamport", String(lamport_timestamp)],
...tags,
],
content: message,
},
2023-06-30 14:05:57 +00:00
);
if (nostrEvent instanceof Error) {
return nostrEvent;
}
eventsToSend.push(nostrEvent);
}
for (let blob of files) {
2023-09-11 20:40:56 +00:00
const imgEvent = await prepareNostrImageEvent(
2023-06-30 14:05:57 +00:00
sender,
receiverPublicKey,
blob,
kind,
tags,
);
if (imgEvent instanceof Error) {
return imgEvent;
}
2023-09-11 20:40:56 +00:00
let [fileEvent, _] = imgEvent;
// for (const event of fileEvents) {
eventsToSend.push(fileEvent);
// }
2023-06-30 14:05:57 +00:00
}
// send the event
for (const event of eventsToSend) {
const err = await pool.sendEvent(event);
if (err instanceof Error) {
return err;
}
2023-06-30 14:05:57 +00:00
}
return eventsToSend;
2023-06-30 14:05:57 +00:00
}
export async function sendSocialPost(args: {
sender: NostrAccountContext;
message: string;
lamport_timestamp: number;
pool: ConnectionPool;
tags: Tag[];
}) {
const { sender, message, lamport_timestamp, pool, tags } = args;
console.log("sendSocialPost", message);
const event = await prepareNormalNostrEvent(sender, NostrKind.TEXT_NOTE, [
["lamport", String(lamport_timestamp)],
...tags,
], message);
const err = await pool.sendEvent(event);
if (err instanceof Error) {
return err;
}
return event;
2023-06-30 14:05:57 +00:00
}
export function getAllEncryptedMessagesOf(
2023-07-11 09:19:57 +00:00
publicKey: PublicKey,
2023-06-30 14:05:57 +00:00
relay: ConnectionPool,
) {
const stream1 = getAllEncryptedMessagesSendBy(
publicKey,
relay,
);
const stream2 = getAllEncryptedMessagesReceivedBy(
publicKey,
relay,
);
return merge(stream1, stream2);
}
async function* getAllEncryptedMessagesSendBy(
2023-07-11 09:19:57 +00:00
publicKey: PublicKey,
2023-06-30 14:05:57 +00:00
relay: ConnectionPool,
) {
let resp = await relay.newSub(
2023-08-03 09:13:16 +00:00
`getAllEncryptedMessagesSendBy`,
2023-06-30 14:05:57 +00:00
{
2023-07-11 09:19:57 +00:00
authors: [publicKey.hex],
2023-06-30 14:05:57 +00:00
kinds: [4],
},
);
if (resp instanceof Error) {
throw resp;
}
2023-08-28 17:58:05 +00:00
for await (const nostrMessage of resp.chan) {
2023-06-30 14:05:57 +00:00
yield nostrMessage;
}
}
async function* getAllEncryptedMessagesReceivedBy(
2023-07-11 09:19:57 +00:00
publicKey: PublicKey,
2023-06-30 14:05:57 +00:00
relay: ConnectionPool,
) {
let resp = await relay.newSub(
2023-08-03 09:13:16 +00:00
`getAllEncryptedMessagesReceivedBy`,
2023-06-30 14:05:57 +00:00
{
kinds: [4],
2023-07-11 09:19:57 +00:00
"#p": [publicKey.hex],
2023-06-30 14:05:57 +00:00
},
);
if (resp instanceof Error) {
throw resp;
}
2023-08-28 17:58:05 +00:00
for await (const nostrMessage of resp.chan) {
2023-06-30 14:05:57 +00:00
yield nostrMessage;
}
}
async function decryptMessage(
relayResponse: RelayResponse_Event,
ctx: NostrAccountContext,
publicKey: string,
): Promise<NostrEvent | DecryptionFailure> {
return decryptNostrEvent(relayResponse.event, ctx, publicKey);
}
function merge<T>(...iters: AsyncIterable<T>[]) {
let merged = csp.chan<T>();
async function coroutine<T>(
source: AsyncIterable<T>,
destination: csp.Channel<T>,
) {
for await (let ele of source) {
if (destination.closed()) {
return;
}
let err = await destination.put(ele);
if (err instanceof csp.PutToClosedChannelError) {
// this means the merged channel was not closed when
// line 319 is called,
// but during waiting time of line 319, no consumer pops it and it was closed.
// This is normal semantics of channels
// so that it's fine to not throw it up to the call stack
// but then this ele has already been popped from the iter,
// it will be lost.
throw new Error("destination channel should not be closed");
}
}
}
for (let iter of iters) {
coroutine(iter, merged);
}
return merged;
}
//////////////////
// Read from DB //
//////////////////
// get the messages send by and received by pubkey
export function getDirectMessageEventsOf(db: Database_Contextual_View, pubkey: PublicKey) {
2023-06-30 14:05:57 +00:00
return db.filterEvents(filterDMof(pubkey));
}
export function getContactPubkeysOf(db: Database_Contextual_View, pubkey: PublicKey): Set<string> | Error {
2023-06-30 14:05:57 +00:00
const msgs = getDirectMessageEventsOf(db, pubkey);
const contactList = new Set<string>();
for (const event of msgs) {
const whoIAmTalkingTo = whoIamTalkingTo(event, pubkey);
if (whoIAmTalkingTo instanceof Error) {
return whoIAmTalkingTo;
}
contactList.add(whoIAmTalkingTo);
}
return contactList;
}
export function getNewestEventOf(
db: Database_Contextual_View,
pubkey: PublicKey,
): NostrEvent | typeof NotFound {
2023-06-30 14:05:57 +00:00
const events = Array.from(getDirectMessageEventsOf(db, pubkey));
if (events.length === 0) {
return NotFound;
}
let newest = events[0];
for (let event of events.slice(1)) {
if (event.created_at > newest.created_at) {
newest = event;
}
}
return newest;
}
2023-07-11 09:49:58 +00:00
export function get_Kind4_Events_Between(
db: Database_Contextual_View,
myPubKey: string,
contactPubkey: string,
) {
2023-06-30 14:05:57 +00:00
const events = db.filterEvents(
filterDMBetween(myPubKey, contactPubkey),
);
return events;
}
function filterDMof(pubkey: PublicKey) {
2023-06-30 14:05:57 +00:00
return (e: NostrEvent) => {
const isAuthor = e.pubkey === pubkey.hex;
const isReceiver = e.tags.filter((t) => t[0] === "p" && t[1] === pubkey.hex).length === 1;
2023-06-30 14:05:57 +00:00
const isDM = e.kind === NostrKind.DIRECT_MESSAGE;
return isDM && (isAuthor || isReceiver);
};
}
function filterDMBetween(myPubKey: string, contactPubKey: string) {
return (e: NostrEvent) => {
const isDM = e.kind === NostrKind.DIRECT_MESSAGE;
const iAmAuthor = e.pubkey === myPubKey;
const otherIsReceiver = e.tags.filter((t) => t[0] === "p" && t[1] === contactPubKey)
.length > 0;
const otherIsAuthor = e.pubkey === contactPubKey;
const iAmReceiver = e.tags.filter((t) => t[0] === "p" && t[1] === myPubKey).length > 0;
return isDM &&
((iAmAuthor && otherIsReceiver) || (otherIsAuthor && iAmReceiver));
};
}