Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sticker-send-pack-info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

Fix tapping a sticker silently not sending it.
27 changes: 21 additions & 6 deletions src/app/features/room/RoomInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ import {
getImagePackReferencesForMxcWrappedInMap,
} from '$utils/msc4459helper';
import { ImageUsage } from '$plugins/custom-emoji';
import { getPackImageInfo } from '$plugins/custom-emoji/utils';
import { SerializableMap } from '$types/wrapper/SerializableMap';
import { useSettingsLinkBaseUrl } from '$features/settings/useSettingsLinkBaseUrl';
import { AttachmentSheet } from '$components/attachment-sheet/AttachmentSheet';
Expand Down Expand Up @@ -1746,16 +1747,26 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
};

const handleStickerSelect = async (mxc: string, shortcode: string, label: string) => {
const stickerUrl = mxcUrlToHttp(mx, mxc, useAuthentication);
if (!stickerUrl) return;
// Packs declare their own info, so sending does not need the file. Measuring it instead made
// the send fail outright whenever the media fetch did.
let info = getPackImageInfo(mx, room, ImageUsage.Sticker, mxc);

const { blob, image } = await loadImageElementFromMediaUrl(stickerUrl);
const info = getImageInfo(image, blob);
if (!info) {
const stickerUrl = mxcUrlToHttp(mx, mxc, useAuthentication);
if (stickerUrl) {
try {
const { blob, image } = await loadImageElementFromMediaUrl(stickerUrl);
info = getImageInfo(image, blob);
} catch (error) {
log.error('failed to measure sticker, sending without info', { mxc }, error);
}
}
}

const content: StickerEventContent & ReplyEventContent & IContent & IGenericMSC4459 = {
body: label,
url: mxc,
info,
info: info ?? {},
};

// add the image pack reference
Expand Down Expand Up @@ -1784,7 +1795,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
content['m.mentions'] = { ['user_ids']: [replyDraft.userId] };
setReplyDraft(replyDraftBase);
}
mx.sendEvent(roomId, EventType.Sticker, content);
try {
await mx.sendEvent(roomId, EventType.Sticker, content);
} catch (error) {
log.error('failed to send sticker', { roomId }, error);
}
};

const handleGifSelect = async (gif: GifData, spoiler?: boolean) => {
Expand Down
24 changes: 24 additions & 0 deletions src/app/plugins/custom-emoji/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { MatrixClient, MatrixEvent, Room } from '$types/matrix-sdk';

import { getAccountData, getStateEvent, getStateEvents } from '$utils/room';

import type { IImageInfo } from '$types/matrix/common';
import type { ImageUsage } from './types';
import { ImagePack } from './ImagePack';
import type { PackMetaReader } from './PackMetaReader';
Expand Down Expand Up @@ -127,3 +128,26 @@ export function getUserImagePack(mx: MatrixClient): ImagePack | undefined {
const userImagePack = ImagePack.fromMatrixEvent(userId, packEvent);
return userImagePack;
}

/**
* The info a pack declares for one of its images: dimensions, mimetype and size, so sending a pack
* image never requires downloading it first.
*/
export function getPackImageInfo(
mx: MatrixClient,
room: Room,
usage: ImageUsage,
mxcUrl: string
): IImageInfo | undefined {
const userPack = getUserImagePack(mx);
const packs = [
...getRoomImagePacks(room),
...(userPack ? [userPack] : []),
...getGlobalImagePacks(mx),
];
for (const pack of packs) {
const info = pack.getImages(usage).find((image) => image.url === mxcUrl)?.info;
if (info) return info;
}
return undefined;
}
Loading