Put correct migration
[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
36 %__MODULE__{}
37 |> creation_cng(%{user_id: user.id, conversation_id: conversation.id, read: read})
38 |> Repo.insert(
39 on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
40 returning: true,
41 conflict_target: [:user_id, :conversation_id]
42 )
43 end
44
45 def read_cng(struct, params) do
46 struct
47 |> cast(params, [:read])
48 |> validate_required([:read])
49 end
50
51 def mark_as_read(%User{} = user, %Conversation{} = conversation) do
52 with %__MODULE__{} = participation <- for_user_and_conversation(user, conversation) do
53 mark_as_read(participation)
54 end
55 end
56
57 def mark_as_read(participation) do
58 participation
59 |> read_cng(%{read: true})
60 |> Repo.update()
61 |> case do
62 {:ok, participation} ->
63 participation = Repo.preload(participation, :user)
64 User.set_unread_conversation_count(participation.user)
65 {:ok, participation}
66
67 error ->
68 error
69 end
70 end
71
72 def mark_all_as_read(user) do
73 {_, participations} =
74 __MODULE__
75 |> where([p], p.user_id == ^user.id)
76 |> where([p], not p.read)
77 |> update([p], set: [read: true])
78 |> select([p], p)
79 |> Repo.update_all([])
80
81 User.set_unread_conversation_count(user)
82 {:ok, participations}
83 end
84
85 def mark_as_unread(participation) do
86 participation
87 |> read_cng(%{read: false})
88 |> Repo.update()
89 end
90
91 def for_user(user, params \\ %{}) do
92 from(p in __MODULE__,
93 where: p.user_id == ^user.id,
94 order_by: [desc: p.updated_at],
95 preload: [conversation: [:users]]
96 )
97 |> Pleroma.Pagination.fetch_paginated(params)
98 end
99
100 def for_user_and_conversation(user, conversation) do
101 from(p in __MODULE__,
102 where: p.user_id == ^user.id,
103 where: p.conversation_id == ^conversation.id
104 )
105 |> Repo.one()
106 end
107
108 def for_user_with_last_activity_id(user, params \\ %{}) do
109 for_user(user, params)
110 |> Enum.map(fn participation ->
111 activity_id =
112 ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
113 "user" => user,
114 "blocking_user" => user
115 })
116
117 %{
118 participation
119 | last_activity_id: activity_id
120 }
121 end)
122 |> Enum.filter(& &1.last_activity_id)
123 end
124
125 def get(_, _ \\ [])
126 def get(nil, _), do: nil
127
128 def get(id, params) do
129 query =
130 if preload = params[:preload] do
131 from(p in __MODULE__,
132 preload: ^preload
133 )
134 else
135 __MODULE__
136 end
137
138 Repo.get(query, id)
139 end
140
141 def set_recipients(participation, user_ids) do
142 user_ids =
143 [participation.user_id | user_ids]
144 |> Enum.uniq()
145
146 Repo.transaction(fn ->
147 query =
148 from(r in RecipientShip,
149 where: r.participation_id == ^participation.id
150 )
151
152 Repo.delete_all(query)
153
154 users =
155 from(u in User,
156 where: u.id in ^user_ids
157 )
158 |> Repo.all()
159
160 RecipientShip.create(users, participation)
161 :ok
162 end)
163
164 {:ok, Repo.preload(participation, :recipients, force: true)}
165 end
166
167 def unread_conversation_count_for_user(user) do
168 from(p in __MODULE__,
169 where: p.user_id == ^user.id,
170 where: not p.read,
171 select: %{count: count(p.id)}
172 )
173 end
174 end