Commit 9a063dea authored by nicole mikołajczyk's avatar nicole mikołajczyk Committed by lain
Browse files

Count and display post quotes

parent 4c5b45ed
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+46 −0
Original line number Diff line number Diff line
@@ -328,6 +328,52 @@ def decrease_replies_count(ap_id) do
    end
  end

  def increase_quotes_count(ap_id) do
    Object
    |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
    |> update([o],
      set: [
        data:
          fragment(
            """
            safe_jsonb_set(?, '{quotesCount}',
              (coalesce((?->>'quotesCount')::int, 0) + 1)::varchar::jsonb, true)
            """,
            o.data,
            o.data
          )
      ]
    )
    |> Repo.update_all([])
    |> case do
      {1, [object]} -> set_cache(object)
      _ -> {:error, "Not found"}
    end
  end

  def decrease_quotes_count(ap_id) do
    Object
    |> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
    |> update([o],
      set: [
        data:
          fragment(
            """
            safe_jsonb_set(?, '{quotesCount}',
              (greatest(0, (?->>'quotesCount')::int - 1))::varchar::jsonb, true)
            """,
            o.data,
            o.data
          )
      ]
    )
    |> Repo.update_all([])
    |> case do
      {1, [object]} -> set_cache(object)
      _ -> {:error, "Not found"}
    end
  end

  def increase_vote_count(ap_id, name, actor) do
    with %Object{} = object <- Object.normalize(ap_id, fetch: false),
         "Question" <- object.data["type"] do
+21 −0
Original line number Diff line number Diff line
@@ -96,6 +96,17 @@ defp increase_replies_count_if_reply(%{

  defp increase_replies_count_if_reply(_create_data), do: :noop

  defp increase_quotes_count_if_quote(%{
         "object" => %{"quoteUrl" => quote_ap_id} = object,
         "type" => "Create"
       }) do
    if is_public?(object) do
      Object.increase_quotes_count(quote_ap_id)
    end
  end

  defp increase_quotes_count_if_quote(_create_data), do: :noop

  @object_types ~w[ChatMessage Question Answer Audio Video Image Event Article Note Page]
  @impl true
  def persist(%{"type" => type} = object, meta) when type in @object_types do
@@ -299,6 +310,7 @@ defp do_create(%{to: to, actor: actor, context: context, object: object} = param
    with {:ok, activity} <- insert(create_data, local, fake),
         {:fake, false, activity} <- {:fake, fake, activity},
         _ <- increase_replies_count_if_reply(create_data),
         _ <- increase_quotes_count_if_quote(create_data),
         {:quick_insert, false, activity} <- {:quick_insert, quick_insert?, activity},
         {:ok, _actor} <- increase_note_count_if_public(actor, activity),
         {:ok, _actor} <- update_last_status_at_if_public(actor, activity),
@@ -1237,6 +1249,14 @@ defp restrict_unauthenticated(query, nil) do

  defp restrict_unauthenticated(query, _), do: query

  defp restrict_quote_url(query, %{quote_url: quote_url}) do
    from([_activity, object] in query,
      where: fragment("(?)->'quoteUrl' = ?", object.data, ^quote_url)
    )
  end

  defp restrict_quote_url(query, _), do: query

  defp exclude_poll_votes(query, %{include_poll_votes: true}), do: query

  defp exclude_poll_votes(query, _) do
@@ -1399,6 +1419,7 @@ def fetch_activities_query(recipients, opts \\ %{}) do
      |> restrict_instance(opts)
      |> restrict_announce_object_actor(opts)
      |> restrict_filtered(opts)
      |> restrict_quote_url(opts)
      |> maybe_restrict_deactivated_users(opts)
      |> exclude_poll_votes(opts)
      |> exclude_chat_messages(opts)
+1 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ defmacro status_object_fields do
      field(:replies_count, :integer, default: 0)
      field(:like_count, :integer, default: 0)
      field(:announcement_count, :integer, default: 0)
      field(:quotes_count, :integer, default: 0)
      field(:inReplyTo, ObjectValidators.ObjectID)
      field(:quoteUrl, ObjectValidators.ObjectID)
      field(:url, ObjectValidators.BareUri)
+8 −0
Original line number Diff line number Diff line
@@ -209,6 +209,10 @@ def handle(%{data: %{"type" => "Create"}} = activity, meta) do
        Object.increase_replies_count(in_reply_to)
      end

      if quote_url = object.data["quoteUrl"] do
        Object.increase_quotes_count(quote_url)
      end

      reply_depth = (meta[:depth] || 0) + 1

      # FIXME: Force inReplyTo to replies
@@ -305,6 +309,10 @@ def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object,
              Object.decrease_replies_count(in_reply_to)
            end

            if quote_url = deleted_object.data["quoteUrl"] do
              Object.decrease_quotes_count(quote_url)
            end

            MessageReference.delete_for_object(deleted_object)

            ap_streamer().stream_out(object)
Loading