Salmon# fixed publish an activity to remote accounts
[akkoma] / lib / pleroma / activity.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity do
6 use Ecto.Schema
7 alias Pleroma.{Repo, Activity, Notification}
8 import Ecto.Query
9
10 @type t :: %__MODULE__{}
11
12 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
13 @mastodon_notification_types %{
14 "Create" => "mention",
15 "Follow" => "follow",
16 "Announce" => "reblog",
17 "Like" => "favourite"
18 }
19
20 schema "activities" do
21 field(:data, :map)
22 field(:local, :boolean, default: true)
23 field(:actor, :string)
24 field(:recipients, {:array, :string})
25 has_many(:notifications, Notification, on_delete: :delete_all)
26
27 timestamps()
28 end
29
30 def get_by_ap_id(ap_id) do
31 Repo.one(
32 from(
33 activity in Activity,
34 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
35 )
36 )
37 end
38
39 # TODO:
40 # Go through these and fix them everywhere.
41 # Wrong name, only returns create activities
42 def all_by_object_ap_id_q(ap_id) do
43 from(
44 activity in Activity,
45 where:
46 fragment(
47 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
48 activity.data,
49 activity.data,
50 ^to_string(ap_id)
51 ),
52 where: fragment("(?)->>'type' = 'Create'", activity.data)
53 )
54 end
55
56 # Wrong name, returns all.
57 def all_non_create_by_object_ap_id_q(ap_id) do
58 from(
59 activity in Activity,
60 where:
61 fragment(
62 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
63 activity.data,
64 activity.data,
65 ^to_string(ap_id)
66 )
67 )
68 end
69
70 # Wrong name plz fix thx
71 def all_by_object_ap_id(ap_id) do
72 Repo.all(all_by_object_ap_id_q(ap_id))
73 end
74
75 def create_activity_by_object_id_query(ap_ids) do
76 from(
77 activity in Activity,
78 where:
79 fragment(
80 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
81 activity.data,
82 activity.data,
83 ^ap_ids
84 ),
85 where: fragment("(?)->>'type' = 'Create'", activity.data)
86 )
87 end
88
89 def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
90 create_activity_by_object_id_query([ap_id])
91 |> Repo.one()
92 end
93
94 def get_create_activity_by_object_ap_id(_), do: nil
95
96 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
97 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
98 def normalize(_), do: nil
99
100 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
101 get_create_activity_by_object_ap_id(ap_id)
102 end
103
104 def get_in_reply_to_activity(_), do: nil
105
106 for {ap_type, type} <- @mastodon_notification_types do
107 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
108 do: unquote(type)
109 end
110
111 def mastodon_notification_type(%Activity{}), do: nil
112 end