activitypub: use jsonb query for containment instead of recipients_to/recipients_cc.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
1 defmodule Pleroma.Web.ActivityPub.ActivityPub do
2 alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
3 alias Pleroma.Web.ActivityPub.{Transmogrifier, MRF}
4 alias Pleroma.Web.WebFinger
5 alias Pleroma.Web.Federator
6 alias Pleroma.Web.OStatus
7 import Ecto.Query
8 import Pleroma.Web.ActivityPub.Utils
9 require Logger
10
11 @httpoison Application.get_env(:pleroma, :httpoison)
12
13 @instance Application.get_env(:pleroma, :instance)
14
15 # For Announce activities, we filter the recipients based on following status for any actors
16 # that match actual users. See issue #164 for more information about why this is necessary.
17 defp get_recipients(%{"type" => "Announce"} = data) do
18 to = data["to"] || []
19 cc = data["cc"] || []
20 recipients = to ++ cc
21 actor = User.get_cached_by_ap_id(data["actor"])
22
23 recipients
24 |> Enum.filter(fn recipient ->
25 case User.get_cached_by_ap_id(recipient) do
26 nil ->
27 true
28
29 user ->
30 User.following?(user, actor)
31 end
32 end)
33
34 {recipients, to, cc}
35 end
36
37 defp get_recipients(data) do
38 to = data["to"] || []
39 cc = data["cc"] || []
40 recipients = to ++ cc
41 {recipients, to, cc}
42 end
43
44 defp check_actor_is_active(actor) do
45 if not is_nil(actor) do
46 with user <- User.get_cached_by_ap_id(actor),
47 nil <- user.info["deactivated"] do
48 :ok
49 else
50 _e -> :reject
51 end
52 else
53 :ok
54 end
55 end
56
57 def insert(map, local \\ true) when is_map(map) do
58 with nil <- Activity.normalize(map),
59 map <- lazy_put_activity_defaults(map),
60 :ok <- check_actor_is_active(map["actor"]),
61 {:ok, map} <- MRF.filter(map),
62 :ok <- insert_full_object(map) do
63 {recipients, recipients_to, recipients_cc} = get_recipients(map)
64
65 {:ok, activity} =
66 Repo.insert(%Activity{
67 data: map,
68 local: local,
69 actor: map["actor"],
70 recipients: recipients,
71 recipients_to: recipients_to,
72 recipients_cc: recipients_cc
73 })
74
75 Notification.create_notifications(activity)
76 stream_out(activity)
77 {:ok, activity}
78 else
79 %Activity{} = activity -> {:ok, activity}
80 error -> {:error, error}
81 end
82 end
83
84 def stream_out(activity) do
85 public = "https://www.w3.org/ns/activitystreams#Public"
86
87 if activity.data["type"] in ["Create", "Announce"] do
88 Pleroma.Web.Streamer.stream("user", activity)
89 Pleroma.Web.Streamer.stream("list", activity)
90
91 if Enum.member?(activity.data["to"], public) do
92 Pleroma.Web.Streamer.stream("public", activity)
93
94 if activity.local do
95 Pleroma.Web.Streamer.stream("public:local", activity)
96 end
97
98 if activity.data["object"]["attachment"] != [] do
99 Pleroma.Web.Streamer.stream("public:media", activity)
100
101 if activity.local do
102 Pleroma.Web.Streamer.stream("public:local:media", activity)
103 end
104 end
105 else
106 if !Enum.member?(activity.data["cc"] || [], public) &&
107 !Enum.member?(
108 activity.data["to"],
109 User.get_by_ap_id(activity.data["actor"]).follower_address
110 ),
111 do: Pleroma.Web.Streamer.stream("direct", activity)
112 end
113 end
114 end
115
116 def create(%{to: to, actor: actor, context: context, object: object} = params) do
117 additional = params[:additional] || %{}
118 # only accept false as false value
119 local = !(params[:local] == false)
120 published = params[:published]
121
122 with create_data <-
123 make_create_data(
124 %{to: to, actor: actor, published: published, context: context, object: object},
125 additional
126 ),
127 {:ok, activity} <- insert(create_data, local),
128 :ok <- maybe_federate(activity),
129 {:ok, _actor} <- User.increase_note_count(actor) do
130 {:ok, activity}
131 end
132 end
133
134 def accept(%{to: to, actor: actor, object: object} = params) do
135 # only accept false as false value
136 local = !(params[:local] == false)
137
138 with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
139 {:ok, activity} <- insert(data, local),
140 :ok <- maybe_federate(activity) do
141 {:ok, activity}
142 end
143 end
144
145 def reject(%{to: to, actor: actor, object: object} = params) do
146 # only accept false as false value
147 local = !(params[:local] == false)
148
149 with data <- %{"to" => to, "type" => "Reject", "actor" => actor, "object" => object},
150 {:ok, activity} <- insert(data, local),
151 :ok <- maybe_federate(activity) do
152 {:ok, activity}
153 end
154 end
155
156 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
157 # only accept false as false value
158 local = !(params[:local] == false)
159
160 with data <- %{
161 "to" => to,
162 "cc" => cc,
163 "type" => "Update",
164 "actor" => actor,
165 "object" => object
166 },
167 {:ok, activity} <- insert(data, local),
168 :ok <- maybe_federate(activity) do
169 {:ok, activity}
170 end
171 end
172
173 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
174 def like(
175 %User{ap_id: ap_id} = user,
176 %Object{data: %{"id" => _}} = object,
177 activity_id \\ nil,
178 local \\ true
179 ) do
180 with nil <- get_existing_like(ap_id, object),
181 like_data <- make_like_data(user, object, activity_id),
182 {:ok, activity} <- insert(like_data, local),
183 {:ok, object} <- add_like_to_object(activity, object),
184 :ok <- maybe_federate(activity) do
185 {:ok, activity, object}
186 else
187 %Activity{} = activity -> {:ok, activity, object}
188 error -> {:error, error}
189 end
190 end
191
192 def unlike(
193 %User{} = actor,
194 %Object{} = object,
195 activity_id \\ nil,
196 local \\ true
197 ) do
198 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
199 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
200 {:ok, unlike_activity} <- insert(unlike_data, local),
201 {:ok, _activity} <- Repo.delete(like_activity),
202 {:ok, object} <- remove_like_from_object(like_activity, object),
203 :ok <- maybe_federate(unlike_activity) do
204 {:ok, unlike_activity, like_activity, object}
205 else
206 _e -> {:ok, object}
207 end
208 end
209
210 def announce(
211 %User{ap_id: _} = user,
212 %Object{data: %{"id" => _}} = object,
213 activity_id \\ nil,
214 local \\ true
215 ) do
216 with true <- is_public?(object),
217 announce_data <- make_announce_data(user, object, activity_id),
218 {:ok, activity} <- insert(announce_data, local),
219 {:ok, object} <- add_announce_to_object(activity, object),
220 :ok <- maybe_federate(activity) do
221 {:ok, activity, object}
222 else
223 error -> {:error, error}
224 end
225 end
226
227 def unannounce(
228 %User{} = actor,
229 %Object{} = object,
230 activity_id \\ nil,
231 local \\ true
232 ) do
233 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
234 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
235 {:ok, unannounce_activity} <- insert(unannounce_data, local),
236 :ok <- maybe_federate(unannounce_activity),
237 {:ok, _activity} <- Repo.delete(announce_activity),
238 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
239 {:ok, unannounce_activity, object}
240 else
241 _e -> {:ok, object}
242 end
243 end
244
245 def follow(follower, followed, activity_id \\ nil, local \\ true) do
246 with data <- make_follow_data(follower, followed, activity_id),
247 {:ok, activity} <- insert(data, local),
248 :ok <- maybe_federate(activity) do
249 {:ok, activity}
250 end
251 end
252
253 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
254 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
255 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
256 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
257 {:ok, activity} <- insert(unfollow_data, local),
258 :ok <- maybe_federate(activity) do
259 {:ok, activity}
260 end
261 end
262
263 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
264 user = User.get_cached_by_ap_id(actor)
265
266 data = %{
267 "type" => "Delete",
268 "actor" => actor,
269 "object" => id,
270 "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
271 }
272
273 with Repo.delete(object),
274 Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
275 {:ok, activity} <- insert(data, local),
276 :ok <- maybe_federate(activity),
277 {:ok, _actor} <- User.decrease_note_count(user) do
278 {:ok, activity}
279 end
280 end
281
282 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
283 ap_config = Application.get_env(:pleroma, :activitypub)
284 unfollow_blocked = Keyword.get(ap_config, :unfollow_blocked)
285 outgoing_blocks = Keyword.get(ap_config, :outgoing_blocks)
286
287 with true <- unfollow_blocked do
288 follow_activity = fetch_latest_follow(blocker, blocked)
289
290 if follow_activity do
291 unfollow(blocker, blocked, nil, local)
292 end
293 end
294
295 with true <- outgoing_blocks,
296 block_data <- make_block_data(blocker, blocked, activity_id),
297 {:ok, activity} <- insert(block_data, local),
298 :ok <- maybe_federate(activity) do
299 {:ok, activity}
300 else
301 _e -> {:ok, nil}
302 end
303 end
304
305 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
306 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
307 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
308 {:ok, activity} <- insert(unblock_data, local),
309 :ok <- maybe_federate(activity) do
310 {:ok, activity}
311 end
312 end
313
314 def fetch_activities_for_context(context, opts \\ %{}) do
315 public = ["https://www.w3.org/ns/activitystreams#Public"]
316
317 recipients =
318 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
319
320 query = from(activity in Activity)
321
322 query =
323 query
324 |> restrict_blocked(opts)
325 |> restrict_recipients(recipients, opts["user"])
326
327 query =
328 from(
329 activity in query,
330 where:
331 fragment(
332 "?->>'type' = ? and ?->>'context' = ?",
333 activity.data,
334 "Create",
335 activity.data,
336 ^context
337 ),
338 order_by: [desc: :id]
339 )
340
341 Repo.all(query)
342 end
343
344 def fetch_public_activities(opts \\ %{}) do
345 q = fetch_activities_query(["https://www.w3.org/ns/activitystreams#Public"], opts)
346
347 q
348 |> restrict_unlisted()
349 |> Repo.all()
350 |> Enum.reverse()
351 end
352
353 @valid_visibilities ~w[direct unlisted public private]
354
355 defp restrict_visibility(query, %{visibility: "direct"}) do
356 public = "https://www.w3.org/ns/activitystreams#Public"
357
358 from(
359 activity in query,
360 join: sender in User,
361 on: sender.ap_id == activity.actor,
362 # Are non-direct statuses with no to/cc possible?
363 where:
364 fragment(
365 "not (? && ?)",
366 [^public, sender.follower_address],
367 activity.recipients
368 )
369 )
370 end
371
372 defp restrict_visibility(_query, %{visibility: visibility})
373 when visibility not in @valid_visibilities do
374 Logger.error("Could not restrict visibility to #{visibility}")
375 end
376
377 defp restrict_visibility(query, _visibility), do: query
378
379 def fetch_user_activities(user, reading_user, params \\ %{}) do
380 params =
381 params
382 |> Map.put("type", ["Create", "Announce"])
383 |> Map.put("actor_id", user.ap_id)
384 |> Map.put("whole_db", true)
385
386 recipients =
387 if reading_user do
388 ["https://www.w3.org/ns/activitystreams#Public"] ++
389 [reading_user.ap_id | reading_user.following]
390 else
391 ["https://www.w3.org/ns/activitystreams#Public"]
392 end
393
394 fetch_activities(recipients, params)
395 |> Enum.reverse()
396 end
397
398 defp restrict_since(query, %{"since_id" => since_id}) do
399 from(activity in query, where: activity.id > ^since_id)
400 end
401
402 defp restrict_since(query, _), do: query
403
404 defp restrict_tag(query, %{"tag" => tag}) do
405 from(
406 activity in query,
407 where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
408 )
409 end
410
411 defp restrict_tag(query, _), do: query
412
413 defp restrict_to_cc(query, recipients_to, recipients_cc) do
414 from(
415 activity in query,
416 where:
417 fragment(
418 "(?->'to' \\?| ?) or (?->'cc' \\?| ?)",
419 activity.data,
420 ^recipients_to,
421 activity.data,
422 ^recipients_cc
423 )
424 )
425 end
426
427 defp restrict_recipients(query, [], _user), do: query
428
429 defp restrict_recipients(query, recipients, nil) do
430 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
431 end
432
433 defp restrict_recipients(query, recipients, user) do
434 from(
435 activity in query,
436 where: fragment("? && ?", ^recipients, activity.recipients),
437 or_where: activity.actor == ^user.ap_id
438 )
439 end
440
441 defp restrict_limit(query, %{"limit" => limit}) do
442 from(activity in query, limit: ^limit)
443 end
444
445 defp restrict_limit(query, _), do: query
446
447 defp restrict_local(query, %{"local_only" => true}) do
448 from(activity in query, where: activity.local == true)
449 end
450
451 defp restrict_local(query, _), do: query
452
453 defp restrict_max(query, %{"max_id" => max_id}) do
454 from(activity in query, where: activity.id < ^max_id)
455 end
456
457 defp restrict_max(query, _), do: query
458
459 defp restrict_actor(query, %{"actor_id" => actor_id}) do
460 from(activity in query, where: activity.actor == ^actor_id)
461 end
462
463 defp restrict_actor(query, _), do: query
464
465 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
466 restrict_type(query, %{"type" => [type]})
467 end
468
469 defp restrict_type(query, %{"type" => type}) do
470 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
471 end
472
473 defp restrict_type(query, _), do: query
474
475 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
476 from(
477 activity in query,
478 where: fragment("? <@ (? #> '{\"object\",\"likes\"}')", ^ap_id, activity.data)
479 )
480 end
481
482 defp restrict_favorited_by(query, _), do: query
483
484 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
485 from(
486 activity in query,
487 where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[])
488 )
489 end
490
491 defp restrict_media(query, _), do: query
492
493 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
494 from(
495 activity in query,
496 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
497 )
498 end
499
500 defp restrict_replies(query, _), do: query
501
502 # Only search through last 100_000 activities by default
503 defp restrict_recent(query, %{"whole_db" => true}), do: query
504
505 defp restrict_recent(query, _) do
506 since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
507
508 from(activity in query, where: activity.id > ^since)
509 end
510
511 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
512 blocks = info["blocks"] || []
513 domain_blocks = info["domain_blocks"] || []
514
515 from(
516 activity in query,
517 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
518 where: fragment("not (?->'to' \\?| ?)", activity.data, ^blocks),
519 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks)
520 )
521 end
522
523 defp restrict_blocked(query, _), do: query
524
525 defp restrict_unlisted(query) do
526 from(
527 activity in query,
528 where:
529 fragment(
530 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
531 activity.data,
532 ^["https://www.w3.org/ns/activitystreams#Public"]
533 )
534 )
535 end
536
537 def fetch_activities_query(recipients, opts \\ %{}) do
538 base_query =
539 from(
540 activity in Activity,
541 limit: 20,
542 order_by: [fragment("? desc nulls last", activity.id)]
543 )
544
545 base_query
546 |> restrict_recipients(recipients, opts["user"])
547 |> restrict_tag(opts)
548 |> restrict_since(opts)
549 |> restrict_local(opts)
550 |> restrict_limit(opts)
551 |> restrict_max(opts)
552 |> restrict_actor(opts)
553 |> restrict_type(opts)
554 |> restrict_favorited_by(opts)
555 |> restrict_recent(opts)
556 |> restrict_blocked(opts)
557 |> restrict_media(opts)
558 |> restrict_visibility(opts)
559 |> restrict_replies(opts)
560 end
561
562 def fetch_activities(recipients, opts \\ %{}) do
563 fetch_activities_query(recipients, opts)
564 |> Repo.all()
565 |> Enum.reverse()
566 end
567
568 def fetch_activities_bounded(recipients_to, recipients_cc, opts \\ %{}) do
569 fetch_activities_query([], opts)
570 |> restrict_to_cc(recipients_to, recipients_cc)
571 |> Repo.all()
572 |> Enum.reverse()
573 end
574
575 def upload(file) do
576 data = Upload.store(file, Application.get_env(:pleroma, :instance)[:dedupe_media])
577 Repo.insert(%Object{data: data})
578 end
579
580 def user_data_from_user_object(data) do
581 avatar =
582 data["icon"]["url"] &&
583 %{
584 "type" => "Image",
585 "url" => [%{"href" => data["icon"]["url"]}]
586 }
587
588 banner =
589 data["image"]["url"] &&
590 %{
591 "type" => "Image",
592 "url" => [%{"href" => data["image"]["url"]}]
593 }
594
595 locked = data["manuallyApprovesFollowers"] || false
596 data = Transmogrifier.maybe_fix_user_object(data)
597
598 user_data = %{
599 ap_id: data["id"],
600 info: %{
601 "ap_enabled" => true,
602 "source_data" => data,
603 "banner" => banner,
604 "locked" => locked
605 },
606 avatar: avatar,
607 name: data["name"],
608 follower_address: data["followers"],
609 bio: data["summary"]
610 }
611
612 # nickname can be nil because of virtual actors
613 user_data =
614 if data["preferredUsername"] do
615 Map.put(
616 user_data,
617 :nickname,
618 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
619 )
620 else
621 Map.put(user_data, :nickname, nil)
622 end
623
624 {:ok, user_data}
625 end
626
627 def fetch_and_prepare_user_from_ap_id(ap_id) do
628 with {:ok, %{status_code: 200, body: body}} <-
629 @httpoison.get(ap_id, [Accept: "application/activity+json"], follow_redirect: true),
630 {:ok, data} <- Jason.decode(body) do
631 user_data_from_user_object(data)
632 else
633 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
634 end
635 end
636
637 def make_user_from_ap_id(ap_id) do
638 if _user = User.get_by_ap_id(ap_id) do
639 Transmogrifier.upgrade_user_from_ap_id(ap_id)
640 else
641 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
642 User.insert_or_update_user(data)
643 else
644 e -> {:error, e}
645 end
646 end
647 end
648
649 def make_user_from_nickname(nickname) do
650 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
651 make_user_from_ap_id(ap_id)
652 else
653 _e -> {:error, "No AP id in WebFinger"}
654 end
655 end
656
657 @quarantined_instances Keyword.get(@instance, :quarantined_instances, [])
658
659 def should_federate?(inbox, public) do
660 if public do
661 true
662 else
663 inbox_info = URI.parse(inbox)
664 inbox_info.host not in @quarantined_instances
665 end
666 end
667
668 def publish(actor, activity) do
669 followers =
670 if actor.follower_address in activity.recipients do
671 {:ok, followers} = User.get_followers(actor)
672 followers |> Enum.filter(&(!&1.local))
673 else
674 []
675 end
676
677 public = is_public?(activity)
678
679 remote_inboxes =
680 (Pleroma.Web.Salmon.remote_users(activity) ++ followers)
681 |> Enum.filter(fn user -> User.ap_enabled?(user) end)
682 |> Enum.map(fn %{info: %{"source_data" => data}} ->
683 (data["endpoints"] && data["endpoints"]["sharedInbox"]) || data["inbox"]
684 end)
685 |> Enum.uniq()
686 |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
687
688 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
689 json = Jason.encode!(data)
690
691 Enum.each(remote_inboxes, fn inbox ->
692 Federator.enqueue(:publish_single_ap, %{
693 inbox: inbox,
694 json: json,
695 actor: actor,
696 id: activity.data["id"]
697 })
698 end)
699 end
700
701 def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
702 Logger.info("Federating #{id} to #{inbox}")
703 host = URI.parse(inbox).host
704
705 digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
706
707 signature =
708 Pleroma.Web.HTTPSignatures.sign(actor, %{
709 host: host,
710 "content-length": byte_size(json),
711 digest: digest
712 })
713
714 @httpoison.post(
715 inbox,
716 json,
717 [
718 {"Content-Type", "application/activity+json"},
719 {"signature", signature},
720 {"digest", digest}
721 ],
722 hackney: [pool: :default]
723 )
724 end
725
726 # TODO:
727 # This will create a Create activity, which we need internally at the moment.
728 def fetch_object_from_id(id) do
729 if object = Object.get_cached_by_ap_id(id) do
730 {:ok, object}
731 else
732 Logger.info("Fetching #{id} via AP")
733
734 with true <- String.starts_with?(id, "http"),
735 {:ok, %{body: body, status_code: code}} when code in 200..299 <-
736 @httpoison.get(
737 id,
738 [Accept: "application/activity+json"],
739 follow_redirect: true,
740 timeout: 10000,
741 recv_timeout: 20000
742 ),
743 {:ok, data} <- Jason.decode(body),
744 nil <- Object.normalize(data),
745 params <- %{
746 "type" => "Create",
747 "to" => data["to"],
748 "cc" => data["cc"],
749 "actor" => data["attributedTo"],
750 "object" => data
751 },
752 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
753 {:ok, Object.normalize(activity.data["object"])}
754 else
755 object = %Object{} ->
756 {:ok, object}
757
758 _e ->
759 Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
760
761 case OStatus.fetch_activity_from_url(id) do
762 {:ok, [activity | _]} -> {:ok, Object.normalize(activity.data["object"])}
763 e -> e
764 end
765 end
766 end
767 end
768
769 def is_public?(activity) do
770 "https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++
771 (activity.data["cc"] || []))
772 end
773
774 def visible_for_user?(activity, nil) do
775 is_public?(activity)
776 end
777
778 def visible_for_user?(activity, user) do
779 x = [user.ap_id | user.following]
780 y = activity.data["to"] ++ (activity.data["cc"] || [])
781 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
782 end
783 end