Local copy of Pleroma, an ActivityPub server software. Contains modifications running live on fedi.underscore.world
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

factory.ex 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 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. alias Pleroma.Object
  7. alias Pleroma.User
  8. def participation_factory do
  9. conversation = insert(:conversation)
  10. user = insert(:user)
  11. %Pleroma.Conversation.Participation{
  12. conversation: conversation,
  13. user: user,
  14. read: false
  15. }
  16. end
  17. def conversation_factory do
  18. %Pleroma.Conversation{
  19. ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
  20. }
  21. end
  22. def user_factory do
  23. user = %User{
  24. name: sequence(:name, &"Test テスト User #{&1}"),
  25. email: sequence(:email, &"user#{&1}@example.com"),
  26. nickname: sequence(:nickname, &"nick#{&1}"),
  27. password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
  28. bio: sequence(:bio, &"Tester Number #{&1}"),
  29. info: %{},
  30. last_digest_emailed_at: NaiveDateTime.utc_now()
  31. }
  32. %{
  33. user
  34. | ap_id: User.ap_id(user),
  35. follower_address: User.ap_followers(user),
  36. following_address: User.ap_following(user),
  37. following: [User.ap_id(user)]
  38. }
  39. end
  40. def note_factory(attrs \\ %{}) do
  41. text = sequence(:text, &"This is :moominmamma: note #{&1}")
  42. user = attrs[:user] || insert(:user)
  43. data = %{
  44. "type" => "Note",
  45. "content" => text,
  46. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  47. "actor" => user.ap_id,
  48. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  49. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  50. "likes" => [],
  51. "like_count" => 0,
  52. "context" => "2hu",
  53. "summary" => "2hu",
  54. "tag" => ["2hu"],
  55. "emoji" => %{
  56. "2hu" => "corndog.png"
  57. }
  58. }
  59. %Pleroma.Object{
  60. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  61. }
  62. end
  63. def direct_note_factory do
  64. user2 = insert(:user)
  65. %Pleroma.Object{data: data} = note_factory()
  66. %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
  67. end
  68. def article_factory do
  69. note_factory()
  70. |> Map.put("type", "Article")
  71. end
  72. def tombstone_factory do
  73. data = %{
  74. "type" => "Tombstone",
  75. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  76. "formerType" => "Note",
  77. "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
  78. }
  79. %Pleroma.Object{
  80. data: data
  81. }
  82. end
  83. def direct_note_activity_factory do
  84. dm = insert(:direct_note)
  85. data = %{
  86. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  87. "type" => "Create",
  88. "actor" => dm.data["actor"],
  89. "to" => dm.data["to"],
  90. "object" => dm.data,
  91. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  92. "context" => dm.data["context"]
  93. }
  94. %Pleroma.Activity{
  95. data: data,
  96. actor: data["actor"],
  97. recipients: data["to"]
  98. }
  99. end
  100. def note_activity_factory(attrs \\ %{}) do
  101. user = attrs[:user] || insert(:user)
  102. note = attrs[:note] || insert(:note, user: user)
  103. data_attrs = attrs[:data_attrs] || %{}
  104. attrs = Map.drop(attrs, [:user, :note, :data_attrs])
  105. data =
  106. %{
  107. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  108. "type" => "Create",
  109. "actor" => note.data["actor"],
  110. "to" => note.data["to"],
  111. "object" => note.data["id"],
  112. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  113. "context" => note.data["context"]
  114. }
  115. |> Map.merge(data_attrs)
  116. %Pleroma.Activity{
  117. data: data,
  118. actor: data["actor"],
  119. recipients: data["to"]
  120. }
  121. |> Map.merge(attrs)
  122. end
  123. def article_activity_factory do
  124. article = insert(:article)
  125. data = %{
  126. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  127. "type" => "Create",
  128. "actor" => article.data["actor"],
  129. "to" => article.data["to"],
  130. "object" => article.data,
  131. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  132. "context" => article.data["context"]
  133. }
  134. %Pleroma.Activity{
  135. data: data,
  136. actor: data["actor"],
  137. recipients: data["to"]
  138. }
  139. end
  140. def announce_activity_factory(attrs \\ %{}) do
  141. note_activity = attrs[:note_activity] || insert(:note_activity)
  142. user = attrs[:user] || insert(:user)
  143. data = %{
  144. "type" => "Announce",
  145. "actor" => note_activity.actor,
  146. "object" => note_activity.data["id"],
  147. "to" => [user.follower_address, note_activity.data["actor"]],
  148. "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
  149. "context" => note_activity.data["context"]
  150. }
  151. %Pleroma.Activity{
  152. data: data,
  153. actor: user.ap_id,
  154. recipients: data["to"]
  155. }
  156. end
  157. def like_activity_factory(attrs \\ %{}) do
  158. note_activity = attrs[:note_activity] || insert(:note_activity)
  159. object = Object.normalize(note_activity)
  160. user = insert(:user)
  161. data = %{
  162. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  163. "actor" => user.ap_id,
  164. "type" => "Like",
  165. "object" => object.data["id"],
  166. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  167. }
  168. %Pleroma.Activity{
  169. data: data
  170. }
  171. end
  172. def follow_activity_factory do
  173. follower = insert(:user)
  174. followed = insert(:user)
  175. data = %{
  176. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  177. "actor" => follower.ap_id,
  178. "type" => "Follow",
  179. "object" => followed.ap_id,
  180. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  181. }
  182. %Pleroma.Activity{
  183. data: data,
  184. actor: follower.ap_id
  185. }
  186. end
  187. def websub_subscription_factory do
  188. %Pleroma.Web.Websub.WebsubServerSubscription{
  189. topic: "http://example.org",
  190. callback: "http://example.org/callback",
  191. secret: "here's a secret",
  192. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
  193. state: "requested"
  194. }
  195. end
  196. def websub_client_subscription_factory do
  197. %Pleroma.Web.Websub.WebsubClientSubscription{
  198. topic: "http://example.org",
  199. secret: "here's a secret",
  200. valid_until: nil,
  201. state: "requested",
  202. subscribers: []
  203. }
  204. end
  205. def oauth_app_factory do
  206. %Pleroma.Web.OAuth.App{
  207. client_name: "Some client",
  208. redirect_uris: "https://example.com/callback",
  209. scopes: ["read", "write", "follow", "push"],
  210. website: "https://example.com",
  211. client_id: Ecto.UUID.generate(),
  212. client_secret: "aaa;/&bbb"
  213. }
  214. end
  215. def instance_factory do
  216. %Pleroma.Instances.Instance{
  217. host: "domain.com",
  218. unreachable_since: nil
  219. }
  220. end
  221. def oauth_token_factory do
  222. oauth_app = insert(:oauth_app)
  223. %Pleroma.Web.OAuth.Token{
  224. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  225. refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  226. user: build(:user),
  227. app_id: oauth_app.id,
  228. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
  229. }
  230. end
  231. def oauth_authorization_factory do
  232. %Pleroma.Web.OAuth.Authorization{
  233. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
  234. scopes: ["read", "write", "follow", "push"],
  235. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
  236. user: build(:user),
  237. app: build(:oauth_app)
  238. }
  239. end
  240. def push_subscription_factory do
  241. %Pleroma.Web.Push.Subscription{
  242. user: build(:user),
  243. token: build(:oauth_token),
  244. endpoint: "https://example.com/example/1234",
  245. key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
  246. key_p256dh:
  247. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
  248. data: %{}
  249. }
  250. end
  251. def notification_factory do
  252. %Pleroma.Notification{
  253. user: build(:user)
  254. }
  255. end
  256. def scheduled_activity_factory do
  257. %Pleroma.ScheduledActivity{
  258. user: build(:user),
  259. scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
  260. params: build(:note) |> Map.from_struct() |> Map.get(:data)
  261. }
  262. end
  263. def registration_factory do
  264. user = insert(:user)
  265. %Pleroma.Registration{
  266. user: user,
  267. provider: "twitter",
  268. uid: "171799000",
  269. info: %{
  270. "name" => "John Doe",
  271. "email" => "john@doe.com",
  272. "nickname" => "johndoe",
  273. "description" => "My bio"
  274. }
  275. }
  276. end
  277. def config_factory do
  278. %Pleroma.Web.AdminAPI.Config{
  279. key: sequence(:key, &"some_key_#{&1}"),
  280. group: "pleroma",
  281. value:
  282. sequence(
  283. :value,
  284. fn key ->
  285. :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
  286. end
  287. )
  288. }
  289. end
  290. end