Skip to content

Commit

Permalink
Include message_read attribute in api output of messages to self
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Jan 19, 2025
1 parent 2074e9d commit e096cc3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/views/api/messages/_message.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ json.to_display_name message.recipient.display_name
json.title message.title
json.sent_on message.sent_on.xmlschema

json.message_read message.message_read if current_user.id == message.to_user_id

if current_user.id == message.from_user_id
json.deleted !message.from_user_visible
elsif current_user.id == message.to_user_id
json.message_read message.message_read
json.deleted !message.to_user_visible
end

Expand Down
3 changes: 2 additions & 1 deletion app/views/api/messages/_message.xml.builder
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ attrs = {
"body_format" => message.body_format
}

attrs["message_read"] = message.message_read if current_user.id == message.to_user_id

if current_user.id == message.from_user_id
attrs["deleted"] = !message.from_user_visible
elsif current_user.id == message.to_user_id
attrs["message_read"] = message.message_read
attrs["deleted"] = !message.to_user_visible
end

Expand Down
28 changes: 28 additions & 0 deletions test/controllers/api/messages_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,34 @@ def test_show
assert_equal msg.body, jsm["body"]
end

def test_show_message_to_self_read
user = create(:user)
message = create(:message, :sender => user, :recipient => user)
auth_header = bearer_authorization_header user

get api_message_path(message), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "message", :count => 1 do
assert_dom "> @message_read", "false"
end
end

def test_show_message_to_self_read_json
user = create(:user)
message = create(:message, :sender => user, :recipient => user)
auth_header = bearer_authorization_header user

get api_message_path(message, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
jsm = js["message"]
assert_not_nil jsm
assert jsm.key?("message_read")
assert_not jsm["message_read"]
end

def test_update_status
recipient = create(:user)
sender = create(:user)
Expand Down

0 comments on commit e096cc3

Please sign in to comment.