logo

pleroma

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

factory.ex (18612B)


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