2020-12-22 19:04:33 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Hashtag do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
2021-02-11 16:30:21 +00:00
|
|
|
import Ecto.Query
|
2020-12-22 19:04:33 +00:00
|
|
|
|
2021-02-11 16:30:21 +00:00
|
|
|
alias Ecto.Multi
|
2020-12-22 19:04:33 +00:00
|
|
|
alias Pleroma.Hashtag
|
2021-02-11 16:30:21 +00:00
|
|
|
alias Pleroma.Object
|
2020-12-22 19:04:33 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
schema "hashtags" do
|
|
|
|
field(:name, :string)
|
|
|
|
|
2021-02-11 16:30:21 +00:00
|
|
|
many_to_many(:objects, Object, join_through: "hashtags_objects", on_replace: :delete)
|
2020-12-22 19:04:33 +00:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_by_name(name) do
|
2021-02-15 18:13:14 +00:00
|
|
|
from(h in Hashtag)
|
|
|
|
|> where([h], fragment("name = ?::citext", ^String.downcase(name)))
|
|
|
|
|> Repo.one()
|
2020-12-22 19:04:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_or_create_by_name(name) when is_bitstring(name) do
|
|
|
|
with %Hashtag{} = hashtag <- get_by_name(name) do
|
|
|
|
{:ok, hashtag}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
%Hashtag{}
|
|
|
|
|> changeset(%{name: name})
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_or_create_by_names(names) when is_list(names) do
|
2021-02-15 18:13:14 +00:00
|
|
|
names = Enum.map(names, &String.downcase/1)
|
2021-02-11 16:30:21 +00:00
|
|
|
timestamp = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
|
|
|
|
|
|
|
|
structs =
|
|
|
|
Enum.map(names, fn name ->
|
|
|
|
%Hashtag{}
|
|
|
|
|> changeset(%{name: name})
|
|
|
|
|> Map.get(:changes)
|
|
|
|
|> Map.merge(%{inserted_at: timestamp, updated_at: timestamp})
|
|
|
|
end)
|
|
|
|
|
2021-02-13 19:01:11 +00:00
|
|
|
try do
|
|
|
|
with {:ok, %{query_op: hashtags}} <-
|
|
|
|
Multi.new()
|
|
|
|
|> Multi.insert_all(:insert_all_op, Hashtag, structs, on_conflict: :nothing)
|
|
|
|
|> Multi.run(:query_op, fn _repo, _changes ->
|
2021-02-15 18:13:14 +00:00
|
|
|
{:ok,
|
|
|
|
Repo.all(from(ht in Hashtag, where: ht.name in fragment("?::citext[]", ^names)))}
|
2021-02-13 19:01:11 +00:00
|
|
|
end)
|
|
|
|
|> Repo.transaction() do
|
|
|
|
{:ok, hashtags}
|
|
|
|
else
|
|
|
|
{:error, _name, value, _changes_so_far} -> {:error, value}
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
e -> {:error, e}
|
2021-02-11 16:30:21 +00:00
|
|
|
end
|
2020-12-22 19:04:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(%Hashtag{} = struct, params) do
|
|
|
|
struct
|
2021-02-07 19:24:12 +00:00
|
|
|
|> cast(params, [:name])
|
2020-12-22 19:04:33 +00:00
|
|
|
|> update_change(:name, &String.downcase/1)
|
|
|
|
|> validate_required([:name])
|
|
|
|
|> unique_constraint(:name)
|
|
|
|
end
|
2021-02-11 16:30:21 +00:00
|
|
|
|
|
|
|
def unlink(%Object{id: object_id}) do
|
|
|
|
with {_, hashtag_ids} <-
|
|
|
|
from(hto in "hashtags_objects",
|
|
|
|
where: hto.object_id == ^object_id,
|
|
|
|
select: hto.hashtag_id
|
|
|
|
)
|
2021-02-13 19:01:11 +00:00
|
|
|
|> Repo.delete_all(),
|
|
|
|
{:ok, unreferenced_count} <- delete_unreferenced(hashtag_ids) do
|
|
|
|
{:ok, length(hashtag_ids), unreferenced_count}
|
2021-02-11 16:30:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@delete_unreferenced_query """
|
|
|
|
DELETE FROM hashtags WHERE id IN
|
|
|
|
(SELECT hashtags.id FROM hashtags
|
|
|
|
LEFT OUTER JOIN hashtags_objects
|
|
|
|
ON hashtags_objects.hashtag_id = hashtags.id
|
|
|
|
WHERE hashtags_objects.hashtag_id IS NULL AND hashtags.id = ANY($1));
|
|
|
|
"""
|
|
|
|
|
|
|
|
def delete_unreferenced(ids) do
|
|
|
|
with {:ok, %{num_rows: deleted_count}} <- Repo.query(@delete_unreferenced_query, [ids]) do
|
|
|
|
{:ok, deleted_count}
|
|
|
|
end
|
|
|
|
end
|
2020-12-22 19:04:33 +00:00
|
|
|
end
|