feed_view.ex 1.69 KB
Newer Older
1 2 3 4 5 6 7 8
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Feed.FeedView do
  use Phoenix.HTML
  use Pleroma.Web, :view

Maksim's avatar
Maksim committed
9
  alias Pleroma.Formatter
10 11 12 13 14 15
  alias Pleroma.Object
  alias Pleroma.User
  alias Pleroma.Web.MediaProxy

  require Pleroma.Constants

Maksim's avatar
Maksim committed
16 17 18 19 20 21 22 23 24 25
  def prepare_activity(activity) do
    object = activity_object(activity)

    %{
      activity: activity,
      data: Map.get(object, :data),
      object: object
    }
  end

26 27 28 29 30 31 32 33 34 35 36
  def most_recent_update(activities, user) do
    (List.first(activities) || user).updated_at
    |> NaiveDateTime.to_iso8601()
  end

  def logo(user) do
    user
    |> User.avatar_url()
    |> MediaProxy.url()
  end

Maksim's avatar
Maksim committed
37
  def last_activity(activities), do: List.last(activities)
38

Maksim's avatar
Maksim committed
39
  def activity_object(activity), do: Object.normalize(activity)
40

Maksim's avatar
Maksim committed
41 42 43 44
  def activity_title(%{data: %{"content" => content}}, opts \\ %{}) do
    content
    |> Formatter.truncate(opts[:max_length], opts[:omission])
    |> escape()
45 46
  end

Maksim's avatar
Maksim committed
47
  def activity_content(%{data: %{"content" => content}}) do
48 49 50 51 52
    content
    |> String.replace(~r/[\n\r]/, "")
    |> escape()
  end

Maksim's avatar
Maksim committed
53
  def activity_context(activity), do: activity.data["context"]
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  def attachment_href(attachment) do
    attachment["url"]
    |> hd()
    |> Map.get("href")
  end

  def attachment_type(attachment) do
    attachment["url"]
    |> hd()
    |> Map.get("mediaType")
  end

  def get_href(id) do
    with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
      external_url
    else
      _e -> id
    end
  end

  def escape(html) do
    html
    |> html_escape()
    |> safe_to_string()
  end
end