From 76de12d69d0e8cdb768ae379c83df7f7c285d7a0 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 6 Dec 2019 08:58:34 -0500 Subject: [PATCH] post versions --- lib/philomena/versions.ex | 6 +- lib/philomena/versions/version.ex | 19 ++++- .../topic/post/history_controller.ex | 73 +++++++++++++++++++ lib/philomena_web/router.ex | 1 + .../topic/post/history/index.html.slime | 45 ++++++++++++ .../views/topic/post/history_view.ex | 3 + 6 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 lib/philomena_web/controllers/topic/post/history_controller.ex create mode 100644 lib/philomena_web/templates/topic/post/history/index.html.slime create mode 100644 lib/philomena_web/views/topic/post/history_view.ex diff --git a/lib/philomena/versions.ex b/lib/philomena/versions.ex index da171066..efe222b4 100644 --- a/lib/philomena/versions.ex +++ b/lib/philomena/versions.ex @@ -60,9 +60,9 @@ defmodule Philomena.Versions do {:error, %Ecto.Changeset{}} """ - def create_version(attrs \\ %{}) do - %Version{} - |> Version.changeset(attrs) + def create_version(item_type, item_id, whodunnit, attrs \\ %{}) do + %Version{item_type: item_type, item_id: item_id, whodunnit: whodunnit} + |> Version.changeset(attrs, item_id) |> Repo.insert() end end diff --git a/lib/philomena/versions/version.ex b/lib/philomena/versions/version.ex index 0db017e2..42495220 100644 --- a/lib/philomena/versions/version.ex +++ b/lib/philomena/versions/version.ex @@ -21,9 +21,22 @@ defmodule Philomena.Versions.Version do end @doc false - def changeset(version, attrs) do + def changeset(version, attrs, item_id) do version - |> cast(attrs, []) - |> validate_required([]) + |> cast(attrs, [:body, :edit_reason]) + |> put_object(item_id) + end + + defp put_object(changeset, item_id) do + body = get_field(changeset, :body) + edit_reason = get_field(changeset, :edit_reason) + + object = Jason.encode!(%{ + id: item_id, + body: body, + edit_reason: edit_reason + }) + + change(changeset, object: object) end end diff --git a/lib/philomena_web/controllers/topic/post/history_controller.ex b/lib/philomena_web/controllers/topic/post/history_controller.ex new file mode 100644 index 00000000..fb4556d4 --- /dev/null +++ b/lib/philomena_web/controllers/topic/post/history_controller.ex @@ -0,0 +1,73 @@ +defmodule PhilomenaWeb.Topic.Post.HistoryController do + use PhilomenaWeb, :controller + + alias Philomena.Versions.Version + alias Philomena.Versions + alias Philomena.Forums.Forum + alias Philomena.Topics.Topic + alias Philomena.Posts.Post + alias Philomena.Repo + import Ecto.Query + + plug PhilomenaWeb.CanaryMapPlug, index: :show + plug :load_and_authorize_resource, model: Forum, id_name: "forum_id", id_field: "short_name", persisted: true + plug :load_topic + plug :load_post + + def index(conn, _params) do + post = conn.assigns.post + + versions = + Version + |> where(item_type: "Post", item_id: ^post.id) + |> order_by(desc: :created_at) + |> limit(25) + |> Repo.all() + |> Versions.load_data_and_associations(post) + + render(conn, "index.html", versions: versions) + end + + defp load_topic(conn, _opts) do + user = conn.assigns.current_user + forum = conn.assigns.forum + topic = + Topic + |> where(forum_id: ^forum.id, slug: ^conn.params["topic_id"]) + |> preload(:forum) + |> Repo.one() + + cond do + is_nil(topic) -> + PhilomenaWeb.NotFoundPlug.call(conn) + + not Canada.Can.can?(user, :show, topic) -> + PhilomenaWeb.NotAuthorizedPlug.call(conn) + + true -> + Plug.Conn.assign(conn, :topic, topic) + end + end + + defp load_post(conn, _opts) do + user = conn.assigns.current_user + topic = conn.assigns.topic + + post = + Post + |> where(topic_id: ^topic.id, id: ^conn.params["post_id"]) + |> preload(topic: :forum, user: [awards: :badge]) + |> Repo.one() + + cond do + is_nil(post) -> + PhilomenaWeb.NotFoundPlug.call(conn) + + not Canada.Can.can?(user, :show, post) -> + PhilomenaWeb.NotAuthorizedPlug.call(conn) + + true -> + Plug.Conn.assign(conn, :post, post) + end + end +end \ No newline at end of file diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 17d7b329..4218f733 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -179,6 +179,7 @@ defmodule PhilomenaWeb.Router do resources "/topics", TopicController, only: [:show] do resources "/posts", Topic.PostController, only: [:create] do resources "/reports", Topic.Post.ReportController, only: [:new, :create] + resources "/history", Topic.Post.HistoryController, only: [:index] end end end diff --git a/lib/philomena_web/templates/topic/post/history/index.html.slime b/lib/philomena_web/templates/topic/post/history/index.html.slime new file mode 100644 index 00000000..96be8d41 --- /dev/null +++ b/lib/philomena_web/templates/topic/post/history/index.html.slime @@ -0,0 +1,45 @@ +h1 + ' Viewing last 25 versions of post by + = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @post, conn: @conn + ' in topic + a href=(Routes.forum_topic_path(@conn, :show, @post.topic.forum, @post.topic, post_id: @post.id) <> "#post_#{@post.id}") + = @post.topic.title + += for version <- @versions do + article.block.communication + .block__content.flex.flex--no-wrap + .flex__fixed.spacing-right + = render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn + + .flex__grow.communication__body + span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @post, awards: true, conn: @conn + br + + = render PhilomenaWeb.UserAttributionView, "_anon_user_title.html", object: @post, conn: @conn + + .communication__body__text + = for edit <- version.difference do + = case edit do + - {:eq, value} -> + = escape_nl2br(value) + + - {:ins, value} -> + ins.differ = escape_nl2br(value) + + - {:del, value} -> + del.differ = escape_nl2br(value) + + .block__content.communication__options + .flex.flex--wrap.flex--spaced-out + div + = if version.edit_reason not in [nil, ""] do + ' Reason: + = version.edit_reason + - else + ' No reason given + + .flex__right + ' Edited + => pretty_time(version.created_at) + ' by + => render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: version, conn: @conn \ No newline at end of file diff --git a/lib/philomena_web/views/topic/post/history_view.ex b/lib/philomena_web/views/topic/post/history_view.ex new file mode 100644 index 00000000..01633a21 --- /dev/null +++ b/lib/philomena_web/views/topic/post/history_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Topic.Post.HistoryView do + use PhilomenaWeb, :view +end