Merge branch 'develop' into feature/reports-groups-and-multiple-state-update
[akkoma] / lib / pleroma / conversation / participation.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.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(participation) do
67 participation
68 |> read_cng(%{read: true})
69 |> Repo.update()
70 |> case do
71 {:ok, participation} ->
72 participation = Repo.preload(participation, :user)
73 User.set_unread_conversation_count(participation.user)
74 {:ok, participation}
75
76 error ->
77 error
78 end
79 end
80
81 def mark_all_as_read(%User{local: true} = user, %User{} = target_user) do
82 target_conversation_ids =
83 __MODULE__
84 |> where([p], p.user_id == ^target_user.id)
85 |> select([p], p.conversation_id)
86 |> Repo.all()
87
88 __MODULE__
89 |> where([p], p.user_id == ^user.id)
90 |> where([p], p.conversation_id in ^target_conversation_ids)
91 |> update([p], set: [read: true])
92 |> Repo.update_all([])
93
94 {:ok, user} = User.set_unread_conversation_count(user)
95 {:ok, user, []}
96 end
97
98 def mark_all_as_read(%User{} = user, %User{}), do: {:ok, user, []}
99
100 def mark_all_as_read(%User{} = user) do
101 {_, participations} =
102 __MODULE__
103 |> where([p], p.user_id == ^user.id)
104 |> where([p], not p.read)
105 |> update([p], set: [read: true])
106 |> select([p], p)
107 |> Repo.update_all([])
108
109 {:ok, user} = User.set_unread_conversation_count(user)
110 {:ok, user, participations}
111 end
112
113 def mark_as_unread(participation) do
114 participation
115 |> read_cng(%{read: false})
116 |> Repo.update()
117 end
118
119 def for_user(user, params \\ %{}) do
120 from(p in __MODULE__,
121 where: p.user_id == ^user.id,
122 order_by: [desc: p.updated_at],
123 preload: [conversation: [:users]]
124 )
125 |> Pleroma.Pagination.fetch_paginated(params)
126 end
127
128 def for_user_and_conversation(user, conversation) do
129 from(p in __MODULE__,
130 where: p.user_id == ^user.id,
131 where: p.conversation_id == ^conversation.id
132 )
133 |> Repo.one()
134 end
135
136 def for_user_with_last_activity_id(user, params \\ %{}) do
137 for_user(user, params)
138 |> Enum.map(fn participation ->
139 activity_id =
140 ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
141 "user" => user,
142 "blocking_user" => user
143 })
144
145 %{
146 participation
147 | last_activity_id: activity_id
148 }
149 end)
150 |> Enum.filter(& &1.last_activity_id)
151 end
152
153 def get(_, _ \\ [])
154 def get(nil, _), do: nil
155
156 def get(id, params) do
157 query =
158 if preload = params[:preload] do
159 from(p in __MODULE__,
160 preload: ^preload
161 )
162 else
163 __MODULE__
164 end
165
166 Repo.get(query, id)
167 end
168
169 def set_recipients(participation, user_ids) do
170 user_ids =
171 [participation.user_id | user_ids]
172 |> Enum.uniq()
173
174 Repo.transaction(fn ->
175 query =
176 from(r in RecipientShip,
177 where: r.participation_id == ^participation.id
178 )
179
180 Repo.delete_all(query)
181
182 users =
183 from(u in User,
184 where: u.id in ^user_ids
185 )
186 |> Repo.all()
187
188 RecipientShip.create(users, participation)
189 :ok
190 end)
191
192 {:ok, Repo.preload(participation, :recipients, force: true)}
193 end
194
195 def unread_conversation_count_for_user(user) do
196 from(p in __MODULE__,
197 where: p.user_id == ^user.id,
198 where: not p.read,
199 select: %{count: count(p.id)}
200 )
201 end
202 end