Skip to content

Commit

Permalink
API: Don't return notifications target unless it's a post
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Feb 8, 2020
1 parent 04d0d6d commit 0dbafa1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/notifications_controller.rb
Expand Up @@ -15,7 +15,7 @@ def show
notification = service.get_by_guid(params[:id])

if notification
render json: NotificationPresenter.new(notification).as_api_json(true)
render json: NotificationPresenter.new(notification).as_api_json
else
render_error 404, "Notification with provided guid could not be found"
end
Expand Down
4 changes: 2 additions & 2 deletions app/presenters/notification_presenter.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true

class NotificationPresenter < BasePresenter
def as_api_json(include_target=true)
def as_api_json
data = base_hash
data = data.merge(target: target_json) if include_target && linked_object
data = data.merge(target: target_json) if linked_object&.is_a?(Post)
data
end

Expand Down
2 changes: 1 addition & 1 deletion lib/schemas/api_v1.json
Expand Up @@ -213,7 +213,7 @@
"items": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/short_profile" }
}
},
"required": ["guid", "type", "read", "created_at", "target"],
"required": ["guid", "type", "read", "created_at"],
"additionalProperties": false
},

Expand Down
52 changes: 35 additions & 17 deletions spec/presenters/notification_presenter_spec.rb
@@ -1,31 +1,49 @@
# frozen_string_literal: true

describe NotificationPresenter do
before do
@post = FactoryGirl.create(:status_message)
@notification = FactoryGirl.create(:notification, recipient: alice, target: @post)
end

it "makes json with target when requested" do
json = NotificationPresenter.new(@notification).as_api_json(true)
expect(json[:guid]).to eq(@notification.guid)
it "makes json with target" do
post = FactoryGirl.create(:status_message)
notification = FactoryGirl.create(:notification, recipient: alice, target: post)
json = NotificationPresenter.new(notification).as_api_json
expect(json[:guid]).to eq(notification.guid)
expect(json[:type]).to eq("also_commented")
expect(json[:read]).to be_falsey
expect(json[:created_at]).to eq(@notification.created_at)
expect(json[:target][:guid]).to eq(@post.guid)
expect(json[:created_at]).to eq(notification.created_at)
expect(json[:target][:guid]).to eq(post.guid)
expect(json[:event_creators].length).to eq(1)
end

it "makes json with without target" do
json = NotificationPresenter.new(@notification).as_api_json(false)
expect(json.has_key?(:target)).to be_falsey
end

it "Makes target on mentioned" do
it "returns target on mentioned" do
mentioned_post = FactoryGirl.create(:status_message_in_aspect, author: alice.person, text: text_mentioning(bob))
Notifications::MentionedInPost.notify(mentioned_post, [bob.id])
notification = Notifications::MentionedInPost.last
json = NotificationPresenter.new(notification).as_api_json(true)
json = NotificationPresenter.new(notification).as_api_json
expect(json[:target][:guid]).to eq(mentioned_post.guid)
end

it "returns target on also_commented" do
post = FactoryGirl.create(:status_message)
bob.comment!(post, "cool")
comment2 = FactoryGirl.create(:comment, post: post)
Notifications::AlsoCommented.notify(comment2, [])
notification = Notifications::AlsoCommented.last
json = NotificationPresenter.new(notification).as_api_json
expect(json[:target][:guid]).to eq(post.guid)
end

it "returns no target on started_sharing" do
contact = FactoryGirl.create(:contact)
Notifications::StartedSharing.notify(contact, [bob.id])
notification = Notifications::StartedSharing.last
json = NotificationPresenter.new(notification).as_api_json
expect(json[:target]).to be_nil
end

it "returns no target on contacts_birthday" do
contact = FactoryGirl.create(:contact)
Notifications::ContactsBirthday.notify(contact, [bob.id])
notification = Notifications::ContactsBirthday.last
json = NotificationPresenter.new(notification).as_api_json
expect(json[:target]).to be_nil
end
end

0 comments on commit 0dbafa1

Please sign in to comment.