mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-04 22:40:29 +03:00
improve use room creators hook
This commit is contained in:
parent
e977abca07
commit
abbd0efc7e
3 changed files with 28 additions and 35 deletions
|
|
@ -1,16 +1,32 @@
|
||||||
import { Room } from 'matrix-js-sdk';
|
import { MatrixEvent, Room } from 'matrix-js-sdk';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useStateEvent } from './useStateEvent';
|
import { useStateEvent } from './useStateEvent';
|
||||||
import { StateEvent } from '../../types/matrix/room';
|
import { IRoomCreateContent, StateEvent } from '../../types/matrix/room';
|
||||||
import { getRoomCreators } from '../utils/room';
|
|
||||||
|
|
||||||
export const useRoomCreators = (room: Room): string[] | undefined => {
|
export const getRoomCreators = (createEvent: MatrixEvent): string[] => {
|
||||||
|
const createContent = createEvent.getContent<IRoomCreateContent>();
|
||||||
|
|
||||||
|
const creators: Set<string> = new Set();
|
||||||
|
|
||||||
|
if (createEvent.event.sender) {
|
||||||
|
creators.add(createEvent.event.sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('additional_creators' in createContent && Array.isArray(createContent.additional_creators)) {
|
||||||
|
createContent.additional_creators.forEach((creator) => {
|
||||||
|
if (typeof creator === 'string') {
|
||||||
|
creators.add(creator);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(creators);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useRoomCreators = (room: Room): string[] => {
|
||||||
const createEvent = useStateEvent(room, StateEvent.RoomCreate);
|
const createEvent = useStateEvent(room, StateEvent.RoomCreate);
|
||||||
|
|
||||||
const creators = useMemo(
|
const creators = useMemo(() => (createEvent ? getRoomCreators(createEvent) : []), [createEvent]);
|
||||||
() => (createEvent ? getRoomCreators(createEvent) : undefined),
|
|
||||||
[createEvent]
|
|
||||||
);
|
|
||||||
|
|
||||||
return creators;
|
return creators;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -357,3 +357,7 @@ export const knockRestrictedSupported = (version: string): boolean => {
|
||||||
const unsupportedVersion = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
const unsupportedVersion = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
||||||
return !unsupportedVersion.includes(version);
|
return !unsupportedVersion.includes(version);
|
||||||
};
|
};
|
||||||
|
export const creatorsSupported = (version: string): boolean => {
|
||||||
|
const unsupportedVersion = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];
|
||||||
|
return !unsupportedVersion.includes(version);
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import {
|
||||||
import { CryptoBackend } from 'matrix-js-sdk/lib/common-crypto/CryptoBackend';
|
import { CryptoBackend } from 'matrix-js-sdk/lib/common-crypto/CryptoBackend';
|
||||||
import { AccountDataEvent } from '../../types/matrix/accountData';
|
import { AccountDataEvent } from '../../types/matrix/accountData';
|
||||||
import {
|
import {
|
||||||
IRoomCreateContent,
|
|
||||||
Membership,
|
Membership,
|
||||||
MessageEvent,
|
MessageEvent,
|
||||||
NotificationType,
|
NotificationType,
|
||||||
|
|
@ -520,29 +519,3 @@ export const guessPerfectParent = (
|
||||||
|
|
||||||
return perfectParent;
|
return perfectParent;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getRoomCreators = (createEvent: MatrixEvent): string[] | undefined => {
|
|
||||||
const createContent = createEvent.getContent<IRoomCreateContent>();
|
|
||||||
const roomVersion = createContent.room_version;
|
|
||||||
|
|
||||||
if (['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'].includes(roomVersion)) {
|
|
||||||
// room version doesn't support creators.
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const creators: Set<string> = new Set();
|
|
||||||
|
|
||||||
if (createEvent.event.sender) {
|
|
||||||
creators.add(createEvent.event.sender);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('additional_creators' in createContent && Array.isArray(createContent.additional_creators)) {
|
|
||||||
createContent.additional_creators.forEach((creator) => {
|
|
||||||
if (typeof creator === 'string') {
|
|
||||||
creators.add(creator);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return Array.from(creators);
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue