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 tags
end end
defp hashtags_to_topics(%{data: %{"tag" => tags}}) do defp hashtags_to_topics(object) do
tags object
|> Enum.filter(&is_bitstring(&1)) |> Object.hashtags()
|> Enum.map(fn tag -> "hashtag:" <> tag end) |> Enum.map(fn hashtag -> "hashtag:" <> hashtag end)
end end
defp hashtags_to_topics(_), do: []
defp remote_topics(%{local: true}), do: [] defp remote_topics(%{local: true}), do: []
defp remote_topics(%{actor: actor}) when is_binary(actor), defp remote_topics(%{actor: actor}) when is_binary(actor),

View file

@ -47,16 +47,32 @@ defmodule Pleroma.Object do
end end
def create(data) do def create(data) do
Object.change(%Object{}, %{data: data}) %Object{}
|> Object.change(%{data: data})
|> Repo.insert() |> Repo.insert()
end end
def change(struct, params \\ %{}) do def change(struct, params \\ %{}) do
changeset =
struct struct
|> cast(params, [:data]) |> cast(params, [:data])
|> validate_required([:data]) |> validate_required([:data])
|> unique_constraint(:ap_id, name: :objects_unique_apid_index) |> 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
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(nil), do: nil
def get_by_id(id), do: Repo.get(Object, id) def get_by_id(id), do: Repo.get(Object, id)
@ -344,4 +360,23 @@ 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 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 end

View file

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

View file

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

View file

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

View file

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

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

View file

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

View file

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

View file

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

View file

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

View file

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