1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Utils do
9 alias Pleroma.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Visibility
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.Endpoint
18 alias Pleroma.Web.Router.Helpers
23 require Pleroma.Constants
25 @supported_object_types ["Article", "Note", "Video", "Page", "Question", "Answer", "Audio"]
26 @strip_status_report_states ~w(closed resolved)
27 @supported_report_states ~w(open closed resolved)
28 @valid_visibilities ~w(public unlisted private direct)
30 # Some implementations send the actor URI as the actor field, others send the entire actor object,
31 # so figure out what the actor's URI is based on what we have.
32 def get_ap_id(%{"id" => id} = _), do: id
33 def get_ap_id(id), do: id
35 def normalize_params(params) do
36 Map.put(params, "actor", get_ap_id(params["actor"]))
39 @spec determine_explicit_mentions(map()) :: map()
40 def determine_explicit_mentions(%{"tag" => tag} = _) when is_list(tag) do
42 %{"type" => "Mention", "href" => href} -> [href]
47 def determine_explicit_mentions(%{"tag" => tag} = object) when is_map(tag) do
49 |> Map.put("tag", [tag])
50 |> determine_explicit_mentions()
53 def determine_explicit_mentions(_), do: []
55 @spec label_in_collection?(any(), any()) :: boolean()
56 defp label_in_collection?(ap_id, coll) when is_binary(coll), do: ap_id == coll
57 defp label_in_collection?(ap_id, coll) when is_list(coll), do: ap_id in coll
58 defp label_in_collection?(_, _), do: false
60 @spec label_in_message?(String.t(), map()) :: boolean()
61 def label_in_message?(label, params),
63 [params["to"], params["cc"], params["bto"], params["bcc"]]
64 |> Enum.any?(&label_in_collection?(label, &1))
66 @spec unaddressed_message?(map()) :: boolean()
67 def unaddressed_message?(params),
69 [params["to"], params["cc"], params["bto"], params["bcc"]]
70 |> Enum.all?(&is_nil(&1))
72 @spec recipient_in_message(User.t(), User.t(), map()) :: boolean()
73 def recipient_in_message(%User{ap_id: ap_id} = recipient, %User{} = actor, params),
75 label_in_message?(ap_id, params) || unaddressed_message?(params) ||
76 User.following?(recipient, actor)
78 defp extract_list(target) when is_binary(target), do: [target]
79 defp extract_list(lst) when is_list(lst), do: lst
80 defp extract_list(_), do: []
82 def maybe_splice_recipient(ap_id, params) do
84 !label_in_collection?(ap_id, params["to"]) &&
85 !label_in_collection?(ap_id, params["cc"])
88 cc_list = extract_list(params["cc"])
89 Map.put(params, "cc", [ap_id | cc_list])
95 def make_json_ld_header do
98 "https://www.w3.org/ns/activitystreams",
99 "#{Web.base_url()}/schemas/litepub-0.1.jsonld",
108 DateTime.utc_now() |> DateTime.to_iso8601()
111 def generate_activity_id do
112 generate_id("activities")
115 def generate_context_id do
116 generate_id("contexts")
119 def generate_object_id do
120 Helpers.o_status_url(Endpoint, :object, UUID.generate())
123 def generate_id(type) do
124 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
127 def get_notified_from_object(%{"type" => type} = object) when type in @supported_object_types do
128 fake_create_activity = %{
129 "to" => object["to"],
130 "cc" => object["cc"],
135 get_notified_from_object(fake_create_activity)
138 def get_notified_from_object(object) do
139 Notification.get_notified_from_activity(%Activity{data: object}, false)
142 def create_context(context) do
143 context = context || generate_id("contexts")
145 # Ecto has problems accessing the constraint inside the jsonb,
146 # so we explicitly check for the existed object before insert
147 object = Object.get_cached_by_ap_id(context)
149 with true <- is_nil(object),
150 changeset <- Object.context_mapping(context),
151 {:ok, inserted_object} <- Repo.insert(changeset) do
160 Enqueues an activity for federation if it's local
162 @spec maybe_federate(any()) :: :ok
163 def maybe_federate(%Activity{local: true} = activity) do
164 if Pleroma.Config.get!([:instance, :federating]) do
165 Pleroma.Web.Federator.publish(activity)
171 def maybe_federate(_), do: :ok
174 Adds an id and a published data if they aren't there,
175 also adds it to an included object
177 @spec lazy_put_activity_defaults(map(), boolean) :: map()
178 def lazy_put_activity_defaults(map, fake? \\ false)
180 def lazy_put_activity_defaults(map, true) do
182 |> Map.put_new("id", "pleroma:fakeid")
183 |> Map.put_new_lazy("published", &make_date/0)
184 |> Map.put_new("context", "pleroma:fakecontext")
185 |> Map.put_new("context_id", -1)
186 |> lazy_put_object_defaults(true)
189 def lazy_put_activity_defaults(map, _fake?) do
190 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
193 |> Map.put_new_lazy("id", &generate_activity_id/0)
194 |> Map.put_new_lazy("published", &make_date/0)
195 |> Map.put_new("context", context)
196 |> Map.put_new("context_id", context_id)
197 |> lazy_put_object_defaults(false)
200 # Adds an id and published date if they aren't there.
202 @spec lazy_put_object_defaults(map(), boolean()) :: map()
203 defp lazy_put_object_defaults(%{"object" => map} = activity, true)
207 |> Map.put_new("id", "pleroma:fake_object_id")
208 |> Map.put_new_lazy("published", &make_date/0)
209 |> Map.put_new("context", activity["context"])
210 |> Map.put_new("context_id", activity["context_id"])
211 |> Map.put_new("fake", true)
213 %{activity | "object" => object}
216 defp lazy_put_object_defaults(%{"object" => map} = activity, _)
220 |> Map.put_new_lazy("id", &generate_object_id/0)
221 |> Map.put_new_lazy("published", &make_date/0)
222 |> Map.put_new("context", activity["context"])
223 |> Map.put_new("context_id", activity["context_id"])
225 %{activity | "object" => object}
228 defp lazy_put_object_defaults(activity, _), do: activity
231 Inserts a full object if it is contained in an activity.
233 def insert_full_object(%{"object" => %{"type" => type} = object_data} = map)
234 when is_map(object_data) and type in @supported_object_types do
235 with {:ok, object} <- Object.create(object_data) do
236 map = Map.put(map, "object", object.data["id"])
242 def insert_full_object(map), do: {:ok, map, nil}
244 #### Like-related helpers
247 Returns an existing like if a user already liked an object
249 @spec get_existing_like(String.t(), map()) :: Activity.t() | nil
250 def get_existing_like(actor, %{data: %{"id" => id}}) do
252 |> Activity.Queries.by_actor()
253 |> Activity.Queries.by_object_id(id)
254 |> Activity.Queries.by_type("Like")
260 Returns like activities targeting an object
262 def get_object_likes(%{data: %{"id" => id}}) do
264 |> Activity.Queries.by_object_id()
265 |> Activity.Queries.by_type("Like")
269 @spec make_like_data(User.t(), map(), String.t()) :: map()
271 %User{ap_id: ap_id} = actor,
272 %{data: %{"actor" => object_actor_id, "id" => id}} = object,
275 object_actor = User.get_cached_by_ap_id(object_actor_id)
278 if Visibility.is_public?(object) do
279 [actor.follower_address, object.data["actor"]]
281 [object.data["actor"]]
285 (object.data["to"] ++ (object.data["cc"] || []))
286 |> List.delete(actor.ap_id)
287 |> List.delete(object_actor.follower_address)
295 "context" => object.data["context"]
297 |> maybe_put("id", activity_id)
300 def make_emoji_reaction_data(user, object, emoji, activity_id) do
301 make_like_data(user, object, activity_id)
302 |> Map.put("type", "EmojiReaction")
303 |> Map.put("content", emoji)
306 @spec update_element_in_object(String.t(), list(any), Object.t()) ::
307 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
308 def update_element_in_object(property, element, object) do
310 if is_map(element) do
323 %{"#{property}_count" => length, "#{property}s" => element}
327 |> Changeset.change(data: data)
328 |> Object.update_and_set_cache()
331 @spec add_emoji_reaction_to_object(Activity.t(), Object.t()) ::
332 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
334 def add_emoji_reaction_to_object(
335 %Activity{data: %{"content" => emoji, "actor" => actor}},
338 reactions = object.data["reactions"] || %{}
339 emoji_actors = reactions[emoji] || []
340 new_emoji_actors = [actor | emoji_actors] |> Enum.uniq()
341 new_reactions = Map.put(reactions, emoji, new_emoji_actors)
342 update_element_in_object("reaction", new_reactions, object)
345 def remove_emoji_reaction_from_object(
346 %Activity{data: %{"content" => emoji, "actor" => actor}},
349 reactions = object.data["reactions"] || %{}
350 emoji_actors = reactions[emoji] || []
351 new_emoji_actors = List.delete(emoji_actors, actor)
354 if new_emoji_actors == [] do
355 Map.delete(reactions, emoji)
357 Map.put(reactions, emoji, new_emoji_actors)
360 update_element_in_object("reaction", new_reactions, object)
363 @spec add_like_to_object(Activity.t(), Object.t()) ::
364 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
365 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
366 [actor | fetch_likes(object)]
368 |> update_likes_in_object(object)
371 @spec remove_like_from_object(Activity.t(), Object.t()) ::
372 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
373 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
376 |> List.delete(actor)
377 |> update_likes_in_object(object)
380 defp update_likes_in_object(likes, object) do
381 update_element_in_object("like", likes, object)
384 defp fetch_likes(object) do
385 if is_list(object.data["likes"]) do
392 #### Follow-related helpers
395 Updates a follow activity's state (for locked accounts).
397 @spec update_follow_state_for_all(Activity.t(), String.t()) :: {:ok, Activity} | {:error, any()}
398 def update_follow_state_for_all(
399 %Activity{data: %{"actor" => actor, "object" => object}} = activity,
403 |> Activity.Queries.by_type()
404 |> Activity.Queries.by_actor(actor)
405 |> Activity.Queries.by_object_id(object)
406 |> where(fragment("data->>'state' = 'pending'"))
407 |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
408 |> Repo.update_all([])
410 User.set_follow_state_cache(actor, object, state)
412 activity = Activity.get_by_id(activity.id)
417 def update_follow_state(
418 %Activity{data: %{"actor" => actor, "object" => object}} = activity,
421 new_data = Map.put(activity.data, "state", state)
422 changeset = Changeset.change(activity, data: new_data)
424 with {:ok, activity} <- Repo.update(changeset) do
425 User.set_follow_state_cache(actor, object, state)
431 Makes a follow activity data for the given follower and followed
433 def make_follow_data(
434 %User{ap_id: follower_id},
435 %User{ap_id: followed_id} = _followed,
440 "actor" => follower_id,
441 "to" => [followed_id],
442 "cc" => [Pleroma.Constants.as_public()],
443 "object" => followed_id,
446 |> maybe_put("id", activity_id)
449 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
451 |> Activity.Queries.by_type()
452 |> where(actor: ^follower_id)
453 # this is to use the index
454 |> Activity.Queries.by_object_id(followed_id)
455 |> order_by([activity], fragment("? desc nulls last", activity.id))
460 def get_latest_reaction(internal_activity_id, %{ap_id: ap_id}, emoji) do
461 %{data: %{"object" => object_ap_id}} = Activity.get_by_id(internal_activity_id)
464 |> Activity.Queries.by_type()
465 |> where(actor: ^ap_id)
466 |> where([activity], fragment("?->>'content' = ?", activity.data, ^emoji))
467 |> Activity.Queries.by_object_id(object_ap_id)
468 |> order_by([activity], fragment("? desc nulls last", activity.id))
473 #### Announce-related helpers
476 Retruns an existing announce activity if the notice has already been announced
478 @spec get_existing_announce(String.t(), map()) :: Activity.t() | nil
479 def get_existing_announce(actor, %{data: %{"id" => ap_id}}) do
481 |> Activity.Queries.by_type()
482 |> where(actor: ^actor)
483 # this is to use the index
484 |> Activity.Queries.by_object_id(ap_id)
489 Make announce activity data for the given actor and object
491 # for relayed messages, we only want to send to subscribers
492 def make_announce_data(
493 %User{ap_id: ap_id} = user,
494 %Object{data: %{"id" => id}} = object,
499 "type" => "Announce",
502 "to" => [user.follower_address],
504 "context" => object.data["context"]
506 |> maybe_put("id", activity_id)
509 def make_announce_data(
510 %User{ap_id: ap_id} = user,
511 %Object{data: %{"id" => id}} = object,
516 "type" => "Announce",
519 "to" => [user.follower_address, object.data["actor"]],
520 "cc" => [Pleroma.Constants.as_public()],
521 "context" => object.data["context"]
523 |> maybe_put("id", activity_id)
527 Make unannounce activity data for the given actor and object
529 def make_unannounce_data(
530 %User{ap_id: ap_id} = user,
531 %Activity{data: %{"context" => context, "object" => object}} = activity,
534 object = Object.normalize(object)
539 "object" => activity.data,
540 "to" => [user.follower_address, object.data["actor"]],
541 "cc" => [Pleroma.Constants.as_public()],
544 |> maybe_put("id", activity_id)
547 def make_unlike_data(
548 %User{ap_id: ap_id} = user,
549 %Activity{data: %{"context" => context, "object" => object}} = activity,
552 object = Object.normalize(object)
557 "object" => activity.data,
558 "to" => [user.follower_address, object.data["actor"]],
559 "cc" => [Pleroma.Constants.as_public()],
562 |> maybe_put("id", activity_id)
566 %User{ap_id: actor, follower_address: follower_address},
568 data: %{"id" => undone_activity_id, "context" => context},
569 actor: undone_activity_actor
576 "object" => undone_activity_id,
577 "to" => [follower_address, undone_activity_actor],
578 "cc" => [Pleroma.Constants.as_public()],
581 |> maybe_put("id", activity_id)
584 @spec add_announce_to_object(Activity.t(), Object.t()) ::
585 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
586 def add_announce_to_object(
587 %Activity{data: %{"actor" => actor}},
590 unless actor |> User.get_cached_by_ap_id() |> User.invisible?() do
591 announcements = take_announcements(object)
593 with announcements <- Enum.uniq([actor | announcements]) do
594 update_element_in_object("announcement", announcements, object)
601 def add_announce_to_object(_, object), do: {:ok, object}
603 @spec remove_announce_from_object(Activity.t(), Object.t()) ::
604 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
605 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
606 with announcements <- List.delete(take_announcements(object), actor) do
607 update_element_in_object("announcement", announcements, object)
611 defp take_announcements(%{data: %{"announcements" => announcements}} = _)
612 when is_list(announcements),
615 defp take_announcements(_), do: []
617 #### Unfollow-related helpers
619 def make_unfollow_data(follower, followed, follow_activity, activity_id) do
622 "actor" => follower.ap_id,
623 "to" => [followed.ap_id],
624 "object" => follow_activity.data
626 |> maybe_put("id", activity_id)
629 #### Block-related helpers
630 @spec fetch_latest_block(User.t(), User.t()) :: Activity.t() | nil
631 def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
633 |> Activity.Queries.by_type()
634 |> where(actor: ^blocker_id)
635 # this is to use the index
636 |> Activity.Queries.by_object_id(blocked_id)
637 |> order_by([activity], fragment("? desc nulls last", activity.id))
642 def make_block_data(blocker, blocked, activity_id) do
645 "actor" => blocker.ap_id,
646 "to" => [blocked.ap_id],
647 "object" => blocked.ap_id
649 |> maybe_put("id", activity_id)
652 def make_unblock_data(blocker, blocked, block_activity, activity_id) do
655 "actor" => blocker.ap_id,
656 "to" => [blocked.ap_id],
657 "object" => block_activity.data
659 |> maybe_put("id", activity_id)
662 #### Create-related helpers
664 def make_create_data(params, additional) do
665 published = params.published || make_date()
669 "to" => params.to |> Enum.uniq(),
670 "actor" => params.actor.ap_id,
671 "object" => params.object,
672 "published" => published,
673 "context" => params.context
675 |> Map.merge(additional)
678 #### Listen-related helpers
679 def make_listen_data(params, additional) do
680 published = params.published || make_date()
684 "to" => params.to |> Enum.uniq(),
685 "actor" => params.actor.ap_id,
686 "object" => params.object,
687 "published" => published,
688 "context" => params.context
690 |> Map.merge(additional)
693 #### Flag-related helpers
694 @spec make_flag_data(map(), map()) :: map()
695 def make_flag_data(%{actor: actor, context: context, content: content} = params, additional) do
698 "actor" => actor.ap_id,
699 "content" => content,
700 "object" => build_flag_object(params),
701 "context" => context,
704 |> Map.merge(additional)
707 def make_flag_data(_, _), do: %{}
709 defp build_flag_object(%{account: account, statuses: statuses} = _) do
710 [account.ap_id] ++ build_flag_object(%{statuses: statuses})
713 defp build_flag_object(%{statuses: statuses}) do
714 Enum.map(statuses || [], &build_flag_object/1)
717 defp build_flag_object(act) when is_map(act) or is_binary(act) do
720 %Activity{} = act -> act.data["id"]
721 act when is_map(act) -> act["id"]
722 act when is_binary(act) -> act
725 activity = Activity.get_by_ap_id_with_object(id)
726 actor = User.get_by_ap_id(activity.object.data["actor"])
730 "id" => activity.data["id"],
731 "content" => activity.object.data["content"],
732 "published" => activity.object.data["published"],
733 "actor" => AccountView.render("show.json", %{user: actor})
737 defp build_flag_object(_), do: []
740 Fetches the OrderedCollection/OrderedCollectionPage from `from`, limiting the amount of pages fetched after
741 the first one to `pages_left` pages.
742 If the amount of pages is higher than the collection has, it returns whatever was there.
744 def fetch_ordered_collection(from, pages_left, acc \\ []) do
745 with {:ok, response} <- Tesla.get(from),
746 {:ok, collection} <- Jason.decode(response.body) do
747 case collection["type"] do
748 "OrderedCollection" ->
749 # If we've encountered the OrderedCollection and not the page,
750 # just call the same function on the page address
751 fetch_ordered_collection(collection["first"], pages_left)
753 "OrderedCollectionPage" ->
755 # There are still more pages
756 if Map.has_key?(collection, "next") do
757 # There are still more pages, go deeper saving what we have into the accumulator
758 fetch_ordered_collection(
761 acc ++ collection["orderedItems"]
764 # No more pages left, just return whatever we already have
765 acc ++ collection["orderedItems"]
768 # Got the amount of pages needed, add them all to the accumulator
769 acc ++ collection["orderedItems"]
773 {:error, "Not an OrderedCollection or OrderedCollectionPage"}
778 #### Report-related helpers
779 def get_reports(params, page, page_size) do
782 |> Map.put("type", "Flag")
783 |> Map.put("skip_preload", true)
784 |> Map.put("total", true)
785 |> Map.put("limit", page_size)
786 |> Map.put("offset", (page - 1) * page_size)
788 ActivityPub.fetch_activities([], params, :offset)
791 @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{
792 required(:groups) => [
794 required(:date) => String.t(),
795 required(:account) => %{},
796 required(:status) => %{},
797 required(:actors) => [%User{}],
798 required(:reports) => [%Activity{}]
801 required(:total) => integer
803 def get_reports_grouped_by_status(groups) do
806 |> Enum.map(fn entry ->
808 case Jason.decode(entry.activity) do
809 {:ok, activity} -> activity
810 _ -> build_flag_object(entry.activity)
813 parse_report_group(activity)
817 groups: parsed_groups
821 def parse_report_group(activity) do
822 reports = get_reports_by_status_id(activity["id"])
823 max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"]))
824 actors = Enum.map(reports, & &1.user_actor)
825 {deleted, status} = get_status_data(activity)
828 date: max_date.data["published"],
829 account: activity["actor"],
831 status_deleted: deleted,
832 actors: Enum.uniq(actors),
837 defp get_status_data(activity) do
838 case Activity.get_by_ap_id(activity["id"]) do
846 content: activity["content"],
847 published: activity["published"]
852 def get_reports_by_status_id(ap_id) do
854 where: fragment("(?)->>'type' = 'Flag'", a.data),
855 where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}]),
856 or_where: fragment("(?)->'object' @> ?", a.data, ^[ap_id])
858 |> Activity.with_preloaded_user_actor()
862 @spec get_reported_activities() :: [
864 required(:activity) => String.t(),
865 required(:date) => String.t()
868 def get_reported_activities do
870 where: fragment("(?)->>'type' = 'Flag'", a.data),
872 date: fragment("max(?->>'published') date", a.data),
874 fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity", a.data)
876 group_by: fragment("activity"),
877 order_by: fragment("date DESC")
882 def update_report_state(%Activity{} = activity, state)
883 when state in @strip_status_report_states do
884 {:ok, stripped_activity} = strip_report_status_data(activity)
888 |> Map.put("state", state)
889 |> Map.put("object", stripped_activity.data["object"])
892 |> Changeset.change(data: new_data)
896 def update_report_state(%Activity{} = activity, state) when state in @supported_report_states do
897 new_data = Map.put(activity.data, "state", state)
900 |> Changeset.change(data: new_data)
904 def update_report_state(activity_ids, state) when state in @supported_report_states do
905 activities_num = length(activity_ids)
907 from(a in Activity, where: a.id in ^activity_ids)
908 |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
909 |> Repo.update_all([])
911 {^activities_num, _} -> :ok
912 _ -> {:error, activity_ids}
916 def update_report_state(_, _), do: {:error, "Unsupported state"}
918 def strip_report_status_data(activity) do
919 [actor | reported_activities] = activity.data["object"]
920 stripped_activities = Enum.map(reported_activities, & &1["id"])
921 new_data = put_in(activity.data, ["object"], [actor | stripped_activities])
923 {:ok, %{activity | data: new_data}}
926 def update_activity_visibility(activity, visibility) when visibility in @valid_visibilities do
927 [to, cc, recipients] =
929 |> get_updated_targets(visibility)
930 |> Enum.map(&Enum.uniq/1)
939 |> Object.change(%{data: object_data})
940 |> Object.update_and_set_cache()
948 |> Map.put(:object, object)
949 |> Activity.change(%{data: activity_data, recipients: recipients})
953 def update_activity_visibility(_, _), do: {:error, "Unsupported visibility"}
955 defp get_updated_targets(
956 %Activity{data: %{"to" => to} = data, recipients: recipients},
959 cc = Map.get(data, "cc", [])
960 follower_address = User.get_cached_by_ap_id(data["actor"]).follower_address
961 public = Pleroma.Constants.as_public()
965 to = [public | List.delete(to, follower_address)]
966 cc = [follower_address | List.delete(cc, public)]
967 recipients = [public | recipients]
971 to = [follower_address | List.delete(to, public)]
972 cc = List.delete(cc, public)
973 recipients = List.delete(recipients, public)
977 to = [follower_address | List.delete(to, public)]
978 cc = [public | List.delete(cc, follower_address)]
979 recipients = recipients ++ [follower_address, public]
987 def get_existing_votes(actor, %{data: %{"id" => id}}) do
989 |> Activity.Queries.by_actor()
990 |> Activity.Queries.by_type("Create")
991 |> Activity.with_preloaded_object()
992 |> where([a, object: o], fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(id)))
993 |> where([a, object: o], fragment("(?)->>'type' = 'Answer'", o.data))
997 def maybe_put(map, _key, nil), do: map
998 def maybe_put(map, key, value), do: Map.put(map, key, value)