move room emoji and stickers to new sdk

This commit is contained in:
Ajay Bura 2026-07-24 14:00:03 +05:30
commit 3a94fe7bf4
7 changed files with 105 additions and 67 deletions

View file

@ -2,17 +2,16 @@ import React from 'react';
import { Box, IconButton, Text, Icon, Icons, Scroll, Chip } from 'folds';
import { PackAddress } from '../../plugins/custom-emoji';
import { Page, PageHeader, PageContent } from '../page';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { RoomImagePack } from './RoomImagePack';
import { UserImagePack } from './UserImagePack';
import { useClient } from '../../hooks/useClient';
type ImagePackViewProps = {
address: PackAddress | undefined;
requestClose: () => void;
};
export function ImagePackView({ address, requestClose }: ImagePackViewProps) {
const mx = useMatrixClient();
const room = address && mx.getRoom(address.roomId);
const ct = useClient();
return (
<Page>
@ -38,8 +37,8 @@ export function ImagePackView({ address, requestClose }: ImagePackViewProps) {
<Box grow="Yes">
<Scroll hideTrack visibility="Hover">
<PageContent>
{room && address ? (
<RoomImagePack room={room} stateKey={address.stateKey} />
{address && ct.rooms.join.read.includes(address.roomId) ? (
<RoomImagePack roomId={address.roomId} stateKey={address.stateKey} />
) : (
<UserImagePack />
)}

View file

@ -1,14 +1,14 @@
import React, { FormEventHandler, ReactNode, useMemo, useState } from 'react';
import { Badge, Box, Button, Chip, Icon, Icons, Input, Text } from 'folds';
import { UsageSwitcher, useUsageStr } from './UsageSwitcher';
import { mxcUrlToHttp } from '../../utils/matrix';
import * as css from './style.css';
import { ImageUsage, imageUsageEqual, PackImageReader } from '../../plugins/custom-emoji';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { SettingTile } from '../setting-tile';
import { useObjectURL } from '../../hooks/useObjectURL';
import { createUploadAtom, UploadAtom } from '../../state/upload';
import { replaceSpaceWithDash } from '../../utils/common';
import { useClient } from '../../hooks/useClient';
import { mxcToHttp } from '../../../client/mxcToHttp';
type ImageTileProps = {
defaultShortcode: string;
@ -30,7 +30,7 @@ export function ImageTile({
onDeleteToggle,
deleted,
}: ImageTileProps) {
const mx = useMatrixClient();
const ct = useClient();
const getUsageStr = useUsageStr();
return (
@ -38,7 +38,7 @@ export function ImageTile({
before={
<img
className={css.ImagePackImage}
src={mxcUrlToHttp(mx, image.url, useAuthentication) ?? ''}
src={mxcToHttp(ct.session.baseUrl, image.url, useAuthentication).url() ?? ''}
alt={image.shortcode}
loading="lazy"
/>
@ -120,7 +120,7 @@ export function ImageTileEdit({
onCancel,
onSave,
}: ImageTileEditProps) {
const mx = useMatrixClient();
const ct = useClient();
const defaultUsage = image.usage ?? packUsage;
const [unsavedUsage, setUnsavedUsages] = useState(defaultUsage);
@ -162,7 +162,7 @@ export function ImageTileEdit({
before={
<img
className={css.ImagePackImage}
src={mxcUrlToHttp(mx, image.url, useAuthentication) ?? ''}
src={mxcToHttp(ct.session.baseUrl, image.url, useAuthentication).url() ?? ''}
alt={image.shortcode}
loading="lazy"
/>

View file

@ -13,8 +13,6 @@ import {
Chip,
} from 'folds';
import Linkify from 'linkify-react';
import { mxcUrlToHttp } from '../../utils/matrix';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { nameInitials } from '../../utils/common';
import { BreakWord } from '../../styles/Text.css';
import { LINKIFY_OPTS } from '../../plugins/react-custom-html-parser';
@ -25,6 +23,8 @@ import { createUploadAtom, UploadSuccess } from '../../state/upload';
import { CompactUploadCardRenderer } from '../upload-card';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { PackMetaReader } from '../../plugins/custom-emoji';
import { useClient } from '../../hooks/useClient';
import { mxcToHttp } from '../../../client/mxcToHttp';
type ImagePackAvatarProps = {
url?: string;
@ -50,10 +50,10 @@ type ImagePackProfileProps = {
onEdit?: () => void;
};
export function ImagePackProfile({ meta, canEdit, onEdit }: ImagePackProfileProps) {
const mx = useMatrixClient();
const ct = useClient();
const useAuthentication = useMediaAuthentication();
const avatarUrl = meta.avatar
? mxcUrlToHttp(mx, meta.avatar, useAuthentication) ?? undefined
? mxcToHttp(ct.session.baseUrl, meta.avatar, useAuthentication).url()
: undefined;
return (
@ -97,11 +97,13 @@ type ImagePackProfileEditProps = {
onSave: (meta: PackMetaReader) => void;
};
export function ImagePackProfileEdit({ meta, onCancel, onSave }: ImagePackProfileEditProps) {
const mx = useMatrixClient();
const ct = useClient();
const useAuthentication = useMediaAuthentication();
const [avatar, setAvatar] = useState(meta.avatar);
const avatarUrl = avatar ? mxcUrlToHttp(mx, avatar, useAuthentication) ?? undefined : undefined;
const avatarUrl = avatar
? mxcToHttp(ct.session.baseUrl, avatar, useAuthentication).url()
: undefined;
const [imageFile, setImageFile] = useState<File>();
const avatarFileUrl = useObjectURL(imageFile);

View file

@ -1,28 +1,28 @@
import React, { useCallback, useMemo } from 'react';
import { Room } from 'matrix-js-sdk';
import { usePowerLevels } from '../../hooks/usePowerLevels';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { putStateEvent } from 'neurone/lib/api/matrix/client/v3/rooms';
import { usePowerLevelsContext } from '../../hooks/usePowerLevels';
import { ImagePackContent } from './ImagePackContent';
import { ImagePack, PackContent } from '../../plugins/custom-emoji';
import { StateEvent } from '../../../types/matrix/room';
import { StateEventType } from '../../../types/matrix/room';
import { useRoomImagePack } from '../../hooks/useImagePacks';
import { randomStr } from '../../utils/common';
import { useRoomPermissions } from '../../hooks/useRoomPermissions';
import { useRoomCreators } from '../../hooks/useRoomCreators';
import { useClient } from '../../hooks/useClient';
type RoomImagePackProps = {
room: Room;
roomId: string;
stateKey: string;
};
export function RoomImagePack({ room, stateKey }: RoomImagePackProps) {
const mx = useMatrixClient();
const userId = mx.getUserId()!;
const powerLevels = usePowerLevels(room);
const creators = useRoomCreators(room);
export function RoomImagePack({ roomId, stateKey }: RoomImagePackProps) {
const ct = useClient();
const { userId } = ct.session;
const powerLevels = usePowerLevelsContext();
const creators = useRoomCreators(roomId);
const permissions = useRoomPermissions(creators, powerLevels);
const canEditImagePack = permissions.stateEvent(StateEvent.PoniesRoomEmotes, userId);
const canEditImagePack = permissions.stateEvent(StateEventType.PoniesRoomEmotes, userId);
const fallbackPack = useMemo(() => {
const fakePackId = randomStr(4);
@ -30,26 +30,31 @@ export function RoomImagePack({ room, stateKey }: RoomImagePackProps) {
fakePackId,
{},
{
roomId: room.roomId,
roomId,
stateKey,
}
);
}, [room.roomId, stateKey]);
const imagePack = useRoomImagePack(room, stateKey) ?? fallbackPack;
}, [roomId, stateKey]);
const imagePackResult = useRoomImagePack(roomId, stateKey);
const imagePack = imagePackResult.value ?? fallbackPack;
const handleUpdate = useCallback(
async (packContent: PackContent) => {
const { address } = imagePack;
if (!address) return;
await mx.sendStateEvent(
address.roomId,
StateEvent.PoniesRoomEmotes,
packContent,
address.stateKey
await ct.requestAuthJSON(
putStateEvent({
pathParams: {
roomId: address.roomId,
eventType: StateEventType.PoniesRoomEmotes,
stateKey: address.stateKey,
},
body: packContent,
})
);
},
[mx, imagePack]
[ct, imagePack]
);
return (

View file

@ -1,22 +1,29 @@
import React, { useCallback, useMemo } from 'react';
import { putAccountData } from 'neurone/lib/api/matrix/client/v3/user';
import { ImagePackContent } from './ImagePackContent';
import { ImagePack, PackContent } from '../../plugins/custom-emoji';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { AccountDataEvent } from '../../../types/matrix/accountData';
import { AccountDataType } from '../../../types/matrix/accountData';
import { useUserImagePack } from '../../hooks/useImagePacks';
import { useClient } from '../../hooks/useClient';
export function UserImagePack() {
const mx = useMatrixClient();
const ct = useClient();
const defaultPack = useMemo(() => new ImagePack(mx.getUserId() ?? '', {}, undefined), [mx]);
const imagePack = useUserImagePack();
const defaultPack = useMemo(() => new ImagePack(ct.session.userId ?? '', {}, undefined), [ct]);
const imagePackResult = useUserImagePack();
const imagePack = imagePackResult.value ?? defaultPack;
const handleUpdate = useCallback(
async (packContent: PackContent) => {
await mx.setAccountData(AccountDataEvent.PoniesUserEmotes, packContent);
await ct.requestAuthJSON(
putAccountData({
pathParams: { type: AccountDataType.PoniesUserEmotes, userId: ct.session.userId },
body: packContent,
})
);
},
[mx]
[ct]
);
return <ImagePackContent imagePack={imagePack ?? defaultPack} canEdit onUpdate={handleUpdate} />;
return <ImagePackContent imagePack={imagePack} canEdit onUpdate={handleUpdate} />;
}

View file

@ -16,7 +16,8 @@ import {
IconButton,
Menu,
} from 'folds';
import { MatrixError } from 'matrix-js-sdk';
import { putStateEvent } from 'neurone/lib/api/matrix/client/v3/rooms';
import { parseMatrixResp } from 'neurone/lib/matrix-request';
import { SequenceCard } from '../../../components/sequence-card';
import {
ImagePack,
@ -30,26 +31,26 @@ import { useRoomImagePacks } from '../../../hooks/useImagePacks';
import { LineClamp2 } from '../../../styles/Text.css';
import { SettingTile } from '../../../components/setting-tile';
import { SequenceCardStyle } from '../styles.css';
import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { mxcUrlToHttp } from '../../../utils/matrix';
import { mxcToHttp } from '../../../../client/mxcToHttp';
import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
import { usePowerLevels } from '../../../hooks/usePowerLevels';
import { StateEvent } from '../../../../types/matrix/room';
import { usePowerLevelsContext } from '../../../hooks/usePowerLevels';
import { StateEventType } from '../../../../types/matrix/room';
import { suffixRename } from '../../../utils/common';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useAlive } from '../../../hooks/useAlive';
import { useRoomCreators } from '../../../hooks/useRoomCreators';
import { useRoomPermissions } from '../../../hooks/useRoomPermissions';
import { useClient } from '../../../hooks/useClient';
type CreatePackTileProps = {
packs: ImagePack[];
roomId: string;
};
function CreatePackTile({ packs, roomId }: CreatePackTileProps) {
const mx = useMatrixClient();
const ct = useClient();
const alive = useAlive();
const [addState, addPack] = useAsyncCallback<void, MatrixError, [string, string]>(
const [addState, addPack] = useAsyncCallback<void, unknown, [string, string]>(
useCallback(
async (stateKey, name) => {
const content: PackContent = {
@ -57,9 +58,17 @@ function CreatePackTile({ packs, roomId }: CreatePackTileProps) {
display_name: name,
},
};
await mx.sendStateEvent(roomId, StateEvent.PoniesRoomEmotes as any, content, stateKey);
const resp = await ct.requestAuthJSON(
putStateEvent({
pathParams: { roomId, eventType: StateEventType.PoniesRoomEmotes, stateKey },
body: content,
})
);
const [err] = await parseMatrixResp(resp);
if (err) throw new Error(err.data.error ?? err.data.errcode);
},
[mx, roomId]
[ct, roomId]
)
);
@ -119,7 +128,7 @@ function CreatePackTile({ packs, roomId }: CreatePackTileProps) {
/>
{addState.status === AsyncStatus.Error && (
<Text style={{ color: color.Critical.Main }} size="T300">
{addState.error.message}
{addState.error instanceof Error ? addState.error.message : 'Unknown Error'}
</Text>
)}
</Box>
@ -141,19 +150,21 @@ function CreatePackTile({ packs, roomId }: CreatePackTileProps) {
type RoomPacksProps = {
onViewPack: (imagePack: ImagePack) => void;
};
const EMPTY_PACKS: ImagePack[] = [];
export function RoomPacks({ onViewPack }: RoomPacksProps) {
const mx = useMatrixClient();
const ct = useClient();
const useAuthentication = useMediaAuthentication();
const room = useRoom();
const roomId = useRoom();
const alive = useAlive();
const powerLevels = usePowerLevels(room);
const creators = useRoomCreators(room);
const powerLevels = usePowerLevelsContext();
const creators = useRoomCreators(roomId);
const permissions = useRoomPermissions(creators, powerLevels);
const canEdit = permissions.stateEvent(StateEvent.PoniesRoomEmotes, mx.getSafeUserId());
const canEdit = permissions.stateEvent(StateEventType.PoniesRoomEmotes, ct.session.userId);
const unfilteredPacks = useRoomImagePacks(room);
const unfilteredPacksResult = useRoomImagePacks(roomId);
const unfilteredPacks = unfilteredPacksResult.value ?? EMPTY_PACKS;
const packs = useMemo(() => unfilteredPacks.filter((pack) => !pack.deleted), [unfilteredPacks]);
const [removedPacks, setRemovedPacks] = useState<PackAddress[]>([]);
@ -164,9 +175,21 @@ export function RoomPacks({ onViewPack }: RoomPacksProps) {
for (let i = 0; i < removedPacks.length; i += 1) {
const addr = removedPacks[i];
// eslint-disable-next-line no-await-in-loop
await mx.sendStateEvent(room.roomId, StateEvent.PoniesRoomEmotes as any, {}, addr.stateKey);
const resp = await ct.requestAuthJSON(
putStateEvent({
pathParams: {
roomId,
eventType: StateEventType.PoniesRoomEmotes,
stateKey: addr.stateKey,
},
body: {},
})
);
// eslint-disable-next-line no-await-in-loop
const [err] = await parseMatrixResp(resp);
if (err) throw new Error(err.data.error ?? err.data.errcode);
}
}, [mx, room, removedPacks])
}, [ct, roomId, removedPacks])
);
const applyingChanges = applyState.status === AsyncStatus.Loading;
@ -190,7 +213,9 @@ export function RoomPacks({ onViewPack }: RoomPacksProps) {
const renderPack = (pack: ImagePack) => {
const avatarMxc = pack.getAvatarUrl(ImageUsage.Emoticon);
const avatarUrl = avatarMxc ? mxcUrlToHttp(mx, avatarMxc, useAuthentication) : undefined;
const avatarUrl = avatarMxc
? mxcToHttp(ct.session.baseUrl, avatarMxc, useAuthentication).url()
: undefined;
const { address } = pack;
if (!address) return null;
const removed = !!removedPacks.find((addr) => packAddressEqual(addr, address));
@ -268,7 +293,7 @@ export function RoomPacks({ onViewPack }: RoomPacksProps) {
<>
<Box direction="Column" gap="100">
<Text size="L400">Packs</Text>
{canEdit && <CreatePackTile roomId={room.roomId} packs={packs} />}
{canEdit && <CreatePackTile roomId={roomId} packs={packs} />}
{packs.map(renderPack)}
{packs.length === 0 && (
<SequenceCard

View file

@ -65,7 +65,7 @@ export const useGlobalImagePacks = (): PendingObservableResult<ImagePack[]> => {
useCallback(
(roomPackEvents) =>
roomPackEvents
.filter((p) => !!p)
.filter((p): p is JoinRoomStateObject => !!p)
.map((packEvent) => ImagePack.fromStateEvent(packEvent.roomId, packEvent.event)),
[]
)
@ -150,7 +150,7 @@ export const useRoomsImagePacks = (rooms: string[]): PendingObservableResult<Ima
useCallback(
(roomPackEvents) =>
roomPackEvents
.filter((p) => !!p)
.filter((p): p is JoinRoomStateObject => !!p)
.map((roomPackEvent) =>
ImagePack.fromStateEvent(roomPackEvent.roomId, roomPackEvent.event)
),