Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
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.

336 lines
8.6KB

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