Skip to content

Commit

Permalink
Add userId as a supported configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho authored Jul 25, 2022
1 parent 3d7a18b commit 17617e1
Show file tree
Hide file tree
Showing 82 changed files with 1,090 additions and 1,064 deletions.
10 changes: 7 additions & 3 deletions lib/pubnub/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ class Client
# <dd><b>optional.</b> Required to encrypt messages.</dd>
#
# <dt>uuid</dt>
# <dd><b>optional.</b> Sets given uuid as client uuid, does not generates random uuid on init as usually.</dd>
# <dd><b>optional.</b> <b>Deprecated.</b> Sets given uuid as client uuid, does not generates random uuid on init as usually</dd>
#
# <dt>user_id</dt>
# <dd><b>required.</b> Sets given user_id as client user_id.</dd>
#
# <dt>origin</dt>
# <dd><b>optional.</b> Specifies the fully qualified domain name of the PubNub origin.
Expand Down Expand Up @@ -155,7 +158,7 @@ class Client
# publish_key: :demo,
# secret_key: :secret,
# cipher_key: :other_secret,
# uuid: :mad_max,
# user_id: :mad_max,
# origin: 'custom.pubnub.com',
# callback: ->(envelope) { puts envelope.message },
# connect_callback: ->(message) { puts message },
Expand Down Expand Up @@ -289,7 +292,7 @@ def kill_request_dispatcher(origin, event_type)

def sequence_number_for_publish!
@env[:sequence_number_for_publish] += 1
@env[:sequence_number_for_publish] % 2**32
@env[:sequence_number_for_publish] % 2 ** 32
end

def apply_state(event)
Expand Down Expand Up @@ -379,6 +382,7 @@ def setup_app(options)
Pubnub.logger = options[:logger] || Logger.new('pubnub.log')
Concurrent.global_logger = Pubnub.logger
@subscriber = Subscriber.new(self)
options[:user_id] = options[:uuid] if options[:user_id].nil?
@env = options
end

Expand Down
44 changes: 38 additions & 6 deletions lib/pubnub/client/getters_setters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Client
# Module that holds some getters and setters
module GettersSetters

extend Gem::Deprecate

def sdk_version
"PubNub-Ruby/#{Pubnub::VERSION}"
end
Expand All @@ -15,7 +17,7 @@ def sdk_version
# ===========
# <dl>
# <dt>uuid</dt>
# <dd>New uuid to be set.</dd>
# <dd>New uuid to be set. Note that this will override user_id value</dd>
# </dl>
#
# Returns:
Expand All @@ -26,16 +28,38 @@ def sdk_version
# ==============
# Can't change uuid while subscribed. You have to leave every subscribed channel.
def change_uuid(uuid)
Pubnub.logger.debug('Pubnub::Client') { 'Changing uuid' }
raise('Cannot change UUID while subscribed.') if subscribed?
Validator::Client.validate_uuid uuid
change_user_id(uuid)
end
deprecate :change_uuid, :change_user_id, 2023, 1

@env[:uuid] = uuid
# Parameters:
# ===========
# <dl>
# <dt>user_id</dt>
# <dd>New user_id to be set. Note that this will override uuid value</dd>
# </dl>
#
# Returns:
# ========
# New user_id.
#
# Functionality:
# ==============
# Can't change user_id while subscribed. You have to leave every subscribed channel.
def change_user_id(user_id)
Pubnub.logger.debug('Pubnub::Client') { 'Changing user_id' }
raise('Cannot change user_id while subscribed.') if subscribed?
Validator::Client.validate_user_id user_id

@env[:user_id] = user_id
end

alias session_uuid= change_uuid
alias uuid= change_uuid
alias set_uuid= change_uuid
alias session_user_id= change_user_id
alias user_id= change_user_id
alias set_user_id= change_user_id

# Returns:
# ========
Expand Down Expand Up @@ -97,7 +121,15 @@ def timetoken=(timetoken)
# ========
# Current uuid.
def uuid
@env[:uuid]
user_id
end
deprecate :uuid, :user_id, 2023, 1

# Returns:
# ========
# Current user_id.
def user_id
@env[:user_id]
end

# Returns:
Expand Down
4 changes: 2 additions & 2 deletions lib/pubnub/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def parameters(_set_signature = false)
token = @app.env[:token]
empty_if_blank = {
auth: token ? token : @auth_key,
uuid: @app.env[:uuid],
uuid: @app.user_id,
@telemetry_name => @current_telemetry
}

Expand Down Expand Up @@ -223,7 +223,7 @@ def error_message(parsed_response)
def get_config
{
tls: @app.env[:ssl],
uuid: @app.env[:uuid],
uuid: @app.user_id,
auth_key: @app.env[:auth_key],
origin: @app.current_origin
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/get_memberships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GetMemberships < SingleEvent
def initialize(options, app)
@event = current_operation
@telemetry_name = :l_obj
@uuid = options[:uuid].nil? ? app.env[:uuid] : options[:uuid]
@uuid = options[:uuid].nil? ? app.user_id : options[:uuid]
@limit = [options[:limit], 100].min unless options[:limit].nil?
@sort = options[:sort].join(",") if options[:sort] && !options[:sort].empty?
@filter = options[:filter] if options[:filter] && !options[:filter].empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/get_uuid_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GetUuidMetadata < SingleEvent
def initialize(options, app)
@event = current_operation
@telemetry_name = :l_obj
@uuid = options[:uuid].nil? ? app.env[:uuid] : options[:uuid]
@uuid = options[:uuid].nil? ? app.user_id : options[:uuid]

if options[:include]
@include = "custom" unless [0, '0', false].include?(options[:include][:custom])
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/remove_memberships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RemoveMemberships < SingleEvent
def initialize(options, app)
@event = current_operation
@telemetry_name = :l_obj
@uuid = options[:uuid].nil? ? app.env[:uuid] : options[:uuid]
@uuid = options[:uuid].nil? ? app.user_id : options[:uuid]
@limit = [options[:limit], 100].min unless options[:limit].nil?
@sort = options[:sort].join(",") if options[:sort] && !options[:sort].empty?
@filter = options[:filter] if options[:filter] && !options[:filter].empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/remove_uuid_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RemoveUuidMetadata < SingleEvent
def initialize(options, app)
@event = current_operation
@telemetry_name = :l_obj
@uuid = options[:uuid].nil? ? app.env[:uuid] : options[:uuid]
@uuid = options[:uuid].nil? ? app.user_id : options[:uuid]
super
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/set_memberships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SetMemberships < SingleEvent
def initialize(options, app)
@event = current_operation
@telemetry_name = :l_obj
@uuid = options[:uuid].nil? ? app.env[:uuid] : options[:uuid]
@uuid = options[:uuid].nil? ? app.user_id : options[:uuid]
@limit = [options[:limit], 100].min unless options[:limit].nil?
@sort = options[:sort].join(",") if options[:sort] && !options[:sort].empty?
@filter = options[:filter] if options[:filter] && !options[:filter].empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/set_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def path
'channel',
Formatter.channels_for_url(@channel),
'uuid',
Formatter.encode(@app.uuid),
Formatter.encode(@app.user_id),
'data'
].join('/')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/set_uuid_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SetUuidMetadata < SingleEvent
def initialize(options, app)
@event = current_operation
@telemetry_name = :l_obj
@uuid = options[:uuid].nil? ? app.env[:uuid] : options[:uuid]
@uuid = options[:uuid].nil? ? app.user_id : options[:uuid]

# Clean up user-provided metadata object from nils.
@metadata = options[:metadata].delete_if { |_k, v| v.blank? } unless options[:metadata].nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(options, app)
@telemetry_name = :l_pres
@uuid_looking_for = options[:uuid] || options['uuid']
super
@uuid = app.uuid
@uuid = app.user_id
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/events/where_now.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(options, app)
super
@telemetry_name = :l_pres
@uuid_looking_for = options[:uuid] || options['uuid']
@uuid = app.uuid
@uuid = app.user_id
@event = :where_now
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def secure_call(cb, arg)
def get_config
{
tls: @app.env[:ssl],
uuid: @app.env[:uuid],
uuid: @app.user_id,
auth_key: @app.env[:auth_key],
origin: @app.current_origin
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pubnub/validators/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def validate!(parameters)
validate_origin parameters[:origin], true
validate_subscribe_key parameters[:subscribe_key], true
validate_publish_key parameters[:publish_key]
validate_uuid parameters[:uuid]
validate_user_id parameters[:user_id]
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/pubnub/validators/common_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ def validate_publish_key(publish_key, required = false)
end
end

# Validates given uuid
def validate_uuid(uuid)
if !uuid || uuid.blank?
def validate_user_id(user_id)
if !user_id || user_id.blank?
raise(
InitializationError.new,
'Missing required :uuid parameter.'
'Missing required :user_id parameter.'
)
elsif !([String, Symbol].include?(uuid.class) ||
uuid.blank?)
elsif !([String, Symbol].include?(user_id.class) ||
user_id.blank?)
raise(
InitializationError.new,
'UUID parameter is not valid. \
'user_id parameter is not valid. \
Should be type of String or Symbol.'
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/add_channels_to_push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@pubnub = Pubnub.new(
subscribe_key: 'sub-a-mock-key',
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one"
)

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/audit_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
secret_key: "sec-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/channel_registration_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/delete_messages_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
subscribe_key: "sub-a-mock-key",
publish_key: "pub-a-mock-key",
auth_key: "ruby-test-auth",
uuid: "ruby-test-uuid",
user_id: "ruby-test-uuid",
)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/grant_examples_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
secret_key: "sec-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/grant_examples_2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
secret_key: "sec-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/heartbeat_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/here_now_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand Down
14 changes: 7 additions & 7 deletions spec/examples/history_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
)

Expand All @@ -40,7 +40,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
cipher_key: "super-secret-cipher-key",
random_iv: false
Expand All @@ -65,7 +65,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
cipher_key: "super-secret-cipher-key",
random_iv: false
Expand All @@ -90,7 +90,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
cipher_key: "super-secret-cipher-key",
random_iv: false
Expand All @@ -115,7 +115,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
cipher_key: "super-secret-cipher-key"
)
Expand All @@ -139,7 +139,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
cipher_key: "super-secret-cipher-key"
)
Expand All @@ -163,7 +163,7 @@
@pubnub = Pubnub.new(
publish_key: "pub-a-mock-key",
subscribe_key: "sub-a-mock-key",
uuid: "ruby-test-uuid-client-one",
user_id: "ruby-test-uuid-client-one",
auth_key: "ruby-test-auth-client-one",
cipher_key: "super-secret-cipher-key"
)
Expand Down
Loading

0 comments on commit 17617e1

Please sign in to comment.