Skip to content

Commit

Permalink
Merge pull request #21 from ecency/feature/forward-message
Browse files Browse the repository at this point in the history
Added message forwarding between contacts and channels
  • Loading branch information
feruzm authored Mar 22, 2024
2 parents 21cc307 + 5933ec9 commit 3d4464f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/mutations/resend-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function useResendMessage(
if (currentChannel) {
return sendPublicMessage({ message: message.content });
} else if (currentContact) {
return sendDirectMessage(message.content);
return sendDirectMessage({ message: message.content });
} else {
throw new Error("[Chat][SendMessage] – no receiver");
}
Expand Down
12 changes: 9 additions & 3 deletions lib/mutations/send-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export function useSendMessage(

return useMutation(
["chats/send-message"],
async (message: string) => {
async ({
forwardedFrom,
message,
}: {
message: string;
forwardedFrom?: string;
}) => {
if (!message || message.includes("Uploading")) {
throw new Error(
"[Chat][SendMessage] – empty message or has uploading file",
Expand All @@ -52,9 +58,9 @@ export function useSendMessage(
}

if (currentChannel) {
return sendPublicMessage({ message });
return sendPublicMessage({ message, forwardedFrom });
} else if (currentContact) {
return sendDirectMessage(message);
return sendDirectMessage({ message, forwardedFrom });
} else {
throw new Error("[Chat][SendMessage] – no receiver");
}
Expand Down
66 changes: 40 additions & 26 deletions lib/nostr/mutations/send-direct-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,48 @@ export function useNostrSendDirectMessage(
);
const { mutateAsync: findHealthyRelay } = useFindHealthyRelayQuery();

return useMutation(["chats/send-direct-message"], async (message: string) => {
if (!publicKey || !privateKey || !destinationPublicKey) {
throw new Error(
"[Chat][Nostr] – attempting to send direct message with no private, destination or public key",
return useMutation(
["chats/send-direct-message"],
async ({
message,
forwardedFrom,
}: {
message: string;
forwardedFrom?: string;
}) => {
if (!publicKey || !privateKey || !destinationPublicKey) {
throw new Error(
"[Chat][Nostr] – attempting to send direct message with no private, destination or public key",
);
}

const encryptedMessage = await nip04.encrypt(
ownerPrivateKey,
destinationPublicKey,
message,
);
}
const tags = [["p", destinationPublicKey]];

const encryptedMessage = await nip04.encrypt(
ownerPrivateKey,
destinationPublicKey,
message,
);
const tags = [["p", destinationPublicKey]];
if (parent) {
const relay = await findHealthyRelay(parent);
if (relay) {
tags.push(["e", parent, relay, "root"]);
}
}

if (parent) {
const relay = await findHealthyRelay(parent);
if (relay) {
tags.push(["e", parent, relay, "root"]);
if (forwardedFrom) {
tags.push(["fwd", forwardedFrom]);
}
}
const event = await publishEncryptedMessage({
tags,
eventMetadata: encryptedMessage,
});
return convertEvent<Kind.EncryptedDirectMessage>(
event,
publicKey,
privateKey,
)!!;
});

const event = await publishEncryptedMessage({
tags,
eventMetadata: encryptedMessage,
});
return convertEvent<Kind.EncryptedDirectMessage>(
event,
publicKey,
privateKey,
)!!;
},
);
}
7 changes: 6 additions & 1 deletion lib/nostr/mutations/send-public-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Kind } from "nostr-tools";
interface Payload {
message: string;
mentions?: string[];
forwardedFrom?: string;
}

export function useNostrSendPublicMessage(channelId?: string, parent?: string) {
Expand All @@ -19,7 +20,7 @@ export function useNostrSendPublicMessage(channelId?: string, parent?: string) {

return useMutation(
["chats/send-public-message"],
async ({ message, mentions }: Payload) => {
async ({ message, mentions, forwardedFrom }: Payload) => {
const root = parent || channelId;

if (!root) {
Expand All @@ -39,6 +40,10 @@ export function useNostrSendPublicMessage(channelId?: string, parent?: string) {
mentions.forEach((m) => tags.push(["p", m]));
}

if (forwardedFrom) {
tags.push(["fwd", forwardedFrom]);
}

const event = await publishChannelMessage({
tags,
eventMetadata: message,
Expand Down
18 changes: 9 additions & 9 deletions lib/nostr/types/messages.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export type PublicMessage = {
interface BaseMessage {
id: string;
root: string;
content: string;
creator: string;
created: number;
forwardedFrom?: string;
}

export interface PublicMessage extends BaseMessage {
root: string;
children?: PublicMessage[];
mentions: string[];
sent?: number;
};
}

export type DirectMessage = {
id: string;
export interface DirectMessage extends BaseMessage {
root?: string;
content: string;
peer: string;
creator: string;
created: number;
children?: DirectMessage[];
decrypted: boolean;
sent?: number;
};
}

export type Message = PublicMessage | DirectMessage;
4 changes: 4 additions & 0 deletions lib/nostr/utils/event-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function convertEvent<KIND extends keyof EventConverterResult>(
return new Promise<Message>(async (resolve) => {
const receiver = findTagValue(event, "p")!!;
const peer = receiver === publicKey ? event.pubkey : receiver;
const forwardedFrom = event.tags.find(([t]) => t === "fwd")?.[1];
const encryptedMessage = {
id: event.id,
root: filterTagValue(event, "e").find(
Expand All @@ -54,6 +55,7 @@ export function convertEvent<KIND extends keyof EventConverterResult>(
created: event.created_at,
decrypted: false,
sent: 1,
forwardedFrom,
};

if (!privateKey) {
Expand Down Expand Up @@ -82,6 +84,7 @@ export function convertEvent<KIND extends keyof EventConverterResult>(
const mentions = filterTagValue(event, "p")
.map((mention) => mention?.[1])
.filter((mention) => !!mention);
const forwardedFrom = event.tags.find(([t]) => t === "fwd")?.[1];
if (!root) return null;
return event.content
? {
Expand All @@ -90,6 +93,7 @@ export function convertEvent<KIND extends keyof EventConverterResult>(
content: event.content,
creator: event.pubkey,
mentions,
forwardedFrom,
created: event.created_at,
sent: 1,
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ecency/ns-query",
"description": "React-query based Nostr protocol SDK for Ecency vision and mobile",
"version": "1.1.7",
"version": "1.1.8",
"repository": "https://github.com/ecency/ns-query",
"author": "ildar.timerbaev <dkildar@gmail.com>",
"license": "MIT",
Expand Down

0 comments on commit 3d4464f

Please sign in to comment.