Create Object.hashtags/1 wrapper

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-12-22 03:54:57 +01:00
parent acb03d591b
commit 87b13c5430
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
7 changed files with 20 additions and 20 deletions

View File

@ -344,4 +344,8 @@ defmodule Pleroma.Object do
def self_replies(object, opts \\ []),
do: replies(object, Keyword.put(opts, :self_only, true))
def hashtags(%{"hashtags" => hashtags}), do: hashtags || []
def hashtags(%{"tag" => tags}), do: Enum.filter(tags, &is_bitstring(&1))
def hashtags(_), do: []
end

View File

@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
alias Pleroma.Config
alias Pleroma.FollowingRelationship
alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Web.ActivityPub.MRF
@ -76,7 +77,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
if MRF.subdomain_match?(media_nsfw, actor_host) do
child_object =
child_object
|> Map.put("hashtags", (child_object["hashtags"] || []) ++ ["nsfw"])
|> Map.put("hashtags", Object.hashtags(child_object) ++ ["nsfw"])
|> Map.put("sensitive", true)
Map.put(object, "object", child_object)

View File

@ -863,23 +863,18 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
def maybe_fix_object_url(data), do: data
def add_hashtags(object) do
tags =
((object["hashtags"] || []) ++ (object["tag"] || []))
|> Enum.map(fn
# Expand internal representation tags into AS2 tags.
tag when is_binary(tag) ->
%{
"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
"name" => "##{tag}",
"type" => "Hashtag"
}
# Do not process tags which are already AS2 tag objects.
tag when is_map(tag) ->
tag
hashtags =
object
|> Object.hashtags()
|> Enum.map(fn tag ->
%{
"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
"name" => "##{tag}",
"type" => "Hashtag"
}
end)
Map.put(object, "tag", tags)
Map.put(object, "tag", hashtags ++ (object["tag"] || []))
end
# TODO These should be added on our side on insertion, it doesn't make much

View File

@ -347,7 +347,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
media_attachments: attachments,
poll: render(PollView, "show.json", object: object, for: opts[:for]),
mentions: mentions,
tags: build_tags(object.data["hashtags"] || tags),
tags: build_tags(Object.hashtags(object.data)),
application: %{
name: "Web",
website: nil

View File

@ -22,7 +22,7 @@
<link type="text/html" href='<%= @data["external_url"] %>' rel="alternate"/>
<% end %>
<%= for hashtag <- @data["hashtags"] || [] do %>
<%= for hashtag <- Object.hashtags(@data) do %>
<category term="<%= hashtag %>"></category>
<% end %>

View File

@ -21,7 +21,7 @@
<link><%= @data["external_url"] %></link>
<% end %>
<%= for hashtag <- @data["hashtags"] || [] do %>
<%= for hashtag <- Object.hashtags(@data) do %>
<category term="<%= hashtag %>"></category>
<% end %>

View File

@ -41,7 +41,7 @@
<% end %>
<% end %>
<%= for hashtag <- @data["hashtags"] || [] do %>
<%= for hashtag <- Object.hashtags(@data) do %>
<category term="<%= hashtag %>"></category>
<% end %>