Rewriting messages functionality to use mail boxer

This commit is contained in:
Brenda Wallace
2019-07-20 19:43:29 +12:00
parent 26839acf01
commit d91a5bf55f
11 changed files with 138 additions and 164 deletions

View File

@@ -1,82 +1,67 @@
class ConversationsController < ApplicationController
respond_to :html
before_action :authenticate_member!
before_action :get_mailbox, :get_box # , :get_actor
# before_action :get_mailbox, :get_box
before_action :check_current_subject_in_conversation, :only => [:show, :update, :destroy]
def index
@conversations = if @box.eql? "inbox"
@mailbox.inbox.paginate(page: params[:page])
elsif @box.eql? "sentbox"
@mailbox.sentbox.paginate(page: params[:page])
@conversations = if box.eql? "inbox"
mailbox.inbox.paginate(page: params[:page])
elsif box.eql? "sentbox"
mailbox.sentbox.paginate(page: params[:page])
else
@mailbox.trash.paginate(page: params[:page])
mailbox.trash.paginate(page: params[:page])
end
respond_to do |format|
format.html { render @conversations if request.xhr? }
end
respond_with @conversations
end
def show
# byebug
# @receipts = @mailbox.receipts_for(@conversation)
# @receipts.mark_as_read
# render action: :show
@receipts = mailbox.receipts_for(@conversation)
@receipts.mark_as_read
end
def update
@conversation.untrash(@actor) if params[:untrash].present?
if params[:reply_all].present?
last_receipt = @mailbox.receipts_for(@conversation).last
last_receipt = mailbox.receipts_for(@conversation).last
@receipt = @actor.reply_to_all(last_receipt, params[:body])
end
@receipts = if @box.eql? 'trash'
@mailbox.receipts_for(@conversation).trash
@receipts = if box.eql? 'trash'
mailbox.receipts_for(@conversation).trash
else
@mailbox.receipts_for(@conversation).not_trash
mailbox.receipts_for(@conversation).not_trash
end
redirect_to action: :show
@receipts.mark_as_read
end
def destroy
@conversation.move_to_trash(@actor)
respond_to do |format|
format.html do
if params[:location].present? && (params[:location] == 'conversation')
redirect_to conversations_path(box: :trash)
else
redirect_to conversations_path(box: @box, page: params[:page])
end
end
format.js do
if params[:location].present? && (params[:location] == 'conversation')
render js: "window.location = '#{conversations_path(box: @box, page: params[:page])}';"
else
render 'conversations/destroy'
end
end
end
@conversation = Mailboxer::Conversation.find params[:id]
current_member.mark_as_deleted @conversation
redirect_to conversations_path
end
private
def get_mailbox
@mailbox = current_member.mailbox
def mailbox
current_member.mailbox
end
def get_box
params[:box] = 'inbox' if params[:box].blank? || !%w(inbox sentbox trash).include?(params[:box])
@box = params[:box]
def box
if params[:box].blank? || !%w(inbox sentbox trash).include?(params[:box])
'inbox'
else
params[:box]
end
end
def check_current_subject_in_conversation
@conversation = Mailboxer::Conversation.find_by(id: params[:id])
if @conversation.nil? || !@conversation.is_participant?(current_member)
redirect_to conversations_path(box: @box)
redirect_to conversations_path(box: box)
return
end
end

View File

@@ -1,15 +1,13 @@
class MessagesController < ApplicationController
respond_to :html, :json
before_action :authenticate_member!
before_action :get_mailbox #, :get_box, :get_actor
def index
redirect_to conversations_path(box: @box)
end
# GET /messages/1
# GET /messages/1.xml
def show
if (@message = Message.find_by(id: params[:id])) && (@conversation = @message.conversation)
if @conversation.is_participant?(@actor)
if @conversation.is_participant?(current_member)
redirect_to conversation_path(@conversation, box: @box, anchor: "message_" + @message.id.to_s)
return
end
@@ -18,64 +16,33 @@ class MessagesController < ApplicationController
end
def new
@message = Mailboxer::Message.new
if params[:recipient_id].present?
@recipient = Member.find_by(id: params[:recipient_id])
return if @recipient.nil?
# @recipient = nil if Member.normalize(@recipient) == Actor.normalize(current_subject)
end
end
# GET /messages/1/edit
def edit; end
# POST /messages
# POST /messages.xml
def create
@recipients =
if params[:_recipients].present?
@recipients = params[:_recipients].split(',').map { |r| Actor.find(r) }
else
[]
end
@receipt = @actor.send_message(@recipients, params[:body], params[:subject])
if @receipt.errors.blank?
@conversation = @receipt.conversation
flash[:success] = t('mailboxer.sent')
redirect_to conversation_path(@conversation, box: :sentbox)
if params[:conversation_id].present?
@conversation = Mailboxer::Conversation.find(params[:conversation_id])
# receipts = conversation.receipts_for alfa
current_member.reply_to_conversation(@conversation, params[:body])
redirect_to conversation_path(@conversation)
else
render action: :new
recipient = Member.find(params[:recipient_id])
body = params[:body]
subject = params[:subject]
@conversation = current_member.send_message(recipient, body, subject)
redirect_to conversations_path(box: 'sentbox')
end
end
# PUT /messages/1
# PUT /messages/1.xml
def update; end
# DELETE /messages/1
# DELETE /messages/1.xml
def destroy; end
private
def get_mailbox
@mailbox = current_subject.mailbox
end
def get_actor
@actor = Actor.normalize(current_subject)
end
def get_box
if params[:box].blank? || !%w(inbox sentbox trash).include?(params[:box])
@box = "inbox"
return
end
@box = params[:box]
end
def current_subject
@current_subject ||=
# current_subject_from_params ||

View File

@@ -9,7 +9,7 @@ class Notification < ApplicationRecord
scope :by_recipient, ->(recipient) { where(recipient_id: recipient) }
before_create :replace_blank_subject
# after_create :create_mailboxer_message
after_create :create_mailboxer_message
def self.unread_count
unread.size
@@ -19,9 +19,7 @@ class Notification < ApplicationRecord
self.subject = "(no subject)" if subject.nil? || subject =~ /^\s*$/
end
# def create_mailboxer_message
# byebug
# sender.send_message(recipient, body, subject)
# Notifier.notify(self).deliver_now! if recipient.send_notification_email
# end
def create_mailboxer_message
sender.send_message(recipient, body, subject)
end
end

View File

@@ -5,35 +5,45 @@
- unless @conversations.empty?
= paginate @conversations
.row
.col-2
%ul
- %W[inbox sentbox trash].each do |box|
%li.nav-item
= link_to conversations_path(box: box), class: 'nav-link' do
= box
= icon 'fas', box
.col-10
%table.table
%thead.black.white-text
%tr
%th{scope: "col"} Sender
%th{scope: "col"} Subject
%th{scope: "col"} Sent at
%th{scope: "col"}
%tbody
- @conversations.each do |conversation|
%ul.nav.grey.lighten-4.py-4
- %W[inbox sentbox trash].each do |box|
%li.nav-item= link_to box, conversations_path(box: box), class: 'nav-link'
- @conversations.each do |conversation|
.card.message
.card-body
.row
.col-6.col-md-9
%h3= link_to conversation.subject, conversation_path(conversation)
-# %p
-# - if n.read
-# = icon 'far', 'envelope-open'
-# - else
-# = icon 'far', 'envelope'
-# %strong unread
-# = n.created_at
-# .col-6.col-md-3.text-right
-# = link_to n.sender do
-# %h3
-# = render 'members/tiny', member: n.sender
-# = n.sender
-# = render 'members/follow_buttons', member: n.sender
.card-footer
= link_to 'Read', conversation_path(conversation), class: 'btn btn-primary'
-#= link_to 'Reply', reply_link(conversation), class: 'btn btn-secondary'
= link_to conversation_path(conversation), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default btn-danger btn-xs' do
= delete_icon
= t('buttons.delete')
- unless @conversations.empty?
= paginate @conversations
%tr{class: conversation.receipts_for(current_member).last.is_unread? ? 'active': ''}
%td
-# - conversation.recipients.each do |member|
-# - unless member == current_member
-# .float-left= render 'members/tiny', member: member
-# = link_to member.login_name, member
%td
%h3.h3-responsive
= link_to conversation_path(conversation) do
- if conversation.receipts_for(current_member).last.is_unread?
= icon 'far', 'envelope'
- else
= icon 'far', 'envelope-open'
=conversation.subject
%td
=conversation.updated_at
.text-muted (#{time_ago_in_words conversation.updated_at} ago)
%td
= link_to conversation_path(conversation), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default btn-danger btn-xs' do
= delete_icon
= t('buttons.delete')
- unless @conversations.empty?
= paginate @conversations

View File

@@ -8,32 +8,34 @@
.card-header
%h6 Participants
.card-body
- @conversation.recipients.each do |r|
%li
= render 'members/tiny', member: r
= link_to r, r
-# - @conversation.recipients.each do |r|
-# %li
-# = render 'members/tiny', member: r
-# = link_to r, r
.col-10
%h1= @conversation.subject
- @conversation.messages.each do |message|
.card
.card
- @conversation.messages.order(:created_at).each do |message|
- message
.card-body
.card-title= message.subject
.float-right
= link_to message.sender do
= message.sender
= render 'members/tiny', member: message.sender
%p.text-muted
From
= link_to message.sender, message.sender
on
= message.created_at
.well
:growstuff_markdown
#{ strip_tags(message.body) }
.col
.float-left
= link_to message.sender do
= message.sender
= render 'members/tiny', member: message.sender
.col
%p.text-muted
From
= link_to message.sender, message.sender
on
= message.created_at
.well
:growstuff_markdown
#{ strip_tags(message.body) }
-# .card-footer
-# = link_to 'Reply', @reply_link, class: 'btn btn-primary'
-# = link_to message, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default btn-danger' do
-# = delete_icon
-# = t('buttons.delete')
.card-footer
-# = link_to 'Reply', @reply_link, class: 'btn btn-primary'
-# = link_to message, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default btn-danger' do
-# = delete_icon
-# = t('buttons.delete')
= render 'messages/form', conversation: @conversation

View File

@@ -1 +1 @@
= image_tag(avatar_uri(member, 50), alt: '', class: 'img img-fluid avatar', height: 50)
= image_tag(avatar_uri(member, 50), alt: '', class: 'img img-fluid avatar', height: 50, width: 50)

View File

@@ -1,4 +1,4 @@
= bootstrap_form_for @message do |f|
= bootstrap_form_tag url: '/messages' do |f|
.card
.card-body
-# - if @notification.errors.any?
@@ -11,16 +11,26 @@
-# %li= msg
.field
= f.hidden_field :recipient_id, value: @recipient.id
%p
To
= link_to @recipient, @recipient
= render 'members/tiny', member: @recipient
= f.text_field :subject, value: @subject, class: 'form-control', maxlength: 255
- if @conversation.present?
= f.hidden_field :conversation_id, value: @conversation.id
-# To
-# - @conversation.recipients.each do |recipient|
-# - unless recipient == current_member
-# = link_to recipient, recipient
-# = render 'members/tiny', member: recipient
- elsif @recipient.present?
%p
To
= link_to @recipient, @recipient
= render 'members/tiny', member: @recipient
= f.hidden_field :recipient_id, value: @recipient.id
= f.text_field :subject, value: @subject, class: 'form-control', maxlength: 255
- else
TODO - add recipient field
= f.text_area :body, rows: 12, label: "Type your message here"
%span.help-block= render partial: "shared/markdown_help"
.card-footer
= link_to 'cancel', notifications_path, class: 'btn'
= link_to 'cancel', conversations_path, class: 'btn'
.float-right= f.submit "Send", class: 'btn btn-primary'

View File

@@ -1,6 +1,6 @@
= content_for :title, "Send a message to #{@recipient}"
- content_for :breadcrumbs do
%li.breadcrumb-item= link_to 'Messages', notifications_path
%li.breadcrumb-item= link_to 'Messages', conversations_path
= render 'form'

View File

@@ -12,8 +12,8 @@ Mailboxer.setup do |config|
# Configures if you use or not a search engine and which one you are using
# Supported engines: [:solr,:sphinx,:pg_search]
config.search_enabled = false
config.search_engine = :solr
# config.search_enabled = false
# config.search_engine = :solr
# Configures maximum length of the message subject and body
config.subject_max_length = 255

View File

@@ -97,9 +97,6 @@ Rails.application.routes.draw do
get 'followers' => 'follows#followers'
end
resources :notifications do
get 'reply'
end
resources :messages
resources :conversations

View File

@@ -1,4 +1,9 @@
class NotificationsToMailboxer < ActiveRecord::Migration[5.2]
def change
Notification.all.each do |n|
recipient = Member.with_deleted.find(n.recipient_id)
sender = Member.with_deleted.find(n.sender_id)
sender.send_message(recipient, n.body, n.subject)
end
end
end