Skip to content

Commit 06f8740

Browse files
authored
Merge pull request #690 from zapcannon87/master
feat(im): disabling auto-binding current/default installation and client
2 parents 65bea5e + 87d1e63 commit 06f8740

File tree

5 files changed

+146
-27
lines changed

5 files changed

+146
-27
lines changed

AVOS/LeanCloudObjcTests/IMClientTestCase.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,44 @@ class IMClientTestCase: RTMBaseTestCase {
151151
NotificationCenter.default.removeObserver(observer)
152152
}
153153
}
154+
155+
func testDisableAutoBindingInstallation() {
156+
let installation = LCInstallation.default()
157+
let option = LCIMClientOption()
158+
option.isAutoBindingInstallationDisabled = true
159+
let client = try! LCIMClient(clientId: uuid, option: option)
160+
XCTAssertNil(client.installation)
161+
XCTAssertNil(client.currentDeviceToken)
162+
expecting { (exp) in
163+
client.open { (success, error) in
164+
XCTAssertTrue(success)
165+
XCTAssertNil(error)
166+
exp.fulfill()
167+
}
168+
}
169+
let deviceToken = uuid
170+
var observer: NSObjectProtocol?
171+
expecting(timeout: 5, expectation: {
172+
let exp = self.expectation(description: "will not upload deviceToken")
173+
exp.expectedFulfillmentCount = 1
174+
exp.isInverted = true
175+
return exp
176+
}) { (exp) in
177+
observer = NotificationCenter.default.addObserver(
178+
forName: NSNotification.Name(rawValue: "Test.LCIMClient.reportDeviceToken"),
179+
object: nil,
180+
queue: .main)
181+
{ (notification) in
182+
XCTAssertNil(notification.userInfo?["error"])
183+
exp.fulfill()
184+
}
185+
installation.setDeviceTokenHexString(deviceToken, teamId: "LeanCloud")
186+
}
187+
XCTAssertNil(client.installation)
188+
XCTAssertNil(client.currentDeviceToken)
189+
XCTAssertNotNil(observer)
190+
if let observer = observer {
191+
NotificationCenter.default.removeObserver(observer)
192+
}
193+
}
154194
}
155-
156-

AVOS/LeanCloudObjcTests/LCIMClientTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class LCIMClientTestCase: RTMBaseTestCase {
250250
XCTAssertTrue(installation1.save())
251251

252252
let delegator1 = LCIMClientDelegator.init()
253-
let client1: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, installation: installation1)
253+
let client1: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, option: nil, installation: installation1)
254254
XCTAssertNotNil(client1)
255255
client1.delegate = delegator1
256256
expecting { (exp) in
@@ -269,7 +269,7 @@ class LCIMClientTestCase: RTMBaseTestCase {
269269
XCTAssertTrue(installation2.save())
270270

271271
let delegator2 = LCIMClientDelegator.init()
272-
let client2: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, installation: installation2)
272+
let client2: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, option: nil, installation: installation2)
273273
XCTAssertNotNil(client2)
274274
client2.delegate = delegator2
275275

AVOS/Sources/Realtime/IM/Client/LCIMClient.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ NS_ASSUME_NONNULL_BEGIN
3636

3737
@end
3838

39+
/// The option of the client.
40+
@interface LCIMClientOption : NSObject
41+
42+
/// Set with `true` means disabling auto-binding current/default installation and client.
43+
@property (nonatomic) BOOL isAutoBindingInstallationDisabled;
44+
45+
@end
46+
3947
/// IM Client.
4048
@interface LCIMClient : NSObject
4149

@@ -75,6 +83,14 @@ NS_ASSUME_NONNULL_BEGIN
7583
- (nullable instancetype)initWithClientId:(NSString *)clientId
7684
error:(NSError * __autoreleasing *)error;
7785

86+
/// Initializing with an ID and an option.
87+
/// @param clientId The length of the ID should in range `[1, 64]`.
88+
/// @param option See `LCIMClientOption`.
89+
/// @param error Throws exception when error occurred.
90+
- (nullable instancetype)initWithClientId:(NSString *)clientId
91+
option:(LCIMClientOption * _Nullable)option
92+
error:(NSError * __autoreleasing *)error;
93+
7894
/// Initializing with an ID and a tag.
7995
/// @param clientId The length of the ID should in range `[1, 64]`.
8096
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
@@ -83,12 +99,30 @@ NS_ASSUME_NONNULL_BEGIN
8399
tag:(NSString * _Nullable)tag
84100
error:(NSError * __autoreleasing *)error;
85101

102+
/// Initializing with an ID, a tag and an option.
103+
/// @param clientId The length of the ID should in range `[1, 64]`.
104+
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
105+
/// @param option See `LCIMClientOption`.
106+
/// @param error Throws exception when error occurred.
107+
- (nullable instancetype)initWithClientId:(NSString *)clientId
108+
tag:(NSString * _Nullable)tag
109+
option:(LCIMClientOption * _Nullable)option
110+
error:(NSError * __autoreleasing *)error;
111+
86112
/// Initializing with an `LCUser`.
87113
/// @param user The user should have logged in.
88114
/// @param error Throws exception when error occurred.
89115
- (nullable instancetype)initWithUser:(LCUser *)user
90116
error:(NSError * __autoreleasing *)error;
91117

118+
/// Initializing with an `LCUser` and an option.
119+
/// @param user The user should have logged in.
120+
/// @param option See `LCIMClientOption`.
121+
/// @param error Throws exception when error occurred.
122+
- (nullable instancetype)initWithUser:(LCUser *)user
123+
option:(LCIMClientOption * _Nullable)option
124+
error:(NSError * __autoreleasing *)error;
125+
92126
/// Initializing with an `LCUser` and a tag.
93127
/// @param user The user should have logged in.
94128
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
@@ -97,6 +131,16 @@ NS_ASSUME_NONNULL_BEGIN
97131
tag:(NSString * _Nullable)tag
98132
error:(NSError * __autoreleasing *)error;
99133

134+
/// Initializing with an `LCUser`, a tag and an option.
135+
/// @param user The user should have logged in.
136+
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
137+
/// @param option See `LCIMClientOption`.
138+
/// @param error Throws exception when error occurred.
139+
- (nullable instancetype)initWithUser:(LCUser *)user
140+
tag:(NSString * _Nullable)tag
141+
option:(LCIMClientOption * _Nullable)option
142+
error:(NSError * __autoreleasing *)error;
143+
100144
// MARK: Open & Close
101145

102146
/// Open this client before using instant messaging service,

AVOS/Sources/Realtime/IM/Client/LCIMClient.m

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ - (instancetype)init {
4747

4848
@end
4949

50+
@implementation LCIMClientOption
51+
52+
@end
53+
5054
@implementation LCIMClient {
5155
LCIMClientStatus _status;
5256
}
@@ -75,48 +79,72 @@ - (instancetype)init
7579
- (instancetype)initWithClientId:(NSString *)clientId
7680
error:(NSError *__autoreleasing _Nullable *)error
7781
{
78-
return [self initWithClientId:clientId
79-
tag:nil
80-
error:error];
82+
return [self initWithClientId:clientId tag:nil error:error];
83+
}
84+
85+
- (instancetype)initWithClientId:(NSString *)clientId
86+
option:(LCIMClientOption *)option
87+
error:(NSError *__autoreleasing _Nullable *)error
88+
{
89+
return [self initWithClientId:clientId tag:nil option:option error:error];
90+
}
91+
92+
- (instancetype)initWithClientId:(NSString *)clientId
93+
tag:(NSString *)tag
94+
error:(NSError *__autoreleasing _Nullable *)error
95+
{
96+
return [self initWithClientId:clientId tag:tag option:nil error:error];
8197
}
8298

8399
- (instancetype)initWithClientId:(NSString *)clientId
84100
tag:(NSString *)tag
101+
option:(LCIMClientOption *)option
85102
error:(NSError *__autoreleasing _Nullable *)error
86103
{
87-
return [self initWithClientId:clientId
88-
tag:tag
89-
installation:[LCInstallation defaultInstallation]
90-
error:error];
104+
LCInstallation *installation = [LCInstallation defaultInstallation];
105+
return [self initWithClientId:clientId tag:tag option:option installation:installation error:error];
91106
}
92107

93108
- (instancetype)initWithUser:(LCUser *)user
94109
error:(NSError *__autoreleasing _Nullable *)error
95110
{
96-
return [self initWithUser:user
97-
tag:nil
98-
error:error];
111+
return [self initWithUser:user tag:nil error:error];
112+
}
113+
114+
- (instancetype)initWithUser:(LCUser *)user
115+
option:(LCIMClientOption *)option
116+
error:(NSError *__autoreleasing _Nullable *)error
117+
{
118+
return [self initWithUser:user tag:nil option:option error:error];
99119
}
100120

101121
- (instancetype)initWithUser:(LCUser *)user
102122
tag:(NSString *)tag
103123
error:(NSError *__autoreleasing _Nullable *)error
104124
{
105-
return [self initWithUser:user
106-
tag:tag
107-
installation:[LCInstallation defaultInstallation]
108-
error:error];
125+
return [self initWithUser:user tag:tag option:nil error:error];
126+
}
127+
128+
- (instancetype)initWithUser:(LCUser *)user
129+
tag:(NSString *)tag
130+
option:(LCIMClientOption *)option
131+
error:(NSError *__autoreleasing _Nullable *)error
132+
{
133+
LCInstallation *installation = [LCInstallation defaultInstallation];
134+
return [self initWithUser:user tag:tag option:option installation:installation error:error];
109135
}
110136

111137
- (instancetype)initWithClientId:(NSString *)clientId
112138
tag:(NSString *)tag
139+
option:(LCIMClientOption *)option
113140
installation:(LCInstallation *)installation
114141
error:(NSError *__autoreleasing _Nullable *)error
115142
{
116143
self = [super init];
117144
if (self) {
118145
NSError *err = [self doInitializationWithClientId:clientId
119146
tag:tag
147+
option:option
120148
installation:installation];
121149
if (err) {
122150
if (error) {
@@ -130,6 +158,7 @@ - (instancetype)initWithClientId:(NSString *)clientId
130158

131159
- (instancetype)initWithUser:(LCUser *)user
132160
tag:(NSString *)tag
161+
option:(LCIMClientOption *)option
133162
installation:(LCInstallation *)installation
134163
error:(NSError *__autoreleasing _Nullable *)error
135164
{
@@ -138,6 +167,7 @@ - (instancetype)initWithUser:(LCUser *)user
138167
_user = user;
139168
NSError *err = [self doInitializationWithClientId:user.objectId
140169
tag:tag
170+
option:option
141171
installation:installation];
142172
if (err) {
143173
if (error) {
@@ -151,6 +181,7 @@ - (instancetype)initWithUser:(LCUser *)user
151181

152182
- (NSError *)doInitializationWithClientId:(NSString *)clientId
153183
tag:(NSString *)tag
184+
option:(LCIMClientOption *)option
154185
installation:(LCInstallation *)installation
155186
{
156187
if (!clientId ||
@@ -212,14 +243,17 @@ - (NSError *)doInitializationWithClientId:(NSString *)clientId
212243
delegate:self
213244
queue:_internalSerialQueue];
214245
_conversationManager = [[LCIMClientInternalConversationManager alloc] initWithClient:self];
215-
_installation = installation;
216-
_currentDeviceToken = installation.deviceToken;
217-
[installation addObserver:self
218-
forKeyPath:keyPath(installation, deviceToken)
219-
options:(NSKeyValueObservingOptionNew |
220-
NSKeyValueObservingOptionOld |
221-
NSKeyValueObservingOptionInitial)
222-
context:(__bridge void *)(self)];
246+
BOOL isAutoBindingInstallationEnabled = (option ? !option.isAutoBindingInstallationDisabled : true);
247+
if (isAutoBindingInstallationEnabled) {
248+
_installation = installation;
249+
_currentDeviceToken = installation.deviceToken;
250+
[installation addObserver:self
251+
forKeyPath:keyPath(installation, deviceToken)
252+
options:(NSKeyValueObservingOptionNew |
253+
NSKeyValueObservingOptionOld |
254+
NSKeyValueObservingOptionInitial)
255+
context:(__bridge void *)(self)];
256+
}
223257
_conversationCache = ({
224258
LCIMConversationCache *cache = [[LCIMConversationCache alloc] initWithClientId:_clientId];
225259
cache.client = self;
@@ -1815,7 +1849,8 @@ - (void)removeAllConversationsInMemoryWith:(void (^)(void))callback
18151849
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
18161850
{
18171851
if (context == (__bridge void *)(self)) {
1818-
if ([keyPath isEqualToString:keyPath(self.installation, deviceToken)] &&
1852+
if (self.installation &&
1853+
[keyPath isEqualToString:keyPath(self.installation, deviceToken)] &&
18191854
object == self.installation) {
18201855
NSString *oldToken = [NSString _lc_decoding:change key:NSKeyValueChangeOldKey];
18211856
NSString *newToken = [NSString _lc_decoding:change key:NSKeyValueChangeNewKey];

AVOS/Sources/Realtime/IM/Client/LCIMClient_Internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ void assertContextOfQueue(dispatch_queue_t queue, BOOL isRunIn);
5757

5858
- (instancetype)initWithClientId:(NSString *)clientId
5959
tag:(NSString *)tag
60+
option:(LCIMClientOption *)option
6061
installation:(LCInstallation *)installation
6162
error:(NSError * __autoreleasing *)error;
6263

6364
- (instancetype)initWithUser:(LCUser *)user
6465
tag:(NSString *)tag
66+
option:(LCIMClientOption *)option
6567
installation:(LCInstallation *)installation
6668
error:(NSError * __autoreleasing *)error;
6769

0 commit comments

Comments
 (0)