From 3cb18d05677ce54910281e207d7f93bfc267d586 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 27 Jul 2022 17:21:01 +0200
Subject: [PATCH] Fix #474

---
 Gemfile.lock                                  | 36 +++++++++----------
 .../websites/posts/curations_controller.rb    |  2 +-
 app/services/curator.rb                       | 13 +++++--
 app/services/wordpress.rb                     |  3 ++
 .../websites/posts/curations/new.html.erb     | 17 ++-------
 5 files changed, 36 insertions(+), 35 deletions(-)

diff --git a/Gemfile.lock b/Gemfile.lock
index 1f847af67..dde0f3530 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -89,7 +89,7 @@ GEM
     autoprefixer-rails (10.4.7.0)
       execjs (~> 2)
     aws-eventstream (1.2.0)
-    aws-partitions (1.608.0)
+    aws-partitions (1.610.0)
     aws-sdk-core (3.131.3)
       aws-eventstream (~> 1, >= 1.0.2)
       aws-partitions (~> 1, >= 1.525.0)
@@ -102,15 +102,15 @@ GEM
       aws-sdk-core (~> 3, >= 3.127.0)
       aws-sdk-kms (~> 1)
       aws-sigv4 (~> 1.4)
-    aws-sigv4 (1.5.0)
+    aws-sigv4 (1.5.1)
       aws-eventstream (~> 1, >= 1.0.2)
     bcrypt (3.1.18)
     bindex (0.8.1)
     bootsnap (1.12.0)
       msgpack (~> 1.2)
-    bootstrap (5.1.3)
+    bootstrap (5.2.0)
       autoprefixer-rails (>= 9.1.0)
-      popper_js (>= 2.9.3, < 3)
+      popper_js (>= 2.11.5, < 3)
       sassc-rails (>= 2.0.0)
     bootstrap5-kaminari-views (0.0.1)
       kaminari (>= 0.13)
@@ -211,7 +211,7 @@ GEM
     ffi (1.15.5)
     figaro (1.2.0)
       thor (>= 0.14.0, < 2)
-    font-awesome-sass (6.1.1)
+    font-awesome-sass (6.1.2)
       sassc (~> 2.0)
     front_matter_parser (1.0.1)
     gdpr (1.2.3)
@@ -294,19 +294,19 @@ GEM
     mini_magick (4.11.0)
     mini_mime (1.1.2)
     minitest (5.16.2)
-    msgpack (1.5.3)
+    msgpack (1.5.4)
     multi_json (1.15.0)
     multi_xml (0.6.0)
     multipart-post (2.2.3)
-    mustermann (1.1.2)
+    mustermann (2.0.2)
       ruby2_keywords (~> 0.0.1)
     nesty (1.0.2)
     nio4r (2.5.8)
-    nokogiri (1.13.7-arm64-darwin)
+    nokogiri (1.13.8-arm64-darwin)
       racc (~> 1.4)
-    nokogiri (1.13.7-x86_64-darwin)
+    nokogiri (1.13.8-x86_64-darwin)
       racc (~> 1.4)
-    nokogiri (1.13.7-x86_64-linux)
+    nokogiri (1.13.8-x86_64-linux)
       racc (~> 1.4)
     oauth2 (1.4.10)
       faraday (>= 0.17.3, < 3.0)
@@ -328,8 +328,8 @@ GEM
       omniauth (~> 2.0)
       ruby-saml (~> 1.12)
     orm_adapter (0.5.0)
-    pg (1.4.1)
-    popper_js (2.9.3)
+    pg (1.4.2)
+    popper_js (2.11.5)
     public_suffix (4.0.7)
     puma (5.6.4)
       nio4r (~> 2.0)
@@ -337,7 +337,7 @@ GEM
     rack (2.2.4)
     rack-mini-profiler (2.3.4)
       rack (>= 1.2.0)
-    rack-protection (2.2.1)
+    rack-protection (2.2.2)
       rack
     rack-test (2.0.2)
       rack (>= 1.3)
@@ -425,10 +425,10 @@ GEM
     simple_form_password_with_hints (0.0.6)
       rails
       simple_form
-    sinatra (2.2.1)
-      mustermann (~> 1.0)
+    sinatra (2.2.2)
+      mustermann (~> 2.0)
       rack (~> 2.2)
-      rack-protection (= 2.2.1)
+      rack-protection (= 2.2.2)
       tilt (~> 2.0)
     sixarm_ruby_unaccent (1.2.0)
     spring (4.0.0)
@@ -442,10 +442,10 @@ GEM
     terminal-table (3.0.2)
       unicode-display_width (>= 1.1.1, < 3)
     thor (1.2.1)
-    tilt (2.0.10)
+    tilt (2.0.11)
     typhoeus (1.4.0)
       ethon (>= 0.9.0)
-    tzinfo (2.0.4)
+    tzinfo (2.0.5)
       concurrent-ruby (~> 1.0)
     unf (0.1.4)
       unf_ext
diff --git a/app/controllers/admin/communication/websites/posts/curations_controller.rb b/app/controllers/admin/communication/websites/posts/curations_controller.rb
index 04f417d94..028c45310 100644
--- a/app/controllers/admin/communication/websites/posts/curations_controller.rb
+++ b/app/controllers/admin/communication/websites/posts/curations_controller.rb
@@ -6,7 +6,7 @@ class Admin::Communication::Websites::Posts::CurationsController < Admin::Commun
   def create
     @curator = Curator.new @website, current_user, curation_params[:url]
     if @curator.valid?
-      redirect_to [:edit, :admin, @curator.post],
+      redirect_to [:admin, @curator.post],
                   notice: t('admin.successfully_created_html', model: @curator.post.to_s)
     else
       breadcrumb
diff --git a/app/services/curator.rb b/app/services/curator.rb
index 4a0a31cde..003f5e358 100644
--- a/app/services/curator.rb
+++ b/app/services/curator.rb
@@ -16,15 +16,24 @@ class Curator
   protected
 
   def create_post!
-    text = Wordpress.clean_html("#{page.text}<p><a href=\"#{@url}\" target=\"_blank\">Source</a></p>")
     @post = website.posts.create(
       university: website.university,
       title: page.title,
-      text: text,
       slug: page.title.parameterize,
       author: @user.person,
       published_at: Time.now
     )
+    @chapter = @post.blocks.create(
+      university: website.university,
+      template_kind: :chapter,
+      published: true,
+      position: 0
+    )
+    text = Wordpress.clean_html("#{page.text}<p><a href=\"#{@url}\" target=\"_blank\">Source</a></p>")
+    data = @chapter.data.deep_dup
+    data['text'] = text
+    @chapter.data = data
+    @chapter.save
   end
 
   def attach_image!
diff --git a/app/services/wordpress.rb b/app/services/wordpress.rb
index c2089b182..2b0240d70 100644
--- a/app/services/wordpress.rb
+++ b/app/services/wordpress.rb
@@ -10,6 +10,9 @@ class Wordpress
   end
 
   def self.clean_html(html)
+    # invalid byte sequence in UTF-8
+    # https://stackoverflow.com/questions/32826781/invalid-byte-sequence-in-utf-8-when-sanitizing-wordpress-export-content
+    html = html.force_encoding('UTF-8').scrub
     # Relaxed config : https://github.com/rgrove/sanitize/blob/main/lib/sanitize/config/relaxed.rb
     # iframe attributes from MDN : https://developer.mozilla.org/fr/docs/Web/HTML/Element/iframe
     fragment = Sanitize.fragment(html, Sanitize::Config.merge(Sanitize::Config::RELAXED,
diff --git a/app/views/admin/communication/websites/posts/curations/new.html.erb b/app/views/admin/communication/websites/posts/curations/new.html.erb
index 12ca16ce0..4a8e7f5f4 100644
--- a/app/views/admin/communication/websites/posts/curations/new.html.erb
+++ b/app/views/admin/communication/websites/posts/curations/new.html.erb
@@ -2,21 +2,10 @@
 
 <%= simple_form_for :curation, url: admin_communication_website_post_curations_path, html: { id: 'new_communication_website_post_curation' } do |f| %>
   <%= f.error_notification %>
+  <%= f.input :url,
+              label: 'URL',
+              value: params.dig(:curation, :url) %>
 
-  <div class="row">
-    <div class="col-md-8">
-      <div class="card flex-fill w-100">
-        <div class="card-header">
-          <h5 class="card-title mb-0"><%= t('content') %></h5>
-        </div>
-        <div class="card-body">
-          <%= f.input :url,
-                      label: 'URL',
-                      value: params.dig(:curation, :url) %>
-        </div>
-      </div>
-    </div>
-  </div>
   <% content_for :action_bar_right do %>
     <%= submit f %>
   <% end %>
-- 
GitLab