1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.List do
7 import Ecto.{Changeset, Query}
8 alias Pleroma.{User, Repo, Activity}
11 belongs_to(:user, Pleroma.User)
12 field(:title, :string)
13 field(:following, {:array, :string}, default: [])
18 def title_changeset(list, attrs \\ %{}) do
20 |> cast(attrs, [:title])
21 |> validate_required([:title])
24 def follow_changeset(list, attrs \\ %{}) do
26 |> cast(attrs, [:following])
27 |> validate_required([:following])
30 def for_user(user, _opts) do
34 where: l.user_id == ^user.id,
35 order_by: [desc: l.id],
42 def get(id, %{id: user_id} = _user) do
47 where: l.user_id == ^user_id
53 def get_following(%Pleroma.List{following: following} = _list) do
57 where: u.follower_address in ^following
63 # Get lists the activity should be streamed to.
64 def get_lists_from_activity(%Activity{actor: ap_id}) do
65 actor = User.get_cached_by_ap_id(ap_id)
70 where: fragment("? && ?", l.following, ^[actor.follower_address])
76 # Get lists to which the account belongs.
77 def get_lists_account_belongs(%User{} = owner, account_id) do
78 user = Repo.get(User, account_id)
84 l.user_id == ^owner.id and
87 ^user.follower_address,
95 def rename(%Pleroma.List{} = list, title) do
97 |> title_changeset(%{title: title})
101 def create(title, %User{} = creator) do
102 list = %Pleroma.List{user_id: creator.id, title: title}
106 def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
107 update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
110 def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do
111 update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
114 def delete(%Pleroma.List{} = list) do
118 def update_follows(%Pleroma.List{} = list, attrs) do
120 |> follow_changeset(attrs)