basic moderation logs

This commit is contained in:
Luna D 2021-11-07 19:51:55 +01:00
parent cafba455cd
commit f555c337ea
No known key found for this signature in database
GPG key ID: 81AF416F2CC36FC8
46 changed files with 788 additions and 25 deletions

View file

@ -0,0 +1,77 @@
defmodule Philomena.ModerationLogs do
@moduledoc """
The ModerationLogs context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.ModerationLogs.ModerationLog
@doc """
Returns the list of moderation_logs.
## Examples
iex> list_moderation_logs()
[%ModerationLog{}, ...]
"""
def list_moderation_logs(conn) do
ModerationLog
|> where([ml], ml.created_at > ago(2, "week"))
|> preload(:user)
|> order_by(desc: :created_at)
|> Repo.paginate(conn.assigns.scrivener)
end
@doc """
Gets a single moderation_log.
Raises `Ecto.NoResultsError` if the Moderation log does not exist.
## Examples
iex> get_moderation_log!(123)
%ModerationLog{}
iex> get_moderation_log!(456)
** (Ecto.NoResultsError)
"""
def get_moderation_log!(id), do: Repo.get!(ModerationLog, id)
@doc """
Creates a moderation_log.
## Examples
iex> create_moderation_log(%{field: value})
{:ok, %ModerationLog{}}
iex> create_moderation_log(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_moderation_log(user, type, subject_path, body) do
%ModerationLog{user_id: user.id}
|> ModerationLog.changeset(%{type: type, subject_path: subject_path, body: body})
|> Repo.insert()
end
@doc """
Deletes a moderation_log.
## Examples
iex> delete_moderation_log(moderation_log)
{:ok, %ModerationLog{}}
iex> delete_moderation_log(moderation_log)
{:error, %Ecto.Changeset{}}
"""
def delete_moderation_log(%ModerationLog{} = moderation_log) do
Repo.delete(moderation_log)
end
end

View file

@ -0,0 +1,23 @@
defmodule Philomena.ModerationLogs.ModerationLog do
use Ecto.Schema
import Ecto.Changeset
alias Philomena.Users.User
schema "moderation_logs" do
belongs_to :user, User
field :body, :string
field :type, :string
field :subject_path, :string
timestamps(inserted_at: :created_at, updated_at: false, type: :utc_datetime)
end
@doc false
def changeset(moderation_log, attrs) do
moderation_log
|> cast(attrs, [:body, :type, :subject_path])
|> validate_required([:body, :type, :subject_path])
end
end

View file

@ -22,9 +22,7 @@ defmodule Philomena.Scrapers.Tumblr do
[post_id] = Regex.run(@url_regex, url, capture: :all_but_first) [post_id] = Regex.run(@url_regex, url, capture: :all_but_first)
api_url = api_url =
"https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{ "https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{tumblr_api_key()}"
tumblr_api_key()
}"
Philomena.Http.get(api_url) Philomena.Http.get(api_url)
|> json!() |> json!()

View file

@ -24,6 +24,7 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
alias Philomena.StaticPages.StaticPage alias Philomena.StaticPages.StaticPage
alias Philomena.Adverts.Advert alias Philomena.Adverts.Advert
alias Philomena.SiteNotices.SiteNotice alias Philomena.SiteNotices.SiteNotice
alias Philomena.ModerationLogs.ModerationLog
alias Philomena.Bans.User, as: UserBan alias Philomena.Bans.User, as: UserBan
alias Philomena.Bans.Subnet, as: SubnetBan alias Philomena.Bans.Subnet, as: SubnetBan
@ -127,6 +128,9 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# Manage galleries # Manage galleries
def can?(%User{role: "moderator"}, _action, %Gallery{}), do: true def can?(%User{role: "moderator"}, _action, %Gallery{}), do: true
# See moderation logs
def can?(%User{role: "moderator"}, _action, ModerationLog), do: true
# And some privileged moderators can... # And some privileged moderators can...
# Manage site notices # Manage site notices

View file

@ -13,11 +13,19 @@ defmodule PhilomenaWeb.Admin.ArtistLink.ContactController do
preload: [:user] preload: [:user]
def create(conn, _params) do def create(conn, _params) do
{:ok, _} = {:ok, artist_link} =
ArtistLinks.contact_artist_link(conn.assigns.artist_link, conn.assigns.current_user) ArtistLinks.contact_artist_link(conn.assigns.artist_link, conn.assigns.current_user)
conn conn
|> put_flash(:info, "Artist successfully marked as contacted.") |> put_flash(:info, "Artist successfully marked as contacted.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: artist_link)
|> redirect(to: Routes.admin_artist_link_path(conn, :index)) |> redirect(to: Routes.admin_artist_link_path(conn, :index))
end end
defp log_details(conn, _action, artist_link) do
%{
body: "Contacted artist #{artist_link.user.name} at #{artist_link.uri}",
subject_path: Routes.profile_artist_link_path(conn, :show, artist_link.user, artist_link)
}
end
end end

View file

@ -13,10 +13,18 @@ defmodule PhilomenaWeb.Admin.ArtistLink.RejectController do
preload: [:user] preload: [:user]
def create(conn, _params) do def create(conn, _params) do
{:ok, _} = ArtistLinks.reject_artist_link(conn.assigns.artist_link) {:ok, artist_link} = ArtistLinks.reject_artist_link(conn.assigns.artist_link)
conn conn
|> put_flash(:info, "Artist link successfully marked as rejected.") |> put_flash(:info, "Artist link successfully marked as rejected.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: artist_link)
|> redirect(to: Routes.admin_artist_link_path(conn, :index)) |> redirect(to: Routes.admin_artist_link_path(conn, :index))
end end
defp log_details(conn, _action, artist_link) do
%{
body: "Rejected artist link #{artist_link.uri} created by #{artist_link.user.name}",
subject_path: Routes.profile_artist_link_path(conn, :show, artist_link.user, artist_link)
}
end
end end

View file

@ -13,10 +13,19 @@ defmodule PhilomenaWeb.Admin.ArtistLink.VerificationController do
preload: [:user] preload: [:user]
def create(conn, _params) do def create(conn, _params) do
{:ok, _} = ArtistLinks.verify_artist_link(conn.assigns.artist_link, conn.assigns.current_user) {:ok, result} =
ArtistLinks.verify_artist_link(conn.assigns.artist_link, conn.assigns.current_user)
conn conn
|> put_flash(:info, "Artist link successfully verified.") |> put_flash(:info, "Artist link successfully verified.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: result.artist_link)
|> redirect(to: Routes.admin_artist_link_path(conn, :index)) |> redirect(to: Routes.admin_artist_link_path(conn, :index))
end end
defp log_details(conn, _action, artist_link) do
%{
body: "Verified artist link #{artist_link.uri} created by #{artist_link.user.name}",
subject_path: Routes.profile_artist_link_path(conn, :show, artist_link.user, artist_link)
}
end
end end

View file

@ -9,8 +9,8 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
plug :verify_authorized plug :verify_authorized
plug PhilomenaWeb.UserAttributionPlug plug PhilomenaWeb.UserAttributionPlug
def update(conn, %{"tags" => tags, "image_ids" => image_ids}) do def update(conn, %{"tags" => tag_list, "image_ids" => image_ids}) do
tags = Tag.parse_tag_list(tags) tags = Tag.parse_tag_list(tag_list)
added_tag_names = Enum.reject(tags, &String.starts_with?(&1, "-")) added_tag_names = Enum.reject(tags, &String.starts_with?(&1, "-"))
@ -46,7 +46,15 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
case Images.batch_update(image_ids, added_tags, removed_tags, attributes) do case Images.batch_update(image_ids, added_tags, removed_tags, attributes) do
{:ok, _} -> {:ok, _} ->
json(conn, %{succeeded: image_ids, failed: []}) conn
|> PhilomenaWeb.ModerationLogPlug.call(
details: &log_details/3,
data: %{
tag_list: tag_list,
image_count: Enum.count(image_ids)
}
)
|> json(%{succeeded: image_ids, failed: []})
_error -> _error ->
json(conn, %{succeeded: [], failed: image_ids}) json(conn, %{succeeded: [], failed: image_ids})
@ -59,4 +67,11 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end end
end end
defp log_details(conn, _action, data) do
%{
body: "Batch tagged '#{data.tag_list}' on #{data.image_count} images",
subject_path: Routes.profile_path(conn, :show, conn.assigns.current_user)
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Admin.FingerprintBanController do defmodule PhilomenaWeb.Admin.FingerprintBanController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Bans.Fingerprint, as: FingerprintBan alias Philomena.Bans.Fingerprint, as: FingerprintBan
alias Philomena.Bans alias Philomena.Bans
alias Philomena.Repo alias Philomena.Repo
@ -44,9 +45,10 @@ defmodule PhilomenaWeb.Admin.FingerprintBanController do
def create(conn, %{"fingerprint" => fingerprint_ban_params}) do def create(conn, %{"fingerprint" => fingerprint_ban_params}) do
case Bans.create_fingerprint(conn.assigns.current_user, fingerprint_ban_params) do case Bans.create_fingerprint(conn.assigns.current_user, fingerprint_ban_params) do
{:ok, _fingerprint_ban} -> {:ok, fingerprint_ban} ->
conn conn
|> put_flash(:info, "Fingerprint was successfully banned.") |> put_flash(:info, "Fingerprint was successfully banned.")
|> ModerationLogPlug.call(details: &log_details/3, data: fingerprint_ban)
|> redirect(to: Routes.admin_fingerprint_ban_path(conn, :index)) |> redirect(to: Routes.admin_fingerprint_ban_path(conn, :index))
{:error, changeset} -> {:error, changeset} ->
@ -61,9 +63,10 @@ defmodule PhilomenaWeb.Admin.FingerprintBanController do
def update(conn, %{"fingerprint" => fingerprint_ban_params}) do def update(conn, %{"fingerprint" => fingerprint_ban_params}) do
case Bans.update_fingerprint(conn.assigns.fingerprint, fingerprint_ban_params) do case Bans.update_fingerprint(conn.assigns.fingerprint, fingerprint_ban_params) do
{:ok, _fingerprint_ban} -> {:ok, fingerprint_ban} ->
conn conn
|> put_flash(:info, "Fingerprint ban successfully updated.") |> put_flash(:info, "Fingerprint ban successfully updated.")
|> ModerationLogPlug.call(details: &log_details/3, data: fingerprint_ban)
|> redirect(to: Routes.admin_fingerprint_ban_path(conn, :index)) |> redirect(to: Routes.admin_fingerprint_ban_path(conn, :index))
{:error, changeset} -> {:error, changeset} ->
@ -72,10 +75,11 @@ defmodule PhilomenaWeb.Admin.FingerprintBanController do
end end
def delete(conn, _params) do def delete(conn, _params) do
{:ok, _fingerprint_ban} = Bans.delete_fingerprint(conn.assigns.fingerprint) {:ok, fingerprint_ban} = Bans.delete_fingerprint(conn.assigns.fingerprint)
conn conn
|> put_flash(:info, "Fingerprint ban successfully deleted.") |> put_flash(:info, "Fingerprint ban successfully deleted.")
|> ModerationLogPlug.call(details: &log_details/3, data: fingerprint_ban)
|> redirect(to: Routes.admin_fingerprint_ban_path(conn, :index)) |> redirect(to: Routes.admin_fingerprint_ban_path(conn, :index))
end end
@ -106,4 +110,15 @@ defmodule PhilomenaWeb.Admin.FingerprintBanController do
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end end
end end
defp log_details(conn, action, ban) do
body =
case action do
:create -> "Created a fingerprint ban #{ban.generated_ban_id}"
:update -> "Updated a fingerprint ban #{ban.generated_ban_id}"
:delete -> "Deleted a fingerprint ban #{ban.generated_ban_id}"
end
%{body: body, subject_path: Routes.admin_fingerprint_ban_path(conn, :index)}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Admin.SubnetBanController do defmodule PhilomenaWeb.Admin.SubnetBanController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Bans.Subnet, as: SubnetBan alias Philomena.Bans.Subnet, as: SubnetBan
alias Philomena.Bans alias Philomena.Bans
alias Philomena.Repo alias Philomena.Repo
@ -46,9 +47,10 @@ defmodule PhilomenaWeb.Admin.SubnetBanController do
def create(conn, %{"subnet" => subnet_ban_params}) do def create(conn, %{"subnet" => subnet_ban_params}) do
case Bans.create_subnet(conn.assigns.current_user, subnet_ban_params) do case Bans.create_subnet(conn.assigns.current_user, subnet_ban_params) do
{:ok, _subnet_ban} -> {:ok, subnet_ban} ->
conn conn
|> put_flash(:info, "Subnet was successfully banned.") |> put_flash(:info, "Subnet was successfully banned.")
|> ModerationLogPlug.call(details: &log_details/3, data: subnet_ban)
|> redirect(to: Routes.admin_subnet_ban_path(conn, :index)) |> redirect(to: Routes.admin_subnet_ban_path(conn, :index))
{:error, changeset} -> {:error, changeset} ->
@ -63,9 +65,10 @@ defmodule PhilomenaWeb.Admin.SubnetBanController do
def update(conn, %{"subnet" => subnet_ban_params}) do def update(conn, %{"subnet" => subnet_ban_params}) do
case Bans.update_subnet(conn.assigns.subnet, subnet_ban_params) do case Bans.update_subnet(conn.assigns.subnet, subnet_ban_params) do
{:ok, _subnet_ban} -> {:ok, subnet_ban} ->
conn conn
|> put_flash(:info, "Subnet ban successfully updated.") |> put_flash(:info, "Subnet ban successfully updated.")
|> ModerationLogPlug.call(details: &log_details/3, data: subnet_ban)
|> redirect(to: Routes.admin_subnet_ban_path(conn, :index)) |> redirect(to: Routes.admin_subnet_ban_path(conn, :index))
{:error, changeset} -> {:error, changeset} ->
@ -74,10 +77,11 @@ defmodule PhilomenaWeb.Admin.SubnetBanController do
end end
def delete(conn, _params) do def delete(conn, _params) do
{:ok, _subnet_ban} = Bans.delete_subnet(conn.assigns.subnet) {:ok, subnet_ban} = Bans.delete_subnet(conn.assigns.subnet)
conn conn
|> put_flash(:info, "Subnet ban successfully deleted.") |> put_flash(:info, "Subnet ban successfully deleted.")
|> ModerationLogPlug.call(details: &log_details/3, data: subnet_ban)
|> redirect(to: Routes.admin_subnet_ban_path(conn, :index)) |> redirect(to: Routes.admin_subnet_ban_path(conn, :index))
end end
@ -108,4 +112,15 @@ defmodule PhilomenaWeb.Admin.SubnetBanController do
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end end
end end
defp log_details(conn, action, ban) do
body =
case action do
:create -> "Created a subnet ban #{ban.generated_ban_id}"
:update -> "Updated a subnet ban #{ban.generated_ban_id}"
:delete -> "Deleted a subnet ban #{ban.generated_ban_id}"
end
%{body: body, subject_path: Routes.admin_subnet_ban_path(conn, :index)}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Admin.UserBanController do defmodule PhilomenaWeb.Admin.UserBanController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Bans.User, as: UserBan alias Philomena.Bans.User, as: UserBan
alias Philomena.Bans alias Philomena.Bans
alias Philomena.Repo alias Philomena.Repo
@ -47,9 +48,10 @@ defmodule PhilomenaWeb.Admin.UserBanController do
def create(conn, %{"user" => user_ban_params}) do def create(conn, %{"user" => user_ban_params}) do
case Bans.create_user(conn.assigns.current_user, user_ban_params) do case Bans.create_user(conn.assigns.current_user, user_ban_params) do
{:ok, _user_ban} -> {:ok, user_ban} ->
conn conn
|> put_flash(:info, "User was successfully banned.") |> put_flash(:info, "User was successfully banned.")
|> ModerationLogPlug.call(details: &log_details/3, data: user_ban)
|> redirect(to: Routes.admin_user_ban_path(conn, :index)) |> redirect(to: Routes.admin_user_ban_path(conn, :index))
{:error, :user_ban, changeset, _changes} -> {:error, :user_ban, changeset, _changes} ->
@ -67,9 +69,10 @@ defmodule PhilomenaWeb.Admin.UserBanController do
def update(conn, %{"user" => user_ban_params}) do def update(conn, %{"user" => user_ban_params}) do
case Bans.update_user(conn.assigns.user, user_ban_params) do case Bans.update_user(conn.assigns.user, user_ban_params) do
{:ok, _user_ban} -> {:ok, user_ban} ->
conn conn
|> put_flash(:info, "User ban successfully updated.") |> put_flash(:info, "User ban successfully updated.")
|> ModerationLogPlug.call(details: &log_details/3, data: user_ban)
|> redirect(to: Routes.admin_user_ban_path(conn, :index)) |> redirect(to: Routes.admin_user_ban_path(conn, :index))
{:error, changeset} -> {:error, changeset} ->
@ -78,10 +81,11 @@ defmodule PhilomenaWeb.Admin.UserBanController do
end end
def delete(conn, _params) do def delete(conn, _params) do
{:ok, _user_ban} = Bans.delete_user(conn.assigns.user) {:ok, user_ban} = Bans.delete_user(conn.assigns.user)
conn conn
|> put_flash(:info, "User ban successfully deleted.") |> put_flash(:info, "User ban successfully deleted.")
|> ModerationLogPlug.call(details: &log_details/3, data: user_ban)
|> redirect(to: Routes.admin_user_ban_path(conn, :index)) |> redirect(to: Routes.admin_user_ban_path(conn, :index))
end end
@ -112,4 +116,15 @@ defmodule PhilomenaWeb.Admin.UserBanController do
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end end
end end
defp log_details(conn, action, ban) do
body =
case action do
:create -> "Created a user ban #{ban.generated_ban_id}"
:update -> "Updated a user ban #{ban.generated_ban_id}"
:delete -> "Deleted a user ban #{ban.generated_ban_id}"
end
%{body: body, subject_path: Routes.admin_user_ban_path(conn, :index)}
end
end end

View file

@ -59,9 +59,10 @@ defmodule PhilomenaWeb.Admin.UserController do
def update(conn, %{"user" => user_params}) do def update(conn, %{"user" => user_params}) do
case Users.update_user(conn.assigns.user, user_params) do case Users.update_user(conn.assigns.user, user_params) do
{:ok, _user} -> {:ok, user} ->
conn conn
|> put_flash(:info, "User successfully updated.") |> put_flash(:info, "User successfully updated.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: user)
|> redirect(to: Routes.profile_path(conn, :show, conn.assigns.user)) |> redirect(to: Routes.profile_path(conn, :show, conn.assigns.user))
{:error, %{user: changeset}} -> {:error, %{user: changeset}} ->
@ -79,4 +80,11 @@ defmodule PhilomenaWeb.Admin.UserController do
defp load_roles(conn, _opts) do defp load_roles(conn, _opts) do
assign(conn, :roles, Repo.all(Role)) assign(conn, :roles, Repo.all(Role))
end end
defp log_details(conn, _action, user) do
%{
body: "Updated user details for #{user.name}",
subject_path: Routes.profile_path(conn, :show, user)
}
end
end end

View file

@ -22,6 +22,7 @@ defmodule PhilomenaWeb.Image.AnonymousController do
conn conn
|> put_flash(:info, "Successfully updated anonymity.") |> put_flash(:info, "Successfully updated anonymity.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
@ -31,4 +32,11 @@ defmodule PhilomenaWeb.Image.AnonymousController do
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end end
end end
defp log_details(conn, _action, image) do
%{
body: "Updated anonymity of image >>#{image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -16,6 +16,7 @@ defmodule PhilomenaWeb.Image.Comment.DeleteController do
conn conn
|> put_flash(:info, "Comment successfully destroyed!") |> put_flash(:info, "Comment successfully destroyed!")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: comment)
|> redirect( |> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}" to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
) )
@ -28,4 +29,11 @@ defmodule PhilomenaWeb.Image.Comment.DeleteController do
) )
end end
end end
defp log_details(conn, _action, comment) do
%{
body: "Destroyed comment on image >>#{comment.image_id}",
subject_path: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Image.Comment.HideController do defmodule PhilomenaWeb.Image.Comment.HideController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Comments.Comment alias Philomena.Comments.Comment
alias Philomena.Comments alias Philomena.Comments
@ -15,6 +16,7 @@ defmodule PhilomenaWeb.Image.Comment.HideController do
{:ok, comment} -> {:ok, comment} ->
conn conn
|> put_flash(:info, "Comment successfully hidden!") |> put_flash(:info, "Comment successfully hidden!")
|> ModerationLogPlug.call(details: &log_details/3, data: comment)
|> redirect( |> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}" to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
) )
@ -35,6 +37,7 @@ defmodule PhilomenaWeb.Image.Comment.HideController do
{:ok, comment} -> {:ok, comment} ->
conn conn
|> put_flash(:info, "Comment successfully unhidden!") |> put_flash(:info, "Comment successfully unhidden!")
|> ModerationLogPlug.call(details: &log_details/3, data: comment)
|> redirect( |> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}" to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
) )
@ -47,4 +50,17 @@ defmodule PhilomenaWeb.Image.Comment.HideController do
) )
end end
end end
defp log_details(conn, action, comment) do
body =
case action do
:create -> "Hidden comment on image >>#{comment.image_id} (#{comment.deletion_reason})"
:delete -> "Restored comment on image >>#{comment.image_id}"
end
%{
body: body,
subject_path: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Image.CommentLockController do defmodule PhilomenaWeb.Image.CommentLockController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Images alias Philomena.Images
@ -12,6 +13,7 @@ defmodule PhilomenaWeb.Image.CommentLockController do
conn conn
|> put_flash(:info, "Successfully locked comments.") |> put_flash(:info, "Successfully locked comments.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
@ -20,6 +22,20 @@ defmodule PhilomenaWeb.Image.CommentLockController do
conn conn
|> put_flash(:info, "Successfully unlocked comments.") |> put_flash(:info, "Successfully unlocked comments.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, action, image) do
body =
case action do
:create -> "Locked comments on image >>#{image.id}"
:delete -> "Unlocked comments on image >>#{image.id}"
end
%{
body: body,
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -4,6 +4,7 @@ defmodule PhilomenaWeb.Image.DeleteController do
# N.B.: this would be Image.Hide, because it hides the image, but that is # N.B.: this would be Image.Hide, because it hides the image, but that is
# taken by the user action # taken by the user action
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Images alias Philomena.Images
@ -16,9 +17,10 @@ defmodule PhilomenaWeb.Image.DeleteController do
user = conn.assigns.current_user user = conn.assigns.current_user
case Images.hide_image(image, user, image_params) do case Images.hide_image(image, user, image_params) do
{:ok, _image} -> {:ok, result} ->
conn conn
|> put_flash(:info, "Image successfully hidden.") |> put_flash(:info, "Image successfully hidden.")
|> ModerationLogPlug.call(details: &log_details/3, data: result.image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
_error -> _error ->
@ -35,6 +37,7 @@ defmodule PhilomenaWeb.Image.DeleteController do
{:ok, image} -> {:ok, image} ->
conn conn
|> put_flash(:info, "Hide reason updated.") |> put_flash(:info, "Hide reason updated.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
{:error, _changeset} -> {:error, _changeset} ->
@ -60,10 +63,25 @@ defmodule PhilomenaWeb.Image.DeleteController do
def delete(conn, _params) do def delete(conn, _params) do
image = conn.assigns.image image = conn.assigns.image
{:ok, _image} = Images.unhide_image(image) {:ok, image} = Images.unhide_image(image)
conn conn
|> put_flash(:info, "Image successfully unhidden.") |> put_flash(:info, "Image successfully unhidden.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, action, image) do
body =
case action do
:create -> "Hidden image >>#{image.id} (#{image.deletion_reason})"
:update -> "Changed hide reason of >>#{image.id} (#{image.deletion_reason})"
:delete -> "Restored image >>#{image.id}"
end
%{
body: body,
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Image.DescriptionLockController do defmodule PhilomenaWeb.Image.DescriptionLockController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Images alias Philomena.Images
@ -12,6 +13,7 @@ defmodule PhilomenaWeb.Image.DescriptionLockController do
conn conn
|> put_flash(:info, "Successfully locked description.") |> put_flash(:info, "Successfully locked description.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
@ -20,6 +22,20 @@ defmodule PhilomenaWeb.Image.DescriptionLockController do
conn conn
|> put_flash(:info, "Successfully unlocked description.") |> put_flash(:info, "Successfully unlocked description.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, action, image) do
body =
case action do
:create -> "Locked description editing on image >>#{image.id}"
:delete -> "Unlocked description editing on image >>#{image.id}"
end
%{
body: body,
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -15,6 +15,7 @@ defmodule PhilomenaWeb.Image.DestroyController do
{:ok, image} -> {:ok, image} ->
conn conn
|> put_flash(:info, "Image contents destroyed.") |> put_flash(:info, "Image contents destroyed.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
_error -> _error ->
@ -36,4 +37,11 @@ defmodule PhilomenaWeb.Image.DestroyController do
|> halt() |> halt()
end end
end end
defp log_details(conn, _action, image) do
%{
body: "Hard-deleted image >>#{image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -16,6 +16,7 @@ defmodule PhilomenaWeb.Image.FeatureController do
conn conn
|> put_flash(:info, "Image marked as featured image.") |> put_flash(:info, "Image marked as featured image.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
@ -31,4 +32,11 @@ defmodule PhilomenaWeb.Image.FeatureController do
conn conn
end end
end end
defp log_details(conn, _action, image) do
%{
body: "Featured image >>#{image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -12,6 +12,14 @@ defmodule PhilomenaWeb.Image.HashController do
conn conn
|> put_flash(:info, "Successfully cleared hash.") |> put_flash(:info, "Successfully cleared hash.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, _action, image) do
%{
body: "Cleared hash of image >>#{image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -13,6 +13,14 @@ defmodule PhilomenaWeb.Image.RepairController do
conn conn
|> put_flash(:info, "Repair job enqueued.") |> put_flash(:info, "Repair job enqueued.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: conn.assigns.image)
|> redirect(to: Routes.image_path(conn, :show, conn.assigns.image)) |> redirect(to: Routes.image_path(conn, :show, conn.assigns.image))
end end
defp log_details(conn, _action, image) do
%{
body: "Repaired image >>#{image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -17,6 +17,14 @@ defmodule PhilomenaWeb.Image.ScratchpadController do
conn conn
|> put_flash(:info, "Successfully updated moderation notes.") |> put_flash(:info, "Successfully updated moderation notes.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, _action, image) do
%{
body: "Updated mod notes on image >>#{image.id} (#{image.scratchpad})",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -14,6 +14,14 @@ defmodule PhilomenaWeb.Image.SourceHistoryController do
conn conn
|> put_flash(:info, "Successfully deleted source history.") |> put_flash(:info, "Successfully deleted source history.")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, _action, image) do
%{
body: "Deleted source history for image >>#{image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Image.TagLockController do defmodule PhilomenaWeb.Image.TagLockController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Images alias Philomena.Images
@ -23,6 +24,7 @@ defmodule PhilomenaWeb.Image.TagLockController do
conn conn
|> put_flash(:info, "Successfully updated list of locked tags.") |> put_flash(:info, "Successfully updated list of locked tags.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
@ -31,6 +33,7 @@ defmodule PhilomenaWeb.Image.TagLockController do
conn conn
|> put_flash(:info, "Successfully locked tags.") |> put_flash(:info, "Successfully locked tags.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
@ -39,6 +42,21 @@ defmodule PhilomenaWeb.Image.TagLockController do
conn conn
|> put_flash(:info, "Successfully unlocked tags.") |> put_flash(:info, "Successfully unlocked tags.")
|> ModerationLogPlug.call(details: &log_details/3, data: image)
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))
end end
defp log_details(conn, action, image) do
body =
case action do
:create -> "Locked tags on image >>#{image.id}"
:update -> "Updated list of locked tags on image >>#{image.id}"
:delete -> "Unlocked tags on image >>#{image.id}"
end
%{
body: body,
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -16,7 +16,7 @@ defmodule PhilomenaWeb.Image.TamperController do
image = conn.assigns.image image = conn.assigns.image
user = conn.assigns.user user = conn.assigns.user
{:ok, _result} = {:ok, result} =
ImageVotes.delete_vote_transaction(image, user) ImageVotes.delete_vote_transaction(image, user)
|> Repo.transaction() |> Repo.transaction()
@ -24,6 +24,26 @@ defmodule PhilomenaWeb.Image.TamperController do
conn conn
|> put_flash(:info, "Vote removed.") |> put_flash(:info, "Vote removed.")
|> PhilomenaWeb.ModerationLogPlug.call(
details: &log_details/3,
data: %{vote: result, image: image}
)
|> redirect(to: Routes.image_path(conn, :show, conn.assigns.image)) |> redirect(to: Routes.image_path(conn, :show, conn.assigns.image))
end end
defp log_details(conn, _action, data) do
image = data.image
vote_type =
case data.vote do
%{undownvote: {1, _}} -> "downvote"
%{unupvote: {1, _}} -> "upvote"
_ -> "vote"
end
%{
body: "Deleted #{vote_type} by #{conn.assigns.user.name} on image >>#{data.image.id}",
subject_path: Routes.image_path(conn, :show, image)
}
end
end end

View file

@ -0,0 +1,15 @@
defmodule PhilomenaWeb.ModerationLogController do
use PhilomenaWeb, :controller
alias Philomena.ModerationLogs
alias Philomena.ModerationLogs.ModerationLog
plug :load_and_authorize_resource,
model: ModerationLog,
preload: [:user]
def index(conn, _params) do
moderation_logs = ModerationLogs.list_moderation_logs(conn)
render(conn, "index.html", title: "Moderation Logs", moderation_logs: moderation_logs)
end
end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Tag.ImageController do defmodule PhilomenaWeb.Tag.ImageController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
alias Philomena.Tags alias Philomena.Tags
@ -23,6 +24,7 @@ defmodule PhilomenaWeb.Tag.ImageController do
{:ok, tag} -> {:ok, tag} ->
conn conn
|> put_flash(:info, "Tag image successfully updated.") |> put_flash(:info, "Tag image successfully updated.")
|> ModerationLogPlug.call(details: &log_details/3, data: tag)
|> redirect(to: Routes.tag_path(conn, :show, tag)) |> redirect(to: Routes.tag_path(conn, :show, tag))
{:error, :tag, changeset, _changes} -> {:error, :tag, changeset, _changes} ->
@ -31,10 +33,24 @@ defmodule PhilomenaWeb.Tag.ImageController do
end end
def delete(conn, _params) do def delete(conn, _params) do
{:ok, _tag} = Tags.remove_tag_image(conn.assigns.tag) {:ok, tag} = Tags.remove_tag_image(conn.assigns.tag)
conn conn
|> put_flash(:info, "Tag image successfully removed.") |> put_flash(:info, "Tag image successfully removed.")
|> ModerationLogPlug.call(details: &log_details/3, data: tag)
|> redirect(to: Routes.tag_path(conn, :show, conn.assigns.tag)) |> redirect(to: Routes.tag_path(conn, :show, conn.assigns.tag))
end end
defp log_details(conn, action, tag) do
body =
case action do
:update -> "Updated image on tag '#{tag.name}'"
:delete -> "Removed image on tag '#{tag.name}'"
end
%{
body: body,
subject_path: Routes.tag_path(conn, :show, tag)
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.TagController do defmodule PhilomenaWeb.TagController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias PhilomenaWeb.ImageLoader alias PhilomenaWeb.ImageLoader
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.{Tags, Tags.Tag} alias Philomena.{Tags, Tags.Tag}
@ -97,6 +98,7 @@ defmodule PhilomenaWeb.TagController do
{:ok, tag} -> {:ok, tag} ->
conn conn
|> put_flash(:info, "Tag successfully updated.") |> put_flash(:info, "Tag successfully updated.")
|> ModerationLogPlug.call(details: &log_details/3, data: tag)
|> redirect(to: Routes.tag_path(conn, :show, tag)) |> redirect(to: Routes.tag_path(conn, :show, tag))
{:error, changeset} -> {:error, changeset} ->
@ -105,10 +107,11 @@ defmodule PhilomenaWeb.TagController do
end end
def delete(conn, _params) do def delete(conn, _params) do
{:ok, _tag} = Tags.delete_tag(conn.assigns.tag) {:ok, tag} = Tags.delete_tag(conn.assigns.tag)
conn conn
|> put_flash(:info, "Tag queued for deletion.") |> put_flash(:info, "Tag queued for deletion.")
|> ModerationLogPlug.call(details: &log_details/3, data: tag)
|> redirect(to: "/") |> redirect(to: "/")
end end
@ -169,4 +172,17 @@ defmodule PhilomenaWeb.TagController do
|> halt() |> halt()
end end
end end
defp log_details(conn, action, tag) do
body =
case action do
:update -> "Updated details on tag '#{tag.name}'"
:delete -> "Deleted tag '#{tag.name}'"
end
%{
body: body,
subject_path: Routes.tag_path(conn, :show, tag)
}
end
end end

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.Topic.HideController do
import Plug.Conn import Plug.Conn
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Forums.Forum alias Philomena.Forums.Forum
alias Philomena.Topics.Topic alias Philomena.Topics.Topic
alias Philomena.Topics alias Philomena.Topics
@ -27,6 +28,7 @@ defmodule PhilomenaWeb.Topic.HideController do
{:ok, topic} -> {:ok, topic} ->
conn conn
|> put_flash(:info, "Topic successfully hidden!") |> put_flash(:info, "Topic successfully hidden!")
|> ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -43,6 +45,7 @@ defmodule PhilomenaWeb.Topic.HideController do
{:ok, topic} -> {:ok, topic} ->
conn conn
|> put_flash(:info, "Topic successfully restored!") |> put_flash(:info, "Topic successfully restored!")
|> ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -51,4 +54,20 @@ defmodule PhilomenaWeb.Topic.HideController do
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
end end
end end
defp log_details(conn, action, topic) do
body =
case action do
:create ->
"Hidden topic '#{topic.title}' (#{topic.deletion_reason}) in #{topic.forum.name}"
:delete ->
"Restored topic '#{topic.title}' in #{topic.forum.name}"
end
%{
body: body,
subject_path: Routes.forum_topic_path(conn, :show, topic.forum, topic)
}
end
end end

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.Topic.LockController do
import Plug.Conn import Plug.Conn
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Forums.Forum alias Philomena.Forums.Forum
alias Philomena.Topics.Topic alias Philomena.Topics.Topic
alias Philomena.Topics alias Philomena.Topics
@ -26,6 +27,7 @@ defmodule PhilomenaWeb.Topic.LockController do
{:ok, topic} -> {:ok, topic} ->
conn conn
|> put_flash(:info, "Topic successfully locked!") |> put_flash(:info, "Topic successfully locked!")
|> ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -42,6 +44,7 @@ defmodule PhilomenaWeb.Topic.LockController do
{:ok, topic} -> {:ok, topic} ->
conn conn
|> put_flash(:info, "Topic successfully unlocked!") |> put_flash(:info, "Topic successfully unlocked!")
|> ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -50,4 +53,17 @@ defmodule PhilomenaWeb.Topic.LockController do
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
end end
end end
defp log_details(conn, action, topic) do
body =
case action do
:create -> "Locked topic '#{topic.title}' (#{topic.lock_reason}) in #{topic.forum.name}"
:delete -> "Unlocked topic '#{topic.title}' in #{topic.forum.name}"
end
%{
body: body,
subject_path: Routes.forum_topic_path(conn, :show, topic.forum, topic)
}
end
end end

View file

@ -29,6 +29,7 @@ defmodule PhilomenaWeb.Topic.MoveController do
conn conn
|> put_flash(:info, "Topic successfully moved!") |> put_flash(:info, "Topic successfully moved!")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -37,4 +38,11 @@ defmodule PhilomenaWeb.Topic.MoveController do
|> redirect(to: Routes.forum_topic_path(conn, :show, conn.assigns.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, conn.assigns.forum, topic))
end end
end end
defp log_details(conn, _action, topic) do
%{
body: "Topic '#{topic.title}' moved to #{topic.forum.name}",
subject_path: Routes.forum_topic_path(conn, :show, topic.forum, topic)
}
end
end end

View file

@ -19,6 +19,7 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do
{:ok, post} -> {:ok, post} ->
conn conn
|> put_flash(:info, "Post successfully destroyed!") |> put_flash(:info, "Post successfully destroyed!")
|> PhilomenaWeb.ModerationLogPlug.call(details: &log_details/3, data: post)
|> redirect( |> redirect(
to: to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
@ -35,4 +36,13 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do
) )
end end
end end
defp log_details(conn, _action, post) do
%{
body: "Destroyed forum post ##{post.id} in topic '#{post.topic.title}'",
subject_path:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
"#post_#{post.id}"
}
end
end end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Topic.Post.HideController do defmodule PhilomenaWeb.Topic.Post.HideController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Posts.Post alias Philomena.Posts.Post
alias Philomena.Posts alias Philomena.Posts
@ -20,6 +21,7 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
{:ok, post} -> {:ok, post} ->
conn conn
|> put_flash(:info, "Post successfully hidden.") |> put_flash(:info, "Post successfully hidden.")
|> ModerationLogPlug.call(details: &log_details/3, data: post)
|> redirect( |> redirect(
to: to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
@ -44,6 +46,7 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
{:ok, post} -> {:ok, post} ->
conn conn
|> put_flash(:info, "Post successfully unhidden.") |> put_flash(:info, "Post successfully unhidden.")
|> ModerationLogPlug.call(details: &log_details/3, data: post)
|> redirect( |> redirect(
to: to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
@ -60,4 +63,22 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
) )
end end
end end
defp log_details(conn, action, post) do
body =
case action do
:create ->
"Hidden forum post ##{post.id} in topic '#{post.topic.title}' (#{post.deletion_reason})"
:delete ->
"Restored forum post ##{post.id} in topic '#{post.topic.title}'"
end
%{
body: body,
subject_path:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
"#post_#{post.id}"
}
end
end end

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.Topic.StickController do
import Plug.Conn import Plug.Conn
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ModerationLogPlug
alias Philomena.Forums.Forum alias Philomena.Forums.Forum
alias Philomena.Topics.Topic alias Philomena.Topics.Topic
alias Philomena.Topics alias Philomena.Topics
@ -25,6 +26,7 @@ defmodule PhilomenaWeb.Topic.StickController do
{:ok, topic} -> {:ok, topic} ->
conn conn
|> put_flash(:info, "Topic successfully stickied!") |> put_flash(:info, "Topic successfully stickied!")
|> ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -41,6 +43,7 @@ defmodule PhilomenaWeb.Topic.StickController do
{:ok, topic} -> {:ok, topic} ->
conn conn
|> put_flash(:info, "Topic successfully unstickied!") |> put_flash(:info, "Topic successfully unstickied!")
|> ModerationLogPlug.call(details: &log_details/3, data: topic)
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} -> {:error, _changeset} ->
@ -49,4 +52,17 @@ defmodule PhilomenaWeb.Topic.StickController do
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic)) |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
end end
end end
defp log_details(conn, action, topic) do
body =
case action do
:create -> "Stickied topic '#{topic.title}' in #{topic.forum.name}"
:delete -> "Unstickied topic '#{topic.title}' in #{topic.forum.name}"
end
%{
body: body,
subject_path: Routes.forum_topic_path(conn, :show, topic.forum, topic)
}
end
end end

View file

@ -0,0 +1,42 @@
defmodule PhilomenaWeb.ModerationLogPlug do
@moduledoc """
This plug writes moderation logs.
## Example
plug PhilomenaWeb.ModerationLogPlug, [details: &log_details/2]
"""
@controller_regex ~r/PhilomenaWeb\.([\w\.]+)Controller\z/
alias Plug.Conn
alias Phoenix.Controller
alias Philomena.ModerationLogs
@doc false
@spec init(any()) :: any()
def init(opts), do: opts
@type log_details :: %{subject_path: String.t(), body: String.t()}
@type details_func :: (Plug.Conn.t(), atom(), any() -> log_details())
@type call_opts :: [details: details_func, data: any()]
@doc false
@spec call(Conn.t(), call_opts) :: Conn.t()
def call(conn, opts) do
details_func = Keyword.fetch!(opts, :details)
userdata = Keyword.get(opts, :data, nil)
user = conn.assigns.current_user
action = Controller.action_name(conn)
%{subject_path: subject_path, body: body} = details_func.(conn, action, userdata)
mod = Controller.controller_module(conn)
[mod_name] = Regex.run(@controller_regex, to_string(mod), capture: :all_but_first)
type = "#{mod_name}:#{action}"
ModerationLogs.create_moderation_log(user, type, subject_path, body)
conn
end
end

View file

@ -318,6 +318,8 @@ defmodule PhilomenaWeb.Router do
resources "/source_changes", FingerprintProfile.SourceChangeController, only: [:index] resources "/source_changes", FingerprintProfile.SourceChangeController, only: [:index]
end end
resources "/moderation_logs", ModerationLogController, only: [:index]
scope "/admin", Admin, as: :admin do scope "/admin", Admin, as: :admin do
resources "/reports", ReportController, only: [:index, :show] do resources "/reports", ReportController, only: [:index, :show] do
resources "/claim", Report.ClaimController, only: [:create, :delete], singleton: true resources "/claim", Report.ClaimController, only: [:create, :delete], singleton: true

View file

@ -38,9 +38,14 @@
= if manages_mod_notes?(@conn) do = if manages_mod_notes?(@conn) do
= link to: Routes.admin_mod_note_path(@conn, :index), class: "header__link" do = link to: Routes.admin_mod_note_path(@conn, :index), class: "header__link" do
i.fa.fa-fw.fa-sticky-note> i.fa.fa-fw.fa-clipboard>
' Mod Notes ' Mod Notes
= if can_see_moderation_log?(@conn) do
= link to: Routes.moderation_log_path(@conn, :index), class: "header__link" do
i.fa.fa-fw.fa-list-alt>
' Mod Logs
= if @duplicate_report_count do = if @duplicate_report_count do
= link to: Routes.duplicate_report_path(@conn, :index), class: "header__link", title: "Duplicates" do = link to: Routes.duplicate_report_path(@conn, :index), class: "header__link", title: "Duplicates" do
' D ' D

View file

@ -0,0 +1,33 @@
elixir:
route = fn p -> Routes.moderation_log_path(@conn, :index, p) end
pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @moderation_logs, route: route, conn: @conn
h1 Listing Moderation Logs
block
.block__header
.page__pagination = pagination
table
thead
tr
th Moderator
th Type
th Body
th Creation time
th Actions
tbody
= for log <- @moderation_logs do
tr
td = render PhilomenaWeb.UserAttributionView, "_user.html", object: %{user: log.user}, conn: @conn
td = log.type
td = log.body
td = pretty_time(log.created_at)
td
= link to: log.subject_path do
i.fa.fa-eye>
' View subject
block
.block__header
.page__pagination = pagination

View file

@ -117,6 +117,9 @@ defmodule PhilomenaWeb.LayoutView do
def manages_bans?(conn), def manages_bans?(conn),
do: can?(conn, :create, Philomena.Bans.User) do: can?(conn, :create, Philomena.Bans.User)
def can_see_moderation_log?(conn),
do: can?(conn, :index, Philomena.ModerationLogs.ModerationLog)
def viewport_meta_tag(conn) do def viewport_meta_tag(conn) do
ua = get_user_agent(conn) ua = get_user_agent(conn)

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.ModerationLogView do
use PhilomenaWeb, :view
end

View file

@ -0,0 +1,20 @@
defmodule Philomena.Repo.Migrations.CreateModerationLogs do
use Ecto.Migration
def change do
create table(:moderation_logs) do
add :user_id, references(:users, on_delete: :delete_all), null: false
add :body, :varchar, null: false
add :subject_path, :varchar, null: false
add :type, :varchar, null: false
timestamps(inserted_at: :created_at, updated_at: false, type: :utc_datetime)
end
create index(:moderation_logs, [:user_id])
create index(:moderation_logs, [:type])
create index(:moderation_logs, [:created_at])
create index(:moderation_logs, [:user_id, :created_at])
create index(:moderation_logs, [:type, :created_at])
end
end

View file

@ -1054,6 +1054,39 @@ CREATE SEQUENCE public.mod_notes_id_seq
ALTER SEQUENCE public.mod_notes_id_seq OWNED BY public.mod_notes.id; ALTER SEQUENCE public.mod_notes_id_seq OWNED BY public.mod_notes.id;
--
-- Name: moderation_logs; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.moderation_logs (
id bigint NOT NULL,
user_id bigint NOT NULL,
body character varying NOT NULL,
subject_path character varying NOT NULL,
type character varying NOT NULL,
created_at timestamp(0) without time zone NOT NULL
);
--
-- Name: moderation_logs_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.moderation_logs_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: moderation_logs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.moderation_logs_id_seq OWNED BY public.moderation_logs.id;
-- --
-- Name: notifications; Type: TABLE; Schema: public; Owner: - -- Name: notifications; Type: TABLE; Schema: public; Owner: -
-- --
@ -2244,6 +2277,13 @@ ALTER TABLE ONLY public.messages ALTER COLUMN id SET DEFAULT nextval('public.mes
ALTER TABLE ONLY public.mod_notes ALTER COLUMN id SET DEFAULT nextval('public.mod_notes_id_seq'::regclass); ALTER TABLE ONLY public.mod_notes ALTER COLUMN id SET DEFAULT nextval('public.mod_notes_id_seq'::regclass);
--
-- Name: moderation_logs id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.moderation_logs ALTER COLUMN id SET DEFAULT nextval('public.moderation_logs_id_seq'::regclass);
-- --
-- Name: notifications id; Type: DEFAULT; Schema: public; Owner: - -- Name: notifications id; Type: DEFAULT; Schema: public; Owner: -
-- --
@ -2603,6 +2643,14 @@ ALTER TABLE ONLY public.mod_notes
ADD CONSTRAINT mod_notes_pkey PRIMARY KEY (id); ADD CONSTRAINT mod_notes_pkey PRIMARY KEY (id);
--
-- Name: moderation_logs moderation_logs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.moderation_logs
ADD CONSTRAINT moderation_logs_pkey PRIMARY KEY (id);
-- --
-- Name: notifications notifications_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- Name: notifications notifications_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- --
@ -4001,6 +4049,41 @@ CREATE INDEX index_vpns_on_ip ON public.vpns USING gist (ip inet_ops);
CREATE INDEX intensities_index ON public.images USING btree (se_intensity, sw_intensity, ne_intensity, nw_intensity, average_intensity); CREATE INDEX intensities_index ON public.images USING btree (se_intensity, sw_intensity, ne_intensity, nw_intensity, average_intensity);
--
-- Name: moderation_logs_created_at_index; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX moderation_logs_created_at_index ON public.moderation_logs USING btree (created_at);
--
-- Name: moderation_logs_type_created_at_index; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX moderation_logs_type_created_at_index ON public.moderation_logs USING btree (type, created_at);
--
-- Name: moderation_logs_type_index; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX moderation_logs_type_index ON public.moderation_logs USING btree (type);
--
-- Name: moderation_logs_user_id_created_at_index; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX moderation_logs_user_id_created_at_index ON public.moderation_logs USING btree (user_id, created_at);
--
-- Name: moderation_logs_user_id_index; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX moderation_logs_user_id_index ON public.moderation_logs USING btree (user_id);
-- --
-- Name: user_tokens_context_token_index; Type: INDEX; Schema: public; Owner: - -- Name: user_tokens_context_token_index; Type: INDEX; Schema: public; Owner: -
-- --
@ -4831,6 +4914,14 @@ ALTER TABLE ONLY public.image_tag_locks
ADD CONSTRAINT image_tag_locks_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.tags(id) ON DELETE CASCADE; ADD CONSTRAINT image_tag_locks_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.tags(id) ON DELETE CASCADE;
--
-- Name: moderation_logs moderation_logs_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.moderation_logs
ADD CONSTRAINT moderation_logs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
-- --
-- Name: user_tokens user_tokens_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- Name: user_tokens user_tokens_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- --
@ -4867,3 +4958,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20210912171343);
INSERT INTO public."schema_migrations" (version) VALUES (20210917190346); INSERT INTO public."schema_migrations" (version) VALUES (20210917190346);
INSERT INTO public."schema_migrations" (version) VALUES (20210921025336); INSERT INTO public."schema_migrations" (version) VALUES (20210921025336);
INSERT INTO public."schema_migrations" (version) VALUES (20210929181319); INSERT INTO public."schema_migrations" (version) VALUES (20210929181319);
INSERT INTO public."schema_migrations" (version) VALUES (20211107130226);

View file

@ -0,0 +1,29 @@
defmodule Philomena.ModerationLogsTest do
use Philomena.DataCase
alias Philomena.ModerationLogs
describe "moderation_logs" do
alias Philomena.ModerationLogs.ModerationLog
import Philomena.ModerationLogsFixtures
@invalid_attrs %{}
test "list_moderation_logs/0 returns all moderation_logs" do
moderation_log = moderation_log_fixture()
assert ModerationLogs.list_moderation_logs() == [moderation_log]
end
test "create_moderation_log/1 with valid data creates a moderation_log" do
valid_attrs = %{}
assert {:ok, %ModerationLog{} = moderation_log} =
ModerationLogs.create_moderation_log(valid_attrs)
end
test "create_moderation_log/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = ModerationLogs.create_moderation_log(@invalid_attrs)
end
end
end

View file

@ -0,0 +1,16 @@
defmodule PhilomenaWeb.ModerationLogControllerTest do
use PhilomenaWeb.ConnCase
import Philomena.ModerationLogsFixtures
@create_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
describe "index" do
test "lists all moderation_logs", %{conn: conn} do
conn = get(conn, Routes.moderation_log_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Moderation logs"
end
end
end

View file

@ -0,0 +1,18 @@
defmodule Philomena.ModerationLogsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Philomena.ModerationLogs` context.
"""
@doc """
Generate a moderation_log.
"""
def moderation_log_fixture(attrs \\ %{}) do
{:ok, moderation_log} =
attrs
|> Enum.into(%{})
|> Philomena.ModerationLogs.create_moderation_log()
moderation_log
end
end