ab59a529e61f2b6824247553acbd21d790fb8dbc
[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 end
31
32 def read_cng(struct, params) do
33 struct
34 |> cast(params, [:read])
35 |> validate_required([:read])
36 end
37
38 def mark_as_read(participation) do
39 participation
40 |> read_cng(%{read: true})
41 |> Repo.update()
42 end
43
44 def mark_as_unread(participation) do
45 participation
46 |> read_cng(%{read: false})
47 |> Repo.update()
48 end
49 end