2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Post do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the posts-related GraphQL calls
|
|
|
|
"""
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
import Mobilizon.Users.Guards
|
2021-09-10 11:35:32 +02:00
|
|
|
alias Mobilizon.{Actors, Posts}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2021-07-23 11:34:28 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Permission, Utils}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Posts.Post
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@public_accessible_visibilities [:public, :unlisted]
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find posts for group.
|
|
|
|
|
|
|
|
Returns only if actor requesting is a member of the group
|
|
|
|
"""
|
|
|
|
def find_posts_for_group(
|
|
|
|
%Actor{id: group_id} = group,
|
|
|
|
%{page: page, limit: limit} = args,
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_user: %User{role: user_role},
|
|
|
|
current_actor: %Actor{id: actor_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:member, true} <-
|
2020-08-27 11:53:24 +02:00
|
|
|
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)},
|
2020-07-09 17:24:28 +02:00
|
|
|
%Page{} = page <- Posts.get_posts_for_group(group, page, limit) do
|
|
|
|
{:ok, page}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
|
|
|
find_posts_for_group(group, args, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_posts_for_group(
|
|
|
|
%Actor{} = group,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
with %Page{} = page <- Posts.get_public_posts_for_group(group, page, limit) do
|
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_posts_for_group(
|
|
|
|
_group,
|
|
|
|
_args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_post(
|
|
|
|
parent,
|
|
|
|
%{slug: slug},
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_user: %User{role: user_role},
|
|
|
|
current_actor: %Actor{} = current_profile
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:post, %Post{attributed_to: %Actor{}} = post} <-
|
2020-07-09 17:24:28 +02:00
|
|
|
{:post, Posts.get_post_by_slug_with_preloads(slug)},
|
2021-08-05 11:01:40 +02:00
|
|
|
{:member, true} <-
|
|
|
|
{:member,
|
|
|
|
Permission.can_access_group_object?(current_profile, post) or is_moderator(user_role)} do
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, post}
|
|
|
|
else
|
|
|
|
{:member, false} -> get_post(parent, %{slug: slug}, nil)
|
2020-10-01 15:07:15 +02:00
|
|
|
{:post, _} -> {:error, :post_not_found}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_post(
|
|
|
|
_parent,
|
|
|
|
%{slug: slug},
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
case {:post, Posts.get_post_by_slug_with_preloads(slug)} do
|
|
|
|
{:post, %Post{visibility: visibility, draft: false} = post}
|
|
|
|
when visibility in @public_accessible_visibilities ->
|
|
|
|
{:ok, post}
|
|
|
|
|
|
|
|
{:post, _} ->
|
2020-10-01 15:07:15 +02:00
|
|
|
{:error, :post_not_found}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_post(_parent, _args, _resolution) do
|
2020-10-01 15:07:15 +02:00
|
|
|
{:error, :post_not_found}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_post(
|
|
|
|
_parent,
|
|
|
|
%{attributed_to_id: group_id} = args,
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: actor_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
2020-10-01 15:07:15 +02:00
|
|
|
%Actor{} = group <- Actors.get_actor(group_id),
|
|
|
|
args <-
|
|
|
|
Map.update(args, :picture, nil, fn picture ->
|
|
|
|
process_picture(picture, group)
|
|
|
|
end),
|
2020-11-26 11:41:13 +01:00
|
|
|
args <- extract_pictures_from_post_body(args, actor_id),
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, _, %Post{} = post} <-
|
|
|
|
ActivityPub.create(
|
|
|
|
:post,
|
|
|
|
args
|
|
|
|
|> Map.put(:author_id, actor_id)
|
|
|
|
|> Map.put(:attributed_to_id, group_id),
|
|
|
|
true,
|
|
|
|
%{}
|
|
|
|
) do
|
|
|
|
{:ok, post}
|
|
|
|
else
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-10-01 15:07:15 +02:00
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
{:error, error}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_post(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create posts")}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_post(
|
|
|
|
_parent,
|
|
|
|
%{id: id} = args,
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: actor_id, url: actor_url}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:uuid, {:ok, _uuid}} <- {:uuid, Ecto.UUID.cast(id)},
|
2020-10-01 15:07:15 +02:00
|
|
|
{:post, %Post{attributed_to: %Actor{id: group_id} = group} = post} <-
|
2020-07-09 17:24:28 +02:00
|
|
|
{:post, Posts.get_post_with_preloads(id)},
|
2020-10-01 15:07:15 +02:00
|
|
|
args <-
|
|
|
|
Map.update(args, :picture, nil, fn picture ->
|
|
|
|
process_picture(picture, group)
|
|
|
|
end),
|
2020-11-26 11:41:13 +01:00
|
|
|
args <- extract_pictures_from_post_body(args, actor_id),
|
2020-07-09 17:24:28 +02:00
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %Post{} = post} <-
|
2020-10-19 19:21:39 +02:00
|
|
|
ActivityPub.update(post, args, true, %{"actor" => actor_url}) do
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, post}
|
|
|
|
else
|
|
|
|
{:uuid, :error} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Post ID is not a valid ID")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
{:post, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Post doesn't exist")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_post(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to update posts")}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_post(
|
|
|
|
_parent,
|
|
|
|
%{id: post_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: actor_id} = actor
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
|
|
|
with {:uuid, {:ok, _uuid}} <- {:uuid, Ecto.UUID.cast(post_id)},
|
|
|
|
{:post, %Post{attributed_to: %Actor{id: group_id}} = post} <-
|
|
|
|
{:post, Posts.get_post_with_preloads(post_id)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
|
|
|
{:ok, _, %Post{} = post} <-
|
|
|
|
ActivityPub.delete(post, actor) do
|
|
|
|
{:ok, post}
|
|
|
|
else
|
|
|
|
{:uuid, :error} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Post ID is not a valid ID")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
{:post, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Post doesn't exist")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
{:member, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not member of group")}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_post(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to delete posts")}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
2020-10-01 15:07:15 +02:00
|
|
|
|
|
|
|
defp process_picture(nil, _), do: nil
|
2020-11-26 11:41:13 +01:00
|
|
|
defp process_picture(%{media_id: _picture_id} = args, _), do: args
|
2020-10-01 15:07:15 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
defp process_picture(%{media: media}, %Actor{id: actor_id}) do
|
2021-06-10 09:36:25 +02:00
|
|
|
with uploaded when is_map(uploaded) <-
|
|
|
|
media
|
|
|
|
|> Map.get(:file)
|
|
|
|
|> Utils.make_media_data(description: Map.get(media, :name)) do
|
|
|
|
%{
|
|
|
|
file: Map.take(uploaded, [:url, :name, :content_type, :size]),
|
|
|
|
metadata: Map.take(uploaded, [:width, :height, :blurhash]),
|
|
|
|
actor_id: actor_id
|
|
|
|
}
|
|
|
|
end
|
2020-10-01 15:07:15 +02:00
|
|
|
end
|
2020-11-26 11:41:13 +01:00
|
|
|
|
|
|
|
@spec extract_pictures_from_post_body(map(), String.t()) :: map()
|
|
|
|
defp extract_pictures_from_post_body(%{body: body} = args, actor_id) do
|
|
|
|
pictures = Mobilizon.GraphQL.API.Utils.extract_pictures_from_body(body, actor_id)
|
|
|
|
Map.put(args, :media, pictures)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_pictures_from_post_body(args, _actor_id), do: args
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|