2019-10-07 06:20:41 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-07 06:20:41 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Feed.FeedView do
|
|
|
|
use Phoenix.HTML
|
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
2019-11-07 23:23:24 -07:00
|
|
|
alias Pleroma.Formatter
|
2019-10-07 06:20:41 -06:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.MediaProxy
|
|
|
|
|
|
|
|
require Pleroma.Constants
|
|
|
|
|
2019-12-17 12:13:45 -07:00
|
|
|
@spec pub_date(String.t() | DateTime.t()) :: String.t()
|
|
|
|
def pub_date(date) when is_binary(date) do
|
|
|
|
date
|
|
|
|
|> Timex.parse!("{ISO:Extended}")
|
|
|
|
|> pub_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def pub_date(%DateTime{} = date), do: Timex.format!(date, "{RFC822}")
|
|
|
|
|
2020-01-24 12:08:10 -07:00
|
|
|
def prepare_activity(activity, opts \\ []) do
|
2020-04-16 09:05:36 -06:00
|
|
|
object = Object.normalize(activity)
|
2019-11-07 23:23:24 -07:00
|
|
|
|
2020-01-24 12:08:10 -07:00
|
|
|
actor =
|
|
|
|
if opts[:actor] do
|
|
|
|
Pleroma.User.get_cached_by_ap_id(activity.actor)
|
|
|
|
end
|
|
|
|
|
2019-11-07 23:23:24 -07:00
|
|
|
%{
|
|
|
|
activity: activity,
|
|
|
|
data: Map.get(object, :data),
|
2020-01-24 12:08:10 -07:00
|
|
|
actor: actor
|
2019-11-07 23:23:24 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-24 12:08:10 -07:00
|
|
|
def most_recent_update(activities) do
|
|
|
|
with %{updated_at: updated_at} <- List.first(activities) do
|
|
|
|
NaiveDateTime.to_iso8601(updated_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-07 06:20:41 -06:00
|
|
|
def most_recent_update(activities, user) do
|
|
|
|
(List.first(activities) || user).updated_at
|
|
|
|
|> NaiveDateTime.to_iso8601()
|
|
|
|
end
|
|
|
|
|
2019-12-17 12:13:45 -07:00
|
|
|
def feed_logo do
|
|
|
|
case Pleroma.Config.get([:feed, :logo]) do
|
|
|
|
nil ->
|
2020-12-12 07:30:08 -07:00
|
|
|
"#{Pleroma.Web.base_url()}/static/logo.svg"
|
2019-12-17 12:13:45 -07:00
|
|
|
|
|
|
|
logo ->
|
|
|
|
"#{Pleroma.Web.base_url()}#{logo}"
|
|
|
|
end
|
|
|
|
|> MediaProxy.url()
|
|
|
|
end
|
|
|
|
|
2019-10-07 06:20:41 -06:00
|
|
|
def logo(user) do
|
|
|
|
user
|
|
|
|
|> User.avatar_url()
|
|
|
|
|> MediaProxy.url()
|
|
|
|
end
|
|
|
|
|
2019-11-07 23:23:24 -07:00
|
|
|
def last_activity(activities), do: List.last(activities)
|
2019-10-07 06:20:41 -06:00
|
|
|
|
2020-04-16 09:05:36 -06:00
|
|
|
def activity_title(%{"content" => content}, opts \\ %{}) do
|
2019-11-07 23:23:24 -07:00
|
|
|
content
|
2019-12-17 12:13:45 -07:00
|
|
|
|> Pleroma.Web.Metadata.Utils.scrub_html()
|
|
|
|
|> Pleroma.Emoji.Formatter.demojify()
|
2019-11-07 23:23:24 -07:00
|
|
|
|> Formatter.truncate(opts[:max_length], opts[:omission])
|
|
|
|
|> escape()
|
2019-10-07 06:20:41 -06:00
|
|
|
end
|
|
|
|
|
2020-04-16 09:05:36 -06:00
|
|
|
def activity_content(%{"content" => content}) do
|
2019-10-07 06:20:41 -06:00
|
|
|
content
|
|
|
|
|> String.replace(~r/[\n\r]/, "")
|
|
|
|
|> escape()
|
|
|
|
end
|
|
|
|
|
2019-12-17 12:13:45 -07:00
|
|
|
def activity_content(_), do: ""
|
|
|
|
|
2020-11-10 00:44:22 -07:00
|
|
|
def activity_context(activity), do: escape(activity.data["context"])
|
2019-10-07 06:20:41 -06:00
|
|
|
|
|
|
|
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
|