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
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Activity
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
def follow(%Actor{} = follower, %Actor{} = followed) do
|
|
|
|
case ActivityPub.follow(follower, followed) do
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow} ->
|
|
|
|
{:ok, activity, follow}
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
|
|
e ->
|
|
|
|
Logger.warn("Error while following actor: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow(%Actor{} = follower, %Actor{} = followed) do
|
|
|
|
case ActivityPub.unfollow(follower, followed) do
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow} ->
|
|
|
|
{:ok, activity, follow}
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
|
|
e ->
|
|
|
|
Logger.warn("Error while unfollowing actor: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept(%Actor{} = follower, %Actor{} = followed) do
|
2019-12-03 11:29:51 +01:00
|
|
|
Logger.debug("We're trying to accept a follow")
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
with %Follower{approved: false} = follow <-
|
2019-09-11 03:16:37 +02:00
|
|
|
Actors.is_following(follower, followed),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, %Activity{} = activity, %Follower{approved: true} = follow} <-
|
2019-07-30 16:40:59 +02:00
|
|
|
ActivityPub.accept(
|
2019-10-25 17:43:37 +02:00
|
|
|
:follow,
|
|
|
|
follow,
|
2019-12-03 11:29:51 +01:00
|
|
|
true
|
|
|
|
) do
|
|
|
|
{:ok, activity, follow}
|
|
|
|
else
|
|
|
|
%Follower{approved: true} ->
|
|
|
|
{:error, "Follow already accepted"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reject(%Actor{} = follower, %Actor{} = followed) do
|
|
|
|
Logger.debug("We're trying to reject a follow")
|
|
|
|
|
2020-01-30 20:27:25 +01:00
|
|
|
with {:follower, %Follower{} = follow} <-
|
|
|
|
{:follower, Actors.is_following(follower, followed)},
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, %Activity{} = activity, %Follower{} = follow} <-
|
|
|
|
ActivityPub.reject(
|
|
|
|
:follow,
|
|
|
|
follow,
|
|
|
|
true
|
2019-10-25 17:43:37 +02:00
|
|
|
) do
|
2019-12-03 11:29:51 +01:00
|
|
|
{:ok, activity, follow}
|
2019-07-30 16:40:59 +02:00
|
|
|
else
|
2020-01-30 20:27:25 +01:00
|
|
|
{:follower, nil} ->
|
|
|
|
{:error, "Follow not found"}
|
|
|
|
|
|
|
|
{:follower, %Follower{approved: true}} ->
|
2019-07-30 16:40:59 +02:00
|
|
|
{:error, "Follow already accepted"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|