Nodeinfo: remove null features; relay feature.
[akkoma] / lib / pleroma / user.ex
index 24bc808945d02c4619cc1771b5c3bed43604471d..a290db04adb91135c321c6ffec2a70e17da9c192 100644 (file)
@@ -2,6 +2,7 @@ defmodule Pleroma.User do
   use Ecto.Schema
 
   import Ecto.{Changeset, Query}
+  alias Ecto.Multi
   alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
   alias Comeonin.Pbkdf2
   alias Pleroma.Formatter
@@ -63,10 +64,6 @@ defmodule Pleroma.User do
     |> validate_required([:following])
   end
 
-  def info_changeset(struct, params \\ %{}) do
-    raise "NOT VALID ANYMORE"
-  end
-
   def user_info(%User{} = user) do
     oneself = if user.local, do: 1, else: 0
 
@@ -821,45 +818,40 @@ defmodule Pleroma.User do
     CommonUtils.format_input(bio, mentions, tags, "text/plain") |> Formatter.emojify(emoji)
   end
 
-  def tag(user_identifiers, tags), do: tag_or_untag(user_identifiers, tags, :tag)
+  def tag(user_identifiers, tags) when is_list(user_identifiers) do
+    Repo.transaction(fn ->
+      for user_identifier <- user_identifiers, do: tag(user_identifier, tags)
+    end)
+  end
 
-  def untag(user_identifiers, tags), do: tag_or_untag(user_identifiers, tags, :untag)
+  def untag(user_identifiers, tags) when is_list(user_identifiers) do
+    Repo.transaction(fn ->
+      for user_identifier <- user_identifiers, do: untag(user_identifier, tags)
+    end)
+  end
 
-  defp tag_or_untag(user_identifier, tags, action) when not is_list(user_identifier),
-    do: tag_or_untag([user_identifier], tags, action)
+  def tag(nickname, tags) when is_binary(nickname), do: tag(User.get_by_nickname(nickname), tags)
 
-  defp tag_or_untag([hd | _] = nicknames, tags, action) when is_binary(hd) do
-    users = Repo.all(from(u in User, where: u.nickname in ^nicknames))
+  def untag(nickname, tags) when is_binary(nickname),
+    do: untag(User.get_by_nickname(nickname), tags)
 
-    if length(users) == length(nicknames) do
-      tag_or_untag(users, tags, action)
-    else
-      {:error, :not_found}
-    end
-  end
+  def tag(%User{} = user, tags),
+    do: update_tags(user, Enum.uniq(user.tags ++ normalize_tags(tags)))
 
-  defp tag_or_untag([hd | _] = users, tags, action) when is_map(hd) do
-    tags =
-      [tags]
-      |> List.flatten()
-      |> Enum.map(&String.downcase(&1))
+  def untag(%User{} = user, tags), do: update_tags(user, user.tags -- normalize_tags(tags))
 
-    Repo.transaction(fn ->
-      for user <- users do
-        new_tags =
-          if action == :tag do
-            Enum.uniq(user.tags ++ tags)
-          else
-            user.tags -- tags
-          end
+  defp update_tags(%User{} = user, new_tags) do
+    {:ok, updated_user} =
+      user
+      |> change(%{tags: new_tags})
+      |> Repo.update()
 
-        {:ok, updated_user} =
-          user
-          |> change(%{tags: new_tags})
-          |> Repo.update()
+    updated_user
+  end
 
-        updated_user
-      end
-    end)
+  defp normalize_tags(tags) do
+    [tags]
+    |> List.flatten()
+    |> Enum.map(&String.downcase(&1))
   end
 end