X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Flist.ex;h=89aa7b5d48a04252e1f1fe163740ba1f0bb2baa0;hb=9460983032257022ff29c063901f6b714e4fbf59;hp=16955b3b559bf3f34bd339941a1267be58a814bc;hpb=9ce928d8238877f870e099cb2fe010d322717856;p=akkoma
diff --git a/lib/pleroma/list.ex b/lib/pleroma/list.ex
index 16955b3b5..89aa7b5d4 100644
--- a/lib/pleroma/list.ex
+++ b/lib/pleroma/list.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.List do
@@ -13,7 +13,7 @@ defmodule Pleroma.List do
alias Pleroma.User
schema "lists" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:title, :string)
field(:following, {:array, :string}, default: [])
field(:ap_id, :string)
@@ -84,22 +84,11 @@ defmodule Pleroma.List do
end
# Get lists to which the account belongs.
- def get_lists_account_belongs(%User{} = owner, account_id) do
- user = User.get_cached_by_id(account_id)
-
- query =
- from(
- l in Pleroma.List,
- where:
- l.user_id == ^owner.id and
- fragment(
- "? = ANY(?)",
- ^user.follower_address,
- l.following
- )
- )
-
- Repo.all(query)
+ def get_lists_account_belongs(%User{} = owner, user) do
+ Pleroma.List
+ |> where([l], l.user_id == ^owner.id)
+ |> where([l], fragment("? = ANY(?)", ^user.follower_address, l.following))
+ |> Repo.all()
end
def rename(%Pleroma.List{} = list, title) do
@@ -109,15 +98,19 @@ defmodule Pleroma.List do
end
def create(title, %User{} = creator) do
- list = %Pleroma.List{user_id: creator.id, title: title}
+ changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
- Repo.transaction(fn ->
- list = Repo.insert!(list)
+ if changeset.valid? do
+ Repo.transaction(fn ->
+ list = Repo.insert!(changeset)
- list
- |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
- |> Repo.update!()
- end)
+ list
+ |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
+ |> Repo.update!()
+ end)
+ else
+ {:error, changeset}
+ end
end
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
@@ -146,4 +139,10 @@ defmodule Pleroma.List do
end
def memberships(_), do: []
+
+ def member?(%Pleroma.List{following: following}, %User{follower_address: follower_address}) do
+ Enum.member?(following, follower_address)
+ end
+
+ def member?(_, _), do: false
end