2019-07-30 14:40:59 +00:00
|
|
|
defmodule MobilizonWeb.API.Follows do
|
|
|
|
@moduledoc """
|
|
|
|
Common API for following, unfollowing, accepting and rejecting stuff.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-10-25 15:43:37 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub.Activity
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
def follow(%Actor{} = follower, %Actor{} = followed) do
|
|
|
|
case ActivityPub.follow(follower, followed) do
|
|
|
|
{:ok, activity, _} ->
|
|
|
|
{:ok, activity}
|
|
|
|
|
|
|
|
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
|
|
|
|
{:ok, activity, _} ->
|
|
|
|
{:ok, activity}
|
|
|
|
|
|
|
|
e ->
|
|
|
|
Logger.warn("Error while unfollowing actor: #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept(%Actor{} = follower, %Actor{} = followed) do
|
2019-10-25 15:43:37 +00:00
|
|
|
with %Follower{approved: false} = follow <-
|
2019-09-11 01:16:37 +00:00
|
|
|
Actors.is_following(follower, followed),
|
2019-10-25 15:43:37 +00:00
|
|
|
{:ok, %Activity{} = activity, %Follower{approved: true}} <-
|
2019-07-30 14:40:59 +00:00
|
|
|
ActivityPub.accept(
|
2019-10-25 15:43:37 +00:00
|
|
|
:follow,
|
|
|
|
follow,
|
|
|
|
%{approved: true}
|
|
|
|
) do
|
2019-07-30 14:40:59 +00:00
|
|
|
{:ok, activity}
|
|
|
|
else
|
|
|
|
%Follower{approved: true} ->
|
|
|
|
{:error, "Follow already accepted"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|