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
7 alias Pleroma.Conversation
10 alias Pleroma.Web.ActivityPub.ActivityPub
14 schema "conversation_participations" do
15 belongs_to(:user, User, type: Pleroma.FlakeId)
16 belongs_to(:conversation, Conversation)
17 field(:read, :boolean, default: false)
18 field(:last_activity_id, Pleroma.FlakeId, virtual: true)
23 def creation_cng(struct, params) do
25 |> cast(params, [:user_id, :conversation_id])
26 |> validate_required([:user_id, :conversation_id])
29 def create_for_user_and_conversation(user, conversation) do
31 |> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
33 on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
35 conflict_target: [:user_id, :conversation_id]
39 def read_cng(struct, params) do
41 |> cast(params, [:read])
42 |> validate_required([:read])
45 def mark_as_read(participation) do
47 |> read_cng(%{read: true})
51 def mark_as_unread(participation) do
53 |> read_cng(%{read: false})
57 def for_user(user, params \\ %{}) do
59 where: p.user_id == ^user.id,
60 order_by: [desc: p.updated_at]
62 |> Pleroma.Pagination.fetch_paginated(params)
63 |> Repo.preload(conversation: [:users])
66 def for_user_with_last_activity_id(user, params \\ %{}) do
67 for_user(user, params)
68 |> Enum.map(fn participation ->
70 ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
72 "blocking_user" => user
77 | last_activity_id: activity_id