Merge remote-tracking branch 'remotes/upstream/develop' into 1335-user-api-id-fields...
[akkoma] / lib / pleroma / list.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.List do
6 use Ecto.Schema
7
8 import Ecto.Query
9 import Ecto.Changeset
10
11 alias Pleroma.Activity
12 alias Pleroma.Repo
13 alias Pleroma.User
14
15 schema "lists" do
16 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
17 field(:title, :string)
18 field(:following, {:array, :string}, default: [])
19 field(:ap_id, :string)
20
21 timestamps()
22 end
23
24 def title_changeset(list, attrs \\ %{}) do
25 list
26 |> cast(attrs, [:title])
27 |> validate_required([:title])
28 end
29
30 def follow_changeset(list, attrs \\ %{}) do
31 list
32 |> cast(attrs, [:following])
33 |> validate_required([:following])
34 end
35
36 def for_user(user, _opts) do
37 query =
38 from(
39 l in Pleroma.List,
40 where: l.user_id == ^user.id,
41 order_by: [desc: l.id],
42 limit: 50
43 )
44
45 Repo.all(query)
46 end
47
48 def get(id, %{id: user_id} = _user) do
49 query =
50 from(
51 l in Pleroma.List,
52 where: l.id == ^id,
53 where: l.user_id == ^user_id
54 )
55
56 Repo.one(query)
57 end
58
59 def get_by_ap_id(ap_id) do
60 Repo.get_by(__MODULE__, ap_id: ap_id)
61 end
62
63 def get_following(%Pleroma.List{following: following} = _list) do
64 q =
65 from(
66 u in User,
67 where: u.follower_address in ^following
68 )
69
70 {:ok, Repo.all(q)}
71 end
72
73 # Get lists the activity should be streamed to.
74 def get_lists_from_activity(%Activity{actor: ap_id}) do
75 actor = User.get_cached_by_ap_id(ap_id)
76
77 query =
78 from(
79 l in Pleroma.List,
80 where: fragment("? && ?", l.following, ^[actor.follower_address])
81 )
82
83 Repo.all(query)
84 end
85
86 # Get lists to which the account belongs.
87 def get_lists_account_belongs(%User{} = owner, user) do
88 Pleroma.List
89 |> where([l], l.user_id == ^owner.id)
90 |> where([l], fragment("? = ANY(?)", ^user.follower_address, l.following))
91 |> Repo.all()
92 end
93
94 def rename(%Pleroma.List{} = list, title) do
95 list
96 |> title_changeset(%{title: title})
97 |> Repo.update()
98 end
99
100 def create(title, %User{} = creator) do
101 changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
102
103 if changeset.valid? do
104 Repo.transaction(fn ->
105 list = Repo.insert!(changeset)
106
107 list
108 |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
109 |> Repo.update!()
110 end)
111 else
112 {:error, changeset}
113 end
114 end
115
116 def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
117 update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
118 end
119
120 def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do
121 update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
122 end
123
124 def delete(%Pleroma.List{} = list) do
125 Repo.delete(list)
126 end
127
128 def update_follows(%Pleroma.List{} = list, attrs) do
129 list
130 |> follow_changeset(attrs)
131 |> Repo.update()
132 end
133
134 def memberships(%User{follower_address: follower_address}) do
135 Pleroma.List
136 |> where([l], ^follower_address in l.following)
137 |> select([l], l.ap_id)
138 |> Repo.all()
139 end
140
141 def memberships(_), do: []
142
143 def member?(%Pleroma.List{following: following}, %User{follower_address: follower_address}) do
144 Enum.member?(following, follower_address)
145 end
146
147 def member?(_, _), do: false
148 end