Skip to content

Commit 48824e3

Browse files
authored
Merge pull request #107 from dozro/feat/steal-sticker-alt
add button to add a sticker you see in a message to your own user sticker pack
2 parents 4d1213b + 6f56e4f commit 48824e3

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
sable: minor
3+
---
4+
5+
add button to save a sticker you see in the message timeline to your personal account sticker pack

src/app/features/room/message/Message.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ import { MessageDeleteItem } from '$components/message/modals/MessageDelete';
8181
import { MessageReportItem } from '$components/message/modals/MessageReport';
8282
import { filterPronounsByLanguage } from '$utils/pronouns';
8383
import { useMentionClickHandler } from '$hooks/useMentionClickHandler';
84+
import {
85+
addStickerToDefaultPack,
86+
doesStickerExistInDefaultPack,
87+
} from '$utils/addStickerToDefaultStickerPack';
8488
import { MessageEditor } from './MessageEditor';
8589
import * as css from './styles.css';
8690

@@ -669,6 +673,7 @@ function MessageInternal(
669673
});
670674

671675
const isThreadedMessage = mEvent.threadRootId !== undefined;
676+
const isStickerMessage = mEvent.getType() === 'm.sticker';
672677

673678
const evtId = mEvent.getId()!;
674679
const evtTimeline = room.getTimelineForEvent(evtId);
@@ -820,6 +825,33 @@ function MessageInternal(
820825
</Text>
821826
</MenuItem>
822827
)}
828+
{isStickerMessage &&
829+
!doesStickerExistInDefaultPack(mx, mEvent.getContent().url) && (
830+
<MenuItem
831+
size="300"
832+
after={<Icon size="100" src={Icons.Star} />}
833+
radii="300"
834+
onClick={() => {
835+
addStickerToDefaultPack(
836+
mx,
837+
`sticker-${mEvent.getId()}`,
838+
mEvent.getContent().url,
839+
mEvent.getContent().body,
840+
mEvent.getContent().info
841+
);
842+
closeMenu();
843+
}}
844+
>
845+
<Text
846+
className={css.MessageMenuItemText}
847+
as="span"
848+
size="T300"
849+
truncate
850+
>
851+
Add to User Sticker Pack
852+
</Text>
853+
</MenuItem>
854+
)}
823855
{relations && <MessageAllReactionItem room={room} relations={relations} />}
824856
<MenuItem
825857
size="300"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { PackContent, ImageUsage } from '$plugins/custom-emoji';
2+
import { AccountDataEvent } from '$types/matrix/accountData';
3+
import { IImageInfo } from '$types/matrix/common';
4+
import { MatrixClient } from 'matrix-js-sdk';
5+
6+
// Utility function to add a sticker to the default sticker pack
7+
export async function addStickerToDefaultPack(
8+
mx: MatrixClient,
9+
shortcode: string,
10+
mxc: string,
11+
body?: string,
12+
info?: IImageInfo
13+
) {
14+
// current content of the default sticker pack, which is stored in account data under the key 'PoniesUserEmotes'
15+
const current =
16+
mx.getAccountData(AccountDataEvent.PoniesUserEmotes)?.getContent<PackContent>() ?? {};
17+
18+
// modified content with the new sticker added.
19+
// We add the new sticker under the "images" key, using the shortcode as the key for the sticker.
20+
// The sticker content includes the mxc URL, body, info, and usage (which we set to "sticker").
21+
const next: PackContent = {
22+
...current,
23+
images: {
24+
...(current.images ?? {}),
25+
[shortcode]: {
26+
...(current.images?.[shortcode] ?? {}),
27+
url: mxc,
28+
body,
29+
info,
30+
usage: [ImageUsage.Sticker],
31+
},
32+
},
33+
};
34+
35+
// update the account data with the modified content, which effectively adds the new sticker to the default sticker pack.
36+
await mx.setAccountData(AccountDataEvent.PoniesUserEmotes, next);
37+
}
38+
39+
// check if a sticker exists in the account sticker pack
40+
export function doesStickerExistInDefaultPack(mx: MatrixClient, mxc: string) {
41+
const imgs = mx
42+
.getAccountData(AccountDataEvent.PoniesUserEmotes)
43+
?.getContent<PackContent>().images;
44+
if (imgs === undefined) return false;
45+
return Object.values(imgs).some((image) => image.url === mxc) ?? false;
46+
}

0 commit comments

Comments
 (0)