X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Flist.ex;h=c572380c2325b90e0dd58e26a7e71789401c67bc;hb=b40b10b53d00d13f24b5667acc02b1642abc6ec4;hp=2c799bc334a2042b4ff20950205ec490aebcefaa;hpb=89fbed88212657e3dcd4bbcb2c0718b07802037f;p=akkoma
diff --git a/lib/pleroma/list.ex b/lib/pleroma/list.ex
index 2c799bc33..c572380c2 100644
--- a/lib/pleroma/list.ex
+++ b/lib/pleroma/list.ex
@@ -1,16 +1,22 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors
+# Copyright © 2017-2019 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.List do
use Ecto.Schema
- import Ecto.{Changeset, Query}
- alias Pleroma.{User, Repo, Activity}
+
+ import Ecto.Query
+ import Ecto.Changeset
+
+ alias Pleroma.Activity
+ alias Pleroma.Repo
+ alias Pleroma.User
schema "lists" do
- belongs_to(:user, Pleroma.User)
+ belongs_to(:user, User, type: Pleroma.FlakeId)
field(:title, :string)
field(:following, {:array, :string}, default: [])
+ field(:ap_id, :string)
timestamps()
end
@@ -50,6 +56,10 @@ defmodule Pleroma.List do
Repo.one(query)
end
+ def get_by_ap_id(ap_id) do
+ Repo.get_by(__MODULE__, ap_id: ap_id)
+ end
+
def get_following(%Pleroma.List{following: following} = _list) do
q =
from(
@@ -75,7 +85,7 @@ defmodule Pleroma.List do
# Get lists to which the account belongs.
def get_lists_account_belongs(%User{} = owner, account_id) do
- user = Repo.get(User, account_id)
+ user = User.get_cached_by_id(account_id)
query =
from(
@@ -99,8 +109,19 @@ defmodule Pleroma.List do
end
def create(title, %User{} = creator) do
- list = %Pleroma.List{user_id: creator.id, title: title}
- Repo.insert(list)
+ changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
+
+ if changeset.valid? do
+ Repo.transaction(fn ->
+ list = Repo.insert!(changeset)
+
+ 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
@@ -120,4 +141,19 @@ defmodule Pleroma.List do
|> follow_changeset(attrs)
|> Repo.update()
end
+
+ def memberships(%User{follower_address: follower_address}) do
+ Pleroma.List
+ |> where([l], ^follower_address in l.following)
+ |> select([l], l.ap_id)
+ |> Repo.all()
+ 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