diff --git a/app/components/message_component.rb b/app/components/message_component.rb
index e13834d88..4671bf082 100644
--- a/app/components/message_component.rb
+++ b/app/components/message_component.rb
@@ -1,8 +1,9 @@
class MessageComponent < ViewComponent::Base
renders_many :attachments
- def initialize(message:, notice:)
+ def initialize(message:, notice:, inbox_tag:)
@message = message
@notice = notice
+ @inbox_tag = inbox_tag
end
end
diff --git a/app/components/message_drafts_component.html.erb b/app/components/message_drafts_component.html.erb
index eb6b81510..2be517649 100644
--- a/app/components/message_drafts_component.html.erb
+++ b/app/components/message_drafts_component.html.erb
@@ -1,5 +1,5 @@
- <%= render MessageThreadHeaderComponent.new(@message.thread) %>
+ <%= render MessageThreadHeaderComponent.new(@message.thread, @inbox_tag) %>
<% if @notice %>
diff --git a/app/components/message_thread_header_component.html.erb b/app/components/message_thread_header_component.html.erb
index 7bcfc526e..3e667d9a9 100644
--- a/app/components/message_thread_header_component.html.erb
+++ b/app/components/message_thread_header_component.html.erb
@@ -15,6 +15,12 @@
<% end %>
<% end %>
<%= render Common::TagSelectorComponent.new(@message_thread) %>
+
+ <% if @message_thread.tags.include?(@inbox_tag) %>
+ <%= button_to 'Archivovať', archive_message_thread_path(@message_thread), method: :post, class: "flex justify-center items-center gap-2.5 px-3.5 py-2.5 rounded-md bg-blue-600 text-base font-medium text-left text-white hover:cursor-pointer" %>
+ <% else %>
+ <%= button_to 'Presunúť do doručených', move_to_inbox_message_thread_path(@message_thread), method: :post, class: "flex justify-center items-center gap-2.5 px-3.5 py-2.5 rounded-md bg-blue-600 text-base font-medium text-left text-white hover:cursor-pointer" %>
+ <% end %>
diff --git a/app/components/message_thread_header_component.rb b/app/components/message_thread_header_component.rb
index c1bb566f6..69744d44f 100644
--- a/app/components/message_thread_header_component.rb
+++ b/app/components/message_thread_header_component.rb
@@ -1,5 +1,6 @@
class MessageThreadHeaderComponent < ViewComponent::Base
- def initialize(message_thread)
+ def initialize(message_thread, inbox_tag)
@message_thread = message_thread
+ @inbox_tag = inbox_tag
end
end
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 2d248cb8f..d048496f4 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base
include Pundit::Authorization
after_action :verify_authorized
after_action :verify_policy_scoped, only: :index
- before_action :set_menu_context
+ before_action :set_menu_context, :set_inbox_tag
def pundit_user
Current.user
@@ -14,7 +14,11 @@ def get_menu
end
def set_menu_context
- @tags = policy_scope(Tag, policy_scope_class: TagPolicy::Scope).where(visible: true)
+ @tags = policy_scope(Tag, policy_scope_class: TagPolicy::Scope).where(visible: true).where.not(name: Tag::INBOX_TAG_NAME)
@menu = SidebarMenu.new(controller_name, action_name, { tags: @tags })
end
+
+ def set_inbox_tag
+ @inbox_tag = Tag.inbox_tag(Current.tenant) if pundit_user
+ end
end
diff --git a/app/controllers/message_threads_controller.rb b/app/controllers/message_threads_controller.rb
index df11b65c3..f20064941 100644
--- a/app/controllers/message_threads_controller.rb
+++ b/app/controllers/message_threads_controller.rb
@@ -1,5 +1,5 @@
class MessageThreadsController < ApplicationController
- before_action :set_message_thread, only: %i[show update]
+ before_action :set_message_thread, only: %i[show update archive move_to_inbox]
def show
authorize @message_thread
@@ -24,6 +24,7 @@ def index
@message_threads, @next_cursor = MessageThreadCollection.all(
scope: message_thread_policy_scope.includes(:tags),
search_permissions: search_permissions,
+ inbox_part: search_params[:type] || 'inbox',
query: search_params[:q],
no_visible_tags: search_params[:no_visible_tags] == '1' && Current.user.admin?,
cursor: cursor
@@ -51,6 +52,24 @@ def merge
redirect_to @selected_message_threads.first
end
+ def archive
+ authorize MessageThread
+ @message_thread.archive
+
+ flash[:notice] = 'Vlákno bolo úspešne archivované'
+
+ redirect_to message_thread_path(@message_thread)
+ end
+
+ def move_to_inbox
+ authorize MessageThread
+ @message_thread.move_to_inbox
+
+ flash[:notice] = 'Vlákno bolo presunuté do doručených správ'
+
+ redirect_to message_thread_path(@message_thread)
+ end
+
private
def set_message_thread
@@ -72,6 +91,6 @@ def message_thread_params
end
def search_params
- params.permit(:q, :no_visible_tags, :format, cursor: MessageThreadCollection::CURSOR_PARAMS)
+ params.permit(:q, :no_visible_tags, :type, :format, cursor: MessageThreadCollection::CURSOR_PARAMS)
end
end
diff --git a/app/lib/sidebar_menu.rb b/app/lib/sidebar_menu.rb
index 72b1180de..9bbcb5cbf 100644
--- a/app/lib/sidebar_menu.rb
+++ b/app/lib/sidebar_menu.rb
@@ -22,16 +22,16 @@ def initial_structure(controller, action)
def default_main_menu
[
TW::SidebarMenuItemComponent.new(name: 'Prehľad', url: root_path, icon: Icons::DashboardComponent.new),
- TW::SidebarMenuItemComponent.new(name: 'Správy', url: message_threads_path, icon: Icons::SchrankaComponent.new),
+ TW::SidebarMenuItemComponent.new(name: 'Doručené', url: message_threads_path, icon: Icons::SchrankaComponent.new),
+ TW::SidebarMenuItemComponent.new(name: 'Archivované', url: message_threads_path(type: 'archived'), icon: Icons::ArchivedMessagesComponent.new),
+ TW::SidebarMenuItemComponent.new(name: 'Všetky správy', url: message_threads_path(type: 'all'), icon: Icons::SchrankaComponent.new),
Layout::TagListComponent.new(tags: @parameters[:tags])
]
end
def admin_main_menu
+ default_main_menu +
[
- TW::SidebarMenuItemComponent.new(name: 'Prehľad', url: root_path, icon: Icons::DashboardComponent.new),
- TW::SidebarMenuItemComponent.new(name: 'Správy', url: message_threads_path, icon: Icons::SchrankaComponent.new),
- Layout::TagListComponent.new(tags: @parameters[:tags]),
TW::SidebarMenuDividerComponent.new(name: 'Nastavenia'),
TW::SidebarMenuItemComponent.new(name: 'Nastavenie pravidiel', url: settings_automation_rules_path, icon: Icons::SettingsComponent.new),
TW::SidebarMenuDividerComponent.new(name: 'Administrácia'),
diff --git a/app/models/govbox/message.rb b/app/models/govbox/message.rb
index 824ec0971..b5d0efb55 100644
--- a/app/models/govbox/message.rb
+++ b/app/models/govbox/message.rb
@@ -101,6 +101,7 @@ def self.create_message_objects(message, raw_message)
end
def self.create_message_tag(message, govbox_message)
+ inbox_tag = Tag.inbox_tag(govbox_message.box.tenant)
tag = Tag.find_or_create_by!(
name: "slovensko.sk:#{govbox_message.folder.full_name}",
tenant: govbox_message.box.tenant,
@@ -109,5 +110,6 @@ def self.create_message_tag(message, govbox_message)
message.tags << tag
message.thread.tags << tag unless message.thread.tags.include?(tag)
+ message.thread.tags << inbox_tag unless message.thread.tags.include?(inbox_tag)
end
end
diff --git a/app/models/message_thread.rb b/app/models/message_thread.rb
index 55e0f2d96..f5a910cf4 100644
--- a/app/models/message_thread.rb
+++ b/app/models/message_thread.rb
@@ -56,4 +56,18 @@ def self.merge_threads
target_thread.save!
end
end
+
+ def move_to_inbox
+ inbox_tag = Tag.inbox_tag(folder.box.tenant_id)
+
+ tags << inbox_tag unless tags.include?(inbox_tag)
+ save! # to fire events
+ end
+
+ def archive
+ inbox_tag = Tag.inbox_tag(folder.box.tenant_id)
+
+ tags.delete(inbox_tag) if tags.include?(inbox_tag)
+ save! # to fire events
+ end
end
diff --git a/app/models/message_thread_collection.rb b/app/models/message_thread_collection.rb
index 400d5a9a0..a5e5830ab 100644
--- a/app/models/message_thread_collection.rb
+++ b/app/models/message_thread_collection.rb
@@ -21,8 +21,14 @@ def self.serialize_cursor(cursor)
cursor
end
- def self.all(scope: nil, search_permissions:, query: nil, no_visible_tags: false, cursor:)
+ def self.all(scope: nil, search_permissions:, query: nil, inbox_part:, no_visible_tags: false, cursor:)
parsed_query = Searchable::MessageThreadQuery.parse(query.to_s)
+ if inbox_part == 'inbox'
+ parsed_query[:filter_labels] << Tag::INBOX_TAG_NAME
+ elsif inbox_part == 'archived'
+ parsed_query[:filter_out_labels] << Tag::INBOX_TAG_NAME
+ end
+
filter = Searchable::MessageThreadQuery.labels_to_ids(
parsed_query,
tenant_id: search_permissions.fetch(:tenant_id),
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 09bd3746b..71b6c8e9d 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -22,6 +22,12 @@ class Tag < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: { scope: :tenant_id, case_sensitive: false }
+ INBOX_TAG_NAME = 'Inbox'
+
after_update_commit ->(tag) { EventBus.publish(:tag_renamed, tag) if previous_changes.key?("name") }
after_destroy ->(tag) { EventBus.publish(:tag_removed, tag) }
+
+ def self.inbox_tag(tenant_id)
+ where(tenant_id: tenant_id).find_by_name!(INBOX_TAG_NAME)
+ end
end
diff --git a/app/policies/message_thread_policy.rb b/app/policies/message_thread_policy.rb
index 6ba588a6a..a6d1ddf77 100644
--- a/app/policies/message_thread_policy.rb
+++ b/app/policies/message_thread_policy.rb
@@ -38,4 +38,12 @@ def update?
def merge?
true
end
+
+ def archive?
+ true
+ end
+
+ def move_to_inbox?
+ true
+ end
end
diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb
index a04952616..7753ca5a5 100644
--- a/app/views/messages/show.html.erb
+++ b/app/views/messages/show.html.erb
@@ -1,5 +1,5 @@
<%= render TW::FlashComponent.new(flash: flash) %>
-<%= render MessageComponent.new(message: @message, notice: @notice) do |component| %>
+<%= render MessageComponent.new(message: @message, notice: @notice, inbox_tag: @inbox_tag) do |component| %>
<% @message.objects.each do |message_object| %>
<% component.with_attachment do %>
<% render MessageAttachmentComponent.new(message_attachment: message_object) %>
diff --git a/config/routes.rb b/config/routes.rb
index 12d0b86f0..5de8e1715 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -43,6 +43,10 @@
collection do
post 'merge'
end
+ member do
+ post 'archive'
+ post 'move_to_inbox'
+ end
resources :messages
end
resources :message_threads_tags