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

Introduce userId as uuid replacement #436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 18 additions & 11 deletions PubNub/Core/PubNub+Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ NS_ASSUME_NONNULL_BEGIN
- (PNConfiguration *)currentConfiguration;

/**
* @brief Retrieve UUID which has been used during client initialization.
* @brief Retrieve unique identifier to be used as a device identifier.
*
* @return User-provided or generated unique user identifier.
* @return User-provided unique identifier to be used as a device identifier.
*/
- (NSString *)uuid;
- (NSString *)uuid DEPRECATED_MSG_ATTRIBUTE("use 'userId' instead.");

/**
* @brief Retrieve unique identifier to be used as a device identifier.
*
* @return User-provided unique identifier to be used as a device identifier.
*/
- (NSString *)userId;


#pragma mark - Initialization
Expand Down Expand Up @@ -121,14 +128,14 @@ NS_ASSUME_NONNULL_BEGIN
* @brief Make copy of client with it's current state using new configuration.
*
* @discussion Allow to retrieve reference on client which will have same state as receiver, but
* will use updated configuration. If authorization and/or uuid keys has been changed while
* subscribed, this method will trigger \c leave presence event on behalf of current uuid and
* will use updated configuration. If authorization and/or userId keys has been changed while
* subscribed, this method will trigger \c leave presence event on behalf of current userId and
* subscribe using new one.
*
* @note Copy will be returned asynchronous, because some operations may require communication with
* \b PubNub network (like switching active \c uuid while subscribed).
* \b PubNub network (like switching active \c userId while subscribed).
*
* @note Re-subscription with new \c uuid will be done using catchup and all messages which has been
* @note Re-subscription with new \c userId will be done using catchup and all messages which has been
* sent while client changed configuration will be handled.
*
* @note All listeners will be copied to new client.
Expand All @@ -155,14 +162,14 @@ NS_ASSUME_NONNULL_BEGIN
* @brief Make copy of client with it's current state using new configuration.
*
* @discussion Allow to retrieve reference on client which will have same state as receiver, but
* will use updated configuration. If authorization and/or uuid keys has been changed while
* subscribed, this method will trigger \c leave presence event on behalf of current uuid and
* will use updated configuration. If authorization and/or userId keys has been changed while
* subscribed, this method will trigger \c leave presence event on behalf of current userId and
* subscribe using new one.
*
* @note Copy will be returned asynchronous, because some operations may require communication with
* \b PubNub network (like switching active \c uuid while subscribed).
* \b PubNub network (like switching active \c userId while subscribed).
*
* @note Re-subscription with new \c uuid will be done using catchup and all messages which has been
* @note Re-subscription with new \c userId will be done using catchup and all messages which has been
* sent while client changed configuration will be handled.
* @note All listeners will be copied to new client.
*
Expand Down
24 changes: 14 additions & 10 deletions PubNub/Core/PubNub+Core.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ - (Class)errorStatusClassForRequest:(PNRequest *)request;
/**
* @brief Store provided unique user identifier in keychain.
*
* @param uuid Unique user identifier which has been provided with \b PNConfiguration instance.
* @param userId Unique user identifier which has been provided with \b PNConfiguration instance.
* @param identifier Account publish or subscribe key which has been provided with \c PNConfiguration instance.
*
* @since 4.15.3
*/
- (void)storeUUID:(NSString *)uuid forIdentifier:(NSString *)identifier;
- (void)storeUserId:(NSString *)userId forIdentifier:(NSString *)identifier;

/**
* @brief Create and configure \b PubNub client logger instance.
Expand Down Expand Up @@ -240,7 +240,11 @@ - (PNConfiguration *)currentConfiguration {
}

- (NSString *)uuid {
return self.configuration.uuid;
return self.userId;
}

- (NSString *)userId {
return self.configuration.userId;
}


Expand All @@ -265,7 +269,7 @@ + (instancetype)clientWithConfiguration:(PNConfiguration *)configuration callbac
- (instancetype)initWithConfiguration:(PNConfiguration *)configuration callbackQueue:(dispatch_queue_t)callbackQueue {
if ((self = [super init])) {
NSString *storageIdentifier = configuration.publishKey ?: configuration.subscribeKey;
[self storeUUID:configuration.uuid forIdentifier:storageIdentifier];
[self storeUserId:configuration.userId forIdentifier:storageIdentifier];
[self setupClientLogger];

PNLogClientInfo(self.logger, @"<PubNub> PubNub SDK %@ (%@)", kPNLibraryVersion, kPNCommit);
Expand Down Expand Up @@ -351,13 +355,13 @@ - (void)copyWithConfiguration:(PNConfiguration *)configuration
// Stop any interactions on subscription loop.
[self cancelSubscribeOperations];

BOOL uuidChanged = ![configuration.uuid isEqualToString:self.configuration.uuid];
BOOL userIdChanged = ![configuration.userId isEqualToString:self.configuration.userId];
BOOL authKeyChanged = ((self.configuration.authKey && !configuration.authKey) ||
(!self.configuration.authKey && configuration.authKey) ||
(configuration.authKey && self.configuration.authKey &&
![configuration.authKey isEqualToString:self.configuration.authKey]));

if (uuidChanged || authKeyChanged) {
if (userIdChanged || authKeyChanged) {
[self unsubscribeFromChannels:self.subscriberManager.channels
groups:self.subscriberManager.channelGroups
withPresence:YES
Expand Down Expand Up @@ -554,7 +558,7 @@ - (void)prepareRequiredParameters {
@"{pub-key}": (self.configuration.publishKey?: @"")};

NSMutableDictionary *queryComponents = [@{
@"uuid": [PNString percentEscapedString:(self.configuration.uuid?: @"")],
@"uuid": [PNString percentEscapedString:(self.configuration.userId?: @"")],
@"deviceid": (self.configuration.deviceID?: @""),
@"instanceid": self.instanceID,
@"pnsdk":[NSString stringWithFormat:@"PubNub-%@%%2F%@", kPNClientName, kPNLibraryVersion]
Expand All @@ -581,7 +585,7 @@ - (NSInteger)packetSizeForOperation:(PNOperationType)operationType

- (void)appendClientInformation:(PNResult *)result {
result.TLSEnabled = self.configuration.isTLSEnabled;
result.uuid = self.configuration.uuid;
result.userId = self.configuration.userId;
result.authKey = self.configuration.authToken ?: self.configuration.authKey;
result.origin = self.configuration.origin;
}
Expand Down Expand Up @@ -678,9 +682,9 @@ - (Class)errorStatusClassForRequest:(PNRequest *)request {
return class;
}

- (void)storeUUID:(NSString *)uuid forIdentifier:(NSString *)identifier {
- (void)storeUserId:(NSString *)userId forIdentifier:(NSString *)identifier {
id<PNKeyValueStorage> storage = [PNDataStorage persistentClientDataWithIdentifier:identifier];
[storage storeValue:uuid forKey:kPNConfigurationUUIDKey];
[storage storeValue:userId forKey:kPNConfigurationUserIdKey];
}

- (void)setupClientLogger {
Expand Down
14 changes: 7 additions & 7 deletions PubNub/Core/PubNub+Objects.m
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ - (void)sendFetchChannelMembersRequestUsingBuilderParameters:(NSDictionary *)par
- (void)setUUIDMetadataWithRequest:(PNSetUUIDMetadataRequest *)request
completion:(PNSetUUIDMetadataCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request withCompletion:^(PNSetUUIDMetadataStatus *status) {
Expand All @@ -535,7 +535,7 @@ - (void)setUUIDMetadataWithRequest:(PNSetUUIDMetadataRequest *)request
- (void)removeUUIDMetadataWithRequest:(PNRemoveUUIDMetadataRequest *)request
completion:(PNRemoveUUIDMetadataCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request withCompletion:^(PNAcknowledgmentStatus *status) {
Expand All @@ -552,7 +552,7 @@ - (void)removeUUIDMetadataWithRequest:(PNRemoveUUIDMetadataRequest *)request
- (void)uuidMetadataWithRequest:(PNFetchUUIDMetadataRequest *)request
completion:(PNFetchUUIDMetadataCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request
Expand Down Expand Up @@ -662,7 +662,7 @@ - (void)allChannelsMetadataWithRequest:(PNFetchAllChannelsMetadataRequest *)requ
- (void)setMembershipsWithRequest:(PNSetMembershipsRequest *)request
completion:(nullable PNManageMembershipsCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request withCompletion:^(PNManageMembershipsStatus *status) {
Expand All @@ -679,7 +679,7 @@ - (void)setMembershipsWithRequest:(PNSetMembershipsRequest *)request
- (void)removeMembershipsWithRequest:(PNRemoveMembershipsRequest *)request
completion:(PNManageMembershipsCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request withCompletion:^(PNManageMembershipsStatus *status) {
Expand All @@ -696,7 +696,7 @@ - (void)removeMembershipsWithRequest:(PNRemoveMembershipsRequest *)request
- (void)manageMembershipsWithRequest:(PNManageMembershipsRequest *)request
completion:(PNManageMembershipsCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request withCompletion:^(PNManageMembershipsStatus *status) {
Expand All @@ -713,7 +713,7 @@ - (void)manageMembershipsWithRequest:(PNManageMembershipsRequest *)request
- (void)membershipsWithRequest:(PNFetchMembershipsRequest *)request
completion:(PNFetchMembershipsCompletionBlock)block {

request.identifier = request.identifier.length ? request.identifier : self.configuration.uuid;
request.identifier = request.identifier.length ? request.identifier : self.configuration.userId;
__weak __typeof(self) weakSelf = self;

[self performRequest:request
Expand Down
2 changes: 1 addition & 1 deletion PubNub/Core/PubNub+PAM.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @implementation PubNub (PAM)
#pragma mark - PAM

- (PNPAMToken *)parseAuthToken:(NSString *)token {
return [PNPAMToken tokenFromBase64String:token forUUID:self.configuration.uuid];
return [PNPAMToken tokenFromBase64String:token forUUID:self.configuration.userId];
}

- (void)setAuthToken:(NSString *)token {
Expand Down
8 changes: 4 additions & 4 deletions PubNub/Core/PubNub+State.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ NS_ASSUME_NONNULL_BEGIN
* group).
*
* @code
* [self.client setState:@{ @"state": @"online" } forUUID:self.client.uuid onChannel:@"chat"
* [self.client setState:@{ @"state": @"online" } forUUID:self.client.userId onChannel:@"chat"
* withCompletion:^(PNClientStateUpdateStatus *status) {
*
* if (!status.isError) {
Expand Down Expand Up @@ -78,7 +78,7 @@ NS_ASSUME_NONNULL_BEGIN
* @brief Modify state information for \c uuid on specified channel group.
*
* @code
* [self.client setState:@{ @"announcement": @"New red is blue" } forUUID:self.client.uuid
* [self.client setState:@{ @"announcement": @"New red is blue" } forUUID:self.client.userId
* onChannelGroup:@"system" withCompletion:^(PNClientStateUpdateStatus *status) {
*
* if (!status.isError) {
Expand Down Expand Up @@ -112,7 +112,7 @@ NS_ASSUME_NONNULL_BEGIN
* @brief Retrieve state information for \c uuid on specified channel.
*
* @code
* [self.client stateForUUID:self.client.uuid onChannel:@"chat"
* [self.client stateForUUID:self.client.userId onChannel:@"chat"
* withCompletion:^(PNChannelClientStateResult *result, PNErrorStatus *status) {
*
* if (!status.isError) {
Expand Down Expand Up @@ -141,7 +141,7 @@ NS_ASSUME_NONNULL_BEGIN
* @brief Retrieve state information for \c uuid on specified channel group.
*
* @code
* [self.client stateForUUID:self.client.uuid onChannelGroup:@"system"
* [self.client stateForUUID:self.client.userId onChannelGroup:@"system"
* withCompletion:^(PNChannelGroupClientStateResult *result, PNErrorStatus *status) {
*
* if (!status.isError) {
Expand Down
4 changes: 2 additions & 2 deletions PubNub/Core/PubNub+State.m
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ - (void)handleSetStateStatus:(PNClientStateUpdateStatus *)status
groups:(NSArray<NSString *> *)groups
withCompletion:(PNSetStateCompletionBlock)block {

if (status && !status.isError && [uuid isEqualToString:self.configuration.uuid]) {
if (status && !status.isError && [uuid isEqualToString:self.configuration.userId]) {
NSDictionary *state = status.data.state ?: @{};

[self.clientStateManager setState:state forObjects:channels];
Expand All @@ -405,7 +405,7 @@ - (void)handleStateResult:(id)result
fromBuilder:(BOOL)apiCallBuilder
withCompletion:(id)block {

if (result && [uuid isEqualToString:self.configuration.uuid]) {
if (result && [uuid isEqualToString:self.configuration.userId]) {
NSDictionary *state = @{};

if (!apiCallBuilder) {
Expand Down
2 changes: 1 addition & 1 deletion PubNub/Data/Managers/PNSubscriber.m
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ - (void)handleNewPresenceEvent:(PNPresenceEventResult *)data {
// In case of state modification event for current client it should be applied on local storage.
if ([data.data.presenceEvent isEqualToString:@"state-change"]) {
// Check whether state has been changed for current client or not.
if ([data.data.presence.uuid isEqualToString:self.client.configuration.uuid]) {
if ([data.data.presence.uuid isEqualToString:self.client.configuration.userId]) {
[self.client.clientStateManager setState:data.data.presence.state forObjects:@[data.data.channel]];
}
}
Expand Down
53 changes: 43 additions & 10 deletions PubNub/Data/PNConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ NS_ASSUME_NONNULL_BEGIN
* \b PubNub network.
*
* @author Sergey Mamontov
* @version 5.2.0
* @since 4.0
* @copyright © 2010-2018 PubNub, Inc.
* @copyright © 2010-2022PubNub, Inc.
*/
@interface PNConfiguration : NSObject

Expand Down Expand Up @@ -57,8 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, nullable, copy) NSString *authKey;

/**
* @brief Unique client identifier used to identify concrete client user from another which
* currently use \b PubNub services.
* @brief The unique identifier to be used as a device identifier..
*
* @discussion This value is different from \c authKey (which is used only by \b PAM) and represent
* concrete client across server. This identifier is used for presence events to tell what some
Expand All @@ -70,7 +70,25 @@ NS_ASSUME_NONNULL_BEGIN
*
* @default Client will use it's own-generated value if won't be specified by user.
*/
@property (nonatomic, copy, setter = setUUID:) NSString *uuid;
@property (nonatomic, copy, setter = setUUID:) NSString *uuid
DEPRECATED_MSG_ATTRIBUTE("use 'userId' instead.");

/**
* @brief The unique identifier to be used as a device identifier.
*
* @discussion This value is different from \c authKey (which is used only by \b PAM) and represent
* concrete client across server. This identifier is used for presence events to tell what some
* client joined or leaved live feed.
*
* @warning There can't be two same client identifiers online at the same time.
*
* @throw Exception in case if \c userId is empty string.
*
* @default Client will use it's own-generated value if won't be specified by user.
*
* @since 5.2.0
*/
@property (nonatomic, copy, setter = setUserId:) NSString *userId;

/**
* @brief Data encryption key.
Expand Down Expand Up @@ -301,11 +319,9 @@ NS_ASSUME_NONNULL_BEGIN
/**
* @brief Construct configuration instance using minimal required data.
*
* @param publishKey Key which allow client to use data push API.
* @param subscribeKey Key which allow client to subscribe on live feeds pushed from \b PubNub
* service.
* @param uuid Unique client identifier used to identify concrete client user from another which
* currently use \b PubNub services.
* @param publishKey The PubNub Publish Key to be used when publishing data to a channel
* @param subscribeKey The PubNub Subscribe Key to be used when getting data from a channel
* @param uuid The unique identifier to be used as a device identifier.
*
* @throw Exception in case if \c uuid is empty string.
*
Expand All @@ -314,7 +330,24 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)configurationWithPublishKey:(NSString *)publishKey
subscribeKey:(NSString *)subscribeKey
uuid:(NSString *)uuid
NS_SWIFT_NAME(init(publishKey:subscribeKey:uuid:));
NS_SWIFT_NAME(init(publishKey:subscribeKey:uuid:))
DEPRECATED_MSG_ATTRIBUTE("use 'configurationWithPublishKey:subscribeKey:userId:' instead.");

/**
* @brief Construct configuration instance using minimal required information.
*
* @param publishKey The \b PubNub Publish Key to be used when publishing data to a channel
* @param subscribeKey The \b PubNub Subscribe Key to be used when getting data from a channel
* @param userId The unique identifier to be used as a device identifier.
*
* @throw Exception in case if \c userId is empty string.
*
* @return Configured and ready to se configuration instance.
*/
+ (instancetype)configurationWithPublishKey:(NSString *)publishKey
subscribeKey:(NSString *)subscribeKey
userId:(NSString *)userId
NS_SWIFT_NAME(init(publishKey:subscribeKey:userId:));

#pragma mark -

Expand Down
Loading