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