Merge branch 'doc/mastodon-api-accounts-extension' into 'develop'
[akkoma] / lib / pleroma / activity.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity do
6 use Ecto.Schema
7
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.Repo
11
12 import Ecto.Query
13
14 @type t :: %__MODULE__{}
15 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
16
17 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
18 @mastodon_notification_types %{
19 "Create" => "mention",
20 "Follow" => "follow",
21 "Announce" => "reblog",
22 "Like" => "favourite"
23 }
24
25 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
26 into: %{},
27 do: {v, k}
28
29 schema "activities" do
30 field(:data, :map)
31 field(:local, :boolean, default: true)
32 field(:actor, :string)
33 field(:recipients, {:array, :string})
34 has_many(:notifications, Notification, on_delete: :delete_all)
35
36 timestamps()
37 end
38
39 def get_by_ap_id(ap_id) do
40 Repo.one(
41 from(
42 activity in Activity,
43 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
44 )
45 )
46 end
47
48 def get_by_id(id) do
49 Repo.get(Activity, id)
50 end
51
52 def by_object_ap_id(ap_id) do
53 from(
54 activity in Activity,
55 where:
56 fragment(
57 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
58 activity.data,
59 activity.data,
60 ^to_string(ap_id)
61 )
62 )
63 end
64
65 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
66 from(
67 activity in Activity,
68 where:
69 fragment(
70 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
71 activity.data,
72 activity.data,
73 ^ap_ids
74 ),
75 where: fragment("(?)->>'type' = 'Create'", activity.data)
76 )
77 end
78
79 def create_by_object_ap_id(ap_id) do
80 from(
81 activity in Activity,
82 where:
83 fragment(
84 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
85 activity.data,
86 activity.data,
87 ^to_string(ap_id)
88 ),
89 where: fragment("(?)->>'type' = 'Create'", activity.data)
90 )
91 end
92
93 def get_all_create_by_object_ap_id(ap_id) do
94 Repo.all(create_by_object_ap_id(ap_id))
95 end
96
97 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
98 create_by_object_ap_id(ap_id)
99 |> Repo.one()
100 end
101
102 def get_create_by_object_ap_id(_), do: nil
103
104 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
105 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
106 def normalize(_), do: nil
107
108 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
109 get_create_by_object_ap_id(ap_id)
110 end
111
112 def get_in_reply_to_activity(_), do: nil
113
114 def delete_by_ap_id(id) when is_binary(id) do
115 by_object_ap_id(id)
116 |> Repo.delete_all(returning: true)
117 |> elem(1)
118 |> Enum.find(fn
119 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
120 _ -> nil
121 end)
122 end
123
124 def delete_by_ap_id(_), do: nil
125
126 for {ap_type, type} <- @mastodon_notification_types do
127 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
128 do: unquote(type)
129 end
130
131 def mastodon_notification_type(%Activity{}), do: nil
132
133 def from_mastodon_notification_type(type) do
134 Map.get(@mastodon_to_ap_notification_types, type)
135 end
136
137 def all_by_actor_and_id(actor, status_ids \\ [])
138 def all_by_actor_and_id(_actor, []), do: []
139
140 def all_by_actor_and_id(actor, status_ids) do
141 Activity
142 |> where([s], s.id in ^status_ids)
143 |> where([s], s.actor == ^actor)
144 |> Repo.all()
145 end
146 end