common_api.ex 8.25 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
defmodule Pleroma.Web.CommonAPI do
Haelwenn's avatar
Haelwenn committed
6
7
8
9
  alias Pleroma.User
  alias Pleroma.Repo
  alias Pleroma.Activity
  alias Pleroma.Object
10
  alias Pleroma.ThreadMute
lain's avatar
lain committed
11
  alias Pleroma.Web.ActivityPub.ActivityPub
12
  alias Pleroma.Web.ActivityPub.Utils
13
14
15
  alias Pleroma.Formatter

  import Pleroma.Web.CommonAPI.Utils
lain's avatar
lain committed
16

17
18
19
20
21
22
23
24
25
26
27
28
29
  def follow(follower, followed) do
    with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
         {:ok, activity} <- ActivityPub.follow(follower, followed),
         {:ok, follower, followed} <-
           User.wait_and_refresh(
             Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
             follower,
             followed
           ) do
      {:ok, follower, followed, activity}
    end
  end

lain's avatar
lain committed
30
31
  def delete(activity_id, user) do
    with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
32
         %Object{} = object <- Object.normalize(object_id),
33
         true <- User.superuser?(user) || user.ap_id == object.data["actor"],
minibikini's avatar
minibikini committed
34
         {:ok, _} <- unpin(activity_id, user),
35
         {:ok, delete} <- ActivityPub.delete(object) do
lain's avatar
lain committed
36
37
38
      {:ok, delete}
    end
  end
lain's avatar
lain committed
39
40
41

  def repeat(id_or_ap_id, user) do
    with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
42
43
         object <- Object.normalize(activity.data["object"]["id"]),
         nil <- Utils.get_existing_announce(user.ap_id, object) do
lain's avatar
lain committed
44
45
46
47
48
49
50
      ActivityPub.announce(user, object)
    else
      _ ->
        {:error, "Could not repeat"}
    end
  end

normandy's avatar
normandy committed
51
52
  def unrepeat(id_or_ap_id, user) do
    with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
53
         object <- Object.normalize(activity.data["object"]["id"]) do
normandy's avatar
normandy committed
54
55
56
57
58
59
60
      ActivityPub.unannounce(user, object)
    else
      _ ->
        {:error, "Could not unrepeat"}
    end
  end

lain's avatar
lain committed
61
62
  def favorite(id_or_ap_id, user) do
    with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
63
64
         object <- Object.normalize(activity.data["object"]["id"]),
         nil <- Utils.get_existing_like(user.ap_id, object) do
lain's avatar
lain committed
65
66
67
68
69
70
71
      ActivityPub.like(user, object)
    else
      _ ->
        {:error, "Could not favorite"}
    end
  end

lain's avatar
lain committed
72
73
  def unfavorite(id_or_ap_id, user) do
    with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
74
         object <- Object.normalize(activity.data["object"]["id"]) do
lain's avatar
lain committed
75
76
77
78
79
80
81
      ActivityPub.unlike(user, object)
    else
      _ ->
        {:error, "Could not unfavorite"}
    end
  end

lain's avatar
lain committed
82
83
84
85
  def get_visibility(%{"visibility" => visibility})
      when visibility in ~w{public unlisted private direct},
      do: visibility

lain's avatar
lain committed
86
  def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
87
88
89
90
91
92
93
    case get_replied_to_activity(status_id) do
      nil ->
        "public"

      inReplyTo ->
        Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
    end
94
  end
lain's avatar
lain committed
95

96
97
  def get_visibility(_), do: "public"

98
  def post(user, %{"status" => status} = data) do
99
    visibility = get_visibility(data)
href's avatar
href committed
100
    limit = Pleroma.Config.get([:instance, :limit])
lain's avatar
lain committed
101

102
    with status <- String.trim(status),
103
         attachments <- attachments_from_ids(data),
104
         inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
minibikini's avatar
minibikini committed
105
         {content_html, mentions, tags} <-
106
107
108
           make_content_html(
             status,
             attachments,
minibikini's avatar
minibikini committed
109
             data
110
           ),
minibikini's avatar
minibikini committed
111
         {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
lain's avatar
lain committed
112
         context <- make_context(inReplyTo),
lain's avatar
lain committed
113
         cw <- data["spoiler_text"],
114
         full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
href's avatar
href committed
115
         length when length in 1..limit <- String.length(full_payload),
lain's avatar
lain committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
         object <-
           make_note_data(
             user.ap_id,
             to,
             context,
             content_html,
             attachments,
             inReplyTo,
             tags,
             cw,
             cc
           ),
         object <-
           Map.put(
             object,
             "emoji",
scarlett's avatar
scarlett committed
132
             (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
lain's avatar
lain committed
133
134
135
136
137
138
139
140
141
142
             |> Enum.reduce(%{}, fn {name, file}, acc ->
               Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
             end)
           ) do
      res =
        ActivityPub.create(%{
          to: to,
          actor: user,
          context: context,
          object: object,
143
          additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
lain's avatar
lain committed
144
145
        })

146
      res
lain's avatar
lain committed
147
148
    end
  end
lain's avatar
lain committed
149

lain's avatar
lain committed
150
  # Updates the emojis for a user based on their profile
lain's avatar
lain committed
151
  def update(user) do
152
153
    user =
      with emoji <- emoji_from_profile(user),
lain's avatar
lain committed
154
155
156
           source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
           info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
           change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
157
158
159
160
161
162
163
           {:ok, user} <- User.update_and_set_cache(change) do
        user
      else
        _e ->
          user
      end

lain's avatar
lain committed
164
165
166
167
168
169
170
    ActivityPub.update(%{
      local: true,
      to: [user.follower_address],
      cc: [],
      actor: user.ap_id,
      object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
    })
lain's avatar
lain committed
171
  end
minibikini's avatar
minibikini committed
172

173
174
175
176
177
178
179
180
181
182
183
184
  def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
    with %Activity{
           actor: ^user_ap_id,
           data: %{
             "type" => "Create",
             "object" => %{
               "to" => object_to,
               "type" => "Note"
             }
           }
         } = activity <- get_by_id_or_ap_id(id_or_ap_id),
         true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
minibikini's avatar
minibikini committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
         %{valid?: true} = info_changeset <-
           Pleroma.User.Info.add_pinnned_activity(user.info, activity),
         changeset <-
           Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
         {:ok, _user} <- User.update_and_set_cache(changeset) do
      {:ok, activity}
    else
      %{errors: [pinned_activities: {err, _}]} ->
        {:error, err}

      _ ->
        {:error, "Could not pin"}
    end
  end

  def unpin(id_or_ap_id, user) do
    with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
         %{valid?: true} = info_changeset <-
           Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
         changeset <-
           Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
         {:ok, _user} <- User.update_and_set_cache(changeset) do
      {:ok, activity}
    else
      %{errors: [pinned_activities: {err, _}]} ->
        {:error, err}

      _ ->
        {:error, "Could not unpin"}
    end
  end
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

  def add_mute(user, activity) do
    with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
      {:ok, activity}
    else
      {:error, _} -> {:error, "conversation is already muted"}
    end
  end

  def remove_mute(user, activity) do
    ThreadMute.remove_mute(user.id, activity.data["context"])
    {:ok, activity}
  end

  def thread_muted?(%{id: nil} = _user, _activity), do: false

  def thread_muted?(user, activity) do
    with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
      false
    else
      _ -> true
    end
  end
minibikini's avatar
Reports    
minibikini committed
239
240
241
242

  def report(user, data) do
    with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
         {:account, %User{} = account} <- {:account, User.get_by_id(account_id)},
minibikini's avatar
minibikini committed
243
         {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
minibikini's avatar
Reports    
minibikini committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
         {:ok, statuses} <- get_report_statuses(account, data),
         {:ok, activity} <-
           ActivityPub.flag(%{
             context: Utils.generate_context_id(),
             actor: user,
             account: account,
             statuses: statuses,
             content: content_html
           }) do
      Enum.each(User.all_superusers(), fn superuser ->
        superuser
        |> Pleroma.AdminEmail.report(user, account, statuses, content_html)
        |> Pleroma.Mailer.deliver_async()
      end)

      {:ok, activity}
    else
      {:error, err} -> {:error, err}
      {:account_id, %{}} -> {:error, "Valid `account_id` required"}
      {:account, nil} -> {:error, "Account not found"}
    end
  end
lain's avatar
lain committed
266
end