Skip to content

Commit

Permalink
Simplify functional members state event detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Aug 2, 2021
1 parent eb5b4a6 commit eae64dc
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions Riot/Utils/EventFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -419,28 +419,10 @@ - (BOOL)session:(MXSession *)session updateRoomSummary:(MXRoomSummary *)summary
if (BuildSettings.supportFunctionalMembers)
{
// Customisation for EMS Functional Members
MXEvent *functionalMembersEvent = [self functionalMembersEventFromStateEvents:stateEvents];

if (functionalMembersEvent)
if ([self functionalMembersEventFromStateEvents:stateEvents])
{
MXEvent *existingFunctionalMembersEvent = [self previousFunctionalMembersEventFromStateEvents:roomState.stateEvents];

// If there isn't an existing functional members event we're done, the state has changed.
if (!existingFunctionalMembersEvent)
{
MXLogDebug(@"[EventFormatter] A functional members event has been added to the room.")
return YES;
}

NSArray<NSString*> *serviceMemberIDs = functionalMembersEvent.content[FunctionalMembersServiceMembersKey] ?: @[];
NSArray<NSString*> *existingServiceMemberIDs = existingFunctionalMembersEvent.content[FunctionalMembersServiceMembersKey] ?: @[];

// If the new service members differ from the existing ones, the state has changed.
if (![serviceMemberIDs isEqualToArray:existingServiceMemberIDs])
{
MXLogDebug(@"[EventFormatter] The functional members event has changed.")
return YES;
}
MXLogDebug(@"[EventFormatter] The functional members event has been updated.")
return YES;
}
}

Expand Down Expand Up @@ -471,7 +453,7 @@ - (BOOL)session:(MXSession *)session updateRoomSummary:(MXRoomSummary *)summary
}

/**
Gets the newest state event of type `io.element.functional_members` from the supplied array of state events.
Gets the latest state event of type `io.element.functional_members` from the supplied array of state events.
@return An event of type `io.element.functional_members`, or nil if the event wasn't found.
*/
- (MXEvent *)functionalMembersEventFromStateEvents:(NSArray<MXEvent *> *)stateEvents
Expand All @@ -480,24 +462,6 @@ - (MXEvent *)functionalMembersEventFromStateEvents:(NSArray<MXEvent *> *)stateEv
return [stateEvents filteredArrayUsingPredicate:functionalMembersPredicate].lastObject;
}

/**
Gets the last but one state event of type `io.element.functional_members` from the supplied array of state events.
@return An event of type `io.element.functional_members`, or nil if the event wasn't found.
*/
- (MXEvent *)previousFunctionalMembersEventFromStateEvents:(NSArray<MXEvent *> *)stateEvents
{
NSPredicate *functionalMembersPredicate = [NSPredicate predicateWithFormat:@"type == %@", FunctionalMembersStateEventType];
NSArray<MXEvent *> *events = [stateEvents filteredArrayUsingPredicate:functionalMembersPredicate];

if (events.count <= 1)
{
// There are no previous events, return nil.
return nil;
}

return events[events.count - 2];
}

#pragma mark - Timestamp formatting

- (NSString*)dateStringFromDate:(NSDate *)date withTime:(BOOL)time
Expand Down

0 comments on commit eae64dc

Please sign in to comment.