Resolve merge conflicts
authorrinpatch <rinpatch@sdf.org>
Sat, 20 Jul 2019 19:04:47 +0000 (22:04 +0300)
committerrinpatch <rinpatch@sdf.org>
Sat, 20 Jul 2019 19:04:47 +0000 (22:04 +0300)
1  2 
config/test.exs
lib/pleroma/object/fetcher.ex
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/activity_pub_test.exs
test/web/activity_pub/transmogrifier_test.exs

diff --cc config/test.exs
index 28eea3b0074d4a6593fc719199bf31955bb7c706,92dca18bc166e746e28ea40965b1559c6e88e867..aded8600d169b580d47ad9a571bba923f0dac50d
@@@ -29,9 -29,10 +29,11 @@@ config :pleroma, :instance
    email: "admin@example.com",
    notify_email: "noreply@example.com",
    skip_thread_containment: false,
 -  federating: false
 +  federating: false,
 +  external_user_synchronization: false
  
+ config :pleroma, :activitypub, sign_object_fetches: false
  # Configure your database
  config :pleroma, Pleroma.Repo,
    adapter: Ecto.Adapters.Postgres,
index 1e60d00824f3381b4c14da7f652b122b49befcb8,305ce835702b9a279318033c641572971cb31e85..8d79ddb1fd5af72d79d33fdf2175ef9eb5dc84dc
@@@ -76,15 -84,52 +84,52 @@@ defmodule Pleroma.Object.Fetcher d
      end
    end
  
 -  def fetch_and_contain_remote_object_from_id(id) do
+   defp make_signature(id, date) do
+     uri = URI.parse(id)
+     signature =
+       InternalFetchActor.get_actor()
+       |> Signature.sign(%{
+         "(request-target)": "get #{uri.path}",
+         host: uri.host,
+         date: date
+       })
+     [{:Signature, signature}]
+   end
+   defp sign_fetch(headers, id, date) do
+     if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
+       headers ++ make_signature(id, date)
+     else
+       headers
+     end
+   end
+   defp maybe_date_fetch(headers, date) do
+     if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
+       headers ++ [{:Date, date}]
+     else
+       headers
+     end
+   end
 +  def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
      Logger.info("Fetching object #{id} via AP")
  
+     date =
+       NaiveDateTime.utc_now()
+       |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
+     headers =
+       [{:Accept, "application/activity+json"}]
+       |> maybe_date_fetch(date)
+       |> sign_fetch(id, date)
+     Logger.debug("Fetch headers: #{inspect(headers)}")
      with true <- String.starts_with?(id, "http"),
-          {:ok, %{body: body, status: code}} when code in 200..299 <-
-            HTTP.get(
-              id,
-              [{:Accept, "application/activity+json"}]
-            ),
+          {:ok, %{body: body, status: code}} when code in 200..299 <- HTTP.get(id, headers),
           {:ok, data} <- Jason.decode(body),
           :ok <- Containment.contain_origin_from_id(id, data) do
        {:ok, data}
          {:error, e}
      end
    end
-   def fetch_and_contain_remote_object_from_id(%{"id" => id), do: fetch_and_contain_remote_object_from_id(id)
 +
-     {:error, "id must be a string"}
-   end
++  def fetch_and_contain_remote_object_from_id(%{"id" => id}),
++    do: fetch_and_contain_remote_object_from_id(id)
++
 +  def fetch_and_contain_remote_object_from_id(_id), do: {:error, "id must be a string"}
  end
index 956ec6240ddd2b7873afd70bdcbd8332979c68bb,5ea2b518bc55c97c05b5eb730a7fa7e3f1472b6f..f5f7c6c0443840b83a4fb395928e98a1e76a11c0
@@@ -702,70 -696,29 +702,68 @@@ defmodule Pleroma.User d
      |> update_and_set_cache()
    end
  
-         Logger.error(
-           "Follower/Following counter update for #{user.ap_id} failed.\n#{inspect(e)}"
-         )
 +  def maybe_fetch_follow_information(user) do
 +    with {:ok, user} <- fetch_follow_information(user) do
 +      user
 +    else
 +      e ->
++        Logger.error("Follower/Following counter update for #{user.ap_id} failed.\n#{inspect(e)}")
 +
 +        user
 +    end
 +  end
 +
 +  def fetch_follow_information(user) do
 +    with {:ok, info} <- ActivityPub.fetch_follow_information_for_user(user) do
 +      info_cng = User.Info.follow_information_update(user.info, info)
 +
 +      changeset =
 +        user
 +        |> change()
 +        |> put_embed(:info, info_cng)
 +
 +      update_and_set_cache(changeset)
 +    else
 +      {:error, _} = e -> e
 +      e -> {:error, e}
 +    end
 +  end
 +
    def update_follower_count(%User{} = user) do
 -    follower_count_query =
 -      User.Query.build(%{followers: user, deactivated: false})
 -      |> select([u], %{count: count(u.id)})
 +    unless user.local == false and Pleroma.Config.get([:instance, :external_user_synchronization]) do
 +      follower_count_query =
 +        User.Query.build(%{followers: user, deactivated: false})
 +        |> select([u], %{count: count(u.id)})
 +
 +      User
 +      |> where(id: ^user.id)
 +      |> join(:inner, [u], s in subquery(follower_count_query))
 +      |> update([u, s],
 +        set: [
 +          info:
 +            fragment(
 +              "jsonb_set(?, '{follower_count}', ?::varchar::jsonb, true)",
 +              u.info,
 +              s.count
 +            )
 +        ]
 +      )
 +      |> select([u], u)
 +      |> Repo.update_all([])
 +      |> case do
 +        {1, [user]} -> set_cache(user)
 +        _ -> {:error, user}
 +      end
 +    else
 +      {:ok, maybe_fetch_follow_information(user)}
 +    end
 +  end
  
 -    User
 -    |> where(id: ^user.id)
 -    |> join(:inner, [u], s in subquery(follower_count_query))
 -    |> update([u, s],
 -      set: [
 -        info:
 -          fragment(
 -            "jsonb_set(?, '{follower_count}', ?::varchar::jsonb, true)",
 -            u.info,
 -            s.count
 -          )
 -      ]
 -    )
 -    |> select([u], u)
 -    |> Repo.update_all([])
 -    |> case do
 -      {1, [user]} -> set_cache(user)
 -      _ -> {:error, user}
 +  def maybe_update_following_count(%User{local: false} = user) do
 +    if Pleroma.Config.get([:instance, :external_user_synchronization]) do
 +      {:ok, maybe_fetch_follow_information(user)}
 +    else
 +      user
      end
    end
  
Simple merge