use more correct way of propagating user through models

This commit is contained in:
byte[] 2019-11-18 19:33:27 -05:00
parent 889e2349d0
commit 3ee6c07609
8 changed files with 29 additions and 34 deletions

View file

@ -40,10 +40,10 @@ defmodule Philomena.Comments do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_comment(image, user, attributes, params \\ %{}) do def create_comment(image, attribution, params \\ %{}) do
comment = comment =
struct(Comment, [image_id: image.id] ++ attributes) Ecto.build_assoc(image, :comments)
|> Comment.creation_changeset(user, params) |> Comment.creation_changeset(params, attribution)
image_query = image_query =
Image Image
@ -53,7 +53,7 @@ defmodule Philomena.Comments do
|> Multi.insert(:comment, comment) |> Multi.insert(:comment, comment)
|> Multi.update_all(:image, image_query, inc: [comments_count: 1]) |> Multi.update_all(:image, image_query, inc: [comments_count: 1])
|> Multi.run(:subscribe, fn _repo, _changes -> |> Multi.run(:subscribe, fn _repo, _changes ->
Images.create_subscription(image, user) Images.create_subscription(image, attribution[:user])
end) end)
|> Repo.transaction() |> Repo.transaction()
end end

View file

@ -31,12 +31,13 @@ defmodule Philomena.Comments.Comment do
end end
@doc false @doc false
def creation_changeset(comment, user, attrs) do def creation_changeset(comment, attrs, attribution) do
comment comment
|> cast(attrs, [:body, :anonymous]) |> cast(attrs, [:body, :anonymous])
|> set_name_at_post_time(user)
|> validate_required([:body]) |> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution)
|> put_name_at_post_time()
end end
def changeset(comment, attrs) do def changeset(comment, attrs) do
@ -47,8 +48,8 @@ defmodule Philomena.Comments.Comment do
|> validate_length(:edit_reason, max: 70, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes)
end end
def set_name_at_post_time(changeset, nil), do: changeset defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset),
def set_name_at_post_time(changeset, %{name: name}) do do: change(changeset, name_at_post_time: name)
change(changeset, name_at_post_time: name) defp put_name_at_post_time(changeset),
end do: changeset
end end

View file

@ -15,10 +15,12 @@ defmodule Philomena.Images.Image do
alias Philomena.Users.User alias Philomena.Users.User
alias Philomena.Images.Tagging alias Philomena.Images.Tagging
alias Philomena.Galleries alias Philomena.Galleries
alias Philomena.Comments.Comment
schema "images" do schema "images" do
belongs_to :user, User belongs_to :user, User
belongs_to :deleter, User, source: :deleted_by_id belongs_to :deleter, User, source: :deleted_by_id
has_many :comments, Comment
has_many :upvotes, ImageVote, where: [up: true] has_many :upvotes, ImageVote, where: [up: true]
has_many :downvotes, ImageVote, where: [up: false] has_many :downvotes, ImageVote, where: [up: false]
has_many :faves, ImageFave has_many :faves, ImageFave

View file

@ -41,7 +41,7 @@ defmodule Philomena.Posts do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_post(topic, user, attributes, params \\ %{}) do def create_post(topic, attributes, params \\ %{}) do
topic_query = topic_query =
Topic Topic
|> where(id: ^topic.id) |> where(id: ^topic.id)
@ -61,7 +61,7 @@ defmodule Philomena.Posts do
|> repo.one() |> repo.one()
Ecto.build_assoc(topic, :posts, [topic_position: (last_position || -1) + 1] ++ attributes) Ecto.build_assoc(topic, :posts, [topic_position: (last_position || -1) + 1] ++ attributes)
|> Post.creation_changeset(params, user) |> Post.creation_changeset(params, attributes)
|> repo.insert() |> repo.insert()
end) end)
|> Multi.run(:update_topic, fn repo, %{post: %{id: post_id}} -> |> Multi.run(:update_topic, fn repo, %{post: %{id: post_id}} ->
@ -77,7 +77,7 @@ defmodule Philomena.Posts do
{:ok, count} {:ok, count}
end) end)
|> Multi.run(:subscribe, fn _repo, _changes -> |> Multi.run(:subscribe, fn _repo, _changes ->
Topics.create_subscription(topic, user) Topics.create_subscription(topic, attributes[:user])
end) end)
|> Repo.isolated_transaction(:serializable) |> Repo.isolated_transaction(:serializable)
end end

View file

@ -40,16 +40,17 @@ defmodule Philomena.Posts.Post do
end end
@doc false @doc false
def creation_changeset(post, attrs, user) do def creation_changeset(post, attrs, attribution) do
post post
|> cast(attrs, [:body, :anonymous]) |> cast(attrs, [:body, :anonymous])
|> set_name_at_post_time(user)
|> validate_required([:body]) |> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution)
|> put_name_at_post_time()
end end
def set_name_at_post_time(changeset, nil), do: changeset defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset),
def set_name_at_post_time(changeset, %{name: name}) do do: change(changeset, name_at_post_time: name)
change(changeset, name_at_post_time: name) defp put_name_at_post_time(changeset),
end do: changeset
end end

View file

@ -63,9 +63,8 @@ defmodule PhilomenaWeb.Image.CommentController do
def create(conn, %{"comment" => comment_params}) do def create(conn, %{"comment" => comment_params}) do
attributes = conn.assigns.attributes attributes = conn.assigns.attributes
image = conn.assigns.image image = conn.assigns.image
user = conn.assigns.current_user
case Comments.create_comment(image, user, attributes, comment_params) do case Comments.create_comment(image, attributes, comment_params) do
{:ok, %{comment: comment}} -> {:ok, %{comment: comment}} ->
Comments.notify_comment(comment) Comments.notify_comment(comment)
Comments.reindex_comment(comment) Comments.reindex_comment(comment)

View file

@ -14,9 +14,8 @@ defmodule PhilomenaWeb.Topic.PostController do
attributes = conn.assigns.attributes attributes = conn.assigns.attributes
forum = conn.assigns.forum forum = conn.assigns.forum
topic = conn.assigns.topic topic = conn.assigns.topic
user = conn.assigns.current_user
case Posts.create_post(topic, user, attributes, post_params) do case Posts.create_post(topic, attributes, post_params) do
{:ok, %{post: post}} -> {:ok, %{post: post}} ->
Posts.notify_post(post) Posts.notify_post(post)
Posts.reindex_post(post) Posts.reindex_post(post)

View file

@ -19,13 +19,15 @@ defmodule PhilomenaWeb.UserAttributionPlug do
def call(conn, _opts) do def call(conn, _opts) do
{:ok, remote_ip} = EctoNetwork.INET.cast(conn.remote_ip) {:ok, remote_ip} = EctoNetwork.INET.cast(conn.remote_ip)
conn = Conn.fetch_cookies(conn) conn = Conn.fetch_cookies(conn)
user = Pow.Plug.current_user(conn)
attributes = attributes =
[ [
ip: remote_ip, ip: remote_ip,
fingerprint: conn.cookies["_ses"], fingerprint: conn.cookies["_ses"],
referrer: conn.assigns.referrer, referrer: conn.assigns.referrer,
user_agent: user_agent(conn), user: user,
user_id: user_id(conn) user_agent: user_agent(conn)
] ]
conn conn
@ -38,13 +40,4 @@ defmodule PhilomenaWeb.UserAttributionPlug do
_ -> nil _ -> nil
end end
end end
defp user_id(conn) do
user = Pow.Plug.current_user(conn)
case user do
%{id: id} -> id
_ -> nil
end
end
end end