Commit 042852ec authored by Sadposter's avatar Sadposter Committed by kaniini
Browse files

Add check to prevent multiple follow notifications from the same user

parent 8d047c7a
......@@ -109,7 +109,12 @@ def create_notifications(_), do: {:ok, []}
# TODO move to sql, too.
def create_notification(%Activity{} = activity, %User{} = user) do
unless User.blocks?(user, %{ap_id:["actor"]}) or
user.ap_id ==["actor"] do
user.ap_id ==["actor"] or
(["type"] == "Follow" and
Enum.any?(Notification.for_user(user), fn notif ->["type"] == "Follow" and["actor"] ==["actor"]
end)) do
notification = %Notification{user_id:, activity: activity}
{:ok, notification} = Repo.insert(notification)"user", notification)
......@@ -46,6 +46,43 @@ test "it doesn't create a notification for user if he is the activity author" do
assert nil == Notification.create_notification(activity, author)
test "it doesn't create a notification for follow-unfollow-follow chains" do
user = insert(:user)
followed_user = insert(:user)
{:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" =>})
Notification.create_notification(activity, followed_user)
TwitterAPI.unfollow(user, %{"user_id" =>})
{:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" =>})
assert nil == Notification.create_notification(activity_dupe, followed_user)
test "it doesn't create a notification for like-unlike-like chains" do
user = insert(:user)
liked_user = insert(:user)
{:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
{:ok, fav_status} = TwitterAPI.fav(user,
Notification.create_notification(fav_status, liked_user)
{:ok, dupe} = TwitterAPI.fav(user,
assert nil == Notification.create_notification(dupe, liked_user)
test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
user = insert(:user)
retweeted_user = insert(:user)
{:ok, status} =
TwitterAPI.create_status(retweeted_user, %{
"status" => "Send dupe notifications to the shadow realm"
{:ok, retweeted_activity} = TwitterAPI.repeat(user,
Notification.create_notification(retweeted_activity, retweeted_user)
{:ok, dupe} = TwitterAPI.repeat(user,
assert nil == Notification.create_notification(dupe, retweeted_user)
describe "get notification" do
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment