added Tag schema

This commit is contained in:
Maksim Pechnikov 2020-10-14 09:29:23 +03:00 committed by Alexander Strizhakov
parent 35e8573483
commit 34d8ce945f
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
6 changed files with 72 additions and 17 deletions

37
lib/pleroma/tag.ex Normal file
View File

@ -0,0 +1,37 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Tag do
use Ecto.Schema
import Ecto.Query
alias Pleroma.Repo
alias Pleroma.Web.ActivityPub.MRF
@type t :: %__MODULE__{}
schema "tags" do
field(:name, :string)
timestamps()
end
@spec upsert(String.t()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def upsert(name) do
%__MODULE__{}
|> Ecto.Changeset.change(name: name)
|> Ecto.Changeset.unique_constraint(:name)
|> Repo.insert(on_conflict: :nothing, conflict_target: :name)
end
@spec list_tags() :: list(String.t())
def list_tags do
from(u in __MODULE__, select: u.name)
|> Repo.all()
|> Kernel.++(MRF.TagPolicy.policy_tags())
|> Enum.uniq()
|> Enum.sort()
end
end

View File

@ -1259,15 +1259,6 @@ defmodule Pleroma.User do
|> Repo.all() |> Repo.all()
end end
@spec list_tags() :: list(String.t())
def list_tags do
from(
u in __MODULE__,
select: type(fragment("DISTINCT unnest(?)", u.tags), :string)
)
|> Repo.all()
end
def increase_note_count(%User{} = user) do def increase_note_count(%User{} = user) do
User User
|> where(id: ^user.id) |> where(id: ^user.id)

View File

@ -25,11 +25,7 @@ defmodule Pleroma.Web.AdminAPI.TagController do
action_fallback(AdminAPI.FallbackController) action_fallback(AdminAPI.FallbackController)
def list(%{assigns: %{user: _admin}} = conn, _) do def list(%{assigns: %{user: _admin}} = conn, _) do
tags = tags = Pleroma.Tag.list_tags()
Pleroma.User.list_tags()
|> Kernel.++(Pleroma.Web.ActivityPub.MRF.TagPolicy.policy_tags())
|> Enum.uniq()
|> Enum.sort()
json(conn, tags) json(conn, tags)
end end

View File

@ -0,0 +1,27 @@
defmodule Pleroma.Repo.Migrations.CreateTags do
use Ecto.Migration
def up do
create_if_not_exists table(:tags) do
add(:name, :string, null: false)
timestamps()
end
create_if_not_exists(unique_index(:tags, :name))
flush()
execute(collect_user_tags_query())
end
def down do
drop_if_exists(table(:tags))
end
defp collect_user_tags_query do
"""
INSERT INTO tags(name, inserted_at, updated_at)
SELECT DISTINCT unnest(tags), now(), now() from users
ON CONFLICT DO NOTHING
"""
end
end

View File

@ -490,4 +490,8 @@ defmodule Pleroma.Factory do
context: ["home"] context: ["home"]
} }
end end
def tag_factory do
%Pleroma.Tag{name: "verify"}
end
end end

View File

@ -32,9 +32,9 @@ defmodule Pleroma.Web.AdminAPI.TagControllerTest do
describe "GET /api/pleroma/admin/users/tag" do describe "GET /api/pleroma/admin/users/tag" do
test "it returns user tags and mrf policy tags", %{conn: conn} do test "it returns user tags and mrf policy tags", %{conn: conn} do
insert(:user, %{tags: ["x"]}) insert(:tag, name: "x")
insert(:user, %{tags: ["y"]}) insert(:tag, name: "y")
insert(:user, %{tags: ["unchanged"]}) insert(:tag, name: "unchanged")
response = response =
conn conn