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 \\ []), def self_replies(object, opts \\ []),
do: replies(object, Keyword.put(opts, :self_only, true)) 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 end

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.FollowingRelationship alias Pleroma.FollowingRelationship
alias Pleroma.Object
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.MRF 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 if MRF.subdomain_match?(media_nsfw, actor_host) do
child_object = child_object =
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("sensitive", true)
Map.put(object, "object", child_object) 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 maybe_fix_object_url(data), do: data
def add_hashtags(object) do def add_hashtags(object) do
tags = hashtags =
((object["hashtags"] || []) ++ (object["tag"] || [])) object
|> Enum.map(fn |> Object.hashtags()
# Expand internal representation tags into AS2 tags. |> Enum.map(fn tag ->
tag when is_binary(tag) -> %{
%{ "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}",
"name" => "##{tag}", "type" => "Hashtag"
"type" => "Hashtag" }
}
# Do not process tags which are already AS2 tag objects.
tag when is_map(tag) ->
tag
end) end)
Map.put(object, "tag", tags) Map.put(object, "tag", hashtags ++ (object["tag"] || []))
end end
# TODO These should be added on our side on insertion, it doesn't make much # 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, media_attachments: attachments,
poll: render(PollView, "show.json", object: object, for: opts[:for]), poll: render(PollView, "show.json", object: object, for: opts[:for]),
mentions: mentions, mentions: mentions,
tags: build_tags(object.data["hashtags"] || tags), tags: build_tags(Object.hashtags(object.data)),
application: %{ application: %{
name: "Web", name: "Web",
website: nil website: nil

View file

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

View file

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

View file

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