mirror of
				https://github.com/cinnyapp/cinny.git
				synced 2025-11-04 14:30: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 { useStateEvent } from './useStateEvent';
 | 
			
		||||
import { StateEvent } from '../../types/matrix/room';
 | 
			
		||||
import { getRoomCreators } from '../utils/room';
 | 
			
		||||
import { IRoomCreateContent, StateEvent } from '../../types/matrix/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 creators = useMemo(
 | 
			
		||||
    () => (createEvent ? getRoomCreators(createEvent) : undefined),
 | 
			
		||||
    [createEvent]
 | 
			
		||||
  );
 | 
			
		||||
  const creators = useMemo(() => (createEvent ? getRoomCreators(createEvent) : []), [createEvent]);
 | 
			
		||||
 | 
			
		||||
  return creators;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -357,3 +357,7 @@ export const knockRestrictedSupported = (version: string): boolean => {
 | 
			
		|||
  const unsupportedVersion = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
 | 
			
		||||
  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 { AccountDataEvent } from '../../types/matrix/accountData';
 | 
			
		||||
import {
 | 
			
		||||
  IRoomCreateContent,
 | 
			
		||||
  Membership,
 | 
			
		||||
  MessageEvent,
 | 
			
		||||
  NotificationType,
 | 
			
		||||
| 
						 | 
				
			
			@ -520,29 +519,3 @@ export const guessPerfectParent = (
 | 
			
		|||
 | 
			
		||||
  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