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
13 schema "conversation_participations" do
14 belongs_to(:user, User, type: Pleroma.FlakeId)
15 belongs_to(:conversation, Conversation)
16 field(:read, :boolean, default: false)
21 def creation_cng(struct, params) do
23 |> cast(params, [:user_id, :conversation_id])
24 |> validate_required([:user_id, :conversation_id])
27 def create_for_user_and_conversation(user, conversation) do
29 |> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
31 on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
33 conflict_target: [:user_id, :conversation_id]
37 def read_cng(struct, params) do
39 |> cast(params, [:read])
40 |> validate_required([:read])
43 def mark_as_read(participation) do
45 |> read_cng(%{read: true})
49 def mark_as_unread(participation) do
51 |> read_cng(%{read: false})
55 def for_user(user, params \\ %{}) do
57 where: p.user_id == ^user.id,
58 order_by: [desc: p.updated_at]
60 |> Pleroma.Pagination.fetch_paginated(params)