Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for MSC3881 #1590

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions MatrixSDK/Contrib/Swift/JSONModels/MXJSONModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public enum MXLoginFlowType: Equatable, Hashable {
public enum MXPusherKind: Equatable, Hashable {
case http, none, custom(String)

public init(value: String?) {
guard let value = value else {
self = .none
return
}

self = value == "http" ? .http : .custom(value)
}

public var objectValue: NSObject {
switch self {
case .http: return "http" as NSString
Expand Down
19 changes: 16 additions & 3 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,15 @@ public extension MXRestClient {
- profileTag: The profile tag for this device. Identifies this device in push rules.
- lang: The user's preferred language for push, eg. 'en' or 'en-US'
- data: Dictionary of data as required by your push gateway (generally the notification URI and aps-environment for APNS).
- append: If `true`, the homeserver should add another pusher with the given pushkey and App ID in addition to any others with different user IDs.
- enabled: Whether the pusher should actively create push notifications
- completion: A block object called when the operation succeeds.
- response: indicates whether the request succeeded or not.

- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func setPusher(pushKey: String, kind: MXPusherKind, appId: String, appDisplayName: String, deviceDisplayName: String, profileTag: String, lang: String, data: [String: Any], append: Bool, completion: @escaping (_ response: MXResponse<Void>) -> Void) -> MXHTTPOperation {
return __setPusherWithPushkey(pushKey, kind: kind.objectValue, appId: appId, appDisplayName: appDisplayName, deviceDisplayName: deviceDisplayName, profileTag: profileTag, lang: lang, data: data, append: append, success: currySuccess(completion), failure: curryFailure(completion))
@nonobjc @discardableResult func setPusher(pushKey: String, kind: MXPusherKind, appId: String, appDisplayName: String, deviceDisplayName: String, profileTag: String, lang: String, data: [String: Any], append: Bool, enabled: Bool = true, completion: @escaping (_ response: MXResponse<Void>) -> Void) -> MXHTTPOperation {
return __setPusherWithPushkey(pushKey, kind: kind.objectValue, appId: appId, appDisplayName: appDisplayName, deviceDisplayName: deviceDisplayName, profileTag: profileTag, lang: lang, data: data, append: append, enabled: enabled, success: currySuccess(completion), failure: curryFailure(completion))
}
// TODO: setPusherWithPushKey - futher refinement
/*
Expand All @@ -392,7 +394,18 @@ public extension MXRestClient {
Something like "MXPusherDescriptor"?
*/


/**
Gets all currently active pushers for the authenticated user.

- parameters:
- response: indicates whether the request succeeded or not.

- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func pushers(completion: @escaping (_ response: MXResponse<[MXPusher]>) -> Void) -> MXHTTPOperation {
return __pushers(currySuccess(completion), failure: curryFailure(completion))
}

/**
Get all push notifications rules.

Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/JSONModels/MXMatrixVersions.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ extern const struct MXMatrixVersionsFeatureStruct MXMatrixVersionsFeature;
*/
@property (nonatomic, readonly) BOOL supportsThreads;

/**
Indicate if the server supports Remotely toggling push notifications via MSC3881.
*/
@property (nonatomic, readonly) BOOL supportsRemotelyTogglingPushNotifications;

@end

NS_ASSUME_NONNULL_END
7 changes: 7 additions & 0 deletions MatrixSDK/JSONModels/MXMatrixVersions.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

// Unstable features
static NSString* const kJSONKeyMSC3440 = @"org.matrix.msc3440.stable";
static NSString* const kJSONKeyMSC3881Unstable = @"org.matrix.msc3881";
static NSString* const kJSONKeyMSC3881 = @"org.matrix.msc3881.stable";

@interface MXMatrixVersions ()

Expand Down Expand Up @@ -115,6 +117,11 @@ - (BOOL)supportsThreads
return [self serverSupportsFeature:kJSONKeyMSC3440];
}

- (BOOL)supportsRemotelyTogglingPushNotifications
{
return [self serverSupportsFeature:kJSONKeyMSC3881] || [self serverSupportsFeature:kJSONKeyMSC3881Unstable];
}

#pragma mark - Private

- (BOOL)serverSupportsVersion:(NSString *)version
Expand Down
13 changes: 13 additions & 0 deletions MatrixSDK/JSONModels/Push/MXPusher.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#import "MXJSONModel.h"
#import "MXPusherData.h"

FOUNDATION_EXPORT NSString *const kMXPusherEnabledKey;
FOUNDATION_EXPORT NSString *const kMXPusherDeviceIdKey;

NS_ASSUME_NONNULL_BEGIN

@interface MXPusher : MXJSONModel
Expand Down Expand Up @@ -63,6 +66,16 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, readonly) MXPusherData *data;

/**
Optional: Whether the pusher should actively create push notifications. default YES if `nil`
*/
@property (nonatomic, nullable, readonly) NSNumber *enabled;

/**
Optional: The device_id of the session that registered the pusher.
*/
@property (nonatomic, nullable, readonly) NSString *deviceId;

@end

NS_ASSUME_NONNULL_END
13 changes: 12 additions & 1 deletion MatrixSDK/JSONModels/Push/MXPusher.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@

#import "MXPusher.h"

NSString *const kMXPusherEnabledKey = @"org.matrix.msc3881.enabled";
NSString *const kMXPusherDeviceIdKey = @"org.matrix.msc3881.device_id";

@implementation MXPusher

+ (id)modelFromJSON:(NSDictionary *)JSONDictionary
{
MXPusher *pusher;

NSString *pushkey, *kind, *appId, *appDisplayName, *deviceDisplayName, *profileTag, *lang;
NSString *pushkey, *kind, *appId, *appDisplayName, *deviceDisplayName, *profileTag, *lang, *deviceId;
NSNumber *enabled;
MXJSONModelSetString(pushkey, JSONDictionary[@"pushkey"]);
MXJSONModelSetString(kind, JSONDictionary[@"kind"]);
MXJSONModelSetString(appId, JSONDictionary[@"app_id"]);
MXJSONModelSetString(appDisplayName, JSONDictionary[@"app_display_name"]);
MXJSONModelSetString(deviceDisplayName, JSONDictionary[@"device_display_name"]);
MXJSONModelSetString(profileTag, JSONDictionary[@"profile_tag"]);
MXJSONModelSetString(lang, JSONDictionary[@"lang"]);
if (JSONDictionary[kMXPusherEnabledKey])
{
MXJSONModelSetNumber(enabled, JSONDictionary[kMXPusherEnabledKey]);
}
MXJSONModelSetString(deviceId, JSONDictionary[kMXPusherDeviceIdKey]);

MXPusherData *data;
MXJSONModelSetMXJSONModel(data, MXPusherData, JSONDictionary[@"data"]);
Expand All @@ -45,6 +54,8 @@ + (id)modelFromJSON:(NSDictionary *)JSONDictionary
pusher->_profileTag = profileTag;
pusher->_lang = lang;
pusher->_data = data;
pusher->_enabled = enabled;
pusher->_deviceId = deviceId;
}

return pusher;
Expand Down
33 changes: 32 additions & 1 deletion MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ NS_REFINED_FOR_SWIFT;
@param profileTag The profile tag for this device. Identifies this device in push rules.
@param lang The user's preferred language for push, eg. 'en' or 'en-US'
@param data Dictionary of data as required by your push gateway (generally the notification URI and aps-environment for APNS).
@param append If true, the homeserver should add another pusher with the given pushkey and App ID in addition to any others with different user IDs.
@param success A block object called when the operation succeeds. It provides credentials to use to create a MXRestClient.
@param failure A block object called when the operation fails.

Expand All @@ -748,6 +749,36 @@ NS_REFINED_FOR_SWIFT;
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

/**
Update the pusher for this device on the Home Server.

@param pushkey The pushkey for this pusher. This should be the APNS token formatted as required for your push gateway (base64 is the recommended formatting).
@param kind The kind of pusher your push gateway requires. Generally 'http', or an NSNull to disable the pusher.
@param appId The app ID of this application as required by your push gateway.
@param appDisplayName A human readable display name for this app.
@param deviceDisplayName A human readable display name for this device.
@param profileTag The profile tag for this device. Identifies this device in push rules.
@param lang The user's preferred language for push, eg. 'en' or 'en-US'
@param data Dictionary of data as required by your push gateway (generally the notification URI and aps-environment for APNS).
@param append If true, the homeserver should add another pusher with the given pushkey and App ID in addition to any others with different user IDs.
@param enabled Whether the pusher should actively create push notifications
@param success A block object called when the operation succeeds. It provides credentials to use to create a MXRestClient.
@param failure A block object called when the operation fails.

@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)setPusherWithPushkey:(NSString *)pushkey
kind:(NSObject *)kind
appId:(NSString *)appId
appDisplayName:(NSString *)appDisplayName
deviceDisplayName:(NSString *)deviceDisplayName
profileTag:(NSString *)profileTag
lang:(NSString *)lang
data:(NSDictionary *)data
append:(BOOL)append
enabled:(BOOL)enabled
success:(void (^)(void))success
failure:(void (^)(NSError *))failure NS_REFINED_FOR_SWIFT;

/**
Gets all currently active pushers for the authenticated user.
Expand All @@ -758,7 +789,7 @@ NS_REFINED_FOR_SWIFT;
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)pushers:(void (^)(NSArray<MXPusher *> *pushers))success
failure:(void (^)(NSError *))failure;
failure:(void (^)(NSError *))failure NS_REFINED_FOR_SWIFT;

/**
Get all push notifications rules.
Expand Down
32 changes: 30 additions & 2 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,33 @@ - (MXHTTPOperation*)setPusherWithPushkey:(NSString *)pushkey
append:(BOOL)append
success:(void (^)(void))success
failure:(void (^)(NSError *))failure
{
return [self setPusherWithPushkey:pushkey
kind:kind
appId:appId
appDisplayName:appDisplayName
deviceDisplayName:deviceDisplayName
profileTag:profileTag
lang:lang
data:data
append:append
enabled:YES
success:success
failure:failure];
}

- (MXHTTPOperation*)setPusherWithPushkey:(NSString *)pushkey
kind:(NSObject *)kind
appId:(NSString *)appId
appDisplayName:(NSString *)appDisplayName
deviceDisplayName:(NSString *)deviceDisplayName
profileTag:(NSString *)profileTag
lang:(NSString *)lang
data:(NSDictionary *)data
append:(BOOL)append
enabled:(BOOL)enabled
success:(void (^)(void))success
failure:(void (^)(NSError *))failure
{
// sanity check
if (!pushkey || !kind || !appDisplayName || !deviceDisplayName || !profileTag || !lang || !data)
Expand All @@ -1558,6 +1585,7 @@ - (MXHTTPOperation*)setPusherWithPushkey:(NSString *)pushkey
@"profile_tag": profileTag,
@"lang": lang,
@"data": data,
kMXPusherEnabledKey: @(enabled),
@"append":[NSNumber numberWithBool:append]
};

Expand Down Expand Up @@ -1591,7 +1619,7 @@ - (MXHTTPOperation*)pushers:(void (^)(NSArray<MXPusher *> *pushers))success
[self dispatchProcessing:^{
MXJSONModelSetMXJSONModelArray(pushers, MXPusher, JSONResponse[@"pushers"]);
} andCompletion:^{
success(pushers);
success(pushers ?: @[]);
}];
}
}
Expand Down Expand Up @@ -3200,7 +3228,7 @@ - (MXHTTPOperation*)roomSummaryWith:(NSString*)roomIdOrAlias
success:(void (^)(MXPublicRoom *room))success
failure:(void (^)(NSError *error))failure
{
NSMutableString *path = [NSMutableString stringWithFormat:@"%@/im.nheko.summary/rooms/%@/summary", kMXAPIPrefixPathUnstable, roomIdOrAlias];
NSMutableString *path = [NSMutableString stringWithFormat:@"%@/im.nheko.summary/rooms/%@/summary", kMXAPIPrefixPathUnstable, [MXTools encodeURIComponent:roomIdOrAlias]];
gileluard marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < via.count; i++) {
[path appendFormat:@"%@via=%@", i == 0 ? @"?" : @"&", via[i]];
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/6787.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User sessions: Add support for MSC3881