a58d0ca0dea91b21fc4e4ed196ef3c799899332a
[akkoma] / lib / 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.User
8 alias Pleroma.Conversation
9 alias Pleroma.Repo
10 import Ecto.Changeset
11
12 schema "conversation_participations" do
13 belongs_to(:user, User, type: Pleroma.FlakeId)
14 belongs_to(:conversation, Conversation)
15 field(:read, :boolean, default: false)
16
17 timestamps()
18 end
19
20 def creation_cng(struct, params) do
21 struct
22 |> cast(params, [:user_id, :conversation_id])
23 |> validate_required([:user_id, :conversation_id])
24 end
25
26 def create_for_user_and_conversation(user, conversation) do
27 %__MODULE__{}
28 |> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
29 |> Repo.insert(
30 on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
31 returning: true,
32 conflict_target: [:user_id, :conversation_id]
33 )
34 end
35
36 def read_cng(struct, params) do
37 struct
38 |> cast(params, [:read])
39 |> validate_required([:read])
40 end
41
42 def mark_as_read(participation) do
43 participation
44 |> read_cng(%{read: true})
45 |> Repo.update()
46 end
47
48 def mark_as_unread(participation) do
49 participation
50 |> read_cng(%{read: false})
51 |> Repo.update()
52 end
53 end