Skip to content

Commit

Permalink
action cable set for conversations creation also
Browse files Browse the repository at this point in the history
  • Loading branch information
Eschults committed Aug 26, 2016
1 parent 752a496 commit 9cf8198
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
16 changes: 5 additions & 11 deletions app/assets/javascripts/components/inbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,15 @@ var Inbox = React.createClass({
},

updateMessageList: function(data) {
// if current_user is the sender, update message list and conversation list
if (this.props.user_id === data.sender_id) {
this.setState({
messages: this.state.messages.concat([data.message]),
conversations: data.sender_conversations
})
// if current_user is the receiver, update conversation list and message list only if selected conversation is new message's conversation
} else {
if (this.props.user_id === data.receiver_id) {
// if current_user is currently on the updated conversation, append message and update conversation list
if (this.state.selectedConversationId === data.message.conversation_id) {
this.setState({
messages: this.state.messages.concat([data.message]),
conversations: data.receiver_conversations
})
// if he's not on the updated conversation, update conversation list
} else {
this.setState({
conversations: data.receiver_conversations
Expand All @@ -353,12 +349,10 @@ var Inbox = React.createClass({
var that = this;
App.messages = App.cable.subscriptions.create('MessagesChannel', {
received: function(data) {
var conversationIds = that.state.conversations.map(function(conversation) {
return conversation.id;
})
if (conversationIds.includes(data.message.conversation_id)) {
if (data.receiver_id === that.props.user_id) {
this.updateMessageList(data);
}
debugger
},

connected: function() {
Expand Down
43 changes: 43 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,47 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!

private

def action_cable_params
other_user = @selected_conversation.other_user(current_user)
{
message: {
id: @message.id,
read_at: @message.read_at,
writer_avatar_url: @message.user.avatar_url,
writer_first_name: @message.user.first_name,
created_at: @message.created_at.strftime("%b %e, %l:%M%P"),
content: view_context.render_markdown(@message.content),
conversation_id: @message.conversation.id
},
sender_id: current_user.id,
receiver_id: other_user.id,
sender_conversations: @conversations.map do |conversation|
{
id: conversation.id,
other_user_picture_url: conversation.other_user(current_user).one_avatar_url,
other_user_first_name: conversation.other_user(current_user).first_name,
last_message_created_at: conversation.last_message.created_at.strftime("%b %e"),
last_message_content: conversation.last_message.content,
last_message_read_at: conversation.last_message.read_at,
is_last_message_writer_current_user: conversation.last_message.user == current_user,
user: conversation.other_user(current_user)
}
end,
receiver_conversations: other_user.conversations.map do |conversation|
{
id: conversation.id,
other_user_picture_url: conversation.other_user(other_user).one_avatar_url,
other_user_first_name: conversation.other_user(other_user).first_name,
last_message_created_at: conversation.last_message.created_at.strftime("%b %e"),
last_message_content: conversation.last_message.content,
last_message_read_at: conversation.last_message.read_at,
is_last_message_writer_current_user: conversation.last_message.user == other_user,
user: conversation.other_user(other_user)
}
end
}
end
end
1 change: 1 addition & 0 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def create
@message.save
@messages = @selected_conversation.messages.order(created_at: :desc).page(params[:page]).per(9)
@conversations = current_user.conversations
ActionCable.server.broadcast('messages', action_cable_params)
end

private
Expand Down
40 changes: 0 additions & 40 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def create
@message.save
@messages = @selected_conversation.messages.order(created_at: :desc).page(params[:page]).per(9)
@conversations = current_user.conversations
other_user = @selected_conversation.other_user(current_user)
ActionCable.server.broadcast('messages', action_cable_params)
end

Expand All @@ -17,43 +16,4 @@ def create
def message_params
params.require(:message).permit(:content)
end

def action_cable_params
{
message: {
id: @message.id,
read_at: @message.read_at,
writer_avatar_url: @message.user.avatar_url,
writer_first_name: @message.user.first_name,
created_at: @message.created_at.strftime("%b %e, %l:%M%P"),
content: view_context.render_markdown(@message.content),
conversation_id: @message.conversation.id
},
sender_id: current_user.id,
sender_conversations: @conversations.map do |conversation|
{
id: conversation.id,
other_user_picture_url: conversation.other_user(current_user).one_avatar_url,
other_user_first_name: conversation.other_user(current_user).first_name,
last_message_created_at: conversation.last_message.created_at.strftime("%b %e"),
last_message_content: conversation.last_message.content,
last_message_read_at: conversation.last_message.read_at,
is_last_message_writer_current_user: conversation.last_message.user == current_user,
user: conversation.other_user(current_user)
}
end,
receiver_conversations: other_user.conversations.map do |conversation|
{
id: conversation.id,
other_user_picture_url: conversation.other_user(other_user).one_avatar_url,
other_user_first_name: conversation.other_user(other_user).first_name,
last_message_created_at: conversation.last_message.created_at.strftime("%b %e"),
last_message_content: conversation.last_message.content,
last_message_read_at: conversation.last_message.read_at,
is_last_message_writer_current_user: conversation.last_message.user == other_user,
user: conversation.other_user(other_user)
}
end
}
end
end

0 comments on commit 9cf8198

Please sign in to comment.