logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

note_handling_test.exs (14992B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
  5. use Oban.Testing, repo: Pleroma.Repo
  6. use Pleroma.DataCase
  7. alias Pleroma.Activity
  8. alias Pleroma.Object
  9. alias Pleroma.User
  10. alias Pleroma.Web.ActivityPub.Transmogrifier
  11. alias Pleroma.Web.CommonAPI
  12. import Mock
  13. import Pleroma.Factory
  14. setup_all do
  15. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  16. :ok
  17. end
  18. setup do: clear_config([:instance, :max_remote_account_fields])
  19. describe "handle_incoming" do
  20. test "it works for incoming notices with tag not being an array (kroeg)" do
  21. data = File.read!("test/fixtures/kroeg-array-less-emoji.json") |> Jason.decode!()
  22. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  23. object = Object.normalize(data["object"])
  24. assert object.data["emoji"] == %{
  25. "icon_e_smile" => "https://puckipedia.com/forum/images/smilies/icon_e_smile.png"
  26. }
  27. data = File.read!("test/fixtures/kroeg-array-less-hashtag.json") |> Jason.decode!()
  28. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  29. object = Object.normalize(data["object"])
  30. assert object.data["tag"] == [
  31. %{
  32. "href" => "https://puckipedia.com/ur33-818k/note/hashtag",
  33. "id" => "https://puckipedia.com/ur33-818k/note/hashtag",
  34. "name" => "#test",
  35. "type" => "Hashtag"
  36. }
  37. ]
  38. assert object.data["hashtags"] == ["test"]
  39. end
  40. @tag capture_log: true
  41. test "it fetches reply-to activities if we don't have them" do
  42. data =
  43. File.read!("test/fixtures/mastodon-post-activity.json")
  44. |> Jason.decode!()
  45. object =
  46. data["object"]
  47. |> Map.put("inReplyTo", "https://mstdn.io/users/mayuutann/statuses/99568293732299394")
  48. data = Map.put(data, "object", object)
  49. {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
  50. returned_object = Object.normalize(returned_activity, false)
  51. assert activity =
  52. Activity.get_create_by_object_ap_id(
  53. "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  54. )
  55. assert returned_object.data["inReplyTo"] ==
  56. "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
  57. end
  58. test "it does not fetch reply-to activities beyond max replies depth limit" do
  59. data =
  60. File.read!("test/fixtures/mastodon-post-activity.json")
  61. |> Jason.decode!()
  62. object =
  63. data["object"]
  64. |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
  65. data = Map.put(data, "object", object)
  66. with_mock Pleroma.Web.Federator,
  67. allowed_thread_distance?: fn _ -> false end do
  68. {:ok, returned_activity} = Transmogrifier.handle_incoming(data)
  69. returned_object = Object.normalize(returned_activity, false)
  70. refute Activity.get_create_by_object_ap_id(
  71. "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
  72. )
  73. assert returned_object.data["inReplyTo"] == "https://shitposter.club/notice/2827873"
  74. end
  75. end
  76. test "it does not crash if the object in inReplyTo can't be fetched" do
  77. data =
  78. File.read!("test/fixtures/mastodon-post-activity.json")
  79. |> Jason.decode!()
  80. object =
  81. data["object"]
  82. |> Map.put("inReplyTo", "https://404.site/whatever")
  83. data =
  84. data
  85. |> Map.put("object", object)
  86. assert {:ok, _returned_activity} = Transmogrifier.handle_incoming(data)
  87. end
  88. test "it works for incoming notices" do
  89. data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
  90. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  91. assert data["id"] ==
  92. "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
  93. assert data["context"] ==
  94. "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
  95. assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
  96. assert data["cc"] == [
  97. "http://localtesting.pleroma.lol/users/lain",
  98. "http://mastodon.example.org/users/admin/followers"
  99. ]
  100. assert data["actor"] == "http://mastodon.example.org/users/admin"
  101. object_data = Object.normalize(data["object"]).data
  102. assert object_data["id"] ==
  103. "http://mastodon.example.org/users/admin/statuses/99512778738411822"
  104. assert object_data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
  105. assert object_data["cc"] == [
  106. "http://localtesting.pleroma.lol/users/lain",
  107. "http://mastodon.example.org/users/admin/followers"
  108. ]
  109. assert object_data["actor"] == "http://mastodon.example.org/users/admin"
  110. assert object_data["attributedTo"] == "http://mastodon.example.org/users/admin"
  111. assert object_data["context"] ==
  112. "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
  113. assert object_data["sensitive"] == true
  114. user = User.get_cached_by_ap_id(object_data["actor"])
  115. assert user.note_count == 1
  116. end
  117. test "it does not work for deactivated users" do
  118. data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
  119. insert(:user, ap_id: data["actor"], deactivated: true)
  120. assert {:error, _} = Transmogrifier.handle_incoming(data)
  121. end
  122. test "it works for incoming notices with hashtags" do
  123. data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Jason.decode!()
  124. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  125. object = Object.normalize(data["object"])
  126. assert %{
  127. "href" => "http://mastodon.example.org/tags/moo",
  128. "name" => "#moo",
  129. "type" => "Hashtag"
  130. } in object.data["tag"]
  131. assert object.data["hashtags"] == ["moo"]
  132. end
  133. test "it works for incoming notices with contentMap" do
  134. data = File.read!("test/fixtures/mastodon-post-activity-contentmap.json") |> Jason.decode!()
  135. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  136. object = Object.normalize(data["object"])
  137. assert object.data["content"] ==
  138. "<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
  139. end
  140. test "it works for incoming notices with to/cc not being an array (kroeg)" do
  141. data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
  142. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  143. object = Object.normalize(data["object"])
  144. assert object.data["content"] ==
  145. "<p>henlo from my Psion netBook</p><p>message sent from my Psion netBook</p>"
  146. end
  147. test "it ensures that as:Public activities make it to their followers collection" do
  148. user = insert(:user)
  149. data =
  150. File.read!("test/fixtures/mastodon-post-activity.json")
  151. |> Jason.decode!()
  152. |> Map.put("actor", user.ap_id)
  153. |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
  154. |> Map.put("cc", [])
  155. object =
  156. data["object"]
  157. |> Map.put("attributedTo", user.ap_id)
  158. |> Map.put("to", ["https://www.w3.org/ns/activitystreams#Public"])
  159. |> Map.put("cc", [])
  160. |> Map.put("id", user.ap_id <> "/activities/12345678")
  161. data = Map.put(data, "object", object)
  162. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  163. assert data["cc"] == [User.ap_followers(user)]
  164. end
  165. test "it ensures that address fields become lists" do
  166. user = insert(:user)
  167. data =
  168. File.read!("test/fixtures/mastodon-post-activity.json")
  169. |> Jason.decode!()
  170. |> Map.put("actor", user.ap_id)
  171. |> Map.put("cc", nil)
  172. object =
  173. data["object"]
  174. |> Map.put("attributedTo", user.ap_id)
  175. |> Map.put("cc", nil)
  176. |> Map.put("id", user.ap_id <> "/activities/12345678")
  177. data = Map.put(data, "object", object)
  178. {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
  179. refute is_nil(data["cc"])
  180. end
  181. test "it strips internal likes" do
  182. data =
  183. File.read!("test/fixtures/mastodon-post-activity.json")
  184. |> Jason.decode!()
  185. likes = %{
  186. "first" =>
  187. "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
  188. "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
  189. "totalItems" => 3,
  190. "type" => "OrderedCollection"
  191. }
  192. object = Map.put(data["object"], "likes", likes)
  193. data = Map.put(data, "object", object)
  194. {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
  195. %Object{data: object} = Object.normalize(activity)
  196. assert object["likes"] == []
  197. end
  198. test "it strips internal reactions" do
  199. user = insert(:user)
  200. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  201. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "📢")
  202. %{object: object} = Activity.get_by_id_with_object(activity.id)
  203. assert Map.has_key?(object.data, "reactions")
  204. assert Map.has_key?(object.data, "reaction_count")
  205. object_data = Transmogrifier.strip_internal_fields(object.data)
  206. refute Map.has_key?(object_data, "reactions")
  207. refute Map.has_key?(object_data, "reaction_count")
  208. end
  209. test "it correctly processes messages with non-array to field" do
  210. data =
  211. File.read!("test/fixtures/mastodon-post-activity.json")
  212. |> Jason.decode!()
  213. |> Map.put("to", "https://www.w3.org/ns/activitystreams#Public")
  214. |> put_in(["object", "to"], "https://www.w3.org/ns/activitystreams#Public")
  215. assert {:ok, activity} = Transmogrifier.handle_incoming(data)
  216. assert [
  217. "http://localtesting.pleroma.lol/users/lain",
  218. "http://mastodon.example.org/users/admin/followers"
  219. ] == activity.data["cc"]
  220. assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
  221. end
  222. test "it correctly processes messages with non-array cc field" do
  223. data =
  224. File.read!("test/fixtures/mastodon-post-activity.json")
  225. |> Jason.decode!()
  226. |> Map.put("cc", "http://mastodon.example.org/users/admin/followers")
  227. |> put_in(["object", "cc"], "http://mastodon.example.org/users/admin/followers")
  228. assert {:ok, activity} = Transmogrifier.handle_incoming(data)
  229. assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
  230. assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
  231. end
  232. test "it correctly processes messages with weirdness in address fields" do
  233. data =
  234. File.read!("test/fixtures/mastodon-post-activity.json")
  235. |> Jason.decode!()
  236. |> Map.put("cc", ["http://mastodon.example.org/users/admin/followers", ["¿"]])
  237. |> put_in(["object", "cc"], ["http://mastodon.example.org/users/admin/followers", ["¿"]])
  238. assert {:ok, activity} = Transmogrifier.handle_incoming(data)
  239. assert ["http://mastodon.example.org/users/admin/followers"] == activity.data["cc"]
  240. assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["to"]
  241. end
  242. end
  243. describe "`handle_incoming/2`, Mastodon format `replies` handling" do
  244. setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
  245. setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
  246. setup do
  247. data =
  248. "test/fixtures/mastodon-post-activity.json"
  249. |> File.read!()
  250. |> Jason.decode!()
  251. items = get_in(data, ["object", "replies", "first", "items"])
  252. assert length(items) > 0
  253. %{data: data, items: items}
  254. end
  255. test "schedules background fetching of `replies` items if max thread depth limit allows", %{
  256. data: data,
  257. items: items
  258. } do
  259. Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 10)
  260. {:ok, activity} = Transmogrifier.handle_incoming(data)
  261. object = Object.normalize(activity.data["object"])
  262. assert object.data["replies"] == items
  263. for id <- items do
  264. job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
  265. assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
  266. end
  267. end
  268. test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
  269. %{data: data} do
  270. Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
  271. {:ok, _activity} = Transmogrifier.handle_incoming(data)
  272. assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
  273. end
  274. end
  275. describe "`handle_incoming/2`, Pleroma format `replies` handling" do
  276. setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
  277. setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
  278. setup do
  279. replies = %{
  280. "type" => "Collection",
  281. "items" => [
  282. Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  283. Pleroma.Web.ActivityPub.Utils.generate_object_id()
  284. ]
  285. }
  286. activity =
  287. File.read!("test/fixtures/mastodon-post-activity.json")
  288. |> Jason.decode!()
  289. |> Kernel.put_in(["object", "replies"], replies)
  290. %{activity: activity}
  291. end
  292. test "schedules background fetching of `replies` items if max thread depth limit allows", %{
  293. activity: activity
  294. } do
  295. Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 1)
  296. assert {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(activity)
  297. object = Object.normalize(data["object"])
  298. for id <- object.data["replies"] do
  299. job_args = %{"op" => "fetch_remote", "id" => id, "depth" => 1}
  300. assert_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker, args: job_args)
  301. end
  302. end
  303. test "does NOT schedule background fetching of `replies` beyond max thread depth limit allows",
  304. %{activity: activity} do
  305. Pleroma.Config.put([:instance, :federation_incoming_replies_max_depth], 0)
  306. {:ok, _activity} = Transmogrifier.handle_incoming(activity)
  307. assert all_enqueued(worker: Pleroma.Workers.RemoteFetcherWorker) == []
  308. end
  309. end
  310. end