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 @@ def get_for_ap_id(ap_id) do
2. Create a participation for all the people involved who don't have one already
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),
object <- Pleroma.Object.normalize(activity),
"Create" <- activity.data["type"],
@ -58,7 +58,7 @@ def create_or_bump_for(activity) do
participations =
Enum.map(users, fn user ->
{:ok, participation} =
Participation.create_for_user_and_conversation(user, conversation)
Participation.create_for_user_and_conversation(user, conversation, opts)
participation
end)
@ -84,7 +84,7 @@ def bump_for_all_activities do
Repo.transaction(
fn ->
stream
|> Enum.each(&create_or_bump_for/1)
|> Enum.each(fn a -> create_or_bump_for(a, read: true) end)
end,
timeout: :infinity
)

View file

@ -22,15 +22,17 @@ defmodule Pleroma.Conversation.Participation do
def creation_cng(struct, params) do
struct
|> cast(params, [:user_id, :conversation_id])
|> cast(params, [:user_id, :conversation_id, :read])
|> validate_required([:user_id, :conversation_id])
end
def create_for_user_and_conversation(user, conversation) do
def create_for_user_and_conversation(user, conversation, opts \\ []) do
read = !!opts[:read]
%__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(
on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
returning: true,
conflict_target: [:user_id, :conversation_id]
)

View file

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

View file

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