Skip to content

Commit

Permalink
Merge pull request #1259 from matrix-org/gil/4893_support_pagination_…
Browse files Browse the repository at this point in the history
…in_the_space_summary_api

Support pagination in the Space Summary API
  • Loading branch information
gileluard authored Nov 9, 2021
2 parents 8093a86 + efbb027 commit 3c99858
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 96 deletions.
6 changes: 4 additions & 2 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1883,10 +1883,12 @@ public extension MXRestClient {
/// - spaceId: The room id of the queried space.
/// - suggestedOnly: If `true`, return only child events and rooms where the `m.space.child` event has `suggested: true`.
/// - limit: Optional. A limit to the maximum number of children to return per space. `-1` for no limit
/// - maxDepth: Optional. The maximum depth in the tree (from the root room) to return. `-1` for no limit
/// - paginationToken: Optional. Pagination token given to retrieve the next set of rooms.
/// - parameters: Space children request parameters.
/// - completion: A closure called when the operation completes.
/// - Returns: a `MXHTTPOperation` instance.
@nonobjc @discardableResult func getSpaceChildrenForSpace(withId spaceId: String, suggestedOnly: Bool, limit: Int?, completion: @escaping (_ response: MXResponse<MXSpaceChildrenResponse>) -> Void) -> MXHTTPOperation {
return __getSpaceChildrenForSpace(withId: spaceId, suggestedOnly: suggestedOnly, limit: limit ?? -1, success: currySuccess(completion), failure: curryFailure(completion))
@nonobjc @discardableResult func getSpaceChildrenForSpace(withId spaceId: String, suggestedOnly: Bool, limit: Int?, maxDepth: Int?, paginationToken: String?, completion: @escaping (_ response: MXResponse<MXSpaceChildrenResponse>) -> Void) -> MXHTTPOperation {
return __getSpaceChildrenForSpace(withId: spaceId, suggestedOnly: suggestedOnly, limit: limit ?? -1, maxDepth: maxDepth ?? -1, paginationToken: paginationToken, success: currySuccess(completion), failure: curryFailure(completion))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ internal class MXSuggestedRoomListDataFetcher: NSObject, MXRoomListDataFetcher {
// do the request
// limit should be -1 for no limit
let limit: Int = numberOfItems < 0 ? -1 : numberOfItems
currentHttpOperation = spaceService.getSpaceChildrenForSpace(withId: space.spaceId,
suggestedOnly: true,
limit: limit) { [weak self] response in
currentHttpOperation = spaceService.getSpaceChildrenForSpace(withId: space.spaceId, suggestedOnly: true, limit: limit, maxDepth: 1, paginationToken: nil) { [weak self] response in
guard let self = self else { return }
switch response {
case .success(let summary):
Expand Down
7 changes: 4 additions & 3 deletions MatrixSDK/Data/RoomList/MXRoomListDataSortOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public struct MXRoomListDataSortOptions: Equatable {
internal var sortDescriptors: [NSSortDescriptor] {
var result: [NSSortDescriptor] = []

if suggested {
result.append(NSSortDescriptor(keyPath: \MXRoomSummaryProtocol.spaceChildInfo?.order, ascending: false))
}
// TODO: reintroduce order once it will be supported
// if suggested {
// result.append(NSSortDescriptor(keyPath: \MXRoomSummaryProtocol.spaceChildInfo?.order, ascending: false))
// }

if invitesFirst {
result.append(NSSortDescriptor(keyPath: \MXRoomSummaryProtocol.membership, ascending: true))
Expand Down
4 changes: 4 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2786,12 +2786,16 @@ Note: Clients should consider avoiding this endpoint for URLs posted in encrypte
/// @param spaceId The room id of the queried space.
/// @param suggestedOnly If `true`, return only child events and rooms where the `m.space.child` event has `suggested: true`.
/// @param limit A limit to the maximum number of children to return per space. `-1` for no limit
/// @param maxDepth The maximum depth in the tree (from the root room) to return. The deepest depth returned will not include children events. `-1` for no limit
/// @param paginationToken Pagination token given to retrieve the next set of rooms.
/// @param success A block object called when the operation succeeds. It provides a `MXSpaceChildrenResponse` object.
/// @param failure A block object called when the operation fails.
/// @return a MXHTTPOperation instance.
- (MXHTTPOperation*)getSpaceChildrenForSpaceWithId:(NSString*)spaceId
suggestedOnly:(BOOL)suggestedOnly
limit:(NSInteger)limit
maxDepth:(NSInteger)maxDepth
paginationToken:(NSString*)paginationToken
success:(void (^)(MXSpaceChildrenResponse *spaceChildrenResponse))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;
@end
10 changes: 7 additions & 3 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -5496,12 +5496,16 @@ - (MXHTTPOperation*)relationsForEvent:(NSString*)eventId
- (MXHTTPOperation*)getSpaceChildrenForSpaceWithId:(NSString*)spaceId
suggestedOnly:(BOOL)suggestedOnly
limit:(NSInteger)limit
maxDepth:(NSInteger)maxDepth
paginationToken:(NSString*)paginationToken
success:(void (^)(MXSpaceChildrenResponse *spaceChildrenResponse))success
failure:(void (^)(NSError *error))failure
{
NSString *maxRoomParameter = limit >= 0 ? [NSString stringWithFormat:@"&max_rooms_per_space=%ld", (long)limit] : @"";
NSString *path = [NSString stringWithFormat:@"%@/org.matrix.msc2946/rooms/%@/spaces?suggested_only=%@%@",
kMXAPIPrefixPathUnstable, spaceId, suggestedOnly ? @"true": @"false", maxRoomParameter];
NSString *limitParam = limit >= 0 ? [NSString stringWithFormat:@"&limit=%ld", (long)limit] : @"";
NSString *maxDepthParam = maxDepth >= 0 ? [NSString stringWithFormat:@"&max_depth=%ld", (long)maxDepth] : @"";
NSString *fromParam = paginationToken != nil ? [NSString stringWithFormat:@"&from=%@", paginationToken] : @"";
NSString *path = [NSString stringWithFormat:@"%@/org.matrix.msc2946/rooms/%@/hierarchy?suggested_only=%@%@%@%@",
kMXAPIPrefixPathUnstable, spaceId, suggestedOnly ? @"true": @"false", limitParam, maxDepthParam, fromParam];

MXWeakify(self);
return [httpClient requestWithMethod:@"GET"
Expand Down
23 changes: 1 addition & 22 deletions MatrixSDK/Space/MXSpaceChildInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public class MXSpaceChildInfo: NSObject {
/// The Matrix content URI of the space avatar.
public let avatarUrl: String?

/// The order key is a string which is used to provide a default ordering of siblings in the room list.
/// Orders should be a string of ascii characters in the range \x20 (space) to \x7F (~), and should be less or equal 50 characters.
public let order: String?

/// The number of members joined to the room.
public let activeMemberCount: Int

Expand All @@ -60,18 +56,9 @@ public class MXSpaceChildInfo: NSObject {
/// `true` if the room is suggested. `false` otherwise.
public let suggested: Bool

/// List of space parent IDs
public let parentIds: Set<String>

/// List of children IDs
public let childrenIds: [String]

/// Gives a list of candidate servers that can be used to join the space.
public let viaServers: [String]

/// The parent space room id.
public let parentRoomId: String?

/// Display name of the space child
public var displayName: String? {
return self.name != nil ? self.name : self.canonicalAlias
Expand All @@ -87,14 +74,10 @@ public class MXSpaceChildInfo: NSObject {
topic: String?,
canonicalAlias: String?,
avatarUrl: String?,
order: String?,
activeMemberCount: Int,
autoJoin: Bool,
suggested: Bool,
parentIds: Set<String>,
childrenIds: [String],
viaServers: [String],
parentRoomId: String?) {
childrenIds: [String]) {
self.childRoomId = childRoomId
self.isKnown = isKnown
self.roomTypeString = roomTypeString
Expand All @@ -103,13 +86,9 @@ public class MXSpaceChildInfo: NSObject {
self.topic = topic
self.canonicalAlias = canonicalAlias
self.avatarUrl = avatarUrl
self.order = order
self.activeMemberCount = activeMemberCount
self.autoJoin = autoJoin
self.suggested = suggested
self.parentIds = parentIds
self.childrenIds = childrenIds
self.viaServers = viaServers
self.parentRoomId = parentRoomId
}
}
23 changes: 14 additions & 9 deletions MatrixSDK/Space/MXSpaceChildSummaryResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,49 @@

#import <Foundation/Foundation.h>
#import "MXJSONModel.h"
#import "MXEvent.h"

NS_ASSUME_NONNULL_BEGIN

/// Space child summary
@interface MXSpaceChildSummaryResponse : MXJSONModel

/// The ID of the room.
@property (nonatomic) NSString* roomId;
@property (nonatomic) NSString *roomId;

/// The room type, which is m.space for subspaces.
/// It can be omitted if there is no room type in which case it should be interpreted as a normal room.
@property (nonatomic, nullable) NSString* roomType;
@property (nonatomic, nullable) NSString *roomType;

/// The name of the room, if any.
@property (nonatomic, nullable) NSString* name;
@property (nonatomic, nullable) NSString *name;

/// The topic of the room, if any.
@property (nonatomic, nullable) NSString* topic;
@property (nonatomic, nullable) NSString *topic;

/// The URL for the room's avatar, if one is set.
@property (nonatomic, nullable) NSString* avatarUrl;
@property (nonatomic, nullable) NSString *avatarUrl;

/// Aliases of the room. May be empty.
@property (nonatomic, nullable) NSArray<NSString*>* aliases;
@property (nonatomic, nullable) NSString *joinRules;

@property (nonatomic) NSTimeInterval creationTime;

/// The canonical alias of the room, if any.
@property (nonatomic, nullable) NSString* canonicalAlias;
@property (nonatomic, nullable) NSString *canonicalAlias;

/// Whether guest users may join the room and participate in it. If they can,
/// they will be subject to ordinary power level rules like any other user.
@property (nonatomic, getter = areGuestCanJoin) BOOL guestCanJoin;
@property (nonatomic) BOOL guestCanJoin;

/// Whether the room may be viewed by guest users without joining.
@property (nonatomic, getter = isWorldReadable) BOOL worldReadable;

/// The number of members joined to the room.
@property (nonatomic) NSInteger numJoinedMembers;

/// These are the edges of the graph. The objects in the array are complete (or stripped?) m.room.parent or m.space.child events.
@property (nonatomic, nullable) NSArray<MXEvent*> *childrenState;

@end

NS_ASSUME_NONNULL_END
4 changes: 3 additions & 1 deletion MatrixSDK/Space/MXSpaceChildSummaryResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ + (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary
MXJSONModelSetString(spaceChildSummaryResponse.name, JSONDictionary[@"name"]);
MXJSONModelSetString(spaceChildSummaryResponse.topic, JSONDictionary[@"topic"]);
MXJSONModelSetString(spaceChildSummaryResponse.avatarUrl, JSONDictionary[@"avatar_url"]);
MXJSONModelSetArray(spaceChildSummaryResponse.aliases, JSONDictionary[@"aliases"]);
MXJSONModelSetString(spaceChildSummaryResponse.canonicalAlias, JSONDictionary[@"canonical_alias"]);
MXJSONModelSetBoolean(spaceChildSummaryResponse.guestCanJoin, JSONDictionary[@"guest_can_join"]);
MXJSONModelSetBoolean(spaceChildSummaryResponse.worldReadable, JSONDictionary[@"world_readable"]);
MXJSONModelSetInteger(spaceChildSummaryResponse.numJoinedMembers, JSONDictionary[@"num_joined_members"]);
MXJSONModelSetUInteger(spaceChildSummaryResponse.creationTime, JSONDictionary[@"creation_ts"]);
MXJSONModelSetString(spaceChildSummaryResponse.joinRules, JSONDictionary[@"join_rules"]);
MXJSONModelSetMXJSONModelArray(spaceChildSummaryResponse.childrenState, MXEvent, JSONDictionary[@"children_state"]);
}

return spaceChildSummaryResponse;
Expand Down
4 changes: 0 additions & 4 deletions MatrixSDK/Space/MXSpaceChildrenResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#import <Foundation/Foundation.h>
#import "MXJSONModel.h"
#import "MXSpaceChildSummaryResponse.h"
#import "MXEvent.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -30,9 +29,6 @@ NS_ASSUME_NONNULL_BEGIN
/// Rooms information like name/avatar/type ...
@property (nonatomic, nullable) NSArray<MXSpaceChildSummaryResponse*>* rooms;

/// These are the edges of the graph. The objects in the array are complete (or stripped?) m.room.parent or m.space.child events.
@property (nonatomic, nullable) NSArray<MXEvent*>* events;

@end

NS_ASSUME_NONNULL_END
1 change: 0 additions & 1 deletion MatrixSDK/Space/MXSpaceChildrenResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ + (id)modelFromJSON:(NSDictionary *)JSONDictionary
{
MXJSONModelSetString(spaceChildrenResponse.nextBatch, JSONDictionary[@"next_batch"]);
MXJSONModelSetMXJSONModelArray(spaceChildrenResponse.rooms, MXSpaceChildSummaryResponse, JSONDictionary[@"rooms"]);
MXJSONModelSetMXJSONModelArray(spaceChildrenResponse.events, MXEvent, JSONDictionary[@"events"]);
}

return spaceChildrenResponse;
Expand Down
12 changes: 8 additions & 4 deletions MatrixSDK/Space/MXSpaceChildrenSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ public class MXSpaceChildrenSummary: NSObject {

// MARK - Properties

/// The queried space room summary
public let spaceSummary: MXRoomSummary
/// The queried space room summary. Can be nil in case of batched request
public let spaceInfo: MXSpaceChildInfo?

/// The child summaries of the queried space
public let childInfos: [MXSpaceChildInfo]

/// The token to supply in the `from` param of the next request in order to request more rooms. If this is absent, there are no more results.
public let nextBatch: String?

// MARK - Setup

init(spaceSummary: MXRoomSummary, childInfos: [MXSpaceChildInfo]) {
self.spaceSummary = spaceSummary
init(spaceInfo: MXSpaceChildInfo?, childInfos: [MXSpaceChildInfo], nextBatch: String?) {
self.spaceInfo = spaceInfo
self.childInfos = childInfos
self.nextBatch = nextBatch
}
}
Loading

0 comments on commit 3c99858

Please sign in to comment.