Conversations: Import order, import as read.

This commit is contained in:
lain 2019-05-09 16:39:28 +02:00
parent e6d7f8d223
commit a33bec7d58
4 changed files with 13 additions and 8 deletions

View File

@ -45,7 +45,7 @@ defmodule Pleroma.Conversation do
2. Create a participation for all the people involved who don't have one already 2. Create a participation for all the people involved who don't have one already
3. Bump all relevant participations to 'unread' 3. Bump all relevant participations to 'unread'
""" """
def create_or_bump_for(activity) do def create_or_bump_for(activity, opts \\ []) do
with true <- Pleroma.Web.ActivityPub.Visibility.is_direct?(activity), with true <- Pleroma.Web.ActivityPub.Visibility.is_direct?(activity),
object <- Pleroma.Object.normalize(activity), object <- Pleroma.Object.normalize(activity),
"Create" <- activity.data["type"], "Create" <- activity.data["type"],
@ -58,7 +58,7 @@ defmodule Pleroma.Conversation do
participations = participations =
Enum.map(users, fn user -> Enum.map(users, fn user ->
{:ok, participation} = {:ok, participation} =
Participation.create_for_user_and_conversation(user, conversation) Participation.create_for_user_and_conversation(user, conversation, opts)
participation participation
end) end)
@ -84,7 +84,7 @@ defmodule Pleroma.Conversation do
Repo.transaction( Repo.transaction(
fn -> fn ->
stream stream
|> Enum.each(&create_or_bump_for/1) |> Enum.each(fn a -> create_or_bump_for(a, read: true) end)
end, end,
timeout: :infinity timeout: :infinity
) )

View File

@ -22,15 +22,17 @@ defmodule Pleroma.Conversation.Participation do
def creation_cng(struct, params) do def creation_cng(struct, params) do
struct struct
|> cast(params, [:user_id, :conversation_id]) |> cast(params, [:user_id, :conversation_id, :read])
|> validate_required([:user_id, :conversation_id]) |> validate_required([:user_id, :conversation_id])
end end
def create_for_user_and_conversation(user, conversation) do def create_for_user_and_conversation(user, conversation, opts \\ []) do
read = !!opts[:read]
%__MODULE__{} %__MODULE__{}
|> creation_cng(%{user_id: user.id, conversation_id: conversation.id}) |> creation_cng(%{user_id: user.id, conversation_id: conversation.id, read: read})
|> Repo.insert( |> Repo.insert(
on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]], on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
returning: true, returning: true,
conflict_target: [:user_id, :conversation_id] conflict_target: [:user_id, :conversation_id]
) )

View File

@ -1056,5 +1056,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
Activity Activity
|> restrict_type(%{"type" => "Create"}) |> restrict_type(%{"type" => "Create"})
|> restrict_visibility(%{visibility: "direct"}) |> restrict_visibility(%{visibility: "direct"})
|> order_by([activity], asc: activity.id)
end end
end end

View File

@ -24,7 +24,9 @@ defmodule Pleroma.ConversationTest do
Conversation.bump_for_all_activities() Conversation.bump_for_all_activities()
assert Repo.one(Conversation) assert Repo.one(Conversation)
assert length(Repo.all(Conversation.Participation)) == 2 [participation, _p2] = Repo.all(Conversation.Participation)
assert participation.read
end end
test "it creates a conversation for given ap_id" do test "it creates a conversation for given ap_id" do