Skip to content

Commit

Permalink
fix automatic DM avatar with functional members
Browse files Browse the repository at this point in the history
  • Loading branch information
HarHarLinks committed Feb 16, 2024
1 parent fe46fec commit 0ce2d82
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { TypedEventEmitter } from "./typed-event-emitter";
import { Beacon, BeaconEvent, BeaconEventHandlerMap, getBeaconInfoIdentifier, BeaconIdentifier } from "./beacon";
import { TypedReEmitter } from "../ReEmitter";
import { M_BEACON, M_BEACON_INFO } from "../@types/beacon";
import { UNSTABLE_ELEMENT_FUNCTIONAL_USERS } from "../@types/event"

export interface IMarkerFoundOptions {
/** Whether the timeline was empty before the marker event arrived in the
Expand Down Expand Up @@ -220,6 +221,17 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
this.summaryJoinedMemberCount = count;
}

/**
* Returns the number of joined non-functional members in this room
* This method caches the result.
* @returns The number of non-functional members in this room whose membership is 'join'
*/
public getJoinedFunctionalMemberCount(): number {
return this.getFunctionalMembers().reduce((count, m) => {
return this.getMembers().find((member) => member.userId === m)?.membership === "join" ? count + 1 : count;
}, 0);
}

/**
* Returns the number of invited members in this room
* @returns The number of members in this room whose membership is 'invite'
Expand All @@ -236,6 +248,16 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
return this.invitedMemberCount;
}

/**
* Returns the number of invited members in this room
* @returns The number of members in this room whose membership is 'invite'
*/
public getInvitedFunctionalMemberCount(): number {
return this.getFunctionalMembers().reduce((count, m) => {
return this.getMembers().find((member) => member.userId === m)?.membership === "invite" ? count + 1 : count;
}, 0);
}

/**
* Set the amount of invited members in this room
* @param count - the amount of invited members
Expand All @@ -252,6 +274,20 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
return Object.values(this.members);
}

/**
* Get all functional members in this room.
* @returns A list of MXID Strings.
*/
public getFunctionalMembers(): String[] {
const [functionalUsersStateEvent] = this.getStateEvents(UNSTABLE_ELEMENT_FUNCTIONAL_USERS.name);

if (Array.isArray(functionalUsersStateEvent?.getContent().service_members)) {
return functionalUsersStateEvent.getContent().service_members;
}

return [];
}

/**
* Get all RoomMembers in this room, excluding the user IDs provided.
* @param excludedIds - The user IDs to exclude.
Expand Down
29 changes: 28 additions & 1 deletion src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
}

public getAvatarFallbackMember(): RoomMember | undefined {
const memberCount = this.getInvitedAndJoinedMemberCount();
const memberCount = this.getInvitedAndJoinedMemberCount() - this.getInvitedAndJoinedFunctionalMemberCount();
if (memberCount > 2) {
return;
}
Expand Down Expand Up @@ -1699,6 +1699,25 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
return this.currentState.getInvitedMemberCount();
}

/**
* Returns the number of joined non-functional members in this room
* This method caches the result.
* This is a wrapper around the method of the same name in roomState, returning
* its result for the room's current state.
* @returns The number of non-functional members in this room whose membership is 'join'
*/
public getJoinedFunctionalMemberCount(): number {
return this.currentState.getJoinedFunctionalMemberCount();
}

/**
* Returns the number of non-functional invited members in this room
* @returns The number of non-functional members in this room whose membership is 'invite'
*/
public getInvitedFunctionalMemberCount(): number {
return this.currentState.getInvitedFunctionalMemberCount();
}

/**
* Returns the number of invited + joined members in this room
* @returns The number of members in this room whose membership is 'invite' or 'join'
Expand All @@ -1707,6 +1726,14 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
return this.getInvitedMemberCount() + this.getJoinedMemberCount();
}

/**
* Returns the number of invited + joined non-functional members in this room
* @returns The number of non-functional members in this room whose membership is 'invite' or 'join'
*/
public getInvitedAndJoinedFunctionalMemberCount(): number {
return this.getInvitedFunctionalMemberCount() + this.getJoinedFunctionalMemberCount();
}

/**
* Get a list of members with given membership state.
* @param membership - The membership state.
Expand Down

0 comments on commit 0ce2d82

Please sign in to comment.