From 4879995013aef6f450be26eab4209f219afc68ab Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Thu, 11 Sep 2025 17:56:28 +0200 Subject: [PATCH] Serialize events --- app/models/webhook.rb | 4 +++ app/models/webhook/delivery.rb | 2 +- app/views/cards/_card.json.jbuilder | 26 +++++++++++++++++++ .../cards/comments/_comment.json.jbuilder | 18 +++++++++++++ .../collections/_collection.json.jbuilder | 16 ++++++++++++ app/views/users/_user.json.jbuilder | 7 +++++ app/views/webhooks/event.json.jbuilder | 19 ++++++++++++++ app/views/workflows/_workflow.json.jbuilder | 6 +++++ .../workflows/stages/_stage.json.jbuilder | 9 +++++++ 9 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 app/views/cards/_card.json.jbuilder create mode 100644 app/views/cards/comments/_comment.json.jbuilder create mode 100644 app/views/collections/_collection.json.jbuilder create mode 100644 app/views/users/_user.json.jbuilder create mode 100644 app/views/webhooks/event.json.jbuilder create mode 100644 app/views/workflows/_workflow.json.jbuilder create mode 100644 app/views/workflows/stages/_stage.json.jbuilder diff --git a/app/models/webhook.rb b/app/models/webhook.rb index b35bbb4f0..fc905d1c2 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -44,6 +44,10 @@ class Webhook < ApplicationRecord update_columns active: false end + def renderer + @renderer ||= ApplicationController.renderer.new + end + private def validate_url uri = URI.parse(url.presence) diff --git a/app/models/webhook/delivery.rb b/app/models/webhook/delivery.rb index f80e29871..4cd926107 100644 --- a/app/models/webhook/delivery.rb +++ b/app/models/webhook/delivery.rb @@ -106,6 +106,6 @@ class Webhook::Delivery < ApplicationRecord end def payload - { test: :test }.to_json + webhook.renderer.render(template: "webhooks/event", assigns: { event: event }, format: :json) end end diff --git a/app/views/cards/_card.json.jbuilder b/app/views/cards/_card.json.jbuilder new file mode 100644 index 000000000..fdb242edd --- /dev/null +++ b/app/views/cards/_card.json.jbuilder @@ -0,0 +1,26 @@ +json.cache! card do + json.(card, :id, :title, :status) + json.image_url card.image.presence && url_for(card.image) + + json.golden card.golden? + json.last_active_at card.last_active_at.utc + json.created_at card.created_at.utc + + json.url card_url(card) + + json.collection do + json.partial! "collections/collection", locals: { collection: card.collection } + end + + json.stage do + if card.stage + json.partial! "workflows/stages/stage", stage: card.stage + else + nil + end + end + + json.creator do + json.partial! "users/user", user: card.creator + end +end diff --git a/app/views/cards/comments/_comment.json.jbuilder b/app/views/cards/comments/_comment.json.jbuilder new file mode 100644 index 000000000..a66ae1ef9 --- /dev/null +++ b/app/views/cards/comments/_comment.json.jbuilder @@ -0,0 +1,18 @@ +json.cache! comment do + json.(comment, :id) + + json.created_at comment.created_at.utc + json.updated_at comment.updated_at.utc + + json.body do + json.plain_text comment.body.to_plain_text + json.html comment.body.to_s + end + + json.creator do + json.partial! "users/user", user: comment.creator + end + + json.reactions_url card_comment_reactions_url(comment.card_id, comment.id) + json.url card_comment_url(comment.card_id, comment.id) +end diff --git a/app/views/collections/_collection.json.jbuilder b/app/views/collections/_collection.json.jbuilder new file mode 100644 index 000000000..3c42b2fc9 --- /dev/null +++ b/app/views/collections/_collection.json.jbuilder @@ -0,0 +1,16 @@ +json.cache! collection do + json.(collection, :id, :name, :all_access) + json.created_at collection.created_at.utc + + json.creator do + json.partial! "users/user", user: collection.creator + end + + json.workflow do + if collection.workflow + json.partial! "workflows/workflow", workflow: collection.workflow + else + nil + end + end +end diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder new file mode 100644 index 000000000..6b11459f3 --- /dev/null +++ b/app/views/users/_user.json.jbuilder @@ -0,0 +1,7 @@ +json.cache! user do + json.(user, :id, :name, :email_address, :role, :active) + + json.created_at user.created_at.utc + + json.url user_url(user) +end diff --git a/app/views/webhooks/event.json.jbuilder b/app/views/webhooks/event.json.jbuilder new file mode 100644 index 000000000..ff779c39a --- /dev/null +++ b/app/views/webhooks/event.json.jbuilder @@ -0,0 +1,19 @@ +json.cache! @event do + json.(@event, :id, :action) + json.created_at @event.created_at.utc + + json.eventable do + case @event.eventable + when Card then json.partial! "cards/card", card: @event.eventable + when Comment then json.partial! "cards/comments/comment", comment: @event.eventable + end + end + + json.collection do + json.partial! "collections/collection", locals: { collection: @event.collection } + end + + json.creator do + json.partial! "users/user", user: @event.creator + end +end diff --git a/app/views/workflows/_workflow.json.jbuilder b/app/views/workflows/_workflow.json.jbuilder new file mode 100644 index 000000000..7fa32c7e9 --- /dev/null +++ b/app/views/workflows/_workflow.json.jbuilder @@ -0,0 +1,6 @@ +json.cache! workflow do + json.(workflow, :id, :name) + + json.created_at workflow.created_at.utc + json.updated_at workflow.updated_at.utc +end diff --git a/app/views/workflows/stages/_stage.json.jbuilder b/app/views/workflows/stages/_stage.json.jbuilder new file mode 100644 index 000000000..bb9354261 --- /dev/null +++ b/app/views/workflows/stages/_stage.json.jbuilder @@ -0,0 +1,9 @@ +json.cache! stage do + json.(stage, :id, :name, :color) + + json.created_at stage.created_at.utc + + json.workflow do + json.partial! "workflows/workflow", workflow: stage.workflow + end +end