Add support for GraphQL handling of group follows
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -6,7 +6,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
|
||||
import Mobilizon.Users.Guards
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.{Actors, Events}
|
||||
alias Mobilizon.Actors.{Actor, Member}
|
||||
alias Mobilizon.Actors.{Actor, Follower, Member}
|
||||
alias Mobilizon.Federation.ActivityPub.Actions
|
||||
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
||||
alias Mobilizon.GraphQL.API
|
||||
@@ -320,6 +320,78 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
|
||||
{:error, dgettext("errors", "You need to be logged-in to leave a group")}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Follow a group
|
||||
"""
|
||||
@spec follow_group(any(), map(), Absinthe.Resolution.t()) ::
|
||||
{:ok, Follower.t()} | {:error, String.t()}
|
||||
def follow_group(_parent, %{group_id: group_id, notify: _notify}, %{
|
||||
context: %{current_actor: %Actor{} = actor}
|
||||
}) do
|
||||
case Actors.get_actor(group_id) do
|
||||
%Actor{type: :Group} = group ->
|
||||
with {:ok, _activity, %Follower{} = follower} <- Actions.Follow.follow(actor, group) do
|
||||
{:ok, follower}
|
||||
end
|
||||
|
||||
nil ->
|
||||
{:error, dgettext("errors", "Group not found")}
|
||||
end
|
||||
end
|
||||
|
||||
def follow_group(_parent, _args, _resolution) do
|
||||
{:error, dgettext("errors", "You need to be logged-in to follow a group")}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Update a group follow
|
||||
"""
|
||||
@spec update_group_follow(any(), map(), Absinthe.Resolution.t()) ::
|
||||
{:ok, Member.t()} | {:error, String.t()}
|
||||
def update_group_follow(_parent, %{follow_id: follow_id, notify: notify}, %{
|
||||
context: %{current_actor: %Actor{} = actor}
|
||||
}) do
|
||||
case Actors.get_follower(follow_id) do
|
||||
%Follower{} = follower ->
|
||||
if follower.actor_id == actor.id do
|
||||
# Update notify
|
||||
Actors.update_follower(follower, %{notify: notify})
|
||||
else
|
||||
{:error, dgettext("errors", "Follow does not match your account")}
|
||||
end
|
||||
|
||||
nil ->
|
||||
{:error, dgettext("errors", "Follow not found")}
|
||||
end
|
||||
end
|
||||
|
||||
def update_group_follow(_parent, _args, _resolution) do
|
||||
{:error, dgettext("errors", "You need to be logged-in to update a group follow")}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unfollow a group
|
||||
"""
|
||||
@spec unfollow_group(any(), map(), Absinthe.Resolution.t()) ::
|
||||
{:ok, Follower.t()} | {:error, String.t()}
|
||||
def unfollow_group(_parent, %{group_id: group_id}, %{
|
||||
context: %{current_actor: %Actor{} = actor}
|
||||
}) do
|
||||
case Actors.get_actor(group_id) do
|
||||
%Actor{type: :Group} = group ->
|
||||
with {:ok, _activity, %Follower{} = follower} <- Actions.Follow.unfollow(actor, group) do
|
||||
{:ok, follower}
|
||||
end
|
||||
|
||||
nil ->
|
||||
{:error, dgettext("errors", "Group not found")}
|
||||
end
|
||||
end
|
||||
|
||||
def unfollow_group(_parent, _args, _resolution) do
|
||||
{:error, dgettext("errors", "You need to be logged-in to unfollow a group")}
|
||||
end
|
||||
|
||||
@spec find_events_for_group(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
||||
{:ok, Page.t(Event.t())}
|
||||
def find_events_for_group(
|
||||
|
||||
@@ -17,6 +17,11 @@ defmodule Mobilizon.GraphQL.Schema.Actors.FollowerType do
|
||||
description: "Whether the follow has been approved by the target actor"
|
||||
)
|
||||
|
||||
field(:notify, :boolean,
|
||||
description:
|
||||
"Whether the follower will be notified by the target actor's activity or not (applicable for profile/group follows)"
|
||||
)
|
||||
|
||||
field(:inserted_at, :datetime, description: "When the follow was created")
|
||||
field(:updated_at, :datetime, description: "When the follow was updated")
|
||||
end
|
||||
|
||||
@@ -205,6 +205,12 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
|
||||
value(:private, description: "Visible only to people with the link - or invited")
|
||||
end
|
||||
|
||||
object :group_follow do
|
||||
field(:group, :group, description: "The group followed")
|
||||
field(:profile, :group, description: "The group followed")
|
||||
field(:notify, :boolean, description: "Whether to notify profile from group activity")
|
||||
end
|
||||
|
||||
object :group_queries do
|
||||
@desc "Get all groups"
|
||||
field :groups, :paginated_group_list do
|
||||
@@ -310,5 +316,36 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
|
||||
|
||||
resolve(&Group.delete_group/3)
|
||||
end
|
||||
|
||||
@desc "Follow a group"
|
||||
field :follow_group, :follower do
|
||||
arg(:group_id, non_null(:id), description: "The group ID")
|
||||
|
||||
arg(:notify, :boolean,
|
||||
description: "Whether to notify profile from group activity",
|
||||
default_value: true
|
||||
)
|
||||
|
||||
resolve(&Group.follow_group/3)
|
||||
end
|
||||
|
||||
@desc "Update a group follow"
|
||||
field :update_group_follow, :follower do
|
||||
arg(:follow_id, non_null(:id), description: "The follow ID")
|
||||
|
||||
arg(:notify, :boolean,
|
||||
description: "Whether to notify profile from group activity",
|
||||
default_value: true
|
||||
)
|
||||
|
||||
resolve(&Group.update_group_follow/3)
|
||||
end
|
||||
|
||||
@desc "Unfollow a group"
|
||||
field :unfollow_group, :follower do
|
||||
arg(:group_id, non_null(:id), description: "The group ID")
|
||||
|
||||
resolve(&Group.unfollow_group/3)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,12 +17,14 @@ defmodule Mobilizon.Actors.Follower do
|
||||
url: String.t(),
|
||||
target_actor: Actor.t(),
|
||||
actor: Actor.t(),
|
||||
notify: boolean(),
|
||||
inserted_at: DateTime.t(),
|
||||
updated_at: DateTime.t()
|
||||
}
|
||||
|
||||
@required_attrs [:url, :approved, :target_actor_id, :actor_id]
|
||||
@attrs @required_attrs
|
||||
@optional_attrs [:notify]
|
||||
@attrs @required_attrs ++ @optional_attrs
|
||||
|
||||
@timestamps_opts [type: :utc_datetime]
|
||||
|
||||
@@ -30,6 +32,7 @@ defmodule Mobilizon.Actors.Follower do
|
||||
schema "followers" do
|
||||
field(:approved, :boolean, default: false)
|
||||
field(:url, :string)
|
||||
field(:notify, :boolean, default: true)
|
||||
|
||||
timestamps()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user