automatically subscribe user on post

This commit is contained in:
byte[] 2019-11-17 23:02:08 -05:00
parent eef29910de
commit de48c06f99
4 changed files with 10 additions and 0 deletions

View file

@ -9,6 +9,7 @@ defmodule Philomena.Comments do
alias Philomena.Comments.Comment
alias Philomena.Images.Image
alias Philomena.Images
alias Philomena.Notifications
@doc """
@ -51,6 +52,9 @@ defmodule Philomena.Comments do
Multi.new
|> Multi.insert(:comment, comment)
|> Multi.update_all(:image, image_query, inc: [comments_count: 1])
|> Multi.run(:subscribe, fn _repo, _changes ->
Images.create_subscription(image, user)
end)
|> Repo.transaction()
end

View file

@ -141,6 +141,7 @@ defmodule Philomena.Images do
{:error, %Ecto.Changeset{}}
"""
def create_subscription(image, nil), do: {:ok, nil}
def create_subscription(image, user) do
%Subscription{image_id: image.id, user_id: user.id}
|> Subscription.changeset(%{})

View file

@ -8,6 +8,7 @@ defmodule Philomena.Posts do
alias Philomena.Repo
alias Philomena.Topics.Topic
alias Philomena.Topics
alias Philomena.Posts.Post
alias Philomena.Forums.Forum
alias Philomena.Notifications
@ -75,6 +76,9 @@ defmodule Philomena.Posts do
{:ok, count}
end)
|> Multi.run(:subscribe, fn _repo, _changes ->
Topics.create_subscription(topic, user)
end)
|> Repo.isolated_transaction(:serializable)
end

View file

@ -123,6 +123,7 @@ defmodule Philomena.Topics do
{:error, %Ecto.Changeset{}}
"""
def create_subscription(topic, nil), do: {:ok, nil}
def create_subscription(topic, user) do
%Subscription{topic_id: topic.id, user_id: user.id}
|> Subscription.changeset(%{})