Skip to content

Commit 96940f6

Browse files
authored
Merge pull request #1908 from matrix-org/mauroromito/implement_new_MSC
Support for MSC 4289
2 parents 8e6c4f4 + 5c671d1 commit 96940f6

File tree

13 files changed

+105
-38
lines changed

13 files changed

+105
-38
lines changed

MatrixSDK/Data/MXRoomState.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ Use MXRoomSummary.displayname to get a computed room display name.
283283
*/
284284
- (float)memberNormalizedPowerLevel:(NSString*)userId;
285285

286+
/**
287+
Returns the power level for a given user identifier, this checks also for the creator and additional creators
288+
*/
289+
- (NSInteger)powerLevelOfUserWithUserID:(NSString *)userId;
290+
286291

287292
# pragma mark - Conference call
288293
/**

MatrixSDK/Data/MXRoomState.m

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,53 @@ - (NSString *)avatar
328328
return avatar;
329329
}
330330

331+
- (NSString *)roomVersion
332+
{
333+
NSString *roomVersion;
334+
335+
// Check it from the state events
336+
MXEvent *event = [stateEvents objectForKey:kMXEventTypeStringRoomCreate].lastObject;
337+
NSDictionary<NSString *, id> *eventContent = [self contentOfEvent:event];
338+
339+
if (event && eventContent)
340+
{
341+
MXJSONModelSetString(roomVersion, eventContent[@"room_version"]);
342+
roomVersion = [roomVersion copy];
343+
344+
}
345+
return roomVersion;
346+
}
347+
331348
- (NSString *)creatorUserId
332349
{
333350
NSString *creatorUserId;
334351

335352
// Check it from the state events
336353
MXEvent *event = [stateEvents objectForKey:kMXEventTypeStringRoomCreate].lastObject;
354+
NSString* sender = [event sender];
337355

356+
if (event && sender)
357+
{
358+
creatorUserId = [sender copy];
359+
}
360+
return creatorUserId;
361+
}
362+
363+
- (NSArray<NSString*> *)additionalCreators
364+
{
365+
NSArray<NSString*> *additionalCreators = @[];
366+
367+
// Check it from the state events
368+
MXEvent *event = [stateEvents objectForKey:kMXEventTypeStringRoomCreate].lastObject;
338369
NSDictionary<NSString *, id> *eventContent = [self contentOfEvent:event];
339370

340371
if (event && eventContent)
341372
{
342-
MXJSONModelSetString(creatorUserId, eventContent[@"creator"]);
343-
creatorUserId = [creatorUserId copy];
373+
MXJSONModelSetArray(additionalCreators, eventContent[@"additional_creators"]);
374+
additionalCreators = [additionalCreators copy];
375+
344376
}
345-
return creatorUserId;
377+
return additionalCreators;
346378
}
347379

348380
- (MXRoomHistoryVisibility)historyVisibility
@@ -601,13 +633,52 @@ - (float)memberNormalizedPowerLevel:(NSString*)userId
601633
// Ignore banned and left (kicked) members
602634
if (member.membership != MXMembershipLeave && member.membership != MXMembershipBan)
603635
{
604-
float userPowerLevelFloat = [powerLevels powerLevelOfUserWithUserID:userId];
605-
powerLevel = maxPowerLevel ? userPowerLevelFloat / maxPowerLevel : 1;
636+
float userPowerLevelFloat = [self powerLevelOfUserWithUserID:userId];
637+
if (maxPowerLevel && userPowerLevelFloat >= maxPowerLevel)
638+
{
639+
powerLevel = 1;
640+
}
641+
else
642+
{
643+
powerLevel = maxPowerLevel ? userPowerLevelFloat / maxPowerLevel : 1;
644+
}
606645
}
607646

608647
return powerLevel;
609648
}
610649

650+
- (NSInteger)powerLevelOfUserWithUserID:(NSString *)userId
651+
{
652+
if ([self isMSC4289Supported])
653+
{
654+
if ([userId isEqualToString: [self creatorUserId]] || [[self additionalCreators] containsObject: userId])
655+
{
656+
return NSIntegerMax;
657+
}
658+
}
659+
660+
// By default, use usersDefault
661+
NSInteger userPowerLevel = powerLevels.usersDefault;
662+
663+
NSNumber *powerLevel;
664+
MXJSONModelSetNumber(powerLevel, powerLevels.users[userId]);
665+
if (powerLevel)
666+
{
667+
userPowerLevel = [powerLevel integerValue];
668+
}
669+
670+
return userPowerLevel;
671+
}
672+
673+
- (BOOL)isMSC4289Supported {
674+
NSArray<NSString*> *supportedRoomVersions = @[@"org.matrix.hydra.11",@"12"];
675+
if ([self roomVersion])
676+
{
677+
return [supportedRoomVersions containsObject:[self roomVersion]];
678+
}
679+
return NO;
680+
}
681+
611682
# pragma mark - Conference call
612683
- (BOOL)isOngoingConferenceCall
613684
{

MatrixSDK/JSONModels/MXRoomCreateContent.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ extern NSString* _Nonnull const MXRoomCreateContentRoomTypeJSONKey;
3232
*/
3333
@property (nonatomic, copy, readonly, nullable) NSString *creatorUserId;
3434

35+
/**
36+
The array of `user_id` of the additional room creators.
37+
*/
38+
@property (nonatomic, copy, readonly, nullable) NSArray<NSString*> *additionalCreators;
39+
3540
/**
3641
Room predecessor information if the current room is a new version of an old room (that has a state event `m.room.tombstone`).
3742
*/

MatrixSDK/JSONModels/MXRoomCreateContent.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// Private
2626

2727
static NSString* const kRoomCreateContentUserIdJSONKey = @"creator";
28+
static NSString* const kRoomCreateContentAdditionalCreatorsJSONKey = @"additional_creators";
2829
static NSString* const kRoomCreateContentPredecessorInfoJSONKey = @"predecessor";
2930
static NSString* const kRoomCreateContentRoomVersionJSONKey = @"room_version";
3031
static NSString* const kRoomCreateContentFederateJSONKey = @"m.federate";
@@ -34,6 +35,7 @@
3435
@interface MXRoomCreateContent()
3536

3637
@property (nonatomic, copy, readwrite, nullable) NSString *creatorUserId;
38+
@property (nonatomic, copy, readwrite, nullable) NSArray<NSString*> *additionalCreators;
3739
@property (nonatomic, strong, readwrite, nullable) MXRoomPredecessorInfo *roomPredecessorInfo;
3840
@property (nonatomic, copy, readwrite, nullable) NSString *roomVersion;
3941
@property (nonatomic, readwrite) BOOL isFederated;
@@ -53,6 +55,7 @@ + (id)modelFromJSON:(NSDictionary *)jsonDictionary
5355
roomCreateContent.isFederated = YES;
5456

5557
MXJSONModelSetString(roomCreateContent.creatorUserId, jsonDictionary[kRoomCreateContentUserIdJSONKey]);
58+
MXJSONModelSetArray(roomCreateContent.additionalCreators, jsonDictionary[kRoomCreateContentAdditionalCreatorsJSONKey]);
5659
MXJSONModelSetMXJSONModel(roomCreateContent.roomPredecessorInfo, MXRoomPredecessorInfo, jsonDictionary[kRoomCreateContentPredecessorInfoJSONKey]);
5760
MXJSONModelSetString(roomCreateContent.roomVersion, jsonDictionary[kRoomCreateContentRoomVersionJSONKey]);
5861
MXJSONModelSetBoolean(roomCreateContent.isFederated, jsonDictionary[kRoomCreateContentFederateJSONKey]);
@@ -94,6 +97,11 @@ - (NSDictionary *)JSONDictionary
9497
jsonDictionary[MXRoomCreateContentRoomTypeJSONKey] = self.roomType;
9598
}
9699

100+
if (self.additionalCreators)
101+
{
102+
jsonDictionary[kRoomCreateContentAdditionalCreatorsJSONKey] = self.additionalCreators;
103+
}
104+
97105
return jsonDictionary;
98106
}
99107

MatrixSDK/JSONModels/MXRoomPowerLevels.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ extern NSInteger const kMXRoomPowerLevelNotificationsRoomDefault;
4646
*/
4747
@property (nonatomic) NSInteger usersDefault;
4848

49-
/**
50-
Helper to get the power level of a member of the room.
51-
52-
@param userId the id of the user.
53-
@return his power level.
54-
*/
55-
- (NSInteger)powerLevelOfUserWithUserID:(NSString*)userId;
56-
5749
#pragma mark - Setup
5850

5951
/**

MatrixSDK/JSONModels/MXRoomPowerLevels.m

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ - (instancetype)initWithDefaultSpecValues
117117
return self;
118118
}
119119

120-
- (NSInteger)powerLevelOfUserWithUserID:(NSString *)userId
121-
{
122-
// By default, use usersDefault
123-
NSInteger userPowerLevel = _usersDefault;
124-
125-
NSNumber *powerLevel;
126-
MXJSONModelSetNumber(powerLevel, _users[userId]);
127-
if (powerLevel)
128-
{
129-
userPowerLevel = [powerLevel integerValue];
130-
}
131-
132-
return userPowerLevel;
133-
}
134-
135120
- (NSInteger)minimumPowerLevelForSendingEventAsMessage:(MXEventTypeString)eventTypeString
136121
{
137122
NSInteger minimumPowerLevel;

MatrixSDK/NotificationCenter/Checker/MXPushRuleSenderNotificationPermissionConditionChecker.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ - (BOOL)isCondition:(MXPushRuleCondition*)condition satisfiedBy:(MXEvent*)event
5353
{
5454
MXRoomPowerLevels *roomPowerLevels = roomState.powerLevels;
5555
NSInteger notifLevel = [roomPowerLevels minimumPowerLevelForNotifications:notifLevelKey defaultPower:50];
56-
NSInteger senderPowerLevel = [roomPowerLevels powerLevelOfUserWithUserID:event.sender];
56+
NSInteger senderPowerLevel = [roomState powerLevelOfUserWithUserID:event.sender];
5757

5858
isSatisfied = (senderPowerLevel >= notifLevel);
5959
}

MatrixSDK/Space/MXSpace.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,13 @@ public class MXSpace: NSObject {
327327
}
328328

329329
room.state { roomState in
330-
guard let powerLevels = roomState?.powerLevels else {
330+
guard let roomState,
331+
let powerLevels = roomState.powerLevels else {
331332
MXLog.warning("[MXSpace] canAddRoom: space power levels not found")
332333
completion(false)
333334
return
334335
}
335-
let userPowerLevel = powerLevels.powerLevelOfUser(withUserID: userId)
336+
let userPowerLevel = roomState.powerLevelOfUser(withUserID: userId)
336337
let minimumPowerLevel = self.minimumPowerLevelForAddingRoom(with: powerLevels)
337338
let canAddRoom = userPowerLevel >= minimumPowerLevel
338339

MatrixSDK/VoIP/MXCallManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ + (BOOL)canPlaceConferenceCallInRoom:(MXRoom *)room roomState:(MXRoomState *)roo
13251325
else
13261326
{
13271327
MXRoomPowerLevels *powerLevels = roomState.powerLevels;
1328-
NSInteger oneSelfPowerLevel = [powerLevels powerLevelOfUserWithUserID:room.mxSession.myUserId];
1328+
NSInteger oneSelfPowerLevel = [roomState powerLevelOfUserWithUserID:room.mxSession.myUserId];
13291329

13301330
// Only member with invite power level can create a conference call
13311331
if (oneSelfPowerLevel >= powerLevels.invite)

MatrixSDKTests/MXLocationServiceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class MXLocationServiceTests: XCTestCase {
346346

347347
_ = liveTimeline.listenToEvents([.roomPowerLevels], { event, direction, state in
348348

349-
XCTAssertEqual(liveTimeline.state?.powerLevels.powerLevelOfUser(withUserID: aliceSession.myUserId), expectedPowerLevel);
349+
XCTAssertEqual(liveTimeline.state?.powerLevelOfUser(withUserID: aliceSession.myUserId), expectedPowerLevel);
350350

351351
let aliceLocationService: MXLocationService = aliceSession.locationService
352352

0 commit comments

Comments
 (0)