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, :read])
26 |> validate_required([:user_id, :conversation_id])
29 def create_for_user_and_conversation(user, conversation, opts \\ []) do
33 |> creation_cng(%{user_id: user.id, conversation_id: conversation.id, read: read})
35 on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
37 conflict_target: [:user_id, :conversation_id]
41 def read_cng(struct, params) do
43 |> cast(params, [:read])
44 |> validate_required([:read])
47 def mark_as_read(participation) do
49 |> read_cng(%{read: true})
53 def mark_as_unread(participation) do
55 |> read_cng(%{read: false})
59 def for_user(user, params \\ %{}) do
61 where: p.user_id == ^user.id,
62 order_by: [desc: p.updated_at],
63 preload: [conversation: [:users]]
65 |> Pleroma.Pagination.fetch_paginated(params)
68 def for_user_with_last_activity_id(user, params \\ %{}) do
69 for_user(user, params)
70 |> Enum.map(fn participation ->
72 ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
74 "blocking_user" => user
79 | last_activity_id: activity_id
82 |> Enum.filter(& &1.last_activity_id)