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
11 alias Pleroma.Activity
16 belongs_to(:user, User, type: Pleroma.FlakeId)
17 field(:title, :string)
18 field(:following, {:array, :string}, default: [])
23 def title_changeset(list, attrs \\ %{}) do
25 |> cast(attrs, [:title])
26 |> validate_required([:title])
29 def follow_changeset(list, attrs \\ %{}) do
31 |> cast(attrs, [:following])
32 |> validate_required([:following])
35 def for_user(user, _opts) do
39 where: l.user_id == ^user.id,
40 order_by: [desc: l.id],
47 def get(id, %{id: user_id} = _user) do
52 where: l.user_id == ^user_id
58 def get_following(%Pleroma.List{following: following} = _list) do
62 where: u.follower_address in ^following
68 # Get lists the activity should be streamed to.
69 def get_lists_from_activity(%Activity{actor: ap_id}) do
70 actor = User.get_cached_by_ap_id(ap_id)
75 where: fragment("? && ?", l.following, ^[actor.follower_address])
81 # Get lists to which the account belongs.
82 def get_lists_account_belongs(%User{} = owner, account_id) do
83 user = User.get_cached_by_id(account_id)
89 l.user_id == ^owner.id and
92 ^user.follower_address,
100 def rename(%Pleroma.List{} = list, title) do
102 |> title_changeset(%{title: title})
106 def create(title, %User{} = creator) do
107 list = %Pleroma.List{user_id: creator.id, title: title}
111 def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
112 update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
115 def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do
116 update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
119 def delete(%Pleroma.List{} = list) do
123 def update_follows(%Pleroma.List{} = list, attrs) do
125 |> follow_changeset(attrs)