From 9eb30c1fa4a16c5604c365bf6868f329d91db921 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Thu, 16 Mar 2023 12:41:58 +0100
Subject: [PATCH] Wip

---
 app/models/communication/block.rb             | 13 -----
 app/models/communication/website/category.rb  |  1 -
 app/models/communication/website/git_file.rb  | 18 +++++--
 app/models/communication/website/menu.rb      |  1 -
 app/models/communication/website/menu/item.rb |  1 -
 app/models/communication/website/page.rb      |  1 -
 .../website/page/organization.rb              |  2 +-
 .../communication/website/page/person.rb      |  2 +-
 app/models/communication/website/post.rb      |  1 -
 .../communication/website/with_connections.rb | 11 ++++
 .../website/with_old_dependencies.rb          | 17 -------
 app/models/concerns/with_git.rb               | 51 ++++---------------
 app/models/concerns/with_websites.rb          |  3 ++
 app/models/education/diploma.rb               |  1 -
 app/models/education/program.rb               |  2 +-
 ...ebsites.rb => with_websites_categories.rb} | 16 +-----
 app/models/education/school.rb                | 10 +++-
 app/models/research/hal/publication.rb        |  1 -
 app/models/research/journal.rb                |  1 -
 app/models/research/journal/paper.rb          |  1 -
 app/models/research/journal/volume.rb         |  1 -
 app/models/university/organization.rb         |  1 -
 app/models/university/person.rb               |  3 +-
 config/initializers/active_storage.rb         |  4 --
 24 files changed, 52 insertions(+), 111 deletions(-)
 rename app/models/education/program/{with_websites.rb => with_websites_categories.rb} (50%)

diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb
index dc368edf3..5bdbc37db 100644
--- a/app/models/communication/block.rb
+++ b/app/models/communication/block.rb
@@ -28,7 +28,6 @@ class Communication::Block < ApplicationRecord
   include WithDependencies
   include WithPosition
   include WithUniversity
-  include WithWebsites
   include Sanitizable
 
   IMAGE_MAX_SIZE = 5.megabytes
@@ -84,18 +83,6 @@ class Communication::Block < ApplicationRecord
     template.data
   end
 
-  def website
-    about.website
-  rescue
-    nil
-  end
-
-  def websites
-    about.websites
-  rescue
-    []
-  end
-
   def display_dependencies
     template.display_dependencies
   end
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 8aaf20077..86a79193d 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -45,7 +45,6 @@ class Communication::Website::Category < ApplicationRecord
   include WithBlobs
   include WithBlocks
   include WithFeaturedImage
-  include WithGit
   include WithMenuItemTarget
   include WithPermalink
   include WithPosition
diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb
index 780e6264f..08c744661 100644
--- a/app/models/communication/website/git_file.rb
+++ b/app/models/communication/website/git_file.rb
@@ -29,10 +29,12 @@ class Communication::Website::GitFile < ApplicationRecord
   attr_accessor :will_be_destroyed
 
   def self.sync(website, object, destroy: false)
+    # A dependency does not always have a GitFile (ex: Communication::Block)
+    return unless object.respond_to?(:git_files)
     # Permalinks must be calculated BEFORE renders
-    object.manage_permalink_in_website(website) if Communication::Website::Permalink.supported_by?(object)
-    # Handle optional before-sync process
-    object.before_git_sync
+    manage_permalink object, website
+    # Blobs need to be completely analyzed, which is async
+    analyze_if_blob object
     # The git file might exist or not
     git_file = where(website: website, about: object).first_or_create
     # Mark for destruction if necessary
@@ -54,6 +56,16 @@ class Communication::Website::GitFile < ApplicationRecord
 
   protected
 
+  def self.manage_permalink(object, website)
+    return unless Communication::Website::Permalink.supported_by?(object)
+    object.manage_permalink_in_website(website)
+  end
+
+  def self.analyze_if_blob(object)
+    return unless object.is_a? ActiveStorage::Blob
+    object.analyze unless object.analyzed?
+  end
+
   def template_static
     if about.respond_to? :template_static
       about.template_static
diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb
index e93e33daa..d03c7db26 100644
--- a/app/models/communication/website/menu.rb
+++ b/app/models/communication/website/menu.rb
@@ -29,7 +29,6 @@
 #
 class Communication::Website::Menu < ApplicationRecord
   include Sanitizable
-  include WithGit
   include WithTranslations
   include WithUniversity
   include WithWebsites
diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb
index c8815d4fe..be62c4eec 100644
--- a/app/models/communication/website/menu/item.rb
+++ b/app/models/communication/website/menu/item.rb
@@ -37,7 +37,6 @@ class Communication::Website::Menu::Item < ApplicationRecord
   include WithPosition
   include WithTree
   include WithUniversity
-  include WithWebsites
 
   attr_accessor :skip_publication_callback
 
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 87bbf6d78..c2f25d608 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -52,7 +52,6 @@ class Communication::Website::Page < ApplicationRecord
   include WithBlocks
   include WithDuplication
   include WithFeaturedImage
-  include WithGit
   include WithMenuItemTarget
   include WithPosition
   include WithTree
diff --git a/app/models/communication/website/page/organization.rb b/app/models/communication/website/page/organization.rb
index 1ba321f7c..6bce06ad3 100644
--- a/app/models/communication/website/page/organization.rb
+++ b/app/models/communication/website/page/organization.rb
@@ -47,7 +47,7 @@ class Communication::Website::Page::Organization < Communication::Website::Page
   def display_dependencies
     super + 
     [website.config_default_permalinks] +
-    website.organizations
+    website.connected_organizations
   end
 
   protected
diff --git a/app/models/communication/website/page/person.rb b/app/models/communication/website/page/person.rb
index e6326aaf1..76f9fdc37 100644
--- a/app/models/communication/website/page/person.rb
+++ b/app/models/communication/website/page/person.rb
@@ -47,7 +47,7 @@ class Communication::Website::Page::Person < Communication::Website::Page
   def display_dependencies
     super + 
     [website.config_default_permalinks] +
-    website.people_with_facets
+    website.connected_people
   end
 
   protected
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 2db2dde06..15cb41f33 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -43,7 +43,6 @@ class Communication::Website::Post < ApplicationRecord
   include WithBlocks
   include WithDuplication
   include WithFeaturedImage
-  include WithGit
   include WithMenuItemTarget
   include WithPermalink
   include WithSlug # We override slug_unavailable? method
diff --git a/app/models/communication/website/with_connections.rb b/app/models/communication/website/with_connections.rb
index 0b33bad47..459f23ac1 100644
--- a/app/models/communication/website/with_connections.rb
+++ b/app/models/communication/website/with_connections.rb
@@ -26,6 +26,17 @@ module Communication::Website::WithConnections
     disconnect_object object
   end
 
+  # TODO factoriser avec les extranets
+  def connected_people
+    ids = connections.where(object_type: 'University::Person').pluck(:object_id)
+    University::Person.where(id: ids)
+  end
+
+  def connected_organizations
+    ids = connections.where(object_type: 'University::Organization').pluck(:object_id)
+    University::Organization.where(id: ids)
+  end
+
   protected
 
   def connect_object(object)
diff --git a/app/models/communication/website/with_old_dependencies.rb b/app/models/communication/website/with_old_dependencies.rb
index 100a1d8e2..527d1aa98 100644
--- a/app/models/communication/website/with_old_dependencies.rb
+++ b/app/models/communication/website/with_old_dependencies.rb
@@ -24,23 +24,6 @@ module Communication::Website::WithOldDependencies
 
   end
 
-  # Deprecated
-  def blocks
-    @blocks ||= begin
-      blocks = Communication::Block.where(about_type: 'Communication::Website::Page', about_id: pages)
-      blocks = blocks.or(Communication::Block.where(about_type: 'Communication::Website::Post', about_id: posts))
-      blocks = blocks.or(Communication::Block.where(about_type: 'Education::Program', about_id: education_programs)) if has_education_programs?
-      blocks = blocks.or(Communication::Block.where(about_type: 'Education::Diploma', about_id: education_diplomas)) if has_education_diplomas?
-      # TODO: Blocks from People & Organizations ?
-      blocks
-    end
-  end
-
-  # Deprecated
-  def blocks_dependencies
-    @blocks_dependencies ||= blocks.collect(&:git_dependencies).flatten.compact.uniq
-  end
-
   def education_diplomas
     has_education_diplomas? ? about.diplomas : Education::Diploma.none
   end
diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb
index aa063e78e..90b7dc077 100644
--- a/app/models/concerns/with_git.rb
+++ b/app/models/concerns/with_git.rb
@@ -24,11 +24,6 @@ module WithGit
     path
   end
 
-  def before_git_sync
-    # Can be override to force some process before sync
-    # ex: ActiveStorage Blob analyze
-  end
-
   def save_and_sync
     if save
       sync_with_git
@@ -54,8 +49,8 @@ module WithGit
 
   def sync_with_git
     websites_for_self.each do |website|
-      website.touch
       next unless website.git_repository.valid?
+      Communication::Website::GitFile.sync website, self
       dependencies.each do |object|
         Communication::Website::GitFile.sync website, object
       end
@@ -65,48 +60,20 @@ module WithGit
   handle_asynchronously :sync_with_git, queue: 'default'
 
   def destroy_from_git
-    websites_for_self.each do |website|
+    websites.each do |website|
       next unless website.git_repository.valid?
-      # FIXME
-      dependencies = git_destroy_dependencies(website).to_a.flatten.uniq.compact
-      dependencies.each do |object|
-        Communication::Website::GitFile.sync website, object, destroy: true
-      end
+      Communication::Website::GitFile.sync website, self, destroy: true
+      # # FIXME
+      # dependencies = git_destroy_dependencies(website).to_a.flatten.uniq.compact
+      # dependencies.each do |object|
+      #   Communication::Website::GitFile.sync website, object, destroy: true
+      # end
       website.git_repository.sync!
     end
   end
-
-  # Deprecated
-  def has_website_for_self?(website)
-    websites_for_self.include?(website)
-  end
-  
-  # Deprecated
-  def git_dependencies(website = nil)
-    [self]
-  end
-  
-  # Deprecated
-  def git_destroy_dependencies(website = nil)
-    [self]
-  end
   
   protected
-  
-  # Deprecated
-  def in_block_dependencies?(website)
-    website.blocks_dependencies.include?(self)
-  end
-  
-  # Deprecated
-  def dependencies_through_blocks(website)
-    # We select all blocks having this object as dependency
-    blocks = website.blocks.select { |block| in? block.git_dependencies }
-    # We map them to their parent object (program, page, etc.)
-    blocks.map(&:about).uniq.compact
-  end
-  
-  # Deprecated
+
   def websites_for_self
     if is_a? Communication::Website
       [self]
diff --git a/app/models/concerns/with_websites.rb b/app/models/concerns/with_websites.rb
index d70bd1fb2..3c8b2ad97 100644
--- a/app/models/concerns/with_websites.rb
+++ b/app/models/concerns/with_websites.rb
@@ -1,7 +1,10 @@
+# Le website utilise WithGit sans WithWebsites, parce qu'il en est un
 module WithWebsites
   extend ActiveSupport::Concern
 
   included do 
+    include WithGit
+
     after_save :connect_to_websites
   end
 
diff --git a/app/models/education/diploma.rb b/app/models/education/diploma.rb
index c5352e64d..f63c561c4 100644
--- a/app/models/education/diploma.rb
+++ b/app/models/education/diploma.rb
@@ -25,7 +25,6 @@
 class Education::Diploma < ApplicationRecord
   include Sanitizable
   include WithBlocks
-  include WithGit
   include WithPermalink
   include WithSlug
   include WithUniversity
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index dd9c61993..12d8b9e11 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -57,7 +57,6 @@ class Education::Program < ApplicationRecord
   include WithBlocks
   include WithDiploma
   include WithFeaturedImage
-  include WithGit
   include WithInheritance
   include WithMenuItemTarget
   include WithPermalink
@@ -68,6 +67,7 @@ class Education::Program < ApplicationRecord
   include WithTree
   include WithUniversity
   include WithWebsites
+  include WithWebsitesCategories
 
   rich_text_areas_with_inheritance  :accessibility,
                                     :contacts,
diff --git a/app/models/education/program/with_websites.rb b/app/models/education/program/with_websites_categories.rb
similarity index 50%
rename from app/models/education/program/with_websites.rb
rename to app/models/education/program/with_websites_categories.rb
index 6fe2a4640..186c6dc88 100644
--- a/app/models/education/program/with_websites.rb
+++ b/app/models/education/program/with_websites_categories.rb
@@ -1,4 +1,4 @@
-module Education::Program::WithWebsites
+module Education::Program::WithWebsitesCategories
   extend ActiveSupport::Concern
 
   included do
@@ -12,20 +12,6 @@ module Education::Program::WithWebsites
                dependent: :destroy
   end
 
-  def websites
-    @websites ||= university.websites.reject do |website|
-      website_concerned = false
-      # Site de formation
-      website_concerned = true if website.about == self
-      # Site d'école
-      if website.about&.is_a? Education::School
-        # Formation dispensée dans l'école
-        website_concerned = true if self.in? website.about.programs
-      end
-      !website_concerned
-    end
-  end
-
   def set_websites_categories
     websites.each { |website| website.set_programs_categories! }
   end
diff --git a/app/models/education/school.rb b/app/models/education/school.rb
index 7e919ff72..b08323dd8 100644
--- a/app/models/education/school.rb
+++ b/app/models/education/school.rb
@@ -27,8 +27,8 @@ class Education::School < ApplicationRecord
   include Aboutable
   include Sanitizable
   include WithAlumni
+  include WithBlobs
   include WithCountry
-  include WithGit
   include WithPrograms # must come before WithAlumni and WithTeam
   include WithTeam
   include WithWebsites
@@ -87,4 +87,12 @@ class Education::School < ApplicationRecord
   def has_research_volumes?
     false
   end
+
+  protected
+
+  def explicit_blob_ids
+    [
+      logo&.blob_id
+    ]
+  end
 end
diff --git a/app/models/research/hal/publication.rb b/app/models/research/hal/publication.rb
index ca8f84adf..6cb7aecdb 100644
--- a/app/models/research/hal/publication.rb
+++ b/app/models/research/hal/publication.rb
@@ -22,7 +22,6 @@
 class Research::Hal::Publication < ApplicationRecord
   include Sanitizable
   include WithSlug
-  include WithGit
   include WithWebsites
 
   has_and_belongs_to_many :researchers,
diff --git a/app/models/research/journal.rb b/app/models/research/journal.rb
index 6be6ce32d..5e9c01332 100644
--- a/app/models/research/journal.rb
+++ b/app/models/research/journal.rb
@@ -23,7 +23,6 @@ class Research::Journal < ApplicationRecord
   include Sanitizable
   include Aboutable
   include WithUniversity
-  include WithGit
   include WithWebsites
 
   has_many :communication_websites, class_name: 'Communication::Website', as: :about, dependent: :nullify
diff --git a/app/models/research/journal/paper.rb b/app/models/research/journal/paper.rb
index 800a2a8e8..b699dd1c8 100644
--- a/app/models/research/journal/paper.rb
+++ b/app/models/research/journal/paper.rb
@@ -45,7 +45,6 @@ class Research::Journal::Paper < ApplicationRecord
   include Sanitizable
   include WithBlobs
   include WithBlocks
-  include WithGit
   include WithPermalink
   include WithPosition
   include WithPublication
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index 057e91a2b..dbb1871c0 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -33,7 +33,6 @@ class Research::Journal::Volume < ApplicationRecord
   include Sanitizable
   include WithBlobs
   include WithFeaturedImage
-  include WithGit
   include WithPermalink
   include WithPublication
   include WithSlug
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index cfc1faa51..9081c6dfa 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -45,7 +45,6 @@ class University::Organization < ApplicationRecord
   include WithBlocks
   include WithCountry
   include WithGeolocation
-  include WithGit
   include WithPermalink
   include WithSlug
   include WithUniversity
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 26c0e4f17..eff10be72 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -55,7 +55,6 @@
 class University::Person < ApplicationRecord
   include Sanitizable
   include WithUniversity
-  include WithGit
   include WithBlobs
   include WithCountry
   include WithEducation
@@ -64,7 +63,7 @@ class University::Person < ApplicationRecord
   include WithPicture
   include WithRoles
   include WithBlocks
-  include WithPermalink
+  include WitvihPermalink
   include WithResearch
   include WithTranslations
   include WithWebsites
diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb
index ddc32d906..091896291 100644
--- a/config/initializers/active_storage.rb
+++ b/config/initializers/active_storage.rb
@@ -46,10 +46,6 @@ Rails.application.config.to_prepare do
     def git_path(website)
       "data/media/#{id[0..1]}/#{id}.yml"
     end
-
-    def before_git_sync
-      analyze unless analyzed?
-    end
   end
 
   ActiveStorage::Blob.include ActiveStorageGitPathStatic
-- 
GitLab