Module: Tempest::Jetstream::Decoder

Defined in:
lib/tempest/jetstream/decoder.rb

Class Method Summary collapse

Class Method Details

.decode(payload) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tempest/jetstream/decoder.rb', line 52

def decode(payload)
  message = JSON.parse(payload)
  return nil unless message["kind"] == "commit"

  commit = message["commit"] || {}
  record = commit["record"] || {}
  subject = record["subject"]
  reply = record["reply"]
  reply_parent = reply.is_a?(Hash) ? reply["parent"] : nil

  Event.new(
    kind: :commit,
    did: message["did"],
    time_us: message["time_us"],
    collection: commit["collection"],
    operation: commit["operation"]&.to_sym,
    rkey: commit["rkey"],
    cid: commit["cid"],
    text: record["text"],
    created_at: record["createdAt"],
    subject_uri: subject.is_a?(Hash) ? subject["uri"] : nil,
    facets: Tempest::Facet.parse(record["facets"]),
    reply_parent_uri: reply_parent.is_a?(Hash) ? reply_parent["uri"] : nil,
  )
rescue JSON::ParserError
  nil
end