Improve comment section

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-06-15 16:20:58 +02:00
parent d8bdc132e8
commit 47297127a2
19 changed files with 325 additions and 272 deletions

View File

@@ -10,7 +10,6 @@ defmodule Mobilizon.Federation.ActivityPub.Federator do
use GenServer
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier}

View File

@@ -73,7 +73,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
) do
with {actor_id, ""} <- Integer.parse(actor_id),
{:is_owned, %Actor{} = _organizer_actor} <- User.owns_actor(user, actor_id),
%CommentModel{} = comment <- Conversations.get_comment_with_preload(comment_id) do
%CommentModel{deleted_at: nil} = comment <-
Conversations.get_comment_with_preload(comment_id) do
cond do
{:comment_can_be_managed, true} == CommentModel.can_be_managed_by(comment, actor_id) ->
do_delete_comment(comment)
@@ -90,6 +91,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
{:error, "You cannot delete this comment"}
end
else
%CommentModel{deleted_at: deleted_at} when not is_nil(deleted_at) ->
{:error, "Comment is already deleted"}
{:is_owned, nil} ->
{:error, "Actor id is not owned by authenticated user"}
end

View File

@@ -6,7 +6,7 @@ defmodule Mobilizon.GraphQL.Schema.Conversations.CommentType do
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
alias Mobilizon.{Actors, Events}
alias Mobilizon.{Actors, Conversations}
alias Mobilizon.GraphQL.Resolvers.Comment
@desc "A comment"
@@ -21,13 +21,13 @@ defmodule Mobilizon.GraphQL.Schema.Conversations.CommentType do
field(:primaryLanguage, :string)
field(:replies, list_of(:comment)) do
resolve(dataloader(Events))
resolve(dataloader(Conversations))
end
field(:total_replies, :integer)
field(:in_reply_to_comment, :comment, resolve: dataloader(Events))
field(:in_reply_to_comment, :comment, resolve: dataloader(Conversations))
field(:event, :event, resolve: dataloader(Events))
field(:origin_comment, :comment, resolve: dataloader(Events))
field(:origin_comment, :comment, resolve: dataloader(Conversations))
field(:threadLanguages, non_null(list_of(:string)))
field(:actor, :person, resolve: dataloader(Actors))
field(:inserted_at, :datetime)

View File

@@ -3,7 +3,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do
alias Mobilizon.Actors.Actor
alias Mobilizon.Web.MediaProxy
def build_tags(%Actor{} = actor) do
def build_tags(%Actor{} = actor, _locale \\ "en") do
tags = [
Tag.tag(:meta, property: "og:title", content: Actor.display_name_and_username(actor)),
Tag.tag(:meta, property: "og:url", content: actor.url),

View File

@@ -2,7 +2,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Conversations.Comment do
alias Phoenix.HTML.Tag
alias Mobilizon.Conversations.Comment
def build_tags(%Comment{} = comment) do
def build_tags(%Comment{} = comment, _locale \\ "en") do
[
Tag.tag(:meta, property: "og:title", content: comment.actor.preferred_username),
Tag.tag(:meta, property: "og:url", content: comment.url),

View File

@@ -4,9 +4,10 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
alias Mobilizon.Events.Event
alias Mobilizon.Web.JsonLD.ObjectView
alias Mobilizon.Web.MediaProxy
import Mobilizon.Web.Gettext
def build_tags(%Event{} = event) do
event = Map.put(event, :description, process_description(event.description))
def build_tags(%Event{} = event, locale \\ "en") do
event = Map.put(event, :description, process_description(event.description, locale))
tags = [
Tag.content_tag(:title, event.title <> " - Mobilizon"),
@@ -39,7 +40,14 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
]
end
defp process_description(description) do
defp process_description(nil, locale), do: process_description("", locale)
defp process_description("", locale) do
Gettext.put_locale(locale)
gettext("The event organizer didn't add any description.")
end
defp process_description(description, _locale) do
description
|> HtmlSanitizeEx.strip_tags()
|> String.slice(0..200)

View File

@@ -3,5 +3,5 @@ defprotocol Mobilizon.Service.Metadata do
Build tags
"""
def build_tags(entity)
def build_tags(entity, locale \\ "en")
end

View File

@@ -1,7 +1,7 @@
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Tombstone do
alias Mobilizon.Tombstone
def build_tags(%Tombstone{}) do
def build_tags(%Tombstone{}, _locale \\ "en") do
[]
end
end

View File

@@ -6,9 +6,9 @@ defmodule Mobilizon.Web.ErrorView do
alias Mobilizon.Service.Metadata.Instance
alias Mobilizon.Web.PageView
def render("404.html", %{conn: conn}) do
def render("404.html", _assigns) do
tags = Instance.build_tags()
PageView.inject_tags(conn, tags)
PageView.inject_tags(tags)
end
def render("404.json", _assigns) do

View File

@@ -50,20 +50,19 @@ defmodule Mobilizon.Web.PageView do
def render(page, %{object: object, conn: conn} = _assigns)
when page in ["actor.html", "event.html", "comment.html"] do
tags = object |> Metadata.build_tags()
inject_tags(conn, tags)
locale = get_locale(conn)
tags = object |> Metadata.build_tags(locale)
inject_tags(tags, locale)
end
def render("index.html", %{conn: conn}) do
tags = Instance.build_tags()
inject_tags(conn, tags)
inject_tags(tags, get_locale(conn))
end
@spec inject_tags(Conn.t(), List.t()) :: {:safe, String.t()}
def inject_tags(conn, tags) do
@spec inject_tags(List.t(), String.t()) :: {:safe, String.t()}
def inject_tags(tags, locale \\ "en") do
with {:ok, index_content} <- File.read(index_file_path()) do
locale = get_locale(conn)
do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale)
end
end