CommonAPI: DRY up a bit.
This commit is contained in:
parent
aebec1bac9
commit
057438a657
@ -112,8 +112,22 @@ def unrepeat(id_or_ap_id, user) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec favorite(User.t(), binary()) :: {:ok, Activity.t()} | {:error, any()}
|
@spec favorite(User.t(), binary()) :: {:ok, Activity.t() | :already_liked} | {:error, any()}
|
||||||
def favorite(%User{} = user, id) do
|
def favorite(%User{} = user, id) do
|
||||||
|
case favorite_helper(user, id) do
|
||||||
|
{:ok, _} = res ->
|
||||||
|
res
|
||||||
|
|
||||||
|
{:error, :not_found} = res ->
|
||||||
|
res
|
||||||
|
|
||||||
|
{:error, e} ->
|
||||||
|
Logger.error("Could not favorite #{id}. Error: #{inspect(e, pretty: true)}")
|
||||||
|
{:error, dgettext("errors", "Could not favorite")}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def favorite_helper(user, id) do
|
||||||
with {_, %Activity{object: object}} <- {:find_object, Activity.get_by_id_with_object(id)},
|
with {_, %Activity{object: object}} <- {:find_object, Activity.get_by_id_with_object(id)},
|
||||||
{_, {:ok, like_object, meta}} <- {:build_object, Builder.like(user, object)},
|
{_, {:ok, like_object, meta}} <- {:build_object, Builder.like(user, object)},
|
||||||
{_, {:ok, %Activity{} = activity, _meta}} <-
|
{_, {:ok, %Activity{} = activity, _meta}} <-
|
||||||
@ -138,13 +152,11 @@ def favorite(%User{} = user, id) do
|
|||||||
if {:object, {"already liked by this actor", []}} in changeset.errors do
|
if {:object, {"already liked by this actor", []}} in changeset.errors do
|
||||||
{:ok, :already_liked}
|
{:ok, :already_liked}
|
||||||
else
|
else
|
||||||
Logger.error("Could not favorite #{id}. Error: #{inspect(e, pretty: true)}")
|
{:error, e}
|
||||||
{:error, dgettext("errors", "Could not favorite"), e}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
e ->
|
e ->
|
||||||
Logger.error("Could not favorite #{id}. Error: #{inspect(e, pretty: true)}")
|
{:error, e}
|
||||||
{:error, dgettext("errors", "Could not favorite"), e}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user