Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify sending messages a little bit #4395

Merged
merged 16 commits into from
Sep 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Button from "../Button.svelte";
import ButtonGroup from "../ButtonGroup.svelte";
import type { ChatSummary, OpenChat, UserSummary } from "openchat-client";
import type { CryptocurrencyContent } from "openchat-shared";
import type { CryptocurrencyContent, MessageContext } from "openchat-shared";
import TokenInput from "./TokenInput.svelte";
import Overlay from "../Overlay.svelte";
import AccountInfo from "./AccountInfo.svelte";
Expand All @@ -27,6 +27,7 @@
export let ledger: string;
export let chat: ChatSummary;
export let defaultReceiver: string | undefined;
export let context: MessageContext;

$: lastCryptoSent = client.lastCryptoSent;
$: cryptoBalanceStore = client.cryptoBalance;
Expand Down Expand Up @@ -91,7 +92,7 @@
createdAtNanos: BigInt(Date.now()) * BigInt(1_000_000),
},
};
dispatch("sendTransfer", [content, undefined]);
client.sendMessageWithContent(context, content);
lastCryptoSent.set(ledger);
dispatch("close");
}
Expand Down
78 changes: 25 additions & 53 deletions frontend/app/src/components/home/CurrentChat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
type EventWrapper,
type Mention,
type Message,
type MessageContent,
type OpenChat,
type FilteredProposals,
type User,
Expand All @@ -21,6 +20,7 @@
CommunityMap,
type CommunitySummary,
ICP_SYMBOL,
type AttachmentContent,
} from "openchat-client";
import PollBuilder from "./PollBuilder.svelte";
import CryptoTransferBuilder from "./CryptoTransferBuilder.svelte";
Expand All @@ -44,7 +44,7 @@
kind: "send_message";
textContent: string | undefined;
mentioned: User[];
fileToAttach: MessageContent | undefined;
attachment: AttachmentContent | undefined;
};

type ConfirmedForwardMessage = {
Expand Down Expand Up @@ -82,10 +82,11 @@
let showAcceptRulesModal = false;
let sendMessageContext: ConfirmedActionEvent | undefined = undefined;

$: messageContext = { chatId: chat.id };
$: currentChatTextContent = client.currentChatTextContent;
$: currentChatReplyingTo = client.currentChatReplyingTo;
$: currentChatPinnedMessages = client.currentChatPinnedMessages;
$: currentChatFileToAttach = client.currentChatFileToAttach;
$: currentChatAttachment = client.currentChatAttachment;
$: currentChatEditingEvent = client.currentChatEditingEvent;
$: currentChatDraftMessage = client.currentChatDraftMessage;
$: lastCryptoSent = client.lastCryptoSent;
Expand Down Expand Up @@ -168,7 +169,7 @@
};
}

function fileSelected(ev: CustomEvent<MessageContent>) {
function fileSelected(ev: CustomEvent<AttachmentContent>) {
currentChatDraftMessage.setAttachment(chat.id, ev.detail);
}

Expand Down Expand Up @@ -202,7 +203,7 @@
function send(n: number) {
if (n === ev.detail) return;

sendMessageWithAttachment(randomSentence(), [], undefined);
sendMessageWithAttachment(randomSentence(), undefined);

window.setTimeout(() => send(n + 1), 500);
}
Expand All @@ -216,9 +217,9 @@
if ($currentChatEditingEvent !== undefined) {
client
.editMessageWithAttachment(
chat.id,
messageContext,
text,
$currentChatFileToAttach,
$currentChatAttachment,
$currentChatEditingEvent
)
.then((success) => {
Expand All @@ -227,42 +228,28 @@
}
});
} else {
sendMessageWithAttachment(text, mentioned, $currentChatFileToAttach);
sendMessageWithAttachment(text, $currentChatAttachment, mentioned);
}
}

function sendMessageWithAttachment(
textContent: string | undefined,
mentioned: User[],
fileToAttach: MessageContent | undefined
attachment: AttachmentContent | undefined,
mentioned: User[] = []
) {
if (client.rulesNeedAccepting()) {
showAcceptRulesModal = true;
sendMessageContext = {
kind: "send_message",
textContent,
mentioned,
fileToAttach,
attachment,
};
} else {
client.sendMessageWithAttachment(
chat.id,
events,
textContent,
mentioned,
fileToAttach,
$currentChatReplyingTo,
undefined,
undefined,
undefined
);
client.sendMessageWithAttachment(messageContext, textContent, attachment, mentioned);
}
}

export function sendMessageWithContent(ev: CustomEvent<[MessageContent, string | undefined]>) {
sendMessageWithAttachment(ev.detail[1], [], ev.detail[0]);
}

function forwardMessage(msg: Message) {
if (!canSend || !client.canForward(msg.content)) return;

Expand All @@ -273,7 +260,7 @@
msg,
};
} else {
client.forwardMessage(chat.id, msg, undefined, undefined, undefined);
client.forwardMessage(messageContext, msg);
}
}

Expand All @@ -285,7 +272,7 @@
event: ev.detail,
};
} else {
client.retrySendMessage(chat.id, ev.detail, events, undefined, undefined, undefined);
client.retrySendMessage(messageContext, ev.detail);
}
}

Expand Down Expand Up @@ -319,34 +306,28 @@
switch (sendMessageContext.kind) {
case "send_message": {
client.sendMessageWithAttachment(
chat.id,
events,
messageContext,
sendMessageContext.textContent,
sendMessageContext.attachment,
sendMessageContext.mentioned,
sendMessageContext.fileToAttach,
$currentChatReplyingTo,
undefined,
chatRulesVersion,
communityRulesVersion
);
break;
}
case "forward_message": {
client.forwardMessage(
chat.id,
messageContext,
sendMessageContext.msg,
undefined,
chatRulesVersion,
communityRulesVersion
);
break;
}
case "retry_send_message": {
client.retrySendMessage(
chat.id,
messageContext,
sendMessageContext.event,
events,
undefined,
chatRulesVersion,
communityRulesVersion
);
Expand All @@ -357,7 +338,7 @@
switch (sendMessageContext.kind) {
case "send_message": {
currentChatDraftMessage.setTextContent(chat.id, sendMessageContext.textContent);
currentChatDraftMessage.setAttachment(chat.id, sendMessageContext.fileToAttach);
currentChatDraftMessage.setAttachment(chat.id, sendMessageContext.attachment);
break;
}
}
Expand All @@ -382,31 +363,22 @@
ownedCommunities={importToCommunities} />
{/if}

<PollBuilder
bind:this={pollBuilder}
on:sendPoll={sendMessageWithContent}
bind:open={creatingPoll} />
<PollBuilder context={messageContext} bind:this={pollBuilder} bind:open={creatingPoll} />

{#if creatingCryptoTransfer !== undefined}
<CryptoTransferBuilder
context={messageContext}
{chat}
ledger={creatingCryptoTransfer.ledger}
draftAmount={creatingCryptoTransfer.amount}
defaultReceiver={defaultCryptoTransferReceiver()}
on:sendTransfer={sendMessageWithContent}
on:upgrade
on:close={() => (creatingCryptoTransfer = undefined)} />
{/if}

<GiphySelector
bind:this={giphySelector}
bind:open={selectingGif}
on:sendGiphy={sendMessageWithContent} />
<GiphySelector context={messageContext} bind:this={giphySelector} bind:open={selectingGif} />

<MemeBuilder
bind:this={memeBuilder}
bind:open={buildingMeme}
on:sendMeme={sendMessageWithContent} />
<MemeBuilder context={messageContext} bind:this={memeBuilder} bind:open={buildingMeme} />

<div class="wrapper">
{#if showSearchHeader}
Expand Down Expand Up @@ -460,7 +432,7 @@
{#if showFooter}
<Footer
{chat}
fileToAttach={$currentChatFileToAttach}
attachment={$currentChatAttachment}
editingEvent={$currentChatEditingEvent}
replyingTo={$currentChatReplyingTo}
textContent={$currentChatTextContent}
Expand Down
15 changes: 2 additions & 13 deletions frontend/app/src/components/home/DraftMediaMessage.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<svelte:options immutable />

<script lang="ts">
import type {
CryptocurrencyContent as CryptoContentType,
VideoContent as VideoContentType,
ImageContent as ImageContentType,
AudioContent as AudioContentType,
FileContent as FileContentType,
} from "openchat-client";
import type { AttachmentContent } from "openchat-client";
import VideoContent from "./VideoContent.svelte";
import ImageContent from "./ImageContent.svelte";
import AudioContent from "./AudioContent.svelte";
import FileContent from "./FileContent.svelte";

export let content:
| VideoContentType
| ImageContentType
| AudioContentType
| FileContentType
| CryptoContentType;
export let content: AttachmentContent;
</script>

<div class="msg-preview">
Expand Down
14 changes: 6 additions & 8 deletions frontend/app/src/components/home/Footer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
EventWrapper,
Message,
MessageAction,
MessageContent,
CreatedUser,
OpenChat,
MultiUserChat,
AttachmentContent,
} from "openchat-client";
import { createEventDispatcher, getContext } from "svelte";

Expand All @@ -23,7 +23,7 @@
export let preview: boolean;
export let joining: MultiUserChat | undefined;
export let chat: ChatSummary;
export let fileToAttach: MessageContent | undefined;
export let attachment: AttachmentContent | undefined;
export let editingEvent: EventWrapper<Message> | undefined;
export let replyingTo: EnhancedReplyContext | undefined;
export let textContent: string | undefined;
Expand Down Expand Up @@ -87,15 +87,13 @@

<div class="footer">
<div class="footer-overlay">
{#if editingEvent === undefined && (replyingTo || fileToAttach !== undefined)}
{#if editingEvent === undefined && (replyingTo || attachment !== undefined)}
<div class="draft-container">
{#if replyingTo}
<ReplyingTo chatId={chat.id} readonly on:cancelReply {user} {replyingTo} />
{/if}
{#if fileToAttach !== undefined}
{#if fileToAttach.kind === "image_content" || fileToAttach.kind === "audio_content" || fileToAttach.kind === "video_content" || fileToAttach.kind === "file_content" || fileToAttach.kind === "crypto_content"}
<DraftMediaMessage content={fileToAttach} />
{/if}
{#if attachment !== undefined}
<DraftMediaMessage content={attachment} />
{/if}
</div>
{/if}
Expand All @@ -113,7 +111,7 @@
{preview}
{blocked}
{joining}
{fileToAttach}
{attachment}
{editingEvent}
{replyingTo}
{textContent}
Expand Down
13 changes: 8 additions & 5 deletions frontend/app/src/components/home/GiphySelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
import Overlay from "../Overlay.svelte";
import ModalContent from "../ModalContent.svelte";
import { _ } from "svelte-i18n";
import { createEventDispatcher } from "svelte";
import { getContext } from "svelte";
import { mobileWidth } from "../../stores/screenDimensions";
import type {
GIFObject,
PagedGIFObject,
GiphySearchResponse,
GiphyContent,
MessageContext,
OpenChat,
} from "openchat-client";

const dispatch = createEventDispatcher();
const client = getContext<OpenChat>("client");

export let open: boolean;
export let context: MessageContext;

let refreshing = false;
let message = "";
Expand Down Expand Up @@ -162,8 +165,9 @@
url: selectedGif.images.downsized.url,
mimeType: "image/gif",
},
caption: message === "" ? undefined : message,
};
dispatch("sendGiphy", [content, message === "" ? undefined : message]);
client.sendMessageWithContent(context, content);
open = false;
}
}
Expand Down Expand Up @@ -264,8 +268,7 @@
<ButtonGroup align={$mobileWidth ? "center" : "end"}>
<Button tiny disabled={selectedGif === undefined} on:click={send}
>{$_("send")}</Button>
<Button tiny secondary on:click={() => (open = false)}
>{$_("cancel")}</Button>
<Button tiny secondary on:click={() => (open = false)}>{$_("cancel")}</Button>
</ButtonGroup>
</span>
</ModalContent>
Expand Down
Loading
Loading