diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index 6cb26ce3..1c94a1d8 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -40,10 +40,10 @@ defmodule Philomena.Comments do {:error, %Ecto.Changeset{}} """ - def create_comment(image, user, attributes, params \\ %{}) do + def create_comment(image, attribution, params \\ %{}) do comment = - struct(Comment, [image_id: image.id] ++ attributes) - |> Comment.creation_changeset(user, params) + Ecto.build_assoc(image, :comments) + |> Comment.creation_changeset(params, attribution) image_query = Image @@ -53,7 +53,7 @@ defmodule Philomena.Comments do |> 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) + Images.create_subscription(image, attribution[:user]) end) |> Repo.transaction() end diff --git a/lib/philomena/comments/comment.ex b/lib/philomena/comments/comment.ex index 22bd80a4..a59c2deb 100644 --- a/lib/philomena/comments/comment.ex +++ b/lib/philomena/comments/comment.ex @@ -31,12 +31,13 @@ defmodule Philomena.Comments.Comment do end @doc false - def creation_changeset(comment, user, attrs) do + def creation_changeset(comment, attrs, attribution) do comment |> cast(attrs, [:body, :anonymous]) - |> set_name_at_post_time(user) |> validate_required([:body]) |> validate_length(:body, min: 1, max: 300_000, count: :bytes) + |> change(attribution) + |> put_name_at_post_time() end def changeset(comment, attrs) do @@ -47,8 +48,8 @@ defmodule Philomena.Comments.Comment do |> validate_length(:edit_reason, max: 70, count: :bytes) end - def set_name_at_post_time(changeset, nil), do: changeset - def set_name_at_post_time(changeset, %{name: name}) do - change(changeset, name_at_post_time: name) - end + defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset), + do: change(changeset, name_at_post_time: name) + defp put_name_at_post_time(changeset), + do: changeset end diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index d82f4fc3..4dfa90e0 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -15,10 +15,12 @@ defmodule Philomena.Images.Image do alias Philomena.Users.User alias Philomena.Images.Tagging alias Philomena.Galleries + alias Philomena.Comments.Comment schema "images" do belongs_to :user, User belongs_to :deleter, User, source: :deleted_by_id + has_many :comments, Comment has_many :upvotes, ImageVote, where: [up: true] has_many :downvotes, ImageVote, where: [up: false] has_many :faves, ImageFave diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index fe6e9568..96f7f254 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -41,7 +41,7 @@ defmodule Philomena.Posts do {:error, %Ecto.Changeset{}} """ - def create_post(topic, user, attributes, params \\ %{}) do + def create_post(topic, attributes, params \\ %{}) do topic_query = Topic |> where(id: ^topic.id) @@ -61,7 +61,7 @@ defmodule Philomena.Posts do |> repo.one() Ecto.build_assoc(topic, :posts, [topic_position: (last_position || -1) + 1] ++ attributes) - |> Post.creation_changeset(params, user) + |> Post.creation_changeset(params, attributes) |> repo.insert() end) |> Multi.run(:update_topic, fn repo, %{post: %{id: post_id}} -> @@ -77,7 +77,7 @@ defmodule Philomena.Posts do {:ok, count} end) |> Multi.run(:subscribe, fn _repo, _changes -> - Topics.create_subscription(topic, user) + Topics.create_subscription(topic, attributes[:user]) end) |> Repo.isolated_transaction(:serializable) end diff --git a/lib/philomena/posts/post.ex b/lib/philomena/posts/post.ex index afcaa307..45ba5247 100644 --- a/lib/philomena/posts/post.ex +++ b/lib/philomena/posts/post.ex @@ -40,16 +40,17 @@ defmodule Philomena.Posts.Post do end @doc false - def creation_changeset(post, attrs, user) do + def creation_changeset(post, attrs, attribution) do post |> cast(attrs, [:body, :anonymous]) - |> set_name_at_post_time(user) |> validate_required([:body]) |> validate_length(:body, min: 1, max: 300_000, count: :bytes) + |> change(attribution) + |> put_name_at_post_time() end - def set_name_at_post_time(changeset, nil), do: changeset - def set_name_at_post_time(changeset, %{name: name}) do - change(changeset, name_at_post_time: name) - end + defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset), + do: change(changeset, name_at_post_time: name) + defp put_name_at_post_time(changeset), + do: changeset end diff --git a/lib/philomena_web/controllers/image/comment_controller.ex b/lib/philomena_web/controllers/image/comment_controller.ex index 9f7c8907..750ef842 100644 --- a/lib/philomena_web/controllers/image/comment_controller.ex +++ b/lib/philomena_web/controllers/image/comment_controller.ex @@ -63,9 +63,8 @@ defmodule PhilomenaWeb.Image.CommentController do def create(conn, %{"comment" => comment_params}) do attributes = conn.assigns.attributes 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}} -> Comments.notify_comment(comment) Comments.reindex_comment(comment) diff --git a/lib/philomena_web/controllers/topic/post_controller.ex b/lib/philomena_web/controllers/topic/post_controller.ex index eff9f93d..20500bf7 100644 --- a/lib/philomena_web/controllers/topic/post_controller.ex +++ b/lib/philomena_web/controllers/topic/post_controller.ex @@ -14,9 +14,8 @@ defmodule PhilomenaWeb.Topic.PostController do attributes = conn.assigns.attributes forum = conn.assigns.forum 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}} -> Posts.notify_post(post) Posts.reindex_post(post) diff --git a/lib/philomena_web/plugs/user_attribution_plug.ex b/lib/philomena_web/plugs/user_attribution_plug.ex index b97e31b3..e223694b 100644 --- a/lib/philomena_web/plugs/user_attribution_plug.ex +++ b/lib/philomena_web/plugs/user_attribution_plug.ex @@ -19,13 +19,15 @@ defmodule PhilomenaWeb.UserAttributionPlug do def call(conn, _opts) do {:ok, remote_ip} = EctoNetwork.INET.cast(conn.remote_ip) conn = Conn.fetch_cookies(conn) + user = Pow.Plug.current_user(conn) + attributes = [ ip: remote_ip, fingerprint: conn.cookies["_ses"], referrer: conn.assigns.referrer, - user_agent: user_agent(conn), - user_id: user_id(conn) + user: user, + user_agent: user_agent(conn) ] conn @@ -38,13 +40,4 @@ defmodule PhilomenaWeb.UserAttributionPlug do _ -> nil end end - - defp user_id(conn) do - user = Pow.Plug.current_user(conn) - - case user do - %{id: id} -> id - _ -> nil - end - end end \ No newline at end of file