Store participation_request_count

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-09-22 23:40:31 +02:00
parent c3f59eb9ec
commit 0b65d205c9
6 changed files with 53 additions and 10 deletions

View file

@ -24,7 +24,8 @@ defmodule Pleroma.Constants do
"rules",
"content_type",
"participations",
"participation_count"
"participation_count",
"participation_request_count"
]
)

View file

@ -79,6 +79,7 @@ defmacro event_object_fields do
field(:participation_count, :integer, default: 0)
field(:participations, {:array, ObjectValidators.ObjectID}, default: [])
field(:participation_request_count, :integer, default: 0)
end
end
end

View file

@ -403,6 +403,10 @@ def handle(%{data: %{"type" => "Join"}} = object, meta) do
{:ok, _activity, _} = Pipeline.common_pipeline(accept_data, local: true)
end
if Object.local?(joined_event) and joined_event.data["joinMode"] != "free" do
Utils.update_participation_request_count_in_object(joined_event)
end
Notification.create_notifications(object)
{:ok, object, meta}
@ -412,6 +416,12 @@ def handle(%{data: %{"type" => "Join"}} = object, meta) do
def handle(%{actor: actor_id, data: %{"type" => "Leave", "object" => event_id}} = object, meta) do
with undone_object <- Utils.get_existing_join(actor_id, event_id),
:ok <- handle_undoing(undone_object) do
event = Object.get_by_ap_id(event_id)
if Object.local?(event) and event.data["joinMode"] != "free" do
Utils.update_participation_request_count_in_object(event)
end
{:ok, object, meta}
end
end

View file

@ -428,6 +428,26 @@ defp update_participations_in_object(participations, object) do
update_element_in_object("participation", participations, object)
end
def update_participation_request_count_in_object(object) do
params =
%{
type: "Join",
object: object.data["id"],
state: "pending"
}
count =
[]
|> ActivityPub.fetch_activities_query(params)
|> Repo.aggregate(:count)
data = Map.put(object.data, "participation_request_count", count)
object
|> Changeset.change(data: data)
|> Object.update_and_set_cache()
end
defp fetch_participations(object) do
if is_list(object.data["participations"]) do
object.data["participations"]

View file

@ -363,7 +363,9 @@ def leave(%User{ap_id: participant_ap_id} = user, event_id) do
def accept_join_request(%User{} = user, %User{ap_id: participant_ap_id} = participant, event_id) do
with %Activity{} = join_activity <- Utils.get_existing_join(participant_ap_id, event_id),
{:ok, accept_data, _} <- Builder.accept(user, join_activity),
{:ok, _activity, _} <- Pipeline.common_pipeline(accept_data, local: true) do
{:ok, _activity, _} <- Pipeline.common_pipeline(accept_data, local: true),
event <- Object.get_by_ap_id(event_id),
{:ok, _} <- Utils.update_participation_request_count_in_object(event) do
{:ok, participant}
end
end
@ -371,7 +373,9 @@ def accept_join_request(%User{} = user, %User{ap_id: participant_ap_id} = partic
def reject_join_request(%User{} = user, %User{ap_id: participant_ap_id} = participant, event_id) do
with %Activity{} = join_activity <- Utils.get_existing_join(participant_ap_id, event_id),
{:ok, reject_data, _} <- Builder.reject(user, join_activity),
{:ok, _activity, _} <- Pipeline.common_pipeline(reject_data, local: true) do
{:ok, _activity, _} <- Pipeline.common_pipeline(reject_data, local: true),
event <- Object.get_by_ap_id(event_id),
{:ok, _} <- Utils.update_participation_request_count_in_object(event) do
{:ok, participant}
end
end

View file

@ -771,7 +771,8 @@ defp build_event(%{"type" => "Event"} = data, for_user) do
join_mode: data["joinMode"],
participants_count: data["participation_count"],
location: build_event_location(data["location"]),
join_state: build_event_join_state(for_user, data["id"])
join_state: build_event_join_state(for_user, data["id"]),
participation_request_count: maybe_put_participation_request_count(data, for_user)
}
end
@ -789,13 +790,13 @@ defp build_event_location(%{"type" => "Place"} = location) do
defp build_event_location(_), do: nil
defp maybe_put_address(location, %{"type" => "PostalAddress"}) do
defp maybe_put_address(location, %{"type" => "PostalAddress"} = address) do
Map.merge(location, %{
street: location["streetAddress"],
postal_code: location["postalCode"],
locality: location["addressLocality"],
region: location["addressRegion"],
country: location["addressCountry"]
street: address["streetAddress"],
postal_code: address["postalCode"],
locality: address["addressLocality"],
region: address["addressRegion"],
country: address["addressCountry"]
})
end
@ -811,6 +812,12 @@ defp build_event_join_state(%{ap_id: actor}, id) do
defp build_event_join_state(_, _), do: nil
defp maybe_put_participation_request_count(%{"actor" => actor} = data, %{ap_id: actor}) do
data["participation_request_count"]
end
defp maybe_put_participation_request_count(_, _), do: nil
defp present?(nil), do: false
defp present?(false), do: false
defp present?(_), do: true