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)


268
269
270
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 268

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

.active_record_component(name, record) ⇒ Object



272
273
274
275
276
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 272

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



125
126
127
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 125

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

.call_subscribe_block(definition, connection) ⇒ Object



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

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

.canonical_identity_value(value) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 204

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



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 254

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



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

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

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

.configurationObject



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

def configuration
  Upkeep::Rails.configuration
end

.configured_identity_dependencies(recorder) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 138

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) }
      .map { |definition| [definition, dependency] }
  end
end

.configured_identity_dependencies_for_dependency(dependency) ⇒ Object



155
156
157
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 155

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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 57

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)


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

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

.dependency_nil?(dependency) ⇒ Boolean

Returns:

  • (Boolean)


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

def dependency_nil?(dependency)
  value = dependency.key.fetch(:value)
  return true if value.nil?

  dependency.[:value_class].to_s == "NilClass" ||
    dependency.["value_class"].to_s == "NilClass"
end

.derive(connection) ⇒ Object



22
23
24
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 22

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

.derive_all(connection) ⇒ Object



26
27
28
29
30
31
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 26

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



33
34
35
36
37
38
39
40
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 33

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 42

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



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

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



93
94
95
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 93

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

.global_id_component(name, value) ⇒ Object



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

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

.identifier_components(connection) ⇒ Object



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

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

.identity_component(name, value) ⇒ Object



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

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

.identity_dependencies(recorder) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 129

def identity_dependencies(recorder)
  return [] unless recorder

  recorder.graph.dependency_nodes
    .map(&:payload)
    .select(&:identity?)
    .uniq(&:cache_key)
end

.metadata_value(subscription, key) ⇒ Object



303
304
305
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 303

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

.model_component(name, identity) ⇒ Object



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

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



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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 108

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)


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

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



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

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



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

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

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

.subscribe_identity_value(definition, value) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 187

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



148
149
150
151
152
153
# File 'lib/upkeep/rails/cable/subscriber_identity.rb', line 148

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