2019-04-10 07:34:53 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Conversation.Participation do
|
|
|
|
use Ecto.Schema
|
|
|
|
alias Pleroma.Conversation
|
|
|
|
alias Pleroma.Repo
|
2019-04-10 16:17:22 +00:00
|
|
|
alias Pleroma.User
|
2019-04-10 15:28:02 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2019-04-10 07:34:53 +00:00
|
|
|
import Ecto.Changeset
|
2019-04-10 15:05:33 +00:00
|
|
|
import Ecto.Query
|
2019-04-10 07:34:53 +00:00
|
|
|
|
|
|
|
schema "conversation_participations" do
|
|
|
|
belongs_to(:user, User, type: Pleroma.FlakeId)
|
|
|
|
belongs_to(:conversation, Conversation)
|
|
|
|
field(:read, :boolean, default: false)
|
2019-04-10 15:28:02 +00:00
|
|
|
field(:last_activity_id, Pleroma.FlakeId, virtual: true)
|
2019-04-10 07:34:53 +00:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def creation_cng(struct, params) do
|
|
|
|
struct
|
|
|
|
|> cast(params, [:user_id, :conversation_id])
|
|
|
|
|> validate_required([:user_id, :conversation_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_for_user_and_conversation(user, conversation) do
|
|
|
|
%__MODULE__{}
|
|
|
|
|> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
|
2019-04-10 14:33:45 +00:00
|
|
|
|> Repo.insert(
|
|
|
|
on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
|
|
|
|
returning: true,
|
|
|
|
conflict_target: [:user_id, :conversation_id]
|
|
|
|
)
|
2019-04-10 07:34:53 +00:00
|
|
|
end
|
2019-04-10 13:12:01 +00:00
|
|
|
|
|
|
|
def read_cng(struct, params) do
|
|
|
|
struct
|
|
|
|
|> cast(params, [:read])
|
|
|
|
|> validate_required([:read])
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_read(participation) do
|
|
|
|
participation
|
|
|
|
|> read_cng(%{read: true})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_unread(participation) do
|
|
|
|
participation
|
|
|
|
|> read_cng(%{read: false})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-04-10 15:05:33 +00:00
|
|
|
|
|
|
|
def for_user(user, params \\ %{}) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
where: p.user_id == ^user.id,
|
|
|
|
order_by: [desc: p.updated_at]
|
|
|
|
)
|
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
2019-04-15 20:28:42 +00:00
|
|
|
|> Repo.preload(conversation: [:users])
|
2019-04-10 15:05:33 +00:00
|
|
|
end
|
2019-04-10 15:28:02 +00:00
|
|
|
|
|
|
|
def for_user_with_last_activity_id(user, params \\ %{}) do
|
|
|
|
for_user(user, params)
|
|
|
|
|> Repo.preload(:conversation)
|
|
|
|
|> Enum.map(fn participation ->
|
|
|
|
# TODO: Don't load all those activities, just get the most recent
|
|
|
|
# Involves splitting up the query.
|
|
|
|
activities =
|
|
|
|
ActivityPub.fetch_activities_for_context(participation.conversation.ap_id, %{
|
|
|
|
"user" => user,
|
|
|
|
"blocking_user" => user
|
|
|
|
})
|
|
|
|
|
|
|
|
activity_id =
|
|
|
|
case activities do
|
|
|
|
[activity | _] -> activity.id
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
|
|
|
participation
|
|
|
|
| last_activity_id: activity_id
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
2019-04-10 07:34:53 +00:00
|
|
|
end
|