1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Conversation.Participation do
8 alias Pleroma.Conversation
12 schema "conversation_participations" do
13 belongs_to(:user, User, type: Pleroma.FlakeId)
14 belongs_to(:conversation, Conversation)
15 field(:read, :boolean, default: false)
20 def creation_cng(struct, params) do
22 |> cast(params, [:user_id, :conversation_id])
23 |> validate_required([:user_id, :conversation_id])
26 def create_for_user_and_conversation(user, conversation) do
28 |> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
30 on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
32 conflict_target: [:user_id, :conversation_id]
36 def read_cng(struct, params) do
38 |> cast(params, [:read])
39 |> validate_required([:read])
42 def mark_as_read(participation) do
44 |> read_cng(%{read: true})
48 def mark_as_unread(participation) do
50 |> read_cng(%{read: false})