From ef0fe51e05f33fbb09ddd1e39f43e966c58d3fbb Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Mon, 21 Oct 2019 16:24:41 -0600 Subject: [PATCH] FIX: Include user id in notification webhook (#8195) The payload when receiving a notification webhook is pointless without knowing which user the notification is for. This fix adds the user_id to the notification serializer so that when you receive a notification webhook you can properly identify which user the notification is for. See https://meta.discourse.org/t/getting-the-target-user-for-notification-webhook-events/129052?u=blake for more details. --- app/serializers/notification_serializer.rb | 1 + .../serializers/notification_serializer_spec.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 spec/serializers/notification_serializer_spec.rb diff --git a/app/serializers/notification_serializer.rb b/app/serializers/notification_serializer.rb index 34780b86a6e..e01e639b457 100644 --- a/app/serializers/notification_serializer.rb +++ b/app/serializers/notification_serializer.rb @@ -3,6 +3,7 @@ class NotificationSerializer < ApplicationSerializer attributes :id, + :user_id, :notification_type, :read, :created_at, diff --git a/spec/serializers/notification_serializer_spec.rb b/spec/serializers/notification_serializer_spec.rb new file mode 100644 index 00000000000..63f305e00c4 --- /dev/null +++ b/spec/serializers/notification_serializer_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe NotificationSerializer do + describe '#as_json' do + fab!(:user) { Fabricate(:user) } + let(:notification) { Fabricate(:notification, user: user) } + let(:serializer) { NotificationSerializer.new(notification) } + let(:json) { serializer.as_json } + + it "returns the user_id" do + expect(json[:notification][:user_id]).to eq(user.id) + end + + end +end