Class: Parse::LiveQuery::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/live_query/event.rb

Overview

Represents an event received from the LiveQuery server. Events are emitted when objects matching a subscription's query are created, updated, deleted, or enter/leave the query results.

Examples:

subscription.on(:update) do |event|
  puts "Object updated: #{event.object.id}"
  puts "Original state: #{event.original&.to_h}"
  puts "Event type: #{event.type}"
end

Constant Summary collapse

CREDENTIAL_CLASSES =

Classes whose LiveQuery payloads have credential fields removed.

%w[_User _Session].freeze
CREDENTIAL_KEYS =

Payload keys removed from _User / _Session objects. Parse Server releases without the fix for GHSA-349r could deliver another user's _User / _Session events, sessionToken included, to an $or subscription. Removing these before hydration keeps a bearer token (or a linked-account credential) out of callbacks, logs, and #raw even against such a server.

%w[sessionToken authData _session_token _hashed_password _auth_data].freeze
CREDENTIAL_KEY_PREFIX =

Prefix for per-provider auth-data storage columns.

"_auth_data_"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {}) ⇒ Event

Create a new Event from a LiveQuery server message

Parameters:

  • type (Symbol) —

    event type

  • class_name (String) —

    Parse class name

  • object_data (Hash) —

    object data from server

  • original_data (Hash, nil) (defaults to: nil) —

    original object data (for update/enter/leave)

  • request_id (Integer) —

    subscription request ID

  • raw (Hash) (defaults to: {}) —

    raw server payload



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/parse/live_query/event.rb', line 95

def initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {})
  @type = type.to_sym
  @class_name = class_name
  @request_id = request_id
  @received_at = Time.now
  @raw = self.class.scrub_message(raw, class_name)

  # Credential fields of `_User` / `_Session` objects are removed
  # before hydration (see {CREDENTIAL_KEYS}).
  object_data = self.class.scrub_credentials(object_data, class_name)
  original_data = self.class.scrub_credentials(original_data, class_name)

  # Convert object data to Parse::Object instances
  @object = build_object(class_name, object_data) if object_data
  @original = build_object(class_name, original_data) if original_data
end

Instance Attribute Details

#class_name ⇒ String (readonly)

Returns the Parse class name.

Returns:

  • (String) —

    the Parse class name



31
32
33
# File 'lib/parse/live_query/event.rb', line 31

def class_name
  @class_name
end

#object ⇒ Parse::Object (readonly)

Returns the object affected by this event (current state).

Returns:

  • (Parse::Object) —

    the object affected by this event (current state)



22
23
24
# File 'lib/parse/live_query/event.rb', line 22

def object
  @object
end

#original ⇒ Parse::Object? (readonly)

Returns the original state of the object (for :update, :enter, :leave).

Returns:

  • (Parse::Object, nil) —

    the original state of the object (for :update, :enter, :leave)



25
26
27
# File 'lib/parse/live_query/event.rb', line 25

def original
  @original
end

#raw ⇒ Hash (readonly)

Returns raw payload from the server.

Returns:

  • (Hash) —

    raw payload from the server



37
38
39
# File 'lib/parse/live_query/event.rb', line 37

def raw
  @raw
end

#received_at ⇒ Time (readonly)

Returns when the event was received.

Returns:

  • (Time) —

    when the event was received



34
35
36
# File 'lib/parse/live_query/event.rb', line 34

def received_at
  @received_at
end

#request_id ⇒ Integer (readonly)

Returns the subscription request ID this event belongs to.

Returns:

  • (Integer) —

    the subscription request ID this event belongs to



28
29
30
# File 'lib/parse/live_query/event.rb', line 28

def request_id
  @request_id
end

#type ⇒ Symbol (readonly)

Returns the type of event (:create, :update, :delete, :enter, :leave).

Returns:

  • (Symbol) —

    the type of event (:create, :update, :delete, :enter, :leave)



19
20
21
# File 'lib/parse/live_query/event.rb', line 19

def type
  @type
end

Class Method Details

.scrub_credentials(data, class_name = nil) ⇒ Hash, Object

Remove credential keys from an object payload when it belongs to _User or _Session (by its own className or by class_name). Returns a copy; other payloads are returned unchanged.

Parameters:

  • data (Hash, Object) —

    object payload from the server.

  • class_name (String, nil) (defaults to: nil) —

    the subscription's class.

Returns:



61
62
63
64
65
66
67
68
69
# File 'lib/parse/live_query/event.rb', line 61

def scrub_credentials(data, class_name = nil)
  return data unless data.is_a?(Hash)
  own = data["className"] || data[:className]
  return data unless CREDENTIAL_CLASSES.include?(own.to_s) || CREDENTIAL_CLASSES.include?(class_name.to_s)
  data.reject do |key, _|
    k = key.to_s
    CREDENTIAL_KEYS.include?(k) || k.start_with?(CREDENTIAL_KEY_PREFIX)
  end
end

.scrub_message(message, class_name = nil) ⇒ Hash

Scrub the object and original payloads of a whole server message. Returns a copy when anything changed.

Parameters:

  • message (Hash)
  • class_name (String, nil) (defaults to: nil)

Returns:



77
78
79
80
81
82
83
84
85
# File 'lib/parse/live_query/event.rb', line 77

def scrub_message(message, class_name = nil)
  return message unless message.is_a?(Hash)
  scrubbed = message.dup
  %w[object original].each do |key|
    next unless scrubbed.key?(key)
    scrubbed[key] = scrub_credentials(scrubbed[key], class_name)
  end
  scrubbed
end

Instance Method Details

#create? ⇒ Boolean

Returns true if this is a create event.

Returns:

  • (Boolean) —

    true if this is a create event



113
114
115
# File 'lib/parse/live_query/event.rb', line 113

def create?
  type == :create
end

#delete? ⇒ Boolean

Returns true if this is a delete event.

Returns:

  • (Boolean) —

    true if this is a delete event



123
124
125
# File 'lib/parse/live_query/event.rb', line 123

def delete?
  type == :delete
end

#enter? ⇒ Boolean

Returns true if this is an enter event (object now matches query).

Returns:

  • (Boolean) —

    true if this is an enter event (object now matches query)



128
129
130
# File 'lib/parse/live_query/event.rb', line 128

def enter?
  type == :enter
end

#leave? ⇒ Boolean

Returns true if this is a leave event (object no longer matches query).

Returns:

  • (Boolean) —

    true if this is a leave event (object no longer matches query)



133
134
135
# File 'lib/parse/live_query/event.rb', line 133

def leave?
  type == :leave
end

#parse_object_id ⇒ String

Returns the Parse object ID.

Returns:

  • (String) —

    the Parse object ID



138
139
140
# File 'lib/parse/live_query/event.rb', line 138

def parse_object_id
  object&.id
end

#to_h ⇒ Hash

Returns event as a hash.

Returns:

  • (Hash) —

    event as a hash



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/parse/live_query/event.rb', line 143

def to_h
  {
    type: type,
    class_name: class_name,
    object_id: parse_object_id,
    request_id: request_id,
    received_at: received_at,
    object: object&.as_json,
    original: original&.as_json,
  }
end

#update? ⇒ Boolean

Returns true if this is an update event.

Returns:

  • (Boolean) —

    true if this is an update event



118
119
120
# File 'lib/parse/live_query/event.rb', line 118

def update?
  type == :update
end