1 defmodule Pleroma.List do
3 import Ecto.{Changeset, Query}
4 alias Pleroma.{User, Repo, Activity}
7 belongs_to(:user, Pleroma.User)
9 field(:following, {:array, :string}, default: [])
14 def title_changeset(list, attrs \\ %{}) do
16 |> cast(attrs, [:title])
17 |> validate_required([:title])
20 def follow_changeset(list, attrs \\ %{}) do
22 |> cast(attrs, [:following])
23 |> validate_required([:following])
26 def for_user(user, opts) do
30 where: l.user_id == ^user.id,
31 order_by: [desc: l.id],
38 def get(id, %{id: user_id} = _user) do
43 where: l.user_id == ^user_id
49 def get_following(%Pleroma.List{following: following} = list) do
53 where: u.follower_address in ^following
59 # Get lists the activity should be streamed to.
60 def get_lists_from_activity(%Activity{actor: ap_id}) do
61 actor = User.get_cached_by_ap_id(ap_id)
66 where: fragment("? && ?", l.following, ^[actor.follower_address])
72 def rename(%Pleroma.List{} = list, title) do
74 |> title_changeset(%{title: title})
78 def create(title, %User{} = creator) do
79 list = %Pleroma.List{user_id: creator.id, title: title}
83 def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
84 update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
87 def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do
88 update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
91 def delete(%Pleroma.List{} = list) do
95 def update_follows(%Pleroma.List{} = list, attrs) do
97 |> follow_changeset(attrs)