From 7caafd976ed69f74a6848d2de9c263de74d6cad9 Mon Sep 17 00:00:00 2001 From: Luna D Date: Sun, 22 Dec 2019 17:17:20 -0500 Subject: [PATCH] re-add mod scratchpad by blatantly copy-pasting --- lib/philomena/users.ex | 6 ++++ lib/philomena/users/user.ex | 5 ++++ .../profile/scratchpad_controller.ex | 29 +++++++++++++++++++ .../controllers/profile_controller.ex | 6 +++- lib/philomena_web/router.ex | 2 +- .../templates/profile/_about_me.html.slime | 2 +- .../profile/scratchpad/edit.html.slime | 29 +++++++++++++++++++ .../templates/profile/show.html.slime | 5 ++++ .../views/profile/scratchpad_view.ex | 3 ++ 9 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 lib/philomena_web/controllers/profile/scratchpad_controller.ex create mode 100644 lib/philomena_web/templates/profile/scratchpad/edit.html.slime create mode 100644 lib/philomena_web/views/profile/scratchpad_view.ex diff --git a/lib/philomena/users.ex b/lib/philomena/users.ex index 07bb6e28..48add23c 100644 --- a/lib/philomena/users.ex +++ b/lib/philomena/users.ex @@ -106,6 +106,12 @@ defmodule Philomena.Users do |> Repo.update() end + def update_scratchpad(%User{} = user, attrs) do + user + |> User.scratchpad_changeset(attrs) + |> Repo.update() + end + def watch_tag(%User{} = user, tag) do watched_tag_ids = Enum.uniq([tag.id | user.watched_tag_ids]) diff --git a/lib/philomena/users/user.ex b/lib/philomena/users/user.ex index 1e3afcf8..2c09d471 100644 --- a/lib/philomena/users/user.ex +++ b/lib/philomena/users/user.ex @@ -222,6 +222,11 @@ defmodule Philomena.Users.User do |> validate_format(:personal_title, ~r/\A((?!site|admin|moderator|assistant|developer|\p{C}).)*\z/iu) end + def scratchpad_changeset(user, attrs) do + user + |> cast(attrs, [:scratchpad]) + end + def avatar_changeset(user, attrs) do user |> cast(attrs, [ diff --git a/lib/philomena_web/controllers/profile/scratchpad_controller.ex b/lib/philomena_web/controllers/profile/scratchpad_controller.ex new file mode 100644 index 00000000..df728548 --- /dev/null +++ b/lib/philomena_web/controllers/profile/scratchpad_controller.ex @@ -0,0 +1,29 @@ +defmodule PhilomenaWeb.Profile.ScratchpadController do + use PhilomenaWeb, :controller + + alias Philomena.Users.User + alias Philomena.Users + + plug PhilomenaWeb.FilterBannedUsersPlug + plug PhilomenaWeb.CanaryMapPlug, edit: :index, update: :index + plug :load_resource, model: User, id_name: "profile_id", id_field: "slug", persisted: true + + def edit(conn, _params) do + changeset = Users.change_user(conn.assigns.user) + render(conn, "edit.html", title: "Editing Moderation Scratchpad", changeset: changeset, user: conn.assigns.user) + end + + def update(conn, %{"user" => user_params}) do + user = conn.assigns.user + + case Users.update_scratchpad(user, user_params) do + {:ok, _user} -> + conn + |> put_flash(:info, "Moderation scratchpad successfully updated.") + |> redirect(to: Routes.profile_path(conn, :show, user)) + + {:error, changeset} -> + render(conn, "edit.html", changeset: changeset) + end + end +end diff --git a/lib/philomena_web/controllers/profile_controller.ex b/lib/philomena_web/controllers/profile_controller.ex index 65091802..1977d785 100644 --- a/lib/philomena_web/controllers/profile_controller.ex +++ b/lib/philomena_web/controllers/profile_controller.ex @@ -108,7 +108,10 @@ defmodule PhilomenaWeb.ProfileController do |> Enum.filter(&Canada.Can.can?(current_user, :show, &1.topic)) about_me = - Renderer.render_one(%{body: user.description || ""}, conn) + Renderer.render_one(%{body: user.description || ""}, conn) + + scratchpad = + Renderer.render_one(%{body: user.scratchpad || ""}, conn) commission_information = commission_info(user.commission, conn) @@ -145,6 +148,7 @@ defmodule PhilomenaWeb.ProfileController do statistics: statistics, watcher_counts: watcher_counts, about_me: about_me, + scratchpad: scratchpad, tags: tags, bans: bans, layout_class: "layout--medium", diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 57e698b2..21a501ec 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -159,6 +159,7 @@ defmodule PhilomenaWeb.Router do resources "/reports", Profile.Commission.ReportController, only: [:new, :create] end resources "/description", Profile.DescriptionController, only: [:edit, :update], singleton: true + resources "/scratchpad", Profile.ScratchpadController, only: [:edit, :update], singleton: true resources "/user_links", Profile.UserLinkController resources "/awards", Profile.AwardController, except: [:index, :show] @@ -286,7 +287,6 @@ defmodule PhilomenaWeb.Router do resources "/sources", Image.SourceController, only: [:update], singleton: true resources "/tag_changes", Image.TagChangeController, only: [:index] resources "/source_changes", Image.SourceChangeController, only: [:index] - resources "/description", Image.DescriptionController, only: [:update], singleton: true resources "/navigate", Image.NavigateController, only: [:index] resources "/reports", Image.ReportController, only: [:new, :create] resources "/reporting", Image.ReportingController, only: [:show], singleton: true diff --git a/lib/philomena_web/templates/profile/_about_me.html.slime b/lib/philomena_web/templates/profile/_about_me.html.slime index 1b63540d..224df07a 100644 --- a/lib/philomena_web/templates/profile/_about_me.html.slime +++ b/lib/philomena_web/templates/profile/_about_me.html.slime @@ -8,4 +8,4 @@ ' Want to => link "add some info about yourself?", to: "#" - - true -> \ No newline at end of file + - true -> diff --git a/lib/philomena_web/templates/profile/scratchpad/edit.html.slime b/lib/philomena_web/templates/profile/scratchpad/edit.html.slime new file mode 100644 index 00000000..d381c93e --- /dev/null +++ b/lib/philomena_web/templates/profile/scratchpad/edit.html.slime @@ -0,0 +1,29 @@ +h1 Updating Moderation Scratchpad + += form_for @changeset, Routes.profile_scratchpad_path(@conn, :update, @user), [method: "put"], fn f -> + = if @changeset.action do + .alert.alert-danger + p Oops, something went wrong! Please check the errors below. + .block + .block__header.block__header--js-tabbed + a.selected href="#" data-click-tab="write" + i.fas.fa-edit> + ' Scratchpad + + a href="#" data-click-tab="preview" + i.fa.fa-eye> + ' Preview + + .block__tab.communication-edit__tab.selected data-tab="write" + = render PhilomenaWeb.TextileView, "_help.html", conn: @conn + = render PhilomenaWeb.TextileView, "_toolbar.html", conn: @conn + + .field + = textarea f, :scratchpad, class: "input input--wide input--text js-preview-input js-toolbar-input", placeholder: "God I sure could go for some dragon dick" + = error_tag f, :scratchpad + + .block__tab.communication-edit__tab.hidden data-tab="preview" + ' [Loading preview...] + + .block__content.communication-edit__actions + => submit "Update", class: "button" diff --git a/lib/philomena_web/templates/profile/show.html.slime b/lib/philomena_web/templates/profile/show.html.slime index c140e279..dab90530 100644 --- a/lib/philomena_web/templates/profile/show.html.slime +++ b/lib/philomena_web/templates/profile/show.html.slime @@ -124,6 +124,11 @@ tr td == body td = pretty_time(mod_note.created_at) + = if can_index_user?(@conn) do + .block + a.block__header--single-item href=Routes.profile_scratchpad_path(@conn, :edit, @user) Moderation Scratchpad + .block__content.profile-about + == @scratchpad .column-layout__main = render PhilomenaWeb.ProfileView, "_statistics.html", user: @user, statistics: @statistics, conn: @conn diff --git a/lib/philomena_web/views/profile/scratchpad_view.ex b/lib/philomena_web/views/profile/scratchpad_view.ex new file mode 100644 index 00000000..edd7e749 --- /dev/null +++ b/lib/philomena_web/views/profile/scratchpad_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Profile.ScratchpadView do + use PhilomenaWeb, :view +end