logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

database_test.exs (21859B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Mix.Tasks.Pleroma.DatabaseTest do
  5. use Pleroma.DataCase, async: true
  6. use Oban.Testing, repo: Pleroma.Repo
  7. alias Pleroma.Activity
  8. alias Pleroma.Bookmark
  9. alias Pleroma.Object
  10. alias Pleroma.Repo
  11. alias Pleroma.User
  12. alias Pleroma.Web.CommonAPI
  13. import Pleroma.Factory
  14. setup_all do
  15. Mix.shell(Mix.Shell.Process)
  16. on_exit(fn ->
  17. Mix.shell(Mix.Shell.IO)
  18. end)
  19. :ok
  20. end
  21. describe "running remove_embedded_objects" do
  22. test "it replaces objects with references" do
  23. user = insert(:user)
  24. {:ok, activity} = CommonAPI.post(user, %{status: "test"})
  25. new_data = Map.put(activity.data, "object", activity.object.data)
  26. {:ok, activity} =
  27. activity
  28. |> Activity.change(%{data: new_data})
  29. |> Repo.update()
  30. assert is_map(activity.data["object"])
  31. Mix.Tasks.Pleroma.Database.run(["remove_embedded_objects"])
  32. activity = Activity.get_by_id_with_object(activity.id)
  33. assert is_binary(activity.data["object"])
  34. end
  35. end
  36. describe "prune_objects" do
  37. setup do
  38. deadline = Pleroma.Config.get([:instance, :remote_post_retention_days]) + 1
  39. old_insert_date =
  40. Timex.now()
  41. |> Timex.shift(days: -deadline)
  42. |> Timex.to_naive_datetime()
  43. |> NaiveDateTime.truncate(:second)
  44. %{old_insert_date: old_insert_date}
  45. end
  46. test "it prunes old objects from the database", %{old_insert_date: old_insert_date} do
  47. insert(:note)
  48. %{id: note_remote_public_id} =
  49. :note
  50. |> insert()
  51. |> Ecto.Changeset.change(%{updated_at: old_insert_date})
  52. |> Repo.update!()
  53. note_remote_non_public =
  54. %{id: note_remote_non_public_id, data: note_remote_non_public_data} =
  55. :note
  56. |> insert()
  57. note_remote_non_public
  58. |> Ecto.Changeset.change(%{
  59. updated_at: old_insert_date,
  60. data: note_remote_non_public_data |> update_in(["to"], fn _ -> [] end)
  61. })
  62. |> Repo.update!()
  63. assert length(Repo.all(Object)) == 3
  64. Mix.Tasks.Pleroma.Database.run(["prune_objects"])
  65. assert length(Repo.all(Object)) == 1
  66. refute Object.get_by_id(note_remote_public_id)
  67. refute Object.get_by_id(note_remote_non_public_id)
  68. end
  69. test "it cleans up bookmarks", %{old_insert_date: old_insert_date} do
  70. user = insert(:user)
  71. {:ok, old_object_activity} = CommonAPI.post(user, %{status: "yadayada"})
  72. Repo.one(Object)
  73. |> Ecto.Changeset.change(%{updated_at: old_insert_date})
  74. |> Repo.update!()
  75. {:ok, new_object_activity} = CommonAPI.post(user, %{status: "yadayada"})
  76. {:ok, _} = Bookmark.create(user.id, old_object_activity.id)
  77. {:ok, _} = Bookmark.create(user.id, new_object_activity.id)
  78. assert length(Repo.all(Object)) == 2
  79. assert length(Repo.all(Bookmark)) == 2
  80. Mix.Tasks.Pleroma.Database.run(["prune_objects"])
  81. assert length(Repo.all(Object)) == 1
  82. assert length(Repo.all(Bookmark)) == 1
  83. refute Bookmark.get(user.id, old_object_activity.id)
  84. end
  85. test "with the --keep-non-public option it still keeps non-public posts even if they are not local",
  86. %{old_insert_date: old_insert_date} do
  87. insert(:note)
  88. %{id: note_remote_id} =
  89. :note
  90. |> insert()
  91. |> Ecto.Changeset.change(%{updated_at: old_insert_date})
  92. |> Repo.update!()
  93. note_remote_non_public =
  94. %{data: note_remote_non_public_data} =
  95. :note
  96. |> insert()
  97. note_remote_non_public
  98. |> Ecto.Changeset.change(%{
  99. updated_at: old_insert_date,
  100. data: note_remote_non_public_data |> update_in(["to"], fn _ -> [] end)
  101. })
  102. |> Repo.update!()
  103. assert length(Repo.all(Object)) == 3
  104. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--keep-non-public"])
  105. assert length(Repo.all(Object)) == 2
  106. refute Object.get_by_id(note_remote_id)
  107. end
  108. test "with the --keep-threads and --keep-non-public option it keeps old threads with non-public replies even if the interaction is not local",
  109. %{old_insert_date: old_insert_date} do
  110. # For non-public we only check Create Activities because only these are relevant for threads
  111. # Flags are always non-public, Announces from relays can be non-public...
  112. remote_user1 = insert(:user, local: false)
  113. remote_user2 = insert(:user, local: false)
  114. # Old remote non-public reply (should be kept)
  115. {:ok, old_remote_post1_activity} =
  116. CommonAPI.post(remote_user1, %{status: "some thing", local: false})
  117. old_remote_post1_activity
  118. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  119. |> Repo.update!()
  120. {:ok, old_remote_non_public_reply_activity} =
  121. CommonAPI.post(remote_user2, %{
  122. status: "some reply",
  123. in_reply_to_status_id: old_remote_post1_activity.id
  124. })
  125. old_remote_non_public_reply_activity
  126. |> Ecto.Changeset.change(%{
  127. local: false,
  128. updated_at: old_insert_date,
  129. data: old_remote_non_public_reply_activity.data |> update_in(["to"], fn _ -> [] end)
  130. })
  131. |> Repo.update!()
  132. # Old remote non-public Announce (should be removed)
  133. {:ok, old_remote_post2_activity = %{data: %{"object" => old_remote_post2_id}}} =
  134. CommonAPI.post(remote_user1, %{status: "some thing", local: false})
  135. old_remote_post2_activity
  136. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  137. |> Repo.update!()
  138. {:ok, old_remote_non_public_repeat_activity} =
  139. CommonAPI.repeat(old_remote_post2_activity.id, remote_user2)
  140. old_remote_non_public_repeat_activity
  141. |> Ecto.Changeset.change(%{
  142. local: false,
  143. updated_at: old_insert_date,
  144. data: old_remote_non_public_repeat_activity.data |> update_in(["to"], fn _ -> [] end)
  145. })
  146. |> Repo.update!()
  147. assert length(Repo.all(Object)) == 3
  148. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--keep-threads", "--keep-non-public"])
  149. Repo.all(Pleroma.Activity)
  150. assert length(Repo.all(Object)) == 2
  151. refute Object.get_by_ap_id(old_remote_post2_id)
  152. end
  153. test "with the --keep-threads option it still keeps non-old threads even with no local interactions" do
  154. remote_user = insert(:user, local: false)
  155. remote_user2 = insert(:user, local: false)
  156. {:ok, remote_post_activity} =
  157. CommonAPI.post(remote_user, %{status: "some thing", local: false})
  158. {:ok, remote_post_reply_activity} =
  159. CommonAPI.post(remote_user2, %{
  160. status: "some reply",
  161. in_reply_to_status_id: remote_post_activity.id
  162. })
  163. remote_post_activity
  164. |> Ecto.Changeset.change(%{local: false})
  165. |> Repo.update!()
  166. remote_post_reply_activity
  167. |> Ecto.Changeset.change(%{local: false})
  168. |> Repo.update!()
  169. assert length(Repo.all(Object)) == 2
  170. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--keep-threads"])
  171. assert length(Repo.all(Object)) == 2
  172. end
  173. test "with the --keep-threads option it deletes old threads with no local interaction", %{
  174. old_insert_date: old_insert_date
  175. } do
  176. remote_user = insert(:user, local: false)
  177. remote_user2 = insert(:user, local: false)
  178. {:ok, old_remote_post_activity} =
  179. CommonAPI.post(remote_user, %{status: "some thing", local: false})
  180. old_remote_post_activity
  181. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  182. |> Repo.update!()
  183. {:ok, old_remote_post_reply_activity} =
  184. CommonAPI.post(remote_user2, %{
  185. status: "some reply",
  186. in_reply_to_status_id: old_remote_post_activity.id
  187. })
  188. old_remote_post_reply_activity
  189. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  190. |> Repo.update!()
  191. {:ok, old_favourite_activity} =
  192. CommonAPI.favorite(old_remote_post_activity.id, remote_user2)
  193. old_favourite_activity
  194. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  195. |> Repo.update!()
  196. {:ok, old_repeat_activity} = CommonAPI.repeat(old_remote_post_activity.id, remote_user2)
  197. old_repeat_activity
  198. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  199. |> Repo.update!()
  200. assert length(Repo.all(Object)) == 2
  201. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--keep-threads"])
  202. assert length(Repo.all(Object)) == 0
  203. end
  204. test "with the --keep-threads option it keeps old threads with local interaction", %{
  205. old_insert_date: old_insert_date
  206. } do
  207. remote_user = insert(:user, local: false)
  208. local_user = insert(:user, local: true)
  209. # local reply
  210. {:ok, old_remote_post1_activity} =
  211. CommonAPI.post(remote_user, %{status: "some thing", local: false})
  212. old_remote_post1_activity
  213. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  214. |> Repo.update!()
  215. {:ok, old_local_post2_reply_activity} =
  216. CommonAPI.post(local_user, %{
  217. status: "some reply",
  218. in_reply_to_status_id: old_remote_post1_activity.id
  219. })
  220. old_local_post2_reply_activity
  221. |> Ecto.Changeset.change(%{local: true, updated_at: old_insert_date})
  222. |> Repo.update!()
  223. # local Like
  224. {:ok, old_remote_post3_activity} =
  225. CommonAPI.post(remote_user, %{status: "some thing", local: false})
  226. old_remote_post3_activity
  227. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  228. |> Repo.update!()
  229. {:ok, old_favourite_activity} = CommonAPI.favorite(old_remote_post3_activity.id, local_user)
  230. old_favourite_activity
  231. |> Ecto.Changeset.change(%{local: true, updated_at: old_insert_date})
  232. |> Repo.update!()
  233. # local Announce
  234. {:ok, old_remote_post4_activity} =
  235. CommonAPI.post(remote_user, %{status: "some thing", local: false})
  236. old_remote_post4_activity
  237. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  238. |> Repo.update!()
  239. {:ok, old_repeat_activity} = CommonAPI.repeat(old_remote_post4_activity.id, local_user)
  240. old_repeat_activity
  241. |> Ecto.Changeset.change(%{local: true, updated_at: old_insert_date})
  242. |> Repo.update!()
  243. assert length(Repo.all(Object)) == 4
  244. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--keep-threads"])
  245. assert length(Repo.all(Object)) == 4
  246. end
  247. test "with the --keep-threads option it keeps old threads with bookmarked posts", %{
  248. old_insert_date: old_insert_date
  249. } do
  250. remote_user = insert(:user, local: false)
  251. local_user = insert(:user, local: true)
  252. {:ok, old_remote_post_activity} =
  253. CommonAPI.post(remote_user, %{status: "some thing", local: false})
  254. old_remote_post_activity
  255. |> Ecto.Changeset.change(%{local: false, updated_at: old_insert_date})
  256. |> Repo.update!()
  257. Pleroma.Bookmark.create(local_user.id, old_remote_post_activity.id)
  258. assert length(Repo.all(Object)) == 1
  259. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--keep-threads"])
  260. assert length(Repo.all(Object)) == 1
  261. end
  262. test "We don't have unexpected tables which may contain objects that are referenced by activities" do
  263. # We can delete orphaned activities. For that we look for the objects
  264. # they reference in the 'objects', 'activities', and 'users' table.
  265. # If someone adds another table with objects (idk, maybe with separate
  266. # relations, or collections or w/e), then we need to make sure we
  267. # add logic for that in the 'prune_objects' task so that we don't
  268. # wrongly delete their corresponding activities.
  269. # So when someone adds (or removes) a table, this test will fail.
  270. # Either the table contains objects which can be referenced from the
  271. # activities table
  272. # => in that case the prune_objects job should be adapted so we don't
  273. # delete activities who still have the referenced object.
  274. # Or it doesn't contain objects which can be referenced from the activities table
  275. # => in that case you can add/remove the table to/from this (sorted) list.
  276. assert Repo.query!(
  277. "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';"
  278. ).rows
  279. |> Enum.sort() == [
  280. ["activities"],
  281. ["announcement_read_relationships"],
  282. ["announcements"],
  283. ["apps"],
  284. ["backups"],
  285. ["bookmark_folders"],
  286. ["bookmarks"],
  287. ["chat_message_references"],
  288. ["chats"],
  289. ["config"],
  290. ["conversation_participation_recipient_ships"],
  291. ["conversation_participations"],
  292. ["conversations"],
  293. ["counter_cache"],
  294. ["data_migration_failed_ids"],
  295. ["data_migrations"],
  296. ["deliveries"],
  297. ["filters"],
  298. ["following_relationships"],
  299. ["hashtags"],
  300. ["hashtags_objects"],
  301. ["instances"],
  302. ["lists"],
  303. ["markers"],
  304. ["mfa_tokens"],
  305. ["moderation_log"],
  306. ["notifications"],
  307. ["oauth_authorizations"],
  308. ["oauth_tokens"],
  309. ["oban_jobs"],
  310. ["oban_peers"],
  311. ["objects"],
  312. ["password_reset_tokens"],
  313. ["push_subscriptions"],
  314. ["registrations"],
  315. ["report_notes"],
  316. ["rich_media_card"],
  317. ["rules"],
  318. ["scheduled_activities"],
  319. ["schema_migrations"],
  320. ["thread_mutes"],
  321. # ["user_follows_hashtag"], # not in pleroma
  322. # ["user_frontend_setting_profiles"], # not in pleroma
  323. ["user_invite_tokens"],
  324. ["user_notes"],
  325. ["user_relationships"],
  326. ["users"]
  327. ]
  328. end
  329. test "it prunes orphaned activities with the --prune-orphaned-activities" do
  330. # Add a remote activity which references an Object
  331. %Object{} |> Map.merge(%{data: %{"id" => "object_for_activity"}}) |> Repo.insert()
  332. %Activity{}
  333. |> Map.merge(%{
  334. local: false,
  335. data: %{"id" => "remote_activity_with_object", "object" => "object_for_activity"}
  336. })
  337. |> Repo.insert()
  338. # Add a remote activity which references an activity
  339. %Activity{}
  340. |> Map.merge(%{
  341. local: false,
  342. data: %{
  343. "id" => "remote_activity_with_activity",
  344. "object" => "remote_activity_with_object"
  345. }
  346. })
  347. |> Repo.insert()
  348. # Add a remote activity which references an Actor
  349. %User{} |> Map.merge(%{ap_id: "actor"}) |> Repo.insert()
  350. %Activity{}
  351. |> Map.merge(%{
  352. local: false,
  353. data: %{"id" => "remote_activity_with_actor", "object" => "actor"}
  354. })
  355. |> Repo.insert()
  356. # Add a remote activity without existing referenced object, activity or actor
  357. %Activity{}
  358. |> Map.merge(%{
  359. local: false,
  360. data: %{
  361. "id" => "remote_activity_without_existing_referenced_object",
  362. "object" => "non_existing"
  363. }
  364. })
  365. |> Repo.insert()
  366. # Add a local activity without existing referenced object, activity or actor
  367. %Activity{}
  368. |> Map.merge(%{
  369. local: true,
  370. data: %{"id" => "local_activity_with_actor", "object" => "non_existing"}
  371. })
  372. |> Repo.insert()
  373. # The remote activities without existing reference,
  374. # and only the remote activities without existing reference, are deleted
  375. # if, and only if, we provide the --prune-orphaned-activities option
  376. assert length(Repo.all(Activity)) == 5
  377. Mix.Tasks.Pleroma.Database.run(["prune_objects"])
  378. assert length(Repo.all(Activity)) == 5
  379. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--prune-orphaned-activities"])
  380. activities = Repo.all(Activity)
  381. assert "remote_activity_without_existing_referenced_object" not in Enum.map(
  382. activities,
  383. fn a -> a.data["id"] end
  384. )
  385. assert length(activities) == 4
  386. end
  387. test "it prunes orphaned activities with the --prune-orphaned-activities when the objects are referenced from an array" do
  388. %Object{} |> Map.merge(%{data: %{"id" => "existing_object"}}) |> Repo.insert()
  389. %User{} |> Map.merge(%{ap_id: "existing_actor"}) |> Repo.insert()
  390. # Multiple objects, one object exists (keep)
  391. %Activity{}
  392. |> Map.merge(%{
  393. local: false,
  394. data: %{
  395. "id" => "remote_activity_existing_object",
  396. "object" => ["non_ existing_object", "existing_object"]
  397. }
  398. })
  399. |> Repo.insert()
  400. # Multiple objects, one actor exists (keep)
  401. %Activity{}
  402. |> Map.merge(%{
  403. local: false,
  404. data: %{
  405. "id" => "remote_activity_existing_actor",
  406. "object" => ["non_ existing_object", "existing_actor"]
  407. }
  408. })
  409. |> Repo.insert()
  410. # Multiple objects, one activity exists (keep)
  411. %Activity{}
  412. |> Map.merge(%{
  413. local: false,
  414. data: %{
  415. "id" => "remote_activity_existing_activity",
  416. "object" => ["non_ existing_object", "remote_activity_existing_actor"]
  417. }
  418. })
  419. |> Repo.insert()
  420. # Multiple objects none exist (prune)
  421. %Activity{}
  422. |> Map.merge(%{
  423. local: false,
  424. data: %{
  425. "id" => "remote_activity_without_existing_referenced_object",
  426. "object" => ["owo", "whats_this"]
  427. }
  428. })
  429. |> Repo.insert()
  430. assert length(Repo.all(Activity)) == 4
  431. Mix.Tasks.Pleroma.Database.run(["prune_objects"])
  432. assert length(Repo.all(Activity)) == 4
  433. Mix.Tasks.Pleroma.Database.run(["prune_objects", "--prune-orphaned-activities"])
  434. activities = Repo.all(Activity)
  435. assert length(activities) == 3
  436. assert "remote_activity_without_existing_referenced_object" not in Enum.map(
  437. activities,
  438. fn a -> a.data["id"] end
  439. )
  440. assert length(activities) == 3
  441. end
  442. end
  443. describe "running update_users_following_followers_counts" do
  444. test "following and followers count are updated" do
  445. [user, user2] = insert_pair(:user)
  446. {:ok, %User{} = user, _user2} = User.follow(user, user2)
  447. following = User.following(user)
  448. assert length(following) == 2
  449. assert user.follower_count == 0
  450. {:ok, user} =
  451. user
  452. |> Ecto.Changeset.change(%{follower_count: 3})
  453. |> Repo.update()
  454. assert user.follower_count == 3
  455. assert {:ok, :ok} ==
  456. Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
  457. user = User.get_by_id(user.id)
  458. assert length(User.following(user)) == 2
  459. assert user.follower_count == 0
  460. end
  461. end
  462. describe "running fix_likes_collections" do
  463. test "it turns OrderedCollection likes into empty arrays" do
  464. [user, user2] = insert_pair(:user)
  465. {:ok, %{id: id, object: object}} = CommonAPI.post(user, %{status: "test"})
  466. {:ok, %{object: object2}} = CommonAPI.post(user, %{status: "test test"})
  467. CommonAPI.favorite(id, user2)
  468. likes = %{
  469. "first" =>
  470. "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
  471. "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
  472. "totalItems" => 3,
  473. "type" => "OrderedCollection"
  474. }
  475. new_data = Map.put(object2.data, "likes", likes)
  476. object2
  477. |> Ecto.Changeset.change(%{data: new_data})
  478. |> Repo.update()
  479. assert length(Object.get_by_id(object.id).data["likes"]) == 1
  480. assert is_map(Object.get_by_id(object2.id).data["likes"])
  481. assert :ok == Mix.Tasks.Pleroma.Database.run(["fix_likes_collections"])
  482. assert length(Object.get_by_id(object.id).data["likes"]) == 1
  483. assert Enum.empty?(Object.get_by_id(object2.id).data["likes"])
  484. end
  485. end
  486. describe "ensure_expiration" do
  487. test "it adds to expiration old statuses" do
  488. activity1 = insert(:note_activity)
  489. {:ok, inserted_at, 0} = DateTime.from_iso8601("2015-01-23T23:50:07Z")
  490. activity2 = insert(:note_activity, %{inserted_at: inserted_at})
  491. %{id: activity_id3} = insert(:note_activity)
  492. expires_at = DateTime.add(DateTime.utc_now(), 60 * 61)
  493. Pleroma.Workers.PurgeExpiredActivity.enqueue(
  494. %{
  495. activity_id: activity_id3
  496. },
  497. scheduled_at: expires_at
  498. )
  499. Mix.Tasks.Pleroma.Database.run(["ensure_expiration"])
  500. assert_enqueued(
  501. worker: Pleroma.Workers.PurgeExpiredActivity,
  502. args: %{activity_id: activity1.id},
  503. scheduled_at:
  504. activity1.inserted_at
  505. |> DateTime.from_naive!("Etc/UTC")
  506. |> Timex.shift(days: 365)
  507. )
  508. assert_enqueued(
  509. worker: Pleroma.Workers.PurgeExpiredActivity,
  510. args: %{activity_id: activity2.id},
  511. scheduled_at:
  512. activity2.inserted_at
  513. |> DateTime.from_naive!("Etc/UTC")
  514. |> Timex.shift(days: 365)
  515. )
  516. assert_enqueued(
  517. worker: Pleroma.Workers.PurgeExpiredActivity,
  518. args: %{activity_id: activity_id3},
  519. scheduled_at: expires_at
  520. )
  521. end
  522. end
  523. end