Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Added DTOs for RoomJoinRulesStateEventDTO: heusalagroup/hghs#20
Browse files Browse the repository at this point in the history
  • Loading branch information
thejhh committed Apr 24, 2022
1 parent 3d5e4d5 commit 3d90d8a
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 11 deletions.
26 changes: 15 additions & 11 deletions MatrixCrudRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ import { createRoomGuestAccessContentDTO } from "./types/event/roomGuestAccess/R
import { MatrixStateEventOf } from "./types/core/MatrixStateEventOf";
import { createRoomHistoryVisibilityStateEventDTO } from "./types/event/roomHistoryVisibility/RoomHistoryVisibilityStateEventDTO";
import { createRoomHistoryVisibilityStateContentDTO } from "./types/event/roomHistoryVisibility/RoomHistoryVisibilityStateContentDTO";
import { createRoomJoinRulesAllowConditionDTO, RoomJoinRulesAllowConditionDTO } from "./types/event/roomJoinRules/RoomJoinRulesAllowConditionDTO";
import { RoomMembershipType } from "./types/event/roomJoinRules/RoomMembershipType";
import { createRoomJoinRulesStateContentDTO } from "./types/event/roomJoinRules/RoomJoinRulesStateContentDTO";
import { createRoomJoinRulesStateEventDTO } from "./types/event/roomJoinRules/RoomJoinRulesStateEventDTO";

const LOG = LogService.createLogger('MatrixCrudRepository');

Expand Down Expand Up @@ -378,17 +382,17 @@ export class MatrixCrudRepository<T> implements Repository<T> {
// Allow members from these groups to access the item.
// See also https://github.com/matrix-org/matrix-doc/blob/master/proposals/3083-restricted-rooms.md
if (allowedGroups !== undefined) {
initialState.push({
type: MatrixType.M_ROOM_JOIN_RULES,
state_key: "",
content: {
join_rule: MatrixJoinRule.RESTRICTED,
allow: map(allowedGroups, (item : MatrixRoomId) => ({
type: MatrixType.M_ROOM_MEMBERSHIP,
room_id: item
}))
}
});
initialState.push(
createRoomJoinRulesStateEventDTO(
createRoomJoinRulesStateContentDTO(
MatrixJoinRule.RESTRICTED,
map(
allowedGroups,
(item : MatrixRoomId) : RoomJoinRulesAllowConditionDTO => createRoomJoinRulesAllowConditionDTO(RoomMembershipType.M_ROOM_MEMBERSHIP, item)
)
)
)
);
}

LOG.debug(`createItem: initialState = `, initialState);
Expand Down
3 changes: 3 additions & 0 deletions types/core/MatrixType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export enum MatrixType {
*
* This may be written in wrong syntax
*
* You should probably use RoomMembershipType.M_ROOM_MEMBERSHIP
*
* @see https://github.com/heusalagroup/hghs/issues/20
* @deprecated
*/
M_ROOM_MEMBERSHIP = 'm.room.membership',

Expand Down
41 changes: 41 additions & 0 deletions types/event/roomJoinRules/RoomJoinRulesAllowConditionDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.

import { isRoomMembershipType, RoomMembershipType } from "./RoomMembershipType";
import { hasNoOtherKeysInDevelopment, isRegularObject } from "../../../../core/modules/lodash";
import { isMatrixRoomId, MatrixRoomId } from "../../core/MatrixRoomId";

export interface RoomJoinRulesAllowConditionDTO {
readonly type : RoomMembershipType;
readonly room_id : MatrixRoomId;
}

export function createRoomJoinRulesAllowConditionDTO (
type : RoomMembershipType,
room_id : MatrixRoomId
): RoomJoinRulesAllowConditionDTO {
return {
type,
room_id
};
}

export function isRoomJoinRulesAllowConditionDTO (value: any): value is RoomJoinRulesAllowConditionDTO {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, [
'type',
'room_id'
])
&& isRoomMembershipType(value?.type)
&& isMatrixRoomId(value?.room_id)
);
}

export function stringifyRoomJoinRulesAllowConditionDTO (value: RoomJoinRulesAllowConditionDTO): string {
return `RoomJoinRulesAllowConditionDTO(${value})`;
}

export function parseRoomJoinRulesAllowConditionDTO (value: any): RoomJoinRulesAllowConditionDTO | undefined {
if ( isRoomJoinRulesAllowConditionDTO(value) ) return value;
return undefined;
}
41 changes: 41 additions & 0 deletions types/event/roomJoinRules/RoomJoinRulesStateContentDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.

import { isMatrixJoinRule, MatrixJoinRule } from "./MatrixJoinRule";
import { isRoomJoinRulesAllowConditionDTO, RoomJoinRulesAllowConditionDTO } from "./RoomJoinRulesAllowConditionDTO";
import { hasNoOtherKeysInDevelopment, isArrayOf, isRegularObject } from "../../../../core/modules/lodash";

export interface RoomJoinRulesStateContentDTO {
readonly allow : RoomJoinRulesAllowConditionDTO[];
readonly join_rule : MatrixJoinRule;
}

export function createRoomJoinRulesStateContentDTO (
join_rule : MatrixJoinRule,
allow : RoomJoinRulesAllowConditionDTO[]
): RoomJoinRulesStateContentDTO {
return {
join_rule,
allow
};
}

export function isRoomJoinRulesStateContentDTO (value: any): value is RoomJoinRulesStateContentDTO {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, [
'allow',
'join_rule'
])
&& isArrayOf<RoomJoinRulesAllowConditionDTO>(value?.allow, isRoomJoinRulesAllowConditionDTO)
&& isMatrixJoinRule(value?.join_rule)
);
}

export function stringifyRoomJoinRulesStateContentDTO (value: RoomJoinRulesStateContentDTO): string {
return `RoomJoinRulesStateContentDTO(${value})`;
}

export function parseRoomJoinRulesStateContentDTO (value: any): RoomJoinRulesStateContentDTO | undefined {
if ( isRoomJoinRulesStateContentDTO(value) ) return value;
return undefined;
}
48 changes: 48 additions & 0 deletions types/event/roomJoinRules/RoomJoinRulesStateEventDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.

import { hasNoOtherKeysInDevelopment, isRegularObject, isStringOrUndefined } from "../../../../core/modules/lodash";
import { isRoomJoinRulesStateContentDTO, RoomJoinRulesStateContentDTO } from "./RoomJoinRulesStateContentDTO";
import { MatrixStateEventOf } from "../../core/MatrixStateEventOf";
import { MatrixType } from "../../core/MatrixType";

/**
* @see https://github.com/heusalagroup/hghs/issues/20
*/
export interface RoomJoinRulesStateEventDTO extends MatrixStateEventOf<RoomJoinRulesStateContentDTO> {
readonly type : MatrixType.M_ROOM_JOIN_RULES;
readonly state_key : string;
readonly content : RoomJoinRulesStateContentDTO;
}

export function createRoomJoinRulesStateEventDTO (
content : RoomJoinRulesStateContentDTO
): RoomJoinRulesStateEventDTO {
return {
type: MatrixType.M_ROOM_JOIN_RULES,
state_key: '',
content
};
}

export function isRoomJoinRulesStateEventDTO (value: any): value is RoomJoinRulesStateEventDTO {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, [
'type',
'state_key',
'content'
])
&& value?.type === MatrixType.M_ROOM_HISTORY_VISIBILITY
&& isStringOrUndefined(value?.state_key)
&& isRoomJoinRulesStateContentDTO(value?.content)
);
}

export function stringifyRoomJoinRulesStateEventDTO (value: RoomJoinRulesStateEventDTO): string {
return `RoomJoinRulesStateEventDTO(${value})`;
}

export function parseRoomJoinRulesStateEventDTO (value: any): RoomJoinRulesStateEventDTO | undefined {
if ( isRoomJoinRulesStateEventDTO(value) ) return value;
return undefined;
}
38 changes: 38 additions & 0 deletions types/event/roomJoinRules/RoomMembershipType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.

export enum RoomMembershipType {

/**
* There is also MatrixType.M_ROOM_MEMBERSHIP but that should not be used.
*/
M_ROOM_MEMBERSHIP = "m.room_membership"

}

export function isRoomMembershipType (value: any): value is RoomMembershipType {
switch (value) {
case RoomMembershipType.M_ROOM_MEMBERSHIP:
return true;

default:
return false;

}
}

export function stringifyRoomMembershipType (value: RoomMembershipType): string {
switch (value) {
case RoomMembershipType.M_ROOM_MEMBERSHIP : return 'm.room_membership';
}
throw new TypeError(`Unsupported RoomMembershipType value: ${value}`);
}

export function parseRoomMembershipType (value: any): RoomMembershipType | undefined {
switch (`${value}`.toLowerCase()) {

case 'm.room_membership':
case 'room_membership' : return RoomMembershipType.M_ROOM_MEMBERSHIP;

default : return undefined;
}
}

0 comments on commit 3d90d8a

Please sign in to comment.