From 5484ef9958c4d810d22859c056f021aa3d48e723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 7 Jul 2023 19:56:06 +0200 Subject: [PATCH] WIP triple hashtags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/formatter.ex | 12 +++++++++--- lib/pleroma/hashtag.ex | 12 ++++++++++++ mix.exs | 1 + mix.lock | 2 +- ...20230707142049_add_preferred_name_to_hashtags.exs | 9 +++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 priv/repo/migrations/20230707142049_add_preferred_name_to_hashtags.exs diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 61ccfa7339..b4f5bfcd6f 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -15,8 +15,10 @@ defp linkify_opts do [ hashtag: true, hashtag_handler: &Pleroma.Formatter.hashtag_handler/4, - hashhashtag: true, - hashhashtag_handler: &Pleroma.Formatter.hashhashtag_handler/4, + doublehashtag: true, + doublehashtag_handler: &Pleroma.Formatter.doublehashtag_handler/4, + triplehashtag: true, + triplehashtag_handler: &Pleroma.Formatter.triplehashtag_handler/4, mention: true, mention_handler: &Pleroma.Formatter.mention_handler/4 ] @@ -80,7 +82,11 @@ def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do {link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}} end - def hashhashtag_handler("##" <> tag = tag_text, _buffer, _opts, acc) do + def doublehashtag_handler("##" <> tag = tag_text, _buffer, _opts, acc) do + {tag, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag |> String.downcase()})}} + end + + def triplehashtag_handler("###" <> tag = tag_text, _buffer, _opts, acc) do {tag, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag |> String.downcase()})}} end diff --git a/lib/pleroma/hashtag.ex b/lib/pleroma/hashtag.ex index a43d882209..c75061a793 100644 --- a/lib/pleroma/hashtag.ex +++ b/lib/pleroma/hashtag.ex @@ -15,6 +15,7 @@ defmodule Pleroma.Hashtag do schema "hashtags" do field(:name, :string) + field(:preferred_name, :string) many_to_many(:objects, Object, join_through: "hashtags_objects", on_replace: :delete) @@ -74,10 +75,21 @@ def changeset(%Hashtag{} = struct, params) do struct |> cast(params, [:name]) |> update_change(:name, &normalize_name/1) + |> put_preferred_name |> validate_required([:name]) |> unique_constraint(:name) end + defp put_preferred_name(changeset) do + name = + changeset + |> get_change(:name) + |> String.trim() + + changeset + |> put_change(:preferred_name, name) + end + def unlink(%Object{id: object_id}) do with {_, hashtag_ids} <- from(hto in "hashtags_objects", diff --git a/mix.exs b/mix.exs index b53c976eef..b228531a7a 100644 --- a/mix.exs +++ b/mix.exs @@ -163,6 +163,7 @@ defp deps do {:floki, "~> 0.35"}, {:timex, "~> 3.6"}, {:ueberauth, "~> 0.4"}, + {:linkify, git: "https://gitlab.com/mkljczk/linkify", branch: "master"}, {:linkify, "~> 0.5.3"}, {:http_signatures, "~> 0.1.2"}, {:telemetry, "~> 1.0.0", override: true}, diff --git a/mix.lock b/mix.lock index 8d1d7351de..a570058c87 100644 --- a/mix.lock +++ b/mix.lock @@ -77,7 +77,7 @@ "joken": {:hex, :joken, "2.6.0", "b9dd9b6d52e3e6fcb6c65e151ad38bf4bc286382b5b6f97079c47ade6b1bcc6a", [:mix], [{:jose, "~> 1.11.5", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "5a95b05a71cd0b54abd35378aeb1d487a23a52c324fa7efdffc512b655b5aaa7"}, "jose": {:hex, :jose, "1.11.6", "613fda82552128aa6fb804682e3a616f4bc15565a048dabd05b1ebd5827ed965", [:mix, :rebar3], [], "hexpm", "6275cb75504f9c1e60eeacb771adfeee4905a9e182103aa59b53fed651ff9738"}, "jumper": {:hex, :jumper, "1.0.2", "68cdcd84472a00ac596b4e6459a41b3062d4427cbd4f1e8c8793c5b54f1406a7", [:mix], [], "hexpm", "9b7782409021e01ab3c08270e26f36eb62976a38c1aa64b2eaf6348422f165e1"}, - "linkify": {:hex, :linkify, "0.5.3", "5f8143d8f61f5ff08d3aeeff47ef6509492b4948d8f08007fbf66e4d2246a7f2", [:mix], [], "hexpm", "3ef35a1377d47c25506e07c1c005ea9d38d700699d92ee92825f024434258177"}, + "linkify": {:git, "https://gitlab.com/mkljczk/linkify", "4f1ce6ea211fea247bd6bc00aa63273c018dbf3f", [branch: "master"]}, "majic": {:hex, :majic, "1.0.0", "37e50648db5f5c2ff0c9fb46454d034d11596c03683807b9fb3850676ffdaab3", [:make, :mix], [{:elixir_make, "~> 0.6.1", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "7905858f76650d49695f14ea55cd9aaaee0c6654fa391671d4cf305c275a0a9e"}, "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, "makeup_elixir": {:hex, :makeup_elixir, "0.14.1", "4f0e96847c63c17841d42c08107405a005a2680eb9c7ccadfd757bd31dabccfb", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f2438b1a80eaec9ede832b5c41cd4f373b38fd7aa33e3b22d9db79e640cbde11"}, diff --git a/priv/repo/migrations/20230707142049_add_preferred_name_to_hashtags.exs b/priv/repo/migrations/20230707142049_add_preferred_name_to_hashtags.exs new file mode 100644 index 0000000000..c78fc90c0b --- /dev/null +++ b/priv/repo/migrations/20230707142049_add_preferred_name_to_hashtags.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddPreferredNameToHashtags do + use Ecto.Migration + + def change do + alter table(:hashtags) do + add_if_not_exists(:preferred_name, :text) + end + end +end