X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Factivity_pub%2Futils.ex;h=6a36a6c10673a938bb7fa44ad0e07e975a6a29fd;hb=d16877251a42545bfbef383b1a023aa029730c44;hp=ac3a57e035414e3b3502bd2cfa118db52e2409bf;hpb=bc31bee7c4d5fc96d4c1923b61db7489b78f4558;p=akkoma diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index ac3a57e03..6a36a6c10 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -50,10 +50,13 @@ defmodule Pleroma.Web.ActivityPub.Utils do changeset = Object.context_mapping(context) case Repo.insert(changeset) do - {:ok, object} -> object + {:ok, object} -> + object + # This should be solved by an upsert, but it seems ecto # has problems accessing the constraint inside the jsonb. - {:error, _} -> Object.get_cached_by_ap_id(context) + {:error, _} -> + Object.get_cached_by_ap_id(context) end end @@ -233,6 +236,28 @@ defmodule Pleroma.Web.ActivityPub.Utils do #### Announce-related helpers + @doc """ + Retruns an existing announce activity if the notice has already been announced + """ + def get_existing_announce(actor, %{data: %{"id" => id}}) do + query = + from( + activity in Activity, + where: fragment("(?)->>'actor' = ?", activity.data, ^actor), + # this is to use the index + where: + fragment( + "coalesce((?)->'object'->>'id', (?)->>'object') = ?", + activity.data, + activity.data, + ^id + ), + where: fragment("(?)->>'type' = 'Announce'", activity.data) + ) + + Repo.one(query) + end + @doc """ Make announce activity data for the given actor and object """ @@ -259,6 +284,12 @@ defmodule Pleroma.Web.ActivityPub.Utils do end end + def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do + with announcements <- (object.data["announcements"] || []) |> List.delete(actor) do + update_element_in_object("announcement", announcements, object) + end + end + #### Unfollow-related helpers def make_unfollow_data(follower, followed, follow_activity) do