Skip to content

Commit

Permalink
Implement gmail like inbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrec committed Sep 21, 2023
1 parent db27e7b commit e0d3d11
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/components/icons/archived_messages_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- preserveAspectRatio="none" -->
<svg width="20" height="18" viewBox="0 0 20 18" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3 5H17M3 5C2.46957 5 1.96086 4.78929 1.58579 4.41421C1.21071 4.03914 1 3.53043 1 3C1 2.46957 1.21071 1.96086 1.58579 1.58579C1.96086 1.21071 2.46957 1 3 1H17C17.5304 1 18.0391 1.21071 18.4142 1.58579C18.7893 1.96086 19 2.46957 19 3C19 3.53043 18.7893 4.03914 18.4142 4.41421C18.0391 4.78929 17.5304 5 17 5M3 5V15C3 15.5304 3.21071 16.0391 3.58579 16.4142C3.96086 16.7893 4.46957 17 5 17H15C15.5304 17 16.0391 16.7893 16.4142 16.4142C16.7893 16.0391 17 15.5304 17 15V5M8 9H12" stroke="#9CA3AF" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
2 changes: 2 additions & 0 deletions app/components/icons/archived_messages_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Icons::ArchivedMessagesComponent < ViewComponent::Base
end
2 changes: 1 addition & 1 deletion app/components/message_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex flex-col justify-stretch items-stretch">
<%= render MessageThreadHeaderComponent.new(@message.thread) %>
<%= render MessageThreadHeaderComponent.new(@message.thread, @inbox_tag) %>
<div class="flex flex-col justify-stretch items-stretch gap-2 p-4">
<% if @notice %>
<div class="bg-blue-100 border-t border-b border-blue-500 text-blue-700 px-4 py-3 w-full" role="alert">
Expand Down
3 changes: 2 additions & 1 deletion app/components/message_component.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/components/message_drafts_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex flex-col justify-stretch items-stretch">
<%= render MessageThreadHeaderComponent.new(@message.thread) %>
<%= render MessageThreadHeaderComponent.new(@message.thread, @inbox_tag) %>
<div class="flex flex-col justify-stretch items-stretch gap-2 p-4">
<% if @notice %>
<div class="bg-blue-100 border-t border-b border-blue-500 text-blue-700 px-4 py-3 w-full" role="alert">
Expand Down
6 changes: 6 additions & 0 deletions app/components/message_thread_header_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</div>
<div class="todo flex flex-col justify-start items-end gap-1">
Expand Down
3 changes: 2 additions & 1 deletion app/components/message_thread_header_component.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
23 changes: 21 additions & 2 deletions app/controllers/message_threads_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
8 changes: 4 additions & 4 deletions app/lib/sidebar_menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 2 additions & 0 deletions app/models/govbox/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
14 changes: 14 additions & 0 deletions app/models/message_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion app/models/message_thread_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions app/policies/message_thread_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ def update?
def merge?
true
end

def archive?
true
end

def move_to_inbox?
true
end
end
2 changes: 1 addition & 1 deletion app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
@@ -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) %>
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e0d3d11

Please sign in to comment.