Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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(participation) do
52 participation
53 |> read_cng(%{read: true})
54 |> Repo.update()
55 end
56
57 def mark_as_unread(participation) do
58 participation
59 |> read_cng(%{read: false})
60 |> Repo.update()
61 end
62
63 def for_user(user, params \\ %{}) do
64 from(p in __MODULE__,
65 where: p.user_id == ^user.id,
66 order_by: [desc: p.updated_at],
67 preload: [conversation: [:users]]
68 )
69 |> Pleroma.Pagination.fetch_paginated(params)
70 end
71
72 def for_user_and_conversation(user, conversation) do
73 from(p in __MODULE__,
74 where: p.user_id == ^user.id,
75 where: p.conversation_id == ^conversation.id
76 )
77 |> Repo.one()
78 end
79
80 def for_user_with_last_activity_id(user, params \\ %{}) do
81 for_user(user, params)
82 |> Enum.map(fn participation ->
83 activity_id =
84 ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
85 "user" => user,
86 "blocking_user" => user
87 })
88
89 %{
90 participation
91 | last_activity_id: activity_id
92 }
93 end)
94 |> Enum.filter(& &1.last_activity_id)
95 end
96
97 def get(_, _ \\ [])
98 def get(nil, _), do: nil
99
100 def get(id, params) do
101 query =
102 if preload = params[:preload] do
103 from(p in __MODULE__,
104 preload: ^preload
105 )
106 else
107 __MODULE__
108 end
109
110 Repo.get(query, id)
111 end
112
113 def set_recipients(participation, user_ids) do
114 user_ids =
115 [participation.user_id | user_ids]
116 |> Enum.uniq()
117
118 Repo.transaction(fn ->
119 query =
120 from(r in RecipientShip,
121 where: r.participation_id == ^participation.id
122 )
123
124 Repo.delete_all(query)
125
126 users =
127 from(u in User,
128 where: u.id in ^user_ids
129 )
130 |> Repo.all()
131
132 RecipientShip.create(users, participation)
133 :ok
134 end)
135
136 {:ok, Repo.preload(participation, :recipients, force: true)}
137 end
138 end