Skip to content

Commit

Permalink
Merge pull request #2513 from alphagov/content-modelling/785-show-con…
Browse files Browse the repository at this point in the history
…tent-block-updates-in-mainstream

(Content modelling/785) Show content block updates in Mainstream
  • Loading branch information
baisa authored Feb 12, 2025
2 parents 0761421 + bbb0d2d commit d66f198
Show file tree
Hide file tree
Showing 44 changed files with 689 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/controllers/legacy_editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def show

@tagging_update = tagging_update_form
@artefact = @resource.artefact
@update_events = HostContentUpdateEvent.all_for_artefact(@artefact)
render action: "show"
end

Expand Down Expand Up @@ -115,6 +116,7 @@ def update
@tagging_update = tagging_update_form
@linkables = Tagging::Linkables.new
@artefact = @resource.artefact
@update_events = HostContentUpdateEvent.all_for_artefact(@artefact)
render action: "show"
end
success.json do
Expand All @@ -134,6 +136,7 @@ def linking
@linkables = Tagging::Linkables.new
@tagging_update = tagging_update_form
@artefact = @resource.artefact
@update_events = HostContentUpdateEvent.all_for_artefact(@artefact)
render action: "show"
end

Expand Down
16 changes: 13 additions & 3 deletions app/helpers/action_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module ActionHelper
def edition_actions(edition)
edition.actions.reverse.delete_if do |a|
def edition_actions(edition, update_events)
actions = edition.actions.reject do |a|
[Action::IMPORTANT_NOTE, Action::IMPORTANT_NOTE_RESOLVED].include?(a.request_type)
end
update_actions = update_events.select { |e| e.is_for_edition?(edition) }.map(&:to_action)
actions.append(*update_actions)
actions.sort_by(&:created_at).reverse
end

def action_note?(action)
Expand All @@ -13,9 +16,12 @@ def action_note(action)
notes = []

if action.comment.present?
if action.request_type == Action::RECEIVE_FACT_CHECK
case action.request_type
when Action::RECEIVE_FACT_CHECK
formatted_email_parts = format_email_text(action.comment)
notes.concat(formatted_email_parts)
when HostContentUpdateEvent::Action::CONTENT_BLOCK_UPDATE
notes << content_block_update_comment(action)
else
notes << format_and_auto_link_plain_text(action.comment)
end
Expand All @@ -32,6 +38,10 @@ def action_note(action)
notes.join.html_safe
end

def content_block_update_comment(action)
"#{action.comment} (#{link_to 'View in Content Block Manager', action.block_url, target: '_blank', rel: 'noopener'})"
end

def action_class(action)
action.request_type.tr("_", "-")
end
Expand Down
9 changes: 8 additions & 1 deletion app/lib/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ module Services
def self.publishing_api
@publishing_api ||= GdsApi::PublishingApi.new(
Plek.find("publishing-api"),
bearer_token: ENV["PUBLISHING_API_BEARER_TOKEN"] || "example",
bearer_token: ENV.fetch("PUBLISHING_API_BEARER_TOKEN", "example"),
)
end

def self.signon_api
@signon_api ||= GdsApi::SignonApi.new(
Plek.find("signon", external: true),
bearer_token: ENV.fetch("SIGNON_API_BEARER_TOKEN", "example"),
)
end
end
41 changes: 41 additions & 0 deletions app/models/host_content_update_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class HostContentUpdateEvent < Data.define(:author, :created_at, :content_id, :content_title, :document_type)
Author = Data.define(:name, :email)
def self.all_for_artefact(artefact)
events = Services.publishing_api.get_events_for_content_id(artefact.content_id, {
action: "HostContentUpdateJob",
})

user_uuids = events.map { |event| event["payload"]["source_block"]["updated_by_user_uid"] }.uniq
users = users_with_uuids(user_uuids)

events.map do |event|
HostContentUpdateEvent.new(
author: users[event["payload"]["source_block"]["updated_by_user_uid"]],
created_at: Time.zone.parse(event["created_at"]),
content_id: event["payload"]["source_block"]["content_id"],
content_title: event["payload"]["source_block"]["title"],
document_type: event["payload"]["source_block"]["document_type"],
)
end
end

def self.users_with_uuids(uuids)
Services.signon_api.get_users(uuids:).map { |user|
[user["uid"], Author.new(user["name"], user["email"])]
}.to_h
end

def is_for_edition?(edition)
if edition.published?
created_at.after?(edition.published_at)
elsif edition.archived? && edition.superseded_at
created_at.between?(edition.published_at, edition.superseded_at)
else
false
end
end

def to_action
Action.new(self)
end
end
57 changes: 57 additions & 0 deletions app/models/host_content_update_event/action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class HostContentUpdateEvent
class Action
CONTENT_BLOCK_UPDATE = "content_block_update".freeze

attr_reader :host_content_update_event

def initialize(host_content_update_event)
@host_content_update_event = host_content_update_event
end

def request_type
CONTENT_BLOCK_UPDATE
end

delegate :created_at, to: :host_content_update_event

def requester
host_content_update_event.author
end

def to_s
"Content block updated"
end

def comment
"#{humanized_document_type} updated"
end

def block_url
[
Plek.external_url_for("whitehall-admin"),
"content-block-manager",
"content-block",
"content-id",
host_content_update_event.content_id,
].join("/")
end

def comment_sanitized
false
end

def is_fact_check_request?
false
end

def recipient_id
nil
end

private

def humanized_document_type
host_content_update_event.document_type.delete_prefix("content_block_").humanize
end
end
end
18 changes: 13 additions & 5 deletions app/traits/recordable_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ def created_by
end

def published_by
publication = actions.where(request_type: Action::PUBLISH).first
publication.requester if publication
latest_action_of_type(Action::PUBLISH)&.requester
end

def archived_by
publication = actions.where(request_type: Action::ARCHIVE).first
publication.requester if publication
def published_at
latest_action_of_type(Action::PUBLISH)&.created_at
end

def superseded_at
subsequent_siblings.first&.published_at
end

private

def latest_action_of_type(request_type)
actions.where(request_type:).first
end
end
end
2 changes: 1 addition & 1 deletion app/views/shared/_edition_history.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<div class="panel-collapse collapse<% if edition_counter == 0 %> in<% end %>" id="body<%= edition.version_number %>">
<ul class="panel-body list-unstyled remove-bottom-margin">
<% edition_actions(edition).each do |action| %>
<% edition_actions(edition, update_events).each do |action| %>
<li class="action-<%= action_class(action) %> add-bottom-margin add-left-margin">
<h3 class="h4">
<div class="add-label-margin normal">
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_history.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@
</p>

<div class="panel-group">
<%= render collection: @resource.history, partial: "/shared/edition_history", as: "edition" %>
<%= render collection: @resource.history, partial: "/shared/edition_history", as: "edition", locals: { update_events: @update_events } %>
</div>
</div>
2 changes: 2 additions & 0 deletions test/functional/legacy_editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class LegacyEditionsControllerTest < ActionController::TestCase
login_as_stub_user
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, false)
Expand Down
2 changes: 2 additions & 0 deletions test/integration/add_artefact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class AddArtefactTest < LegacyIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
end

should "create a new artefact" do
Expand Down
2 changes: 2 additions & 0 deletions test/integration/adding_parts_to_guides_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class AddingPartsToGuidesTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:design_system_publications_filter, false)
Expand Down
2 changes: 2 additions & 0 deletions test/integration/adding_variants_to_transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class AddingVariantsToTransactionsTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:design_system_publications_filter, false)
Expand Down
2 changes: 2 additions & 0 deletions test/integration/change_edition_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class ChangeEditionTypeTest < LegacyJavascriptIntegrationTest
stub_linkables
FactoryBot.create(:user, :govuk_editor)
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
end

teardown do
Expand Down
2 changes: 2 additions & 0 deletions test/integration/completed_transactions_create_edit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CompletedTransactionCreateEditTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
end

should "create a new CompletedTransactionEdition" do
Expand Down
2 changes: 2 additions & 0 deletions test/integration/delete_edition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class DeleteEditionTest < LegacyIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:design_system_publications_filter, false)
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edit_artefact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class EditArtefactTest < LegacyIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:design_system_edit, false)
end
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_edit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class EditionEditTest < IntegrationTest
test_strategy.switch!(:design_system_edit, true)
stub_holidays_used_by_fact_check
stub_linkables
stub_events_for_all_content_ids
stub_users_from_signon_api
end

context "edit page" do
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_history_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class EditionHistoryTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
end

context "viewing the history and notes tab" do
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_link_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class EditionLinkCheckTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

@stubbed_api_request = stub_link_checker_api_create_batch(
uris: ["https://www.gov.uk"],
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_major_change_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class EditionMajorChangeTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
end

teardown do
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_scheduled_publishing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class EditionScheduledPublishingTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
# queue up the edition, don't perform inline
Sidekiq::Testing.fake!

Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_tab_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class EditionTabTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

@guide = FactoryBot.create(:guide_edition, state: "draft")
end
Expand Down
2 changes: 2 additions & 0 deletions test/integration/edition_workflow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class EditionWorkflowTest < LegacyJavascriptIntegrationTest
setup do
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

@alice = FactoryBot.create(:user, :govuk_editor, name: "Alice")
@bob = FactoryBot.create(:user, :govuk_editor, name: "Bob")
Expand Down
2 changes: 2 additions & 0 deletions test/integration/guide_create_edit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class GuideCreateEditTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api

@artefact = FactoryBot.create(
:artefact,
Expand Down
2 changes: 2 additions & 0 deletions test/integration/help_page_create_edit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class HelpPageCreateEditTest < LegacyJavascriptIntegrationTest
setup_users
stub_linkables
stub_holidays_used_by_fact_check
stub_events_for_all_content_ids
stub_users_from_signon_api
end

should "create a new HelpPageEdition" do
Expand Down
Loading

0 comments on commit d66f198

Please sign in to comment.