logo

pleroma

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

factory.ex (18158B)


  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 Pleroma.Factory do
  5. use ExMachina.Ecto, repo: Pleroma.Repo
  6. require Pleroma.Constants
  7. alias Pleroma.Object
  8. alias Pleroma.User
  9. @rsa_keys [
  10. "test/fixtures/rsa_keys/key_1.pem",
  11. "test/fixtures/rsa_keys/key_2.pem",
  12. "test/fixtures/rsa_keys/key_3.pem",
  13. "test/fixtures/rsa_keys/key_4.pem",
  14. "test/fixtures/rsa_keys/key_5.pem"
  15. ]
  16. |> Enum.map(&File.read!/1)
  17. def participation_factory do
  18. conversation = insert(:conversation)
  19. user = insert(:user)
  20. %Pleroma.Conversation.Participation{
  21. conversation: conversation,
  22. user: user,
  23. read: false
  24. }
  25. end
  26. def conversation_factory do
  27. %Pleroma.Conversation{
  28. ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
  29. }
  30. end
  31. def user_factory(attrs \\ %{}) do
  32. pem = Enum.random(@rsa_keys)
  33. user = %User{
  34. name: sequence(:name, &"Test テスト User #{&1}"),
  35. email: sequence(:email, &"user#{&1}@example.com"),
  36. nickname: sequence(:nickname, &"nick#{&1}"),
  37. password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
  38. bio: sequence(:bio, &"Tester Number #{&1}"),
  39. is_discoverable: true,
  40. last_digest_emailed_at: NaiveDateTime.utc_now(),
  41. last_refreshed_at: NaiveDateTime.utc_now(),
  42. notification_settings: %Pleroma.User.NotificationSetting{},
  43. multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
  44. keys: pem
  45. }
  46. urls =
  47. if attrs[:local] == false do
  48. base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
  49. ap_id = "https://#{base_domain}/users/#{user.nickname}"
  50. %{
  51. ap_id: ap_id,
  52. follower_address: ap_id <> "/followers",
  53. following_address: ap_id <> "/following",
  54. featured_address: ap_id <> "/collections/featured"
  55. }
  56. else
  57. %{
  58. ap_id: User.ap_id(user),
  59. follower_address: User.ap_followers(user),
  60. following_address: User.ap_following(user),
  61. featured_address: User.ap_featured_collection(user)
  62. }
  63. end
  64. attrs = Map.delete(attrs, :domain)
  65. user
  66. |> Map.put(:raw_bio, user.bio)
  67. |> Map.merge(urls)
  68. |> merge_attributes(attrs)
  69. end
  70. def user_relationship_factory(attrs \\ %{}) do
  71. source = attrs[:source] || insert(:user)
  72. target = attrs[:target] || insert(:user)
  73. relationship_type = attrs[:relationship_type] || :block
  74. %Pleroma.UserRelationship{
  75. source_id: source.id,
  76. target_id: target.id,
  77. relationship_type: relationship_type
  78. }
  79. end
  80. def note_factory(attrs \\ %{}) do
  81. text = sequence(:text, &"This is :moominmamma: note #{&1}")
  82. user = attrs[:user] || insert(:user)
  83. data = %{
  84. "type" => "Note",
  85. "content" => text,
  86. "source" => text,
  87. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  88. "actor" => user.ap_id,
  89. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  90. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  91. "likes" => [],
  92. "like_count" => 0,
  93. "context" => "2hu",
  94. "summary" => "2hu",
  95. "tag" => ["2hu"],
  96. "emoji" => %{
  97. "2hu" => "corndog.png"
  98. }
  99. }
  100. %Pleroma.Object{
  101. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  102. }
  103. end
  104. def attachment_factory(attrs \\ %{}) do
  105. user = attrs[:user] || insert(:user)
  106. data =
  107. attachment_data(user.ap_id, nil)
  108. |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
  109. %Pleroma.Object{
  110. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  111. }
  112. end
  113. def attachment_note_factory(attrs \\ %{}) do
  114. user = attrs[:user] || insert(:user)
  115. {length, attrs} = Map.pop(attrs, :length, 1)
  116. data = %{
  117. "attachment" =>
  118. Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
  119. |> Enum.take(length)
  120. }
  121. build(:note, Map.put(attrs, :data, data))
  122. end
  123. defp attachment_data(ap_id, href) do
  124. href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
  125. %{
  126. "url" => [
  127. %{
  128. "href" => href,
  129. "type" => "Link",
  130. "mediaType" => "image/jpeg"
  131. }
  132. ],
  133. "name" => "some name",
  134. "type" => "Document",
  135. "actor" => ap_id,
  136. "mediaType" => "image/jpeg"
  137. }
  138. end
  139. def followers_only_note_factory(attrs \\ %{}) do
  140. %Pleroma.Object{data: data} = note_factory(attrs)
  141. %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
  142. end
  143. def audio_factory(attrs \\ %{}) do
  144. text = sequence(:text, &"lain radio episode #{&1}")
  145. user = attrs[:user] || insert(:user)
  146. data = %{
  147. "type" => "Audio",
  148. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  149. "artist" => "lain",
  150. "title" => text,
  151. "album" => "lain radio",
  152. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  153. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  154. "actor" => user.ap_id,
  155. "length" => 180_000
  156. }
  157. %Pleroma.Object{
  158. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  159. }
  160. end
  161. def listen_factory do
  162. audio = insert(:audio)
  163. data = %{
  164. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  165. "type" => "Listen",
  166. "actor" => audio.data["actor"],
  167. "to" => audio.data["to"],
  168. "object" => audio.data,
  169. "published" => audio.data["published"]
  170. }
  171. %Pleroma.Activity{
  172. data: data,
  173. actor: data["actor"],
  174. recipients: data["to"]
  175. }
  176. end
  177. def direct_note_factory do
  178. user2 = insert(:user, local: false, inbox: "http://example.com/inbox")
  179. %Pleroma.Object{data: data} = note_factory()
  180. %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
  181. end
  182. def article_factory do
  183. %Pleroma.Object{data: data} = note_factory()
  184. %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
  185. end
  186. def tombstone_factory do
  187. data = %{
  188. "type" => "Tombstone",
  189. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  190. "formerType" => "Note",
  191. "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
  192. }
  193. %Pleroma.Object{
  194. data: data
  195. }
  196. end
  197. def question_factory(attrs \\ %{}) do
  198. user = attrs[:user] || insert(:user)
  199. data = %{
  200. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  201. "type" => "Question",
  202. "actor" => user.ap_id,
  203. "attributedTo" => user.ap_id,
  204. "attachment" => [],
  205. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  206. "cc" => [user.follower_address],
  207. "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
  208. "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
  209. "oneOf" => [
  210. %{
  211. "type" => "Note",
  212. "name" => "chocolate",
  213. "replies" => %{"totalItems" => 0, "type" => "Collection"}
  214. },
  215. %{
  216. "type" => "Note",
  217. "name" => "vanilla",
  218. "replies" => %{"totalItems" => 0, "type" => "Collection"}
  219. }
  220. ]
  221. }
  222. %Pleroma.Object{
  223. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  224. }
  225. end
  226. def direct_note_activity_factory do
  227. dm = insert(:direct_note)
  228. data = %{
  229. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  230. "type" => "Create",
  231. "actor" => dm.data["actor"],
  232. "to" => dm.data["to"],
  233. "object" => dm.data,
  234. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  235. "context" => dm.data["context"]
  236. }
  237. %Pleroma.Activity{
  238. data: data,
  239. actor: data["actor"],
  240. recipients: data["to"]
  241. }
  242. end
  243. def add_activity_factory(attrs \\ %{}) do
  244. featured_collection_activity(attrs, "Add")
  245. end
  246. def remove_activity_factor(attrs \\ %{}) do
  247. featured_collection_activity(attrs, "Remove")
  248. end
  249. defp featured_collection_activity(attrs, type) do
  250. user = attrs[:user] || insert(:user)
  251. note = attrs[:note] || insert(:note, user: user)
  252. data_attrs =
  253. attrs
  254. |> Map.get(:data_attrs, %{})
  255. |> Map.put(:type, type)
  256. attrs = Map.drop(attrs, [:user, :note, :data_attrs])
  257. data =
  258. %{
  259. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  260. "target" => user.featured_address,
  261. "object" => note.data["object"],
  262. "actor" => note.data["actor"],
  263. "type" => "Add",
  264. "to" => [Pleroma.Constants.as_public()],
  265. "cc" => [user.follower_address]
  266. }
  267. |> Map.merge(data_attrs)
  268. %Pleroma.Activity{
  269. data: data,
  270. actor: data["actor"],
  271. recipients: data["to"]
  272. }
  273. |> Map.merge(attrs)
  274. end
  275. def followers_only_note_activity_factory(attrs \\ %{}) do
  276. user = attrs[:user] || insert(:user)
  277. note = insert(:followers_only_note, user: user)
  278. data_attrs = attrs[:data_attrs] || %{}
  279. attrs = Map.drop(attrs, [:user, :note, :data_attrs])
  280. data =
  281. %{
  282. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  283. "type" => "Create",
  284. "actor" => note.data["actor"],
  285. "to" => note.data["to"],
  286. "object" => note.data,
  287. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  288. "context" => note.data["context"]
  289. }
  290. |> Map.merge(data_attrs)
  291. %Pleroma.Activity{
  292. data: data,
  293. actor: data["actor"],
  294. recipients: data["to"]
  295. }
  296. |> Map.merge(attrs)
  297. end
  298. def note_activity_factory(attrs \\ %{}) do
  299. user = attrs[:user] || insert(:user)
  300. note = attrs[:note] || insert(:note, user: user)
  301. data_attrs = attrs[:data_attrs] || %{}
  302. attrs = Map.drop(attrs, [:user, :note, :data_attrs])
  303. data =
  304. %{
  305. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  306. "type" => "Create",
  307. "actor" => note.data["actor"],
  308. "to" => note.data["to"],
  309. "object" => note.data["id"],
  310. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  311. "context" => note.data["context"]
  312. }
  313. |> Map.merge(data_attrs)
  314. %Pleroma.Activity{
  315. data: data,
  316. actor: data["actor"],
  317. recipients: data["to"]
  318. }
  319. |> Map.merge(attrs)
  320. end
  321. def article_activity_factory do
  322. article = insert(:article)
  323. data = %{
  324. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  325. "type" => "Create",
  326. "actor" => article.data["actor"],
  327. "to" => article.data["to"],
  328. "object" => article.data,
  329. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  330. "context" => article.data["context"]
  331. }
  332. %Pleroma.Activity{
  333. data: data,
  334. actor: data["actor"],
  335. recipients: data["to"]
  336. }
  337. end
  338. def announce_activity_factory(attrs \\ %{}) do
  339. note_activity = attrs[:note_activity] || insert(:note_activity)
  340. user = attrs[:user] || insert(:user)
  341. data = %{
  342. "type" => "Announce",
  343. "actor" => note_activity.actor,
  344. "object" => note_activity.data["id"],
  345. "to" => [user.follower_address, note_activity.data["actor"]],
  346. "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
  347. "context" => note_activity.data["context"]
  348. }
  349. %Pleroma.Activity{
  350. data: data,
  351. actor: user.ap_id,
  352. recipients: data["to"]
  353. }
  354. end
  355. def like_activity_factory(attrs \\ %{}) do
  356. note_activity = attrs[:note_activity] || insert(:note_activity)
  357. object = Object.normalize(note_activity, fetch: false)
  358. user = insert(:user)
  359. data =
  360. %{
  361. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  362. "actor" => user.ap_id,
  363. "type" => "Like",
  364. "object" => object.data["id"],
  365. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  366. }
  367. |> Map.merge(attrs[:data_attrs] || %{})
  368. %Pleroma.Activity{
  369. data: data
  370. }
  371. end
  372. def follow_activity_factory do
  373. follower = insert(:user)
  374. followed = insert(:user)
  375. data = %{
  376. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  377. "actor" => follower.ap_id,
  378. "type" => "Follow",
  379. "object" => followed.ap_id,
  380. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  381. }
  382. %Pleroma.Activity{
  383. data: data,
  384. actor: follower.ap_id
  385. }
  386. end
  387. def report_activity_factory(attrs \\ %{}) do
  388. user = attrs[:user] || insert(:user)
  389. activity = attrs[:activity] || insert(:note_activity)
  390. state = attrs[:state] || "open"
  391. data = %{
  392. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  393. "actor" => user.ap_id,
  394. "type" => "Flag",
  395. "object" => [activity.actor, activity.data["id"]],
  396. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  397. "to" => [],
  398. "cc" => [activity.actor],
  399. "context" => activity.data["context"],
  400. "state" => state
  401. }
  402. %Pleroma.Activity{
  403. data: data,
  404. actor: data["actor"],
  405. recipients: data["to"] ++ data["cc"]
  406. }
  407. end
  408. def question_activity_factory(attrs \\ %{}) do
  409. user = attrs[:user] || insert(:user)
  410. question = attrs[:question] || insert(:question, user: user)
  411. data_attrs = attrs[:data_attrs] || %{}
  412. attrs = Map.drop(attrs, [:user, :question, :data_attrs])
  413. data =
  414. %{
  415. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  416. "type" => "Create",
  417. "actor" => question.data["actor"],
  418. "to" => question.data["to"],
  419. "object" => question.data["id"],
  420. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  421. "context" => question.data["context"]
  422. }
  423. |> Map.merge(data_attrs)
  424. %Pleroma.Activity{
  425. data: data,
  426. actor: data["actor"],
  427. recipients: data["to"]
  428. }
  429. |> Map.merge(attrs)
  430. end
  431. def oauth_app_factory do
  432. %Pleroma.Web.OAuth.App{
  433. client_name: sequence(:client_name, &"Some client #{&1}"),
  434. redirect_uris: "https://example.com/callback",
  435. scopes: ["read", "write", "follow", "push", "admin"],
  436. website: "https://example.com",
  437. client_id: Ecto.UUID.generate(),
  438. client_secret: "aaa;/&bbb"
  439. }
  440. end
  441. def instance_factory do
  442. %Pleroma.Instances.Instance{
  443. host: "domain.com",
  444. unreachable_since: nil
  445. }
  446. end
  447. def oauth_token_factory(attrs \\ %{}) do
  448. scopes = Map.get(attrs, :scopes, ["read"])
  449. oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
  450. user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
  451. valid_until =
  452. Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
  453. %Pleroma.Web.OAuth.Token{
  454. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  455. refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  456. scopes: scopes,
  457. user: user,
  458. app: oauth_app,
  459. valid_until: valid_until
  460. }
  461. end
  462. def oauth_admin_token_factory(attrs \\ %{}) do
  463. user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
  464. scopes =
  465. attrs
  466. |> Map.get(:scopes, ["admin"])
  467. |> Kernel.++(["admin"])
  468. |> Enum.uniq()
  469. attrs = Map.merge(attrs, %{user: user, scopes: scopes})
  470. oauth_token_factory(attrs)
  471. end
  472. def oauth_authorization_factory do
  473. %Pleroma.Web.OAuth.Authorization{
  474. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
  475. scopes: ["read", "write", "follow", "push"],
  476. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
  477. user: build(:user),
  478. app: build(:oauth_app)
  479. }
  480. end
  481. def push_subscription_factory do
  482. %Pleroma.Web.Push.Subscription{
  483. user: build(:user),
  484. token: build(:oauth_token),
  485. endpoint: "https://example.com/example/1234",
  486. key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
  487. key_p256dh:
  488. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
  489. data: %{}
  490. }
  491. end
  492. def notification_factory do
  493. %Pleroma.Notification{
  494. user: build(:user)
  495. }
  496. end
  497. def scheduled_activity_factory do
  498. %Pleroma.ScheduledActivity{
  499. user: build(:user),
  500. scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
  501. params: build(:note) |> Map.from_struct() |> Map.get(:data)
  502. }
  503. end
  504. def registration_factory do
  505. user = insert(:user)
  506. %Pleroma.Registration{
  507. user: user,
  508. provider: "twitter",
  509. uid: "171799000",
  510. info: %{
  511. "name" => "John Doe",
  512. "email" => "john@doe.com",
  513. "nickname" => "johndoe",
  514. "description" => "My bio"
  515. }
  516. }
  517. end
  518. def config_factory(attrs \\ %{}) do
  519. %Pleroma.ConfigDB{
  520. key: sequence(:key, &String.to_atom("some_key_#{&1}")),
  521. group: :pleroma,
  522. value:
  523. sequence(
  524. :value,
  525. &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
  526. )
  527. }
  528. |> merge_attributes(attrs)
  529. end
  530. def marker_factory do
  531. %Pleroma.Marker{
  532. user: build(:user),
  533. timeline: "notifications",
  534. lock_version: 0,
  535. last_read_id: "1"
  536. }
  537. end
  538. def mfa_token_factory do
  539. %Pleroma.MFA.Token{
  540. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
  541. authorization: build(:oauth_authorization),
  542. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
  543. user: build(:user)
  544. }
  545. end
  546. def filter_factory do
  547. %Pleroma.Filter{
  548. user: build(:user),
  549. filter_id: sequence(:filter_id, & &1),
  550. phrase: "cofe",
  551. context: ["home"]
  552. }
  553. end
  554. def announcement_factory(params \\ %{}) do
  555. data = Map.get(params, :data, %{})
  556. {_, params} = Map.pop(params, :data)
  557. %Pleroma.Announcement{
  558. data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
  559. }
  560. |> Map.merge(params)
  561. |> Pleroma.Announcement.add_rendered_properties()
  562. end
  563. end