Skip to content

Commit

Permalink
fixed conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
Eschults committed Aug 24, 2016
1 parent 10ca4fb commit b23d0bf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
20 changes: 15 additions & 5 deletions app/assets/javascripts/components/inbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,27 @@ var Inbox = React.createClass({
},

updateMessageList: function(data) {
this.setState({
messages: this.state.messages.concat([data.message]),
conversations: data.conversations
})
if (this.props.user_id === data.sender_id) {
this.setState({
messages: this.state.messages.concat([data.message]),
conversations: data.sender_conversations
})
} else {
this.setState({
messages: this.state.messages.concat([data.message]),
conversations: data.receiver_conversations
})
}
this._scrollWrapper();
},

setupSubscription: function() {
var that = this;
App.messages = App.cable.subscriptions.create('MessagesChannel', {
received: function(data) {
this.updateMessageList(data);
if (that.state.selectedConversationId === data.message.conversation_id) {
this.updateMessageList(data);
}
},

connected: function() {
Expand Down
19 changes: 17 additions & 2 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ 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', {
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)
content: view_context.render_markdown(@message.content),
conversation_id: @message.conversation.id
},
conversations: @conversations.map do |conversation|
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,
Expand All @@ -28,6 +31,18 @@ def create
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
Expand Down
2 changes: 2 additions & 0 deletions app/views/shared/_inbox.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ json.messages do
json.partial! "conversations/message", message: message
end
end

json.user_id current_user.id

0 comments on commit b23d0bf

Please sign in to comment.