Class: Parse::LiveQuery::Event
- Inherits:
-
Object
- Object
- Parse::LiveQuery::Event
- 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.
Constant Summary collapse
- CREDENTIAL_CLASSES =
Classes whose LiveQuery payloads have credential fields removed.
%w[_User _Session].freeze
- CREDENTIAL_KEYS =
Payload keys removed from
_User/_Sessionobjects. Parse Server releases without the fix for GHSA-349r could deliver another user's_User/_Sessionevents,sessionTokenincluded, to an$orsubscription. 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_name ⇒ String
readonly
The Parse class name.
-
#object ⇒ Parse::Object
readonly
The object affected by this event (current state).
-
#original ⇒ Parse::Object?
readonly
The original state of the object (for :update, :enter, :leave).
-
#raw ⇒ Hash
readonly
Raw payload from the server.
-
#received_at ⇒ Time
readonly
When the event was received.
-
#request_id ⇒ Integer
readonly
The subscription request ID this event belongs to.
-
#type ⇒ Symbol
readonly
The type of event (:create, :update, :delete, :enter, :leave).
Class Method Summary collapse
-
.scrub_credentials(data, class_name = nil) ⇒ Hash, Object
Remove credential keys from an object payload when it belongs to
_Useror_Session(by its ownclassNameor byclass_name). -
.scrub_message(message, class_name = nil) ⇒ Hash
Scrub the
objectandoriginalpayloads of a whole server message.
Instance Method Summary collapse
-
#create? ⇒ Boolean
True if this is a create event.
-
#delete? ⇒ Boolean
True if this is a delete event.
-
#enter? ⇒ Boolean
True if this is an enter event (object now matches query).
-
#initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {}) ⇒ Event
constructor
Create a new Event from a LiveQuery server message.
-
#leave? ⇒ Boolean
True if this is a leave event (object no longer matches query).
-
#parse_object_id ⇒ String
The Parse object ID.
-
#to_h ⇒ Hash
Event as a hash.
-
#update? ⇒ Boolean
True if this is an update event.
Constructor Details
#initialize(type:, class_name:, object_data:, original_data: nil, request_id:, raw: {}) ⇒ Event
Create a new Event from a LiveQuery server message
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.(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.
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).
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).
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.
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.
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.
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).
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.
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.
77 78 79 80 81 82 83 84 85 |
# File 'lib/parse/live_query/event.rb', line 77 def (, class_name = nil) return unless .is_a?(Hash) scrubbed = .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.
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.
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).
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).
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.
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.
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.
118 119 120 |
# File 'lib/parse/live_query/event.rb', line 118 def update? type == :update end |