Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications are reversed #88

Closed
szechyjs opened this issue Oct 23, 2018 · 5 comments
Closed

Notifications are reversed #88

szechyjs opened this issue Oct 23, 2018 · 5 comments

Comments

@szechyjs
Copy link
Contributor

It seems that all my notifications are being sent in the wrong direction.

class User < ApplicationRecord
  acts_as_target
end
class Member < ApplicationRecord
  acts_as_notifiable :users,
    targets: ->(user, key) {
      User.with_role(:admin)
    },
    tracked: { only: [:create] }
end

In this case when I member.save I get the following error...

undefined method `subscribes_to_notification?' for #<Member:0x00007fa6ae60bc18>

From tracing it seems it is trying to send to the Member record instead of the Users. Users -> Member when it should be Member -> Users

I'm seeing similar behavior sending sending User -> User notifications as documented in my comment in issue #51 (comment)

@simukappu simukappu added bug need more info We need more information labels Nov 7, 2018
@simukappu
Copy link
Owner

Thank you for your feedback! I will check it in Dummy Rails application including gem.

Does anybody have the same issue? Or does anybody successfully make these configuration? (notification between one models / tracked: { only: [:create] } option etc.)

@szechyjs
Copy link
Contributor Author

szechyjs commented Nov 14, 2018

@simukappu I added the following to spec/rails_app/app/models/user.rb

    acts_as_notifiable :users,
      targets: -> (uer, key) {
        User.all
      },
      tracked: { only: [:create] }

I then create a new user through rails console

2.5.0 :004 > User.create(email: 'foo@bar.com')
   (0.1ms)  begin transaction
  User Create (0.5ms)  INSERT INTO "users" ("email", "confirmation_token", "confirmation_sent_at", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["email", "foo@bar.com"], ["confirmation_token", "dMcVdGtxbzp29wGa3BWP"], ["confirmation_sent_at", "2018-11-14 00:36:16.206725"], ["created_at", "2018-11-14 00:36:16.206592"], ["updated_at", "2018-11-14 00:36:16.206592"]]
  User Load (0.1ms)  SELECT "users".* FROM "users"
  ActivityNotification::Subscription Load (0.1ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."target_id" = ? AND "subscriptions"."target_type" = ? AND "subscriptions"."key" = ? ORDER BY "subscriptions"."id" ASC LIMIT ?  [["target_id", 6], ["target_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Load (0.1ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."target_type" = ? AND "notifications"."target_id" = ? AND "notifications"."notifiable_type" = ? AND "notifications"."key" = ? AND "notifications"."group_id" IS NULL AND "notifications"."group_owner_id" IS NULL AND "notifications"."opened_at" IS NULL ORDER BY "notifications"."created_at" ASC LIMIT ?  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Create (0.4ms)  INSERT INTO "notifications" ("target_type", "target_id", "notifiable_type", "notifiable_id", "key", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["notifiable_id", 1], ["key", "user.create"], ["created_at", "2018-11-14 00:36:16.214351"], ["updated_at", "2018-11-14 00:36:16.214351"]]
  ActivityNotification::Subscription Load (0.1ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."target_id" = ? AND "subscriptions"."target_type" = ? AND "subscriptions"."key" = ? ORDER BY "subscriptions"."id" ASC LIMIT ?  [["target_id", 6], ["target_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Load (0.2ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."target_type" = ? AND "notifications"."target_id" = ? AND "notifications"."notifiable_type" = ? AND "notifications"."key" = ? AND "notifications"."group_id" IS NULL AND "notifications"."group_owner_id" IS NULL AND "notifications"."opened_at" IS NULL ORDER BY "notifications"."created_at" ASC LIMIT ?  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Create (0.1ms)  INSERT INTO "notifications" ("target_type", "target_id", "notifiable_type", "notifiable_id", "key", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["notifiable_id", 2], ["key", "user.create"], ["created_at", "2018-11-14 00:36:16.219487"], ["updated_at", "2018-11-14 00:36:16.219487"]]
  ActivityNotification::Subscription Load (0.1ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."target_id" = ? AND "subscriptions"."target_type" = ? AND "subscriptions"."key" = ? ORDER BY "subscriptions"."id" ASC LIMIT ?  [["target_id", 6], ["target_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Load (0.2ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."target_type" = ? AND "notifications"."target_id" = ? AND "notifications"."notifiable_type" = ? AND "notifications"."key" = ? AND "notifications"."group_id" IS NULL AND "notifications"."group_owner_id" IS NULL AND "notifications"."opened_at" IS NULL ORDER BY "notifications"."created_at" ASC LIMIT ?  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Create (0.2ms)  INSERT INTO "notifications" ("target_type", "target_id", "notifiable_type", "notifiable_id", "key", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["notifiable_id", 3], ["key", "user.create"], ["created_at", "2018-11-14 00:36:16.224960"], ["updated_at", "2018-11-14 00:36:16.224960"]]
  ActivityNotification::Subscription Load (0.1ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."target_id" = ? AND "subscriptions"."target_type" = ? AND "subscriptions"."key" = ? ORDER BY "subscriptions"."id" ASC LIMIT ?  [["target_id", 6], ["target_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Load (0.2ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."target_type" = ? AND "notifications"."target_id" = ? AND "notifications"."notifiable_type" = ? AND "notifications"."key" = ? AND "notifications"."group_id" IS NULL AND "notifications"."group_owner_id" IS NULL AND "notifications"."opened_at" IS NULL ORDER BY "notifications"."created_at" ASC LIMIT ?  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Create (0.2ms)  INSERT INTO "notifications" ("target_type", "target_id", "notifiable_type", "notifiable_id", "key", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["notifiable_id", 4], ["key", "user.create"], ["created_at", "2018-11-14 00:36:16.231452"], ["updated_at", "2018-11-14 00:36:16.231452"]]
  ActivityNotification::Subscription Load (0.1ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."target_id" = ? AND "subscriptions"."target_type" = ? AND "subscriptions"."key" = ? ORDER BY "subscriptions"."id" ASC LIMIT ?  [["target_id", 6], ["target_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Load (0.1ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."target_type" = ? AND "notifications"."target_id" = ? AND "notifications"."notifiable_type" = ? AND "notifications"."key" = ? AND "notifications"."group_id" IS NULL AND "notifications"."group_owner_id" IS NULL AND "notifications"."opened_at" IS NULL ORDER BY "notifications"."created_at" ASC LIMIT ?  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Create (0.2ms)  INSERT INTO "notifications" ("target_type", "target_id", "notifiable_type", "notifiable_id", "key", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["notifiable_id", 5], ["key", "user.create"], ["created_at", "2018-11-14 00:36:16.237322"], ["updated_at", "2018-11-14 00:36:16.237322"]]
  ActivityNotification::Subscription Load (0.4ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."target_id" = ? AND "subscriptions"."target_type" = ? AND "subscriptions"."key" = ? ORDER BY "subscriptions"."id" ASC LIMIT ?  [["target_id", 6], ["target_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Load (1.4ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."target_type" = ? AND "notifications"."target_id" = ? AND "notifications"."notifiable_type" = ? AND "notifications"."key" = ? AND "notifications"."group_id" IS NULL AND "notifications"."group_owner_id" IS NULL AND "notifications"."opened_at" IS NULL ORDER BY "notifications"."created_at" ASC LIMIT ?  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["key", "user.create"], ["LIMIT", 1]]
  ActivityNotification::Notification Create (0.7ms)  INSERT INTO "notifications" ("target_type", "target_id", "notifiable_type", "notifiable_id", "key", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["target_type", "User"], ["target_id", 6], ["notifiable_type", "User"], ["notifiable_id", 6], ["key", "user.create"], ["created_at", "2018-11-14 00:36:16.274179"], ["updated_at", "2018-11-14 00:36:16.274179"]]
   (2.6ms)  commit transaction

It created a notification for each user, however the target was the new user instead of the existing ones.

Screen shot of the new user's notifications http://localhost:3000/users/6/notifications

screen shot 2018-11-13 at 7 37 46 pm

Edit: I updated the model to tracked: true and create and update behave the same. So the only doesn't seem to be related to the problem.

@simukappu
Copy link
Owner

Hi @szechyjs,
Thank you so much for your contribution. I could also reproduce this issue in test app. I have added fixes and new test cace for this issue. Can you try development branch to confirm the issue fixed?

@simukappu simukappu removed the need more info We need more information label Nov 17, 2018
@szechyjs
Copy link
Contributor Author

Seems to be working correctly now 👍

@simukappu
Copy link
Owner

I have Just released this update as v1.6.1. Thank you for your contribution!

simukappu added a commit that referenced this issue Nov 25, 2018
…on_of to avoid ambiguous method name with Notifiable#notify_to - #88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants