post versions

This commit is contained in:
byte[] 2019-12-06 08:58:34 -05:00
parent 8d08c42331
commit 76de12d69d
6 changed files with 141 additions and 6 deletions

View file

@ -60,9 +60,9 @@ defmodule Philomena.Versions do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_version(attrs \\ %{}) do def create_version(item_type, item_id, whodunnit, attrs \\ %{}) do
%Version{} %Version{item_type: item_type, item_id: item_id, whodunnit: whodunnit}
|> Version.changeset(attrs) |> Version.changeset(attrs, item_id)
|> Repo.insert() |> Repo.insert()
end end
end end

View file

@ -21,9 +21,22 @@ defmodule Philomena.Versions.Version do
end end
@doc false @doc false
def changeset(version, attrs) do def changeset(version, attrs, item_id) do
version version
|> cast(attrs, []) |> cast(attrs, [:body, :edit_reason])
|> validate_required([]) |> 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
end end

View file

@ -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

View file

@ -179,6 +179,7 @@ defmodule PhilomenaWeb.Router do
resources "/topics", TopicController, only: [:show] do resources "/topics", TopicController, only: [:show] do
resources "/posts", Topic.PostController, only: [:create] do resources "/posts", Topic.PostController, only: [:create] do
resources "/reports", Topic.Post.ReportController, only: [:new, :create] resources "/reports", Topic.Post.ReportController, only: [:new, :create]
resources "/history", Topic.Post.HistoryController, only: [:index]
end end
end end
end end

View file

@ -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

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.Topic.Post.HistoryView do
use PhilomenaWeb, :view
end