Messaging with Rails and Mailboxer

Thank you for this awesome tutorial!

This is probably easy but I’m totally lost.
Basically I need to let a user send a message to multiple users but keep conversations ‘filtered’ in a way that recipients can only see the messages from the originator and the ones they own.

Does it make sense? Is this suitable for this?

UPDATE
I’ve edited the show view like this:

  <div class="panel-body">
    <% @conversation.messages.each do |message| %>
      <% if message.sender == @conversation.originator or message.sender == current_user %>
      <div class="messages">
        <div class="media">
          <div class="media-left">
            <%= gravatar_for message.sender, 45, message.sender.full_name %>
          </div>
          <div class="media-body">
            <h6 class="media-heading">
              <%= message.sender.full_name %> (<%= message.created_at.strftime("%-d %B %Y, %H:%M") %>)
            </h6>
            <p><%= message.body %></p>
          </div>
        </div>
      </div>
      <hr>
      <% end %>
    <% end %>
  </div>

Is there something more I should take into account?
Is this the best way to do it?

Well, you might extract those conditions somewhere from the views, but all in all this seems to be okay!

Thank you! Actually this won’t work for what I’m trying to achieve.

Is it possible to split a new message in different conversations depending on the number of recipients chosen?

Is this ok?

  def create
    recipients = User.where(id: params['recipients'])
    recipients.each do |recipient|
    	conversation = current_user.send_message(recipient, params[:message][:body], params[:message][:subject]).conversation
    end
    flash[:success] = "Message has been sent!"
    redirect_to conversations_path
  end

Of course, I haven’t tested, but at the first glance it seems okay, though I’m not sure why would you need this :slight_smile:

I need some way a user can send the same message to multiple users but keep the conversations separated.
I guess this may sound weird and it misses the all idea of conversations.

I was thinking I could make this the default behavior and add a checkbox on new message view to set the message to be a private conversation (splitting it) or grouped.

Does it make sense? Is it possible or am I thinking it the wrong way and should build something from scratch?

I’m not sure what’s wrong, but my issue was fixed by copying the initializer from the demo code into my project. Go figure. Thanks for the tutorial!

Thanks so much for taking the time to write this. It really helped in getting mailboxer setup. I couldn’t find any solid docs on implementing the Message#create method for multiple models, since the recipients array is just id’s (might be possible/easier if looking up by unique usernames). I ended up doing it this way for anyone that needs some direction.

In the recipients_options helper, I updated the value to include the class (User, Duck, Cylon, etc.):

<option value='#{user.class.name}:#{user.user_id}'>#{user.name}</option>

Then in the Messages controller, I just had to break it apart to figure out what type of user it is:

def create
    recipients = []
    params[:recipients].each do |r|
      if r.split(':').first === 'User'
        recipient = User.find(r.split(':').last)
        recipients << recipient
      elsif r.split(':').first === 'Duck'
        recipient = Duck.find(r.split(':').last)
        recipients << recipient
      elsif r.split(':').first === 'Cylon'
        recipient = Cylon.find(r.split(':').last)
        recipients << recipient
      end
    end
    conversation = @user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversation_path(conversation)
  end

@bodrovis - Would love to hear your thoughts on this execution.

I’ll try to look into it.

I believe you can stick with eval here.

recipient = eval("#{r.split(':').first}.find(r.split(':').last)"

Just add some error checkings and whitelist models that can be referenced.

@bodrovis That certainly looks cleaner. I’ll take a look at implementing. Thanks again for the update and for writing this great resource.

Hi there

Is this possible to create more than two conversations between two same users?
When I try to do this, it throws:

Validation failed: Notification base This message must be added to existing conversation (#)

Hi! That depends on what code are you using. Generally, yes it should be possible.

@bodrovis, I am calling

user.send_message(recipient, 'body', 'subject')

each time for same user and recipient. So, for each message, should be created a new conversation.

Can you please tell me how can I do this?

If you were following my tutorial, this should work right away. Doesn’t it work in the demo app on Heroku? If does, but you are experiencing errors with the same code, that’s most likely a bug and should be reported to the dev team I believe.

Hi, Great tutorial, thanks.

You mentionned that attachments can be added to messages.
Could you point me the right direction to do it?

Cheers

Thank you!

Here is the source code for send_message method: https://github.com/mailboxer/mailboxer/blob/b53bab9e93dafcfa499205f2e3170c2b8acb0064/lib/mailboxer/models/messageable.rb#L65

And here is the uploader: https://github.com/mailboxer/mailboxer/blob/1618ae474fa49d6cd583838fbaea827f21fe79cb/app/models/mailboxer/message.rb#L14

So basically they use carrierwave to manage that stuff https://github.com/carrierwaveuploader/carrierwave/

Thank you! That was easy :slight_smile:

Hi, I’ve tried using @chosen_recipients to fill the recipient value with user_id. It is able to grab the id i.e. “new?=2” but it does not populate recipient value when the message is being created and sent. any thoughts?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.