1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Conversation.Participation do
7 alias Pleroma.Conversation
8 alias Pleroma.Conversation.Participation.RecipientShip
11 alias Pleroma.Web.ActivityPub.ActivityPub
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)
21 has_many(:recipient_ships, RecipientShip)
22 has_many(:recipients, through: [:recipient_ships, :user])
27 def creation_cng(struct, params) do
29 |> cast(params, [:user_id, :conversation_id, :read])
30 |> validate_required([:user_id, :conversation_id])
33 def create_for_user_and_conversation(user, conversation, opts \\ []) do
35 invisible_conversation = !!opts[:invisible_conversation]
38 if(invisible_conversation, do: [], else: [read: read])
39 |> Keyword.put(:updated_at, NaiveDateTime.utc_now())
44 conversation_id: conversation.id,
45 read: invisible_conversation || read
48 on_conflict: [set: update_on_conflict],
50 conflict_target: [:user_id, :conversation_id]
54 def read_cng(struct, params) do
56 |> cast(params, [:read])
57 |> validate_required([:read])
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)
66 def mark_as_read(participation) do
68 |> where(id: ^participation.id)
69 |> update(set: [read: true])
71 |> Repo.update_all([])
73 {1, [participation]} ->
74 participation = Repo.preload(participation, :user)
75 User.set_unread_conversation_count(participation.user)
83 def mark_all_as_read(%User{local: true} = user, %User{} = target_user) do
84 target_conversation_ids =
86 |> where([p], p.user_id == ^target_user.id)
87 |> select([p], p.conversation_id)
91 |> where([p], p.user_id == ^user.id)
92 |> where([p], p.conversation_id in ^target_conversation_ids)
93 |> update([p], set: [read: true])
94 |> Repo.update_all([])
96 {:ok, user} = User.set_unread_conversation_count(user)
100 def mark_all_as_read(%User{} = user, %User{}), do: {:ok, user, []}
102 def mark_all_as_read(%User{} = user) do
103 {_, participations} =
105 |> where([p], p.user_id == ^user.id)
106 |> where([p], not p.read)
107 |> update([p], set: [read: true])
109 |> Repo.update_all([])
111 {:ok, user} = User.set_unread_conversation_count(user)
112 {:ok, user, participations}
115 def mark_as_unread(participation) do
117 |> read_cng(%{read: false})
121 def for_user(user, params \\ %{}) do
122 from(p in __MODULE__,
123 where: p.user_id == ^user.id,
124 order_by: [desc: p.updated_at],
125 preload: [conversation: [:users]]
127 |> restrict_recipients(user, params)
128 |> Pleroma.Pagination.fetch_paginated(params)
131 def restrict_recipients(query, user, %{recipients: user_ids}) do
137 conversation_subquery =
139 |> group_by([p], p.conversation_id)
142 count(p.user_id) == ^length(user_binary_ids) and
143 fragment("array_agg(?) @> ?", p.user_id, ^user_binary_ids)
145 |> select([p], %{id: p.conversation_id})
148 |> join(:inner, [p], c in subquery(conversation_subquery), on: p.conversation_id == c.id)
151 def restrict_recipients(query, _, _), do: query
153 def for_user_and_conversation(user, conversation) do
154 from(p in __MODULE__,
155 where: p.user_id == ^user.id,
156 where: p.conversation_id == ^conversation.id
161 def for_user_with_last_activity_id(user, params \\ %{}) do
162 for_user(user, params)
163 |> Enum.map(fn participation ->
165 ActivityPub.fetch_latest_direct_activity_id_for_context(
166 participation.conversation.ap_id,
175 | last_activity_id: activity_id
178 |> Enum.reject(&is_nil(&1.last_activity_id))
182 def get(nil, _), do: nil
184 def get(id, params) do
186 if preload = params[:preload] do
187 from(p in __MODULE__,
197 def set_recipients(participation, user_ids) do
199 [participation.user_id | user_ids]
202 Repo.transaction(fn ->
204 from(r in RecipientShip,
205 where: r.participation_id == ^participation.id
208 Repo.delete_all(query)
212 where: u.id in ^user_ids
216 RecipientShip.create(users, participation)
220 {:ok, Repo.preload(participation, :recipients, force: true)}
223 def unread_conversation_count_for_user(user) do
224 from(p in __MODULE__,
225 where: p.user_id == ^user.id,
227 select: %{count: count(p.id)}