Track usage of media files and add a job to clean them
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -1,34 +1,59 @@
|
||||
defmodule Mobilizon.GraphQL.API.Comments do
|
||||
@moduledoc """
|
||||
API for Comments.
|
||||
API for discussions and comments.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.Activity
|
||||
alias Mobilizon.GraphQL.API.Utils
|
||||
|
||||
@doc """
|
||||
Create a comment
|
||||
|
||||
Creates a comment from an actor
|
||||
"""
|
||||
@spec create_comment(map) :: {:ok, Activity.t(), Comment.t()} | any
|
||||
def create_comment(args) do
|
||||
args = extract_pictures_from_comment_body(args)
|
||||
ActivityPub.create(:comment, args, true)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a comment
|
||||
"""
|
||||
@spec update_comment(Comment.t(), map()) :: {:ok, Activity.t(), Comment.t()} | any
|
||||
def update_comment(%Comment{} = comment, args) do
|
||||
args = extract_pictures_from_comment_body(args)
|
||||
ActivityPub.update(comment, args, true)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a comment
|
||||
|
||||
Deletes a comment from an actor
|
||||
"""
|
||||
@spec delete_comment(Comment.t(), Actor.t()) :: {:ok, Activity.t(), Comment.t()} | any
|
||||
def delete_comment(%Comment{} = comment, %Actor{} = actor) do
|
||||
ActivityPub.delete(comment, actor, true)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a discussion (or reply to a discussion)
|
||||
"""
|
||||
@spec create_discussion(map()) :: map()
|
||||
def create_discussion(args) do
|
||||
args = extract_pictures_from_comment_body(args)
|
||||
|
||||
ActivityPub.create(
|
||||
:discussion,
|
||||
args,
|
||||
true
|
||||
)
|
||||
end
|
||||
|
||||
@spec extract_pictures_from_comment_body(map()) :: map()
|
||||
defp extract_pictures_from_comment_body(%{text: text, actor_id: actor_id} = args) do
|
||||
pictures = Utils.extract_pictures_from_body(text, actor_id)
|
||||
Map.put(args, :media, pictures)
|
||||
end
|
||||
|
||||
defp extract_pictures_from_comment_body(args), do: args
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ defmodule Mobilizon.GraphQL.API.Events do
|
||||
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.{Activity, Utils}
|
||||
alias Mobilizon.GraphQL.API.Utils, as: APIUtils
|
||||
|
||||
@doc """
|
||||
Create an event
|
||||
@@ -15,6 +16,7 @@ defmodule Mobilizon.GraphQL.API.Events do
|
||||
@spec create_event(map) :: {:ok, Activity.t(), Event.t()} | any
|
||||
def create_event(args) do
|
||||
with organizer_actor <- Map.get(args, :organizer_actor),
|
||||
args <- extract_pictures_from_event_body(args, organizer_actor),
|
||||
args <-
|
||||
Map.update(args, :picture, nil, fn picture ->
|
||||
process_picture(picture, organizer_actor)
|
||||
@@ -30,6 +32,7 @@ defmodule Mobilizon.GraphQL.API.Events do
|
||||
@spec update_event(map, Event.t()) :: {:ok, Activity.t(), Event.t()} | any
|
||||
def update_event(args, %Event{} = event) do
|
||||
with organizer_actor <- Map.get(args, :organizer_actor),
|
||||
args <- extract_pictures_from_event_body(args, organizer_actor),
|
||||
args <-
|
||||
Map.update(args, :picture, nil, fn picture ->
|
||||
process_picture(picture, organizer_actor)
|
||||
@@ -40,23 +43,32 @@ defmodule Mobilizon.GraphQL.API.Events do
|
||||
|
||||
@doc """
|
||||
Trigger the deletion of an event
|
||||
|
||||
If the event is deleted by
|
||||
"""
|
||||
def delete_event(%Event{} = event, %Actor{} = actor, federate \\ true) do
|
||||
ActivityPub.delete(event, actor, federate)
|
||||
end
|
||||
|
||||
defp process_picture(nil, _), do: nil
|
||||
defp process_picture(%{picture_id: _picture_id} = args, _), do: args
|
||||
defp process_picture(%{media_id: _picture_id} = args, _), do: args
|
||||
|
||||
defp process_picture(%{picture: picture}, %Actor{id: actor_id}) do
|
||||
defp process_picture(%{media: media}, %Actor{id: actor_id}) do
|
||||
%{
|
||||
file:
|
||||
picture
|
||||
media
|
||||
|> Map.get(:file)
|
||||
|> Utils.make_picture_data(description: Map.get(picture, :name)),
|
||||
|> Utils.make_media_data(description: Map.get(media, :name)),
|
||||
actor_id: actor_id
|
||||
}
|
||||
end
|
||||
|
||||
@spec extract_pictures_from_event_body(map(), Actor.t()) :: map()
|
||||
defp extract_pictures_from_event_body(
|
||||
%{description: description} = args,
|
||||
%Actor{id: organizer_actor_id}
|
||||
) do
|
||||
pictures = APIUtils.extract_pictures_from_body(description, organizer_actor_id)
|
||||
Map.put(args, :media, pictures)
|
||||
end
|
||||
|
||||
defp extract_pictures_from_event_body(args, _), do: args
|
||||
end
|
||||
|
||||
@@ -3,7 +3,8 @@ defmodule Mobilizon.GraphQL.API.Utils do
|
||||
Utils for API.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.{Config, Medias}
|
||||
alias Mobilizon.Medias.Media
|
||||
alias Mobilizon.Service.Formatter
|
||||
|
||||
@doc """
|
||||
@@ -40,4 +41,41 @@ defmodule Mobilizon.GraphQL.API.Utils do
|
||||
{:error, "Comment must be up to #{max_size} characters"}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Use the data-media-id attributes to extract media from body text
|
||||
"""
|
||||
@spec extract_pictures_from_body(String.t(), integer() | String.t()) :: list(Media.t())
|
||||
def extract_pictures_from_body(body, actor_id) do
|
||||
body
|
||||
|> do_extract_pictures_from_body()
|
||||
|> Enum.map(&fetch_picture(&1, actor_id))
|
||||
|> Enum.filter(& &1)
|
||||
end
|
||||
|
||||
@spec do_extract_pictures_from_body(String.t()) :: list(String.t())
|
||||
defp do_extract_pictures_from_body(body) when is_nil(body) or body == "", do: []
|
||||
|
||||
defp do_extract_pictures_from_body(body) do
|
||||
{:ok, document} = Floki.parse_document(body)
|
||||
|
||||
document
|
||||
|> Floki.attribute("img", "data-media-id")
|
||||
end
|
||||
|
||||
@spec fetch_picture(String.t() | integer(), String.t() | integer()) :: Media.t() | nil
|
||||
defp fetch_picture(id, actor_id) do
|
||||
with %Media{actor_id: media_actor_id} = media <- Medias.get_media(id),
|
||||
{:owns_media, true} <-
|
||||
{:owns_media, check_actor_owns_media?(actor_id, media_actor_id)} do
|
||||
media
|
||||
else
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@spec check_actor_owns_media?(integer() | String.t(), integer() | String.t()) :: boolean()
|
||||
defp check_actor_owns_media?(actor_id, media_actor_id) do
|
||||
actor_id == media_actor_id || Mobilizon.Actors.is_member?(media_actor_id, actor_id)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user