2020-01-26 21:11:16 +01:00
|
|
|
defmodule Mobilizon.GraphQL.API.Follows do
|
2019-07-30 16:40:59 +02:00
|
|
|
@moduledoc """
|
|
|
|
Common API for following, unfollowing, accepting and rejecting stuff.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@doc """
|
|
|
|
Make an actor (`follower`) follow another (`followed`).
|
|
|
|
"""
|
|
|
|
@spec follow(follower :: Actor.t(), followed :: Actor.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, Activity.t(), Mobilizon.Actors.Follower.t()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, String.t()}
|
2019-07-30 16:40:59 +02:00
|
|
|
def follow(%Actor{} = follower, %Actor{} = followed) do
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Follow.follow(follower, followed)
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@doc """
|
|
|
|
Make an actor (`follower`) unfollow another (`followed`).
|
|
|
|
"""
|
|
|
|
@spec unfollow(follower :: Actor.t(), followed :: Actor.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, Activity.t(), Mobilizon.Actors.Follower.t()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, String.t()}
|
2019-07-30 16:40:59 +02:00
|
|
|
def unfollow(%Actor{} = follower, %Actor{} = followed) do
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Follow.unfollow(follower, followed)
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@doc """
|
|
|
|
Make an actor (`followed`) accept the follow from another (`follower`).
|
|
|
|
"""
|
|
|
|
@spec accept(follower :: Actor.t(), followed :: Actor.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, Activity.t(), Mobilizon.Actors.Follower.t()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, String.t()}
|
|
|
|
def accept(%Actor{url: follower_url} = follower, %Actor{url: followed_url} = followed) do
|
|
|
|
Logger.debug(
|
|
|
|
"We're trying to accept a follow: #{followed_url} is accepting #{follower_url} follow request."
|
|
|
|
)
|
|
|
|
|
|
|
|
case Actors.is_following(follower, followed) do
|
|
|
|
%Follower{approved: false} = follow ->
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Accept.accept(
|
2021-09-24 16:46:42 +02:00
|
|
|
:follow,
|
|
|
|
follow,
|
|
|
|
true
|
|
|
|
)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
%Follower{approved: true} ->
|
|
|
|
{:error, "Follow already accepted"}
|
2021-09-24 16:46:42 +02:00
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, "Can't accept follow: #{follower_url} is not following #{followed_url}."}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@doc """
|
|
|
|
Make an actor (`followed`) reject the follow from another (`follower`).
|
|
|
|
"""
|
|
|
|
@spec reject(follower :: Actor.t(), followed :: Actor.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, Activity.t(), Mobilizon.Actors.Follower.t()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, String.t()}
|
|
|
|
def reject(%Actor{url: follower_url} = follower, %Actor{url: followed_url} = followed) do
|
|
|
|
Logger.debug(
|
|
|
|
"We're trying to reject a follow: #{followed_url} is rejecting #{follower_url} follow request."
|
|
|
|
)
|
2020-01-30 20:27:25 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
case Actors.is_following(follower, followed) do
|
2021-11-29 10:28:42 +01:00
|
|
|
%Follower{approved: false} ->
|
|
|
|
{:error, "Follow already rejected"}
|
2021-09-24 16:46:42 +02:00
|
|
|
|
|
|
|
%Follower{} = follow ->
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Reject.reject(
|
2021-09-24 16:46:42 +02:00
|
|
|
:follow,
|
|
|
|
follow,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, "Follow not found"}
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|