From 57a37971b1c387fecbeca6907156d63ec2c1191e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 17 Nov 2019 20:53:08 -0500 Subject: [PATCH] ensure name_at_post_time is set correctly --- lib/philomena/comments.ex | 4 ++-- lib/philomena/comments/comment.ex | 8 +++++++- lib/philomena_web/controllers/image/comment_controller.ex | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index 276020d2..6f77d0e6 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -39,10 +39,10 @@ defmodule Philomena.Comments do {:error, %Ecto.Changeset{}} """ - def create_comment(image, attributes, params \\ %{}) do + def create_comment(image, user, attributes, params \\ %{}) do comment = struct(Comment, [image_id: image.id] ++ attributes) - |> Comment.creation_changeset(params) + |> Comment.creation_changeset(user, params) image_query = Image diff --git a/lib/philomena/comments/comment.ex b/lib/philomena/comments/comment.ex index 6a05b60c..22bd80a4 100644 --- a/lib/philomena/comments/comment.ex +++ b/lib/philomena/comments/comment.ex @@ -31,9 +31,10 @@ defmodule Philomena.Comments.Comment do end @doc false - def creation_changeset(comment, attrs) do + def creation_changeset(comment, user, attrs) 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) end @@ -45,4 +46,9 @@ defmodule Philomena.Comments.Comment do |> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> 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 end diff --git a/lib/philomena_web/controllers/image/comment_controller.ex b/lib/philomena_web/controllers/image/comment_controller.ex index 750ef842..9f7c8907 100644 --- a/lib/philomena_web/controllers/image/comment_controller.ex +++ b/lib/philomena_web/controllers/image/comment_controller.ex @@ -63,8 +63,9 @@ 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, attributes, comment_params) do + case Comments.create_comment(image, user, attributes, comment_params) do {:ok, %{comment: comment}} -> Comments.notify_comment(comment) Comments.reindex_comment(comment)