move to 2.5.5
[anni] / lib / pleroma / conversation / participation.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Conversation.Participation do
6   use Ecto.Schema
7   alias Pleroma.Conversation
8   alias Pleroma.Conversation.Participation.RecipientShip
9   alias Pleroma.Repo
10   alias Pleroma.User
11   alias Pleroma.Web.ActivityPub.ActivityPub
12   import Ecto.Changeset
13   import Ecto.Query
14
15   schema "conversation_participations" do
16     belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
17     belongs_to(:conversation, Conversation)
18     field(:read, :boolean, default: false)
19     field(:last_activity_id, FlakeId.Ecto.CompatType, virtual: true)
20
21     has_many(:recipient_ships, RecipientShip)
22     has_many(:recipients, through: [:recipient_ships, :user])
23
24     timestamps()
25   end
26
27   def creation_cng(struct, params) do
28     struct
29     |> cast(params, [:user_id, :conversation_id, :read])
30     |> validate_required([:user_id, :conversation_id])
31   end
32
33   def create_for_user_and_conversation(user, conversation, opts \\ []) do
34     read = !!opts[:read]
35     invisible_conversation = !!opts[:invisible_conversation]
36
37     update_on_conflict =
38       if(invisible_conversation, do: [], else: [read: read])
39       |> Keyword.put(:updated_at, NaiveDateTime.utc_now())
40
41     %__MODULE__{}
42     |> creation_cng(%{
43       user_id: user.id,
44       conversation_id: conversation.id,
45       read: invisible_conversation || read
46     })
47     |> Repo.insert(
48       on_conflict: [set: update_on_conflict],
49       returning: true,
50       conflict_target: [:user_id, :conversation_id]
51     )
52   end
53
54   def read_cng(struct, params) do
55     struct
56     |> cast(params, [:read])
57     |> validate_required([:read])
58   end
59
60   def mark_as_read(%User{} = user, %Conversation{} = conversation) do
61     with %__MODULE__{} = participation <- for_user_and_conversation(user, conversation) do
62       mark_as_read(participation)
63     end
64   end
65
66   def mark_as_read(%__MODULE__{} = participation) do
67     participation
68     |> change(read: true)
69     |> Repo.update()
70   end
71
72   def mark_all_as_read(%User{local: true} = user, %User{} = target_user) do
73     target_conversation_ids =
74       __MODULE__
75       |> where([p], p.user_id == ^target_user.id)
76       |> select([p], p.conversation_id)
77       |> Repo.all()
78
79     __MODULE__
80     |> where([p], p.user_id == ^user.id)
81     |> where([p], p.conversation_id in ^target_conversation_ids)
82     |> update([p], set: [read: true])
83     |> Repo.update_all([])
84
85     {:ok, user, []}
86   end
87
88   def mark_all_as_read(%User{} = user, %User{}), do: {:ok, user, []}
89
90   def mark_all_as_read(%User{} = user) do
91     {_, participations} =
92       __MODULE__
93       |> where([p], p.user_id == ^user.id)
94       |> where([p], not p.read)
95       |> update([p], set: [read: true])
96       |> select([p], p)
97       |> Repo.update_all([])
98
99     {:ok, user, participations}
100   end
101
102   def mark_as_unread(participation) do
103     participation
104     |> read_cng(%{read: false})
105     |> Repo.update()
106   end
107
108   def for_user(user, params \\ %{}) do
109     from(p in __MODULE__,
110       where: p.user_id == ^user.id,
111       order_by: [desc: p.updated_at],
112       preload: [conversation: [:users]]
113     )
114     |> restrict_recipients(user, params)
115     |> Pleroma.Pagination.fetch_paginated(params)
116   end
117
118   def restrict_recipients(query, user, %{recipients: user_ids}) do
119     user_binary_ids =
120       [user.id | user_ids]
121       |> Enum.uniq()
122       |> User.binary_id()
123
124     conversation_subquery =
125       __MODULE__
126       |> group_by([p], p.conversation_id)
127       |> having(
128         [p],
129         count(p.user_id) == ^length(user_binary_ids) and
130           fragment("array_agg(?) @> ?", p.user_id, ^user_binary_ids)
131       )
132       |> select([p], %{id: p.conversation_id})
133
134     query
135     |> join(:inner, [p], c in subquery(conversation_subquery), on: p.conversation_id == c.id)
136   end
137
138   def restrict_recipients(query, _, _), do: query
139
140   def for_user_and_conversation(user, conversation) do
141     from(p in __MODULE__,
142       where: p.user_id == ^user.id,
143       where: p.conversation_id == ^conversation.id
144     )
145     |> Repo.one()
146   end
147
148   def for_user_with_last_activity_id(user, params \\ %{}) do
149     for_user(user, params)
150     |> Enum.map(fn participation ->
151       activity_id =
152         ActivityPub.fetch_latest_direct_activity_id_for_context(
153           participation.conversation.ap_id,
154           %{
155             user: user,
156             blocking_user: user
157           }
158         )
159
160       %{
161         participation
162         | last_activity_id: activity_id
163       }
164     end)
165     |> Enum.reject(&is_nil(&1.last_activity_id))
166   end
167
168   def get(_, _ \\ [])
169   def get(nil, _), do: nil
170
171   def get(id, params) do
172     query =
173       if preload = params[:preload] do
174         from(p in __MODULE__,
175           preload: ^preload
176         )
177       else
178         __MODULE__
179       end
180
181     Repo.get(query, id)
182   end
183
184   def set_recipients(participation, user_ids) do
185     user_ids =
186       [participation.user_id | user_ids]
187       |> Enum.uniq()
188
189     Repo.transaction(fn ->
190       query =
191         from(r in RecipientShip,
192           where: r.participation_id == ^participation.id
193         )
194
195       Repo.delete_all(query)
196
197       users =
198         from(u in User,
199           where: u.id in ^user_ids
200         )
201         |> Repo.all()
202
203       RecipientShip.create(users, participation)
204       :ok
205     end)
206
207     {:ok, Repo.preload(participation, :recipients, force: true)}
208   end
209
210   @spec unread_count(User.t()) :: integer()
211   def unread_count(%User{id: user_id}) do
212     from(q in __MODULE__, where: q.user_id == ^user_id and q.read == false)
213     |> Repo.aggregate(:count, :id)
214   end
215
216   def unread_conversation_count_for_user(user) do
217     from(p in __MODULE__,
218       where: p.user_id == ^user.id,
219       where: not p.read,
220       select: %{count: count(p.id)}
221     )
222   end
223
224   def delete(%__MODULE__{} = participation) do
225     Repo.delete(participation)
226   end
227 end