Conversations: Create or bump on inserting a dm.
This commit is contained in:
parent
64c1c3a407
commit
280172f6f6
6 changed files with 152 additions and 6 deletions
|
|
@ -5,10 +5,12 @@
|
|||
defmodule Pleroma.Conversation do
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Conversation.Participation
|
||||
alias Pleroma.User
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "conversations" do
|
||||
# This is the context ap id.
|
||||
field(:ap_id, :string)
|
||||
has_many(:participations, Participation)
|
||||
|
||||
|
|
@ -25,6 +27,44 @@ defmodule Pleroma.Conversation do
|
|||
def create_for_ap_id(ap_id) do
|
||||
%__MODULE__{}
|
||||
|> creation_cng(%{ap_id: ap_id})
|
||||
|> Repo.insert()
|
||||
|> Repo.insert(
|
||||
on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
|
||||
returning: true,
|
||||
conflict_target: :ap_id
|
||||
)
|
||||
end
|
||||
|
||||
def get_for_ap_id(ap_id) do
|
||||
Repo.get_by(__MODULE__, ap_id: ap_id)
|
||||
end
|
||||
|
||||
@doc """
|
||||
This will
|
||||
1. Create a conversation if there isn't one already
|
||||
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
|
||||
with true <- Pleroma.Web.ActivityPub.Visibility.is_direct?(activity),
|
||||
"Create" <- activity.data["type"],
|
||||
"Note" <- activity.data["object"]["type"],
|
||||
ap_id when is_binary(ap_id) <- activity.data["object"]["context"] do
|
||||
{:ok, conversation} = create_for_ap_id(ap_id)
|
||||
|
||||
local_users = User.get_users_from_set(activity.recipients, true)
|
||||
|
||||
participations =
|
||||
Enum.map(local_users, fn user ->
|
||||
{:ok, participation} =
|
||||
Participation.create_for_user_and_conversation(user, conversation)
|
||||
|
||||
participation
|
||||
end)
|
||||
|
||||
%{
|
||||
conversation
|
||||
| participations: participations
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -26,7 +26,11 @@ defmodule Pleroma.Conversation.Participation do
|
|||
def create_for_user_and_conversation(user, conversation) do
|
||||
%__MODULE__{}
|
||||
|> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
|
||||
|> Repo.insert()
|
||||
|> Repo.insert(
|
||||
on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
|
||||
returning: true,
|
||||
conflict_target: [:user_id, :conversation_id]
|
||||
)
|
||||
end
|
||||
|
||||
def read_cng(struct, params) do
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Conversation
|
||||
alias Pleroma.Instances
|
||||
alias Pleroma.Notification
|
||||
alias Pleroma.Object
|
||||
|
|
@ -143,6 +144,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|||
end)
|
||||
|
||||
Notification.create_notifications(activity)
|
||||
Conversation.create_or_bump_for(activity)
|
||||
stream_out(activity)
|
||||
{:ok, activity}
|
||||
else
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue