Encapsulation of tags / hashtags fetching from objects.

This commit is contained in:
Ivan Tashkinov 2020-12-21 22:54:26 +03:00
parent 8e1f32e715
commit ee221277b0
12 changed files with 78 additions and 39 deletions

View file

@ -48,14 +48,12 @@ defmodule Pleroma.Activity.Ir.Topics do
tags
end
defp hashtags_to_topics(%{data: %{"tag" => tags}}) do
tags
|> Enum.filter(&is_bitstring(&1))
|> Enum.map(fn tag -> "hashtag:" <> tag end)
defp hashtags_to_topics(object) do
object
|> Object.hashtags()
|> Enum.map(fn hashtag -> "hashtag:" <> hashtag end)
end
defp hashtags_to_topics(_), do: []
defp remote_topics(%{local: true}), do: []
defp remote_topics(%{actor: actor}) when is_binary(actor),

View file

@ -47,17 +47,33 @@ defmodule Pleroma.Object do
end
def create(data) do
Object.change(%Object{}, %{data: data})
%Object{}
|> Object.change(%{data: data})
|> Repo.insert()
end
def change(struct, params \\ %{}) do
struct
|> cast(params, [:data])
|> validate_required([:data])
|> unique_constraint(:ap_id, name: :objects_unique_apid_index)
changeset =
struct
|> cast(params, [:data])
|> validate_required([:data])
|> unique_constraint(:ap_id, name: :objects_unique_apid_index)
if hashtags_changed?(struct, get_change(changeset, :data)) do
# TODO: modify assoc once it's introduced
changeset
else
changeset
end
end
defp hashtags_changed?(%Object{} = struct, %{"tag" => _} = data) do
Enum.sort(embedded_hashtags(struct)) !=
Enum.sort(object_data_hashtags(data))
end
defp hashtags_changed?(_, _), do: false
def get_by_id(nil), do: nil
def get_by_id(id), do: Repo.get(Object, id)
@ -344,4 +360,23 @@ defmodule Pleroma.Object do
def self_replies(object, opts \\ []),
do: replies(object, Keyword.put(opts, :self_only, true))
def tags(%Object{data: %{"tag" => tags}}) when is_list(tags), do: tags
def tags(_), do: []
def hashtags(object), do: embedded_hashtags(object)
defp embedded_hashtags(%Object{data: data}) do
object_data_hashtags(data)
end
defp embedded_hashtags(_), do: []
defp object_data_hashtags(%{"tag" => tags}) when is_list(tags) do
# Note: AS2 map-type elements are ignored
Enum.filter(tags, &is_bitstring(&1))
end
defp object_data_hashtags(_), do: []
end

View file

@ -74,9 +74,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
object =
if MRF.subdomain_match?(media_nsfw, actor_host) do
tags = (child_object["tag"] || []) ++ ["nsfw"]
child_object = Map.put(child_object, "tag", tags)
child_object = Map.put(child_object, "sensitive", true)
child_object =
child_object
|> Map.put("tag", (child_object["tag"] || []) ++ ["nsfw"])
|> Map.put("sensitive", true)
Map.put(object, "object", child_object)
else
object

View file

@ -32,18 +32,18 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
"""
def fix_object(object, options \\ []) do
object
|> strip_internal_fields
|> fix_actor
|> fix_url
|> fix_attachments
|> fix_context
|> strip_internal_fields()
|> fix_actor()
|> fix_url()
|> fix_attachments()
|> fix_context()
|> fix_in_reply_to(options)
|> fix_emoji
|> fix_tag
|> set_sensitive
|> fix_content_map
|> fix_addressing
|> fix_summary
|> fix_emoji()
|> fix_tag()
|> set_sensitive()
|> fix_content_map()
|> fix_addressing()
|> fix_summary()
|> fix_type(options)
end
@ -315,10 +315,9 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
tags =
tag
|> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
|> Enum.map(fn %{"name" => name} ->
name
|> String.slice(1..-1)
|> String.downcase()
|> Enum.map(fn
%{"name" => "#" <> hashtag} -> String.downcase(hashtag)
%{"name" => hashtag} -> String.downcase(hashtag)
end)
Map.put(object, "tag", tag ++ tags)

View file

@ -32,6 +32,7 @@ defmodule Pleroma.Web.Feed.FeedView do
%{
activity: activity,
object: object,
data: Map.get(object, :data),
actor: actor
}

View file

@ -201,8 +201,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
like_count = object.data["like_count"] || 0
announcement_count = object.data["announcement_count"] || 0
tags = object.data["tag"] || []
sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
hashtags = Object.hashtags(object)
sensitive = object.data["sensitive"] || Enum.member?(hashtags, "nsfw")
tags = Object.tags(object)
tag_mentions =
tags

View file

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

View file

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

View file

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

View file

@ -39,7 +39,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
object = Object.normalize(data["object"])
assert "test" in object.data["tag"]
assert "test" in Object.tags(object)
assert Object.hashtags(object) == ["test"]
end
test "it cleans up incoming notices which are not really DMs" do
@ -220,7 +221,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
object = Object.normalize(data["object"])
assert Enum.at(object.data["tag"], 2) == "moo"
assert Enum.at(Object.tags(object), 2) == "moo"
assert Object.hashtags(object) == ["moo"]
end
test "it works for incoming notices with contentMap" do

View file

@ -493,7 +493,7 @@ defmodule Pleroma.Web.CommonAPITest do
object = Object.normalize(activity)
assert object.data["tag"] == ["2hu"]
assert Object.tags(object) == ["2hu"]
end
test "it adds emoji in the object" do

View file

@ -262,8 +262,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
mentions: [],
tags: [
%{
name: "#{object_data["tag"]}",
url: "/tag/#{object_data["tag"]}"
name: "#{hd(object_data["tag"])}",
url: "/tag/#{hd(object_data["tag"])}"
}
],
application: %{