Module: Upkeep::Rails::Cable::SubscriberIdentity

Defined in:
lib/upkeep/rails/cable/subscriber_identity.rb

Constant Summary collapse

ANONYMOUS_PUBLIC_MODE =
"anonymous_public"
IDENTIFIED_MODE =
"identified"
DECLARATION_REQUIRED_SOURCES =
%w[Current.user current_attribute warden_user].freeze

Class Method Summary collapse

Class Method Details

.active_record?(value) ⇒ Boolean

Returns:

  • (Boolean)


316
317
318
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 316

def active_record?(value)
  defined?(::ActiveRecord::Base) && value.is_a?(::ActiveRecord::Base)
end

.active_record_component(name, record) ⇒ Object



320
321
322
323
324
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 320

def active_record_component(name, record)
  raise UnidentifiedSubscriber, "ActionCable identifier #{name} is an unsaved record" unless record.id

  model_component(name, model: record.class.name, id: record.id)
end

.anonymous_componentsObject



179
180
181
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 179

def anonymous_components
  [ scalar_component(:anonymous_public_subscription, SecureRandom.uuid) ]
end

.call_subscribe_block(definition, connection) ⇒ Object



237
238
239
240
241
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 237

def call_subscribe_block(definition, connection)
  block = definition.subscribe_block
  context = SubscribeContext.new(connection)
  block.arity == 1 ? block.call(context) : context.instance_exec(&block)
end

.canonical_identity_value(value) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 260

def canonical_identity_value(value)
  case value
  when nil, true, false, Numeric, String, Symbol
    value
  when Array
    value.map { |item| canonical_identity_value(item) }
  when Hash
    value.keys.sort_by(&:to_s).to_h { |key| [key.to_s, canonical_identity_value(value.fetch(key))] }
  else
    model_identity = Dependencies.model_identity(value)
    return model_identity if model_identity

    return { global_id: value.to_gid_param } if value.respond_to?(:to_gid_param)

    raise UnidentifiedSubscriber, "identity value #{value.class.name} has no canonical identity"
  end
end

.component_for(name, value) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 302

def component_for(name, value)
  raise UnidentifiedSubscriber, "ActionCable identifier #{name} is nil" if value.nil?

  if active_record?(value)
    active_record_component(name, value)
  elsif scalar?(value)
    scalar_component(name, value)
  elsif value.respond_to?(:to_gid_param)
    global_id_component(name, value)
  else
    raise UnidentifiedSubscriber, "ActionCable identifier #{name} has no canonical identity"
  end
end

.component_for_dependency(definition, dependency) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 218

def component_for_dependency(definition, dependency)
  if definition.absent_dependency?(dependency)
    raise UnidentifiedSubscriber, "captured identity :#{definition.name} from #{definition.source_label} is absent"
  end

  identity_component(definition.name, dependency.key.fetch(:value))
end

.configurationObject



355
356
357
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 355

def configuration
  Upkeep::Rails.configuration
end

.configured_identity_dependencies(recorder) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 192

def configured_identity_dependencies(recorder)
  definitions = configuration.identity_definitions
  return [] if definitions.empty?

  identity_dependencies(recorder).flat_map do |dependency|
    definitions.select { |definition| definition.matches_dependency?(dependency) }
      .reject { |definition| definition.absent_dependency?(dependency) }
      .map { |definition| [definition, dependency] }
  end
end

.configured_identity_dependencies_for_dependency(dependency) ⇒ Object



210
211
212
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 210

def configured_identity_dependencies_for_dependency(dependency)
  configuration.identity_definitions.select { |definition| definition.matches_dependency?(dependency) }
end

.decision_for(_request = nil, recorder:) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 111

def decision_for(_request = nil, recorder:)
  configured_dependencies = configured_identity_dependencies(recorder)
  undeclared_dependencies = undeclared_identity_dependencies(recorder)

  if configured_dependencies.any?
    Decision.new(
      IDENTIFIED_MODE,
      false,
      "identity_dependencies_present",
      configured_dependencies.map { |definition, _dependency| definition.source.to_s }.uniq.sort,
      configured_dependencies.map { |definition, _dependency| definition.name.to_s }.uniq.sort
    )
  elsif undeclared_dependencies.any?
    Decision.new(
      IDENTIFIED_MODE,
      false,
      "identity_setup_required",
      undeclared_dependencies.map { |dependency| dependency.source.to_s }.uniq.sort,
      []
    )
  else
    Decision.new(
      ANONYMOUS_PUBLIC_MODE,
      true,
      nil,
      [],
      []
    )
  end
end

.declaration_required_dependency?(dependency) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 214

def declaration_required_dependency?(dependency)
  DECLARATION_REQUIRED_SOURCES.include?(dependency.source.to_s)
end

.derive(connection) ⇒ Object



76
77
78
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 76

def derive(connection)
  derive_all(connection).last
end

.derive_all(connection) ⇒ Object



80
81
82
83
84
85
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 80

def derive_all(connection)
  components = subscribe_components(connection, configuration.identity_definitions)
  raise UnidentifiedSubscriber, "ActionCable connection has no declared Upkeep identities" if components.empty?

  [for_components(components)]
end

.derive_for_subscription(connection, subscription) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 87

def derive_for_subscription(connection, subscription)
  names = (subscription, :identity_names) || []
  definitions = names.map { |name| configuration.identity_definition(name) }
  components = subscribe_components(connection, definitions)
  raise UnidentifiedSubscriber, "ActionCable connection did not resolve subscription identities #{names.inspect}" if components.empty?

  for_components(components)
end

.derive_from_request(request, recorder:, decision: decision_for(request, recorder: recorder)) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 96

def derive_from_request(request, recorder:, decision: decision_for(request, recorder: recorder))
  components = if decision.anonymous
    anonymous_components
  else
    recorder_components(recorder)
  end

  if components.empty?
    raise UnidentifiedSubscriber,
      "subscription has identity dependencies but no declared Upkeep identity mapping"
  end

  for_components(components)
end

.for_components(components) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 151

def for_components(components)
  canonical_bytes = JSON.generate(components.sort_by { |component| component.fetch(:name) })
  subscriber_id = "action_cable:#{Digest::SHA256.hexdigest(canonical_bytes)}"

  Identity.new(
    subscriber_id,
    Delivery::ActionCableAdapter.stream_name_for(subscriber_id),
    components
  )
end

.for_identifiers(identifiers) ⇒ Object



147
148
149
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 147

def for_identifiers(identifiers)
  for_components(identifiers.map { |name, value| component_for(name, value) })
end

.global_id_component(name, value) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 343

def global_id_component(name, value)
  {
    name: name.to_s,
    kind: "global_id",
    value: value.to_gid_param
  }
end

.identifier_components(connection) ⇒ Object



142
143
144
145
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 142

def identifier_components(connection)
  identifiers = Array(connection.identifiers)
  identifiers.map { |name| component_for(name, connection.public_send(name)) }
end

.identity_component(name, value) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 252

def identity_component(name, value)
  {
    name: name.to_s,
    kind: "identity",
    value: normalize_component_value(value)
  }
end

.identity_dependencies(recorder) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 183

def identity_dependencies(recorder)
  return [] unless recorder

  recorder.graph.dependency_nodes
    .map(&:payload)
    .select { |dependency| Dependencies.partitioning_identity?(dependency) }
    .uniq(&:cache_key)
end

.metadata_value(subscription, key) ⇒ Object



351
352
353
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 351

def (subscription, key)
  subscription.[key] || subscription.[key.to_s]
end

.model_component(name, identity) ⇒ Object



291
292
293
294
295
296
297
298
299
300
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 291

def model_component(name, identity)
  return unless identity.is_a?(Hash) && identity[:model] && identity[:id]

  {
    name: name.to_s,
    kind: "model",
    model: identity.fetch(:model),
    id: identity.fetch(:id).to_s
  }
end

.normalize_component_value(value) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 278

def normalize_component_value(value)
  case value
  when Hash
    value.keys.sort_by(&:to_s).to_h { |key| [key.to_s, normalize_component_value(value.fetch(key))] }
  when Array
    value.map { |item| normalize_component_value(item) }
  when Symbol
    value.to_s
  else
    value
  end
end

.recorder_components(recorder) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 162

def recorder_components(recorder)
  components_by_name = Hash.new { |hash, key| hash[key] = [] }
  configured_identity_dependencies(recorder).each do |definition, dependency|
    component = component_for_dependency(definition, dependency)
    components_by_name[definition.name] << component if component
  end

  components_by_name.map do |name, components|
    unique_components = components.uniq
    if unique_components.size > 1
      raise UnidentifiedSubscriber, "captured identity :#{name} changed during request"
    end

    unique_components.first
  end.compact
end

.scalar?(value) ⇒ Boolean

Returns:

  • (Boolean)


326
327
328
329
330
331
332
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 326

def scalar?(value)
  value.is_a?(String) ||
    value.is_a?(Symbol) ||
    value.is_a?(Integer) ||
    value == true ||
    value == false
end

.scalar_component(name, value) ⇒ Object



334
335
336
337
338
339
340
341
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 334

def scalar_component(name, value)
  {
    name: name.to_s,
    kind: "scalar",
    class: value.class.name,
    value: value.to_s
  }
end

.subscribe_components(connection, definitions) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 226

def subscribe_components(connection, definitions)
  definitions.filter_map do |definition|
    value = call_subscribe_block(definition, connection)
    if definition.absent?(value)
      raise UnidentifiedSubscriber, "subscribe identity :#{definition.name} from #{definition.source_label} is absent"
    end

    identity_component(definition.name, subscribe_identity_value(definition, value))
  end
end

.subscribe_identity_value(definition, value) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 243

def subscribe_identity_value(definition, value)
  case definition.source
  when :session, :cookie
    Dependencies.private_fingerprint(value)
  else
    canonical_identity_value(value)
  end
end

.undeclared_identity_dependencies(recorder) ⇒ Object



203
204
205
206
207
208
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 203

def undeclared_identity_dependencies(recorder)
  identity_dependencies(recorder).select do |dependency|
    declaration_required_dependency?(dependency) &&
      configured_identity_dependencies_for_dependency(dependency).empty?
  end
end