status_view.ex 8.37 KB
Newer Older
1
2
3
4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
6
defmodule Pleroma.Web.MastodonAPI.StatusView do
  use Pleroma.Web, :view
Maksim's avatar
Maksim committed
7

Maksim's avatar
Maksim committed
8
9
10
11
12
13
14
15
  alias Pleroma.Activity
  alias Pleroma.HTML
  alias Pleroma.Repo
  alias Pleroma.User
  alias Pleroma.Web.CommonAPI.Utils
  alias Pleroma.Web.MediaProxy
  alias Pleroma.Web.MastodonAPI.AccountView
  alias Pleroma.Web.MastodonAPI.StatusView
lain's avatar
lain committed
16
17
18
19
20

  # TODO: Add cached version.
  defp get_replied_to_activities(activities) do
    activities
    |> Enum.map(fn
Maksim's avatar
Maksim committed
21
22
      %{data: %{"type" => "Create", "object" => %{"inReplyTo" => in_reply_to}}} ->
        in_reply_to != "" && in_reply_to
lain's avatar
lain committed
23
24
25

      _ ->
        nil
lain's avatar
lain committed
26
    end)
lain's avatar
lain committed
27
    |> Enum.filter(& &1)
lain's avatar
lain committed
28
    |> Activity.create_activity_by_object_id_query()
lain's avatar
lain committed
29
30
31
32
    |> Repo.all()
    |> Enum.reduce(%{}, fn activity, acc ->
      Map.put(acc, activity.data["object"]["id"], activity)
    end)
lain's avatar
lain committed
33
  end
lain's avatar
lain committed
34
35

  def render("index.json", opts) do
lain's avatar
lain committed
36
    replied_to_activities = get_replied_to_activities(opts.activities)
lain's avatar
lain committed
37

Maksim's avatar
Maksim committed
38
39
    opts.activities
    |> render_many(
lain's avatar
lain committed
40
41
42
43
      StatusView,
      "status.json",
      Map.put(opts, :replied_to_activities, replied_to_activities)
    )
44
    |> Enum.filter(fn x -> not is_nil(x) end)
lain's avatar
lain committed
45
46
  end

lain's avatar
lain committed
47
48
49
50
  def render(
        "status.json",
        %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
      ) do
lain's avatar
lain committed
51
52
53
54
55
56
    user = User.get_cached_by_ap_id(activity.data["actor"])
    created_at = Utils.to_masto_date(activity.data["published"])

    reblogged = Activity.get_create_activity_by_object_ap_id(object)
    reblogged = render("status.json", Map.put(opts, :activity, reblogged))

lain's avatar
lain committed
57
58
59
60
61
    mentions =
      activity.recipients
      |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
      |> Enum.filter(& &1)
      |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
lain's avatar
lain committed
62
63

    %{
64
      id: to_string(activity.id),
lain's avatar
lain committed
65
      uri: object,
66
      url: object,
lain's avatar
lain committed
67
68
69
70
      account: AccountView.render("account.json", %{user: user}),
      in_reply_to_id: nil,
      in_reply_to_account_id: nil,
      reblog: reblogged,
71
      content: reblogged[:content] || "",
lain's avatar
lain committed
72
73
      created_at: created_at,
      reblogs_count: 0,
74
      replies_count: 0,
lain's avatar
lain committed
75
      favourites_count: 0,
lain's avatar
lain committed
76
77
      reblogged: false,
      favourited: false,
lain's avatar
lain committed
78
79
80
81
      muted: false,
      sensitive: false,
      spoiler_text: "",
      visibility: "public",
Maksim's avatar
Maksim committed
82
      media_attachments: reblogged[:media_attachments] || [],
lain's avatar
lain committed
83
      mentions: mentions,
Maksim's avatar
Maksim committed
84
      tags: reblogged[:tags] || [],
lain's avatar
lain committed
85
86
87
88
      application: %{
        name: "Web",
        website: nil
      },
lain's avatar
lain committed
89
      language: nil,
lain's avatar
lain committed
90
      emojis: []
lain's avatar
lain committed
91
92
93
    }
  end

lain's avatar
lain committed
94
  def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
lain's avatar
lain committed
95
96
97
98
99
100
    user = User.get_cached_by_ap_id(activity.data["actor"])

    like_count = object["like_count"] || 0
    announcement_count = object["announcement_count"] || 0

    tags = object["tag"] || []
lain's avatar
lain committed
101
    sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
lain's avatar
lain committed
102

lain's avatar
lain committed
103
104
105
106
107
    mentions =
      activity.recipients
      |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
      |> Enum.filter(& &1)
      |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
lain's avatar
lain committed
108

lain's avatar
lain committed
109
    repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
lain's avatar
lain committed
110
    favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
lain's avatar
lain committed
111

lain's avatar
lain committed
112
113
    attachment_data = object["attachment"] || []
    attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
lain's avatar
lain committed
114

115
    created_at = Utils.to_masto_date(object["published"])
116

lain's avatar
lain committed
117
    reply_to = get_reply_to(activity, opts)
lain's avatar
lain committed
118
119
    reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])

120
    content =
Maksim's avatar
Maksim committed
121
122
      object
      |> render_content()
123
      |> HTML.get_cached_scrubbed_html_for_object(User.html_filter_policy(opts[:for]), activity)
124

lain's avatar
lain committed
125
    %{
126
      id: to_string(activity.id),
lain's avatar
lain committed
127
      uri: object["id"],
128
      url: object["external_url"] || object["id"],
lain's avatar
lain committed
129
      account: AccountView.render("account.json", %{user: user}),
130
131
      in_reply_to_id: reply_to && to_string(reply_to.id),
      in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
lain's avatar
lain committed
132
      reblog: nil,
133
      content: content,
134
      created_at: created_at,
lain's avatar
lain committed
135
      reblogs_count: announcement_count,
136
      replies_count: 0,
lain's avatar
lain committed
137
      favourites_count: like_count,
Maksim's avatar
Maksim committed
138
139
      reblogged: present?(repeated),
      favourited: present?(favorited),
lain's avatar
lain committed
140
141
      muted: false,
      sensitive: sensitive,
lain's avatar
lain committed
142
      spoiler_text: object["summary"] || "",
143
      visibility: get_visibility(object),
eal's avatar
eal committed
144
      media_attachments: attachments |> Enum.take(4),
lain's avatar
lain committed
145
      mentions: mentions,
Maksim's avatar
Maksim committed
146
      tags: build_tags(tags),
147
148
149
150
      application: %{
        name: "Web",
        website: nil
      },
Roger Braun's avatar
Roger Braun committed
151
      language: nil,
Maksim's avatar
Maksim committed
152
      emojis: build_emojis(activity.data["object"]["emoji"])
lain's avatar
lain committed
153
154
    }
  end
lain's avatar
lain committed
155

156
  def render("status.json", _) do
157
158
159
    nil
  end

lain's avatar
lain committed
160
  def render("attachment.json", %{attachment: attachment}) do
Haelwenn's avatar
Haelwenn committed
161
    [attachment_url | _] = attachment["url"]
162
    media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
163
    href = attachment_url["href"] |> MediaProxy.url()
lain's avatar
lain committed
164

lain's avatar
lain committed
165
166
167
168
169
170
171
    type =
      cond do
        String.contains?(media_type, "image") -> "image"
        String.contains?(media_type, "video") -> "video"
        String.contains?(media_type, "audio") -> "audio"
        true -> "unknown"
      end
lain's avatar
lain committed
172

lain's avatar
lain committed
173
    <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
lain's avatar
lain committed
174

lain's avatar
lain committed
175
    %{
lain's avatar
lain committed
176
      id: to_string(attachment["id"] || hash_id),
177
      url: href,
lain's avatar
lain committed
178
      remote_url: href,
179
      preview_url: href,
180
      text_url: href,
181
182
      type: type,
      description: attachment["name"]
lain's avatar
lain committed
183
184
    }
  end
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

  def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
    _id = activity.data["object"]["inReplyTo"]
    replied_to_activities[activity.data["object"]["inReplyTo"]]
  end

  def get_reply_to(%{data: %{"object" => object}}, _) do
    if object["inReplyTo"] && object["inReplyTo"] != "" do
      Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
    else
      nil
    end
  end

  def get_visibility(object) do
    public = "https://www.w3.org/ns/activitystreams#Public"
    to = object["to"] || []
    cc = object["cc"] || []

    cond do
csaurus's avatar
csaurus committed
205
206
207
208
209
210
211
212
213
214
215
216
      public in to ->
        "public"

      public in cc ->
        "unlisted"

      # this should use the sql for the object's activity
      Enum.any?(to, &String.contains?(&1, "/followers")) ->
        "private"

      true ->
        "direct"
217
218
    end
  end
219

220
  def render_content(%{"type" => "Video"} = object) do
Maksim's avatar
Maksim committed
221
222
223
224
225
226
    with name when not is_nil(name) and name != "" <- object["name"] do
      "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
    else
      _ -> object["content"] || ""
    end
  end
227

Maksim's avatar
Maksim committed
228
229
230
231
232
233
234
235
  def render_content(%{"type" => object_type} = object)
      when object_type in ["Article", "Page"] do
    with summary when not is_nil(summary) and summary != "" <- object["name"],
         url when is_bitstring(url) <- object["url"] do
      "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
    else
      _ -> object["content"] || ""
    end
236
237
  end

Maksim's avatar
Maksim committed
238
  def render_content(object), do: object["content"] || ""
kaniini's avatar
kaniini committed
239

Maksim's avatar
Maksim committed
240
241
242
243
244
245
246
247
248
249
  @doc """
  Builds a dictionary tags.

  ## Examples

  iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
  [{"name": "fediverse", "url": "/tag/fediverse"},
   {"name": "nextcloud", "url": "/tag/nextcloud"}]

  """
Maksim's avatar
Maksim committed
250
  @spec build_tags(list(any())) :: list(map())
Maksim's avatar
Maksim committed
251
  def build_tags(object_tags) when is_list(object_tags) do
Maksim's avatar
Maksim committed
252
253
    object_tags = for tag when is_binary(tag) <- object_tags, do: tag

Maksim's avatar
Maksim committed
254
255
256
257
258
259
260
    Enum.reduce(object_tags, [], fn tag, tags ->
      tags ++ [%{name: tag, url: "/tag/#{tag}"}]
    end)
  end

  def build_tags(_), do: []

Maksim's avatar
Maksim committed
261
262
263
264
265
266
267
268
  @doc """
  Builds list emojis.

  Arguments: `nil` or list tuple of name and url.

  Returns list emojis.

  ## Examples
269

Maksim's avatar
Maksim committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
  [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]

  """
  @spec build_emojis(nil | list(tuple())) :: list(map())
  def build_emojis(nil), do: []

  def build_emojis(emojis) do
    emojis
    |> Enum.map(fn {name, url} ->
      name = HTML.strip_tags(name)

      url =
        url
        |> HTML.strip_tags()
        |> MediaProxy.url()

      %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
    end)
289
290
  end

Maksim's avatar
Maksim committed
291
292
293
  defp present?(nil), do: false
  defp present?(false), do: false
  defp present?(_), do: true
lain's avatar
lain committed
294
end