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)


296
297
298
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 296

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

.active_record_component(name, record) ⇒ Object



300
301
302
303
304
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 300

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



159
160
161
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 159

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

.call_subscribe_block(definition, connection) ⇒ Object



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

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 240

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



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 282

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



198
199
200
201
202
203
204
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 198

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



335
336
337
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 335

def configuration
  Upkeep::Rails.configuration
end

.configured_identity_dependencies(recorder) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 172

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



190
191
192
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 190

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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 91

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)


194
195
196
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 194

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

.derive(connection) ⇒ Object



56
57
58
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 56

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

.derive_all(connection) ⇒ Object



60
61
62
63
64
65
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 60

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



67
68
69
70
71
72
73
74
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 67

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 76

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



131
132
133
134
135
136
137
138
139
140
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 131

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



127
128
129
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 127

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

.global_id_component(name, value) ⇒ Object



323
324
325
326
327
328
329
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 323

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

.identifier_components(connection) ⇒ Object



122
123
124
125
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 122

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

.identity_component(name, value) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 232

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

.identity_dependencies(recorder) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 163

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



331
332
333
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 331

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

.model_component(name, identity) ⇒ Object



271
272
273
274
275
276
277
278
279
280
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 271

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



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 258

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 142

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)


306
307
308
309
310
311
312
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 306

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



314
315
316
317
318
319
320
321
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 314

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



206
207
208
209
210
211
212
213
214
215
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 206

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



223
224
225
226
227
228
229
230
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 223

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



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

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