allow users to edit their profile description

This commit is contained in:
byte[] 2019-12-05 19:36:45 -05:00
parent 463d86af5c
commit 2a338d86c7
8 changed files with 101 additions and 2 deletions

View file

@ -89,6 +89,12 @@ defmodule Philomena.Users do
|> Repo.update()
end
def update_description(%User{} = user, attrs) do
user
|> User.description_changeset(attrs)
|> Repo.update()
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking user changes.

View file

@ -52,6 +52,10 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# Users and anonymous users can...
#
# Edit their description and personal title
def can?(%User{id: id}, :edit_description, %User{id: id}), do: true
def can?(%User{id: id}, :edit_title, %User{id: id}), do: true
# View conversations they are involved in
def can?(%User{id: id}, :show, %Conversation{to_id: id}), do: true
def can?(%User{id: id}, :show, %Conversation{from_id: id}), do: true

View file

@ -185,6 +185,14 @@ defmodule Philomena.Users.User do
|> validate_search(:watched_images_exclude_str, user, true)
end
def description_changeset(user, attrs) do
user
|> cast(attrs, [:description, :personal_title])
|> validate_length(:description, max: 10_000, count: :bytes)
|> validate_length(:personal_title, max: 24, count: :bytes)
|> validate_format(:personal_title, ~r/\A((?!site|admin|moderator|assistant|developer|\p{C}).)*\z/iu)
end
def create_totp_secret_changeset(user) do
secret = :crypto.strong_rand_bytes(15) |> Base.encode32()
data = Philomena.Users.Encryptor.encrypt_model(secret)

View file

@ -0,0 +1,29 @@
defmodule PhilomenaWeb.Profile.DescriptionController do
use PhilomenaWeb, :controller
alias Philomena.Users.User
alias Philomena.Users
plug PhilomenaWeb.FilterBannedUsersPlug
plug PhilomenaWeb.CanaryMapPlug, edit: :edit_description, update: :edit_description
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", changeset: changeset, user: conn.assigns.user)
end
def update(conn, %{"user" => user_params}) do
user = conn.assigns.user
case Users.update_description(user, user_params) do
{:ok, _user} ->
conn
|> put_flash(:info, "Description successfully updated.")
|> redirect(to: Routes.profile_path(conn, :show, user))
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
end

View file

@ -117,6 +117,7 @@ defmodule PhilomenaWeb.Router do
resources "/items", Profile.Commission.ItemController, only: [:new, :create, :edit, :update, :delete]
resources "/reports", Profile.Commission.ReportController, only: [:new, :create]
end
resources "/description", Profile.DescriptionController, only: [:edit, :update], singleton: true
end
scope "/filters", Filter, as: :filter do

View file

@ -0,0 +1,38 @@
h1 Updating Profile Description
= form_for @changeset, Routes.profile_description_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
span.block__header__title Personal Title
.block__content
.field
= text_input f, :personal_title, class: "input input--short", placeholder: "Title"
= error_tag f, :personal_title
.block
.block__header.block__header--js-tabbed
a.selected href="#" data-click-tab="write"
i.fa.fa-pencil>
' About Me
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, :description, class: "input input--wide input--text js-preview-input js-toolbar-input", placeholder: "Description (up to 10000 characters)"
= error_tag f, :description
.block__tab.communication-edit__tab.hidden data-tab="preview"
' [Loading preview...]
.block__content.communication-edit__actions
=> submit "Update", class: "button"

View file

@ -6,7 +6,13 @@
h1.profile-top__name-header
= @user.name
| 's profile
p = render PhilomenaWeb.UserAttributionView, "_user_title.html", object: %{user: @user}, large: true
= render PhilomenaWeb.UserAttributionView, "_user_title.html", object: %{user: @user}, large: true
br
= if can?(@conn, :edit_description, @user) do
= link "Edit Personal Title", to: Routes.profile_description_path(@conn, :edit, @user)
br
span
' Member since
= pretty_time(@user.created_at)
@ -60,7 +66,11 @@
.block
.block__header
span.block__header__title About Me
= if can?(@conn, :edit_description, @user) do
a.block__header--single-item href=Routes.profile_description_path(@conn, :edit, @user) About Me
- else
span.block__header__title About Me
= render PhilomenaWeb.ProfileView, "_about_me.html", user: @user, about_me: @about_me, conn: @conn
.column-layout__main

View file

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