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.

389 lines
9.8KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 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. }
  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 audio_factory(attrs \\ %{}) do
  63. text = sequence(:text, &"lain radio episode #{&1}")
  64. user = attrs[:user] || insert(:user)
  65. data = %{
  66. "type" => "Audio",
  67. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  68. "artist" => "lain",
  69. "title" => text,
  70. "album" => "lain radio",
  71. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  72. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  73. "actor" => user.ap_id,
  74. "length" => 180_000
  75. }
  76. %Pleroma.Object{
  77. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  78. }
  79. end
  80. def listen_factory do
  81. audio = insert(:audio)
  82. data = %{
  83. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  84. "type" => "Listen",
  85. "actor" => audio.data["actor"],
  86. "to" => audio.data["to"],
  87. "object" => audio.data,
  88. "published" => audio.data["published"]
  89. }
  90. %Pleroma.Activity{
  91. data: data,
  92. actor: data["actor"],
  93. recipients: data["to"]
  94. }
  95. end
  96. def direct_note_factory do
  97. user2 = insert(:user)
  98. %Pleroma.Object{data: data} = note_factory()
  99. %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
  100. end
  101. def article_factory do
  102. note_factory()
  103. |> Map.put("type", "Article")
  104. end
  105. def tombstone_factory do
  106. data = %{
  107. "type" => "Tombstone",
  108. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  109. "formerType" => "Note",
  110. "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
  111. }
  112. %Pleroma.Object{
  113. data: data
  114. }
  115. end
  116. def direct_note_activity_factory do
  117. dm = insert(:direct_note)
  118. data = %{
  119. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  120. "type" => "Create",
  121. "actor" => dm.data["actor"],
  122. "to" => dm.data["to"],
  123. "object" => dm.data,
  124. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  125. "context" => dm.data["context"]
  126. }
  127. %Pleroma.Activity{
  128. data: data,
  129. actor: data["actor"],
  130. recipients: data["to"]
  131. }
  132. end
  133. def note_activity_factory(attrs \\ %{}) do
  134. user = attrs[:user] || insert(:user)
  135. note = attrs[:note] || insert(:note, user: user)
  136. data_attrs = attrs[:data_attrs] || %{}
  137. attrs = Map.drop(attrs, [:user, :note, :data_attrs])
  138. data =
  139. %{
  140. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  141. "type" => "Create",
  142. "actor" => note.data["actor"],
  143. "to" => note.data["to"],
  144. "object" => note.data["id"],
  145. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  146. "context" => note.data["context"]
  147. }
  148. |> Map.merge(data_attrs)
  149. %Pleroma.Activity{
  150. data: data,
  151. actor: data["actor"],
  152. recipients: data["to"]
  153. }
  154. |> Map.merge(attrs)
  155. end
  156. defp expiration_offset_by_minutes(attrs, minutes) do
  157. scheduled_at =
  158. NaiveDateTime.utc_now()
  159. |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
  160. |> NaiveDateTime.truncate(:second)
  161. %Pleroma.ActivityExpiration{}
  162. |> Map.merge(attrs)
  163. |> Map.put(:scheduled_at, scheduled_at)
  164. end
  165. def expiration_in_the_past_factory(attrs \\ %{}) do
  166. expiration_offset_by_minutes(attrs, -60)
  167. end
  168. def expiration_in_the_future_factory(attrs \\ %{}) do
  169. expiration_offset_by_minutes(attrs, 61)
  170. end
  171. def article_activity_factory do
  172. article = insert(:article)
  173. data = %{
  174. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  175. "type" => "Create",
  176. "actor" => article.data["actor"],
  177. "to" => article.data["to"],
  178. "object" => article.data,
  179. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  180. "context" => article.data["context"]
  181. }
  182. %Pleroma.Activity{
  183. data: data,
  184. actor: data["actor"],
  185. recipients: data["to"]
  186. }
  187. end
  188. def announce_activity_factory(attrs \\ %{}) do
  189. note_activity = attrs[:note_activity] || insert(:note_activity)
  190. user = attrs[:user] || insert(:user)
  191. data = %{
  192. "type" => "Announce",
  193. "actor" => note_activity.actor,
  194. "object" => note_activity.data["id"],
  195. "to" => [user.follower_address, note_activity.data["actor"]],
  196. "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
  197. "context" => note_activity.data["context"]
  198. }
  199. %Pleroma.Activity{
  200. data: data,
  201. actor: user.ap_id,
  202. recipients: data["to"]
  203. }
  204. end
  205. def like_activity_factory(attrs \\ %{}) do
  206. note_activity = attrs[:note_activity] || insert(:note_activity)
  207. object = Object.normalize(note_activity)
  208. user = insert(:user)
  209. data =
  210. %{
  211. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  212. "actor" => user.ap_id,
  213. "type" => "Like",
  214. "object" => object.data["id"],
  215. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  216. }
  217. |> Map.merge(attrs[:data_attrs] || %{})
  218. %Pleroma.Activity{
  219. data: data
  220. }
  221. end
  222. def follow_activity_factory do
  223. follower = insert(:user)
  224. followed = insert(:user)
  225. data = %{
  226. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  227. "actor" => follower.ap_id,
  228. "type" => "Follow",
  229. "object" => followed.ap_id,
  230. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  231. }
  232. %Pleroma.Activity{
  233. data: data,
  234. actor: follower.ap_id
  235. }
  236. end
  237. def oauth_app_factory do
  238. %Pleroma.Web.OAuth.App{
  239. client_name: "Some client",
  240. redirect_uris: "https://example.com/callback",
  241. scopes: ["read", "write", "follow", "push"],
  242. website: "https://example.com",
  243. client_id: Ecto.UUID.generate(),
  244. client_secret: "aaa;/&bbb"
  245. }
  246. end
  247. def instance_factory do
  248. %Pleroma.Instances.Instance{
  249. host: "domain.com",
  250. unreachable_since: nil
  251. }
  252. end
  253. def oauth_token_factory do
  254. oauth_app = insert(:oauth_app)
  255. %Pleroma.Web.OAuth.Token{
  256. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  257. scopes: ["read"],
  258. refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  259. user: build(:user),
  260. app_id: oauth_app.id,
  261. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
  262. }
  263. end
  264. def oauth_authorization_factory do
  265. %Pleroma.Web.OAuth.Authorization{
  266. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
  267. scopes: ["read", "write", "follow", "push"],
  268. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
  269. user: build(:user),
  270. app: build(:oauth_app)
  271. }
  272. end
  273. def push_subscription_factory do
  274. %Pleroma.Web.Push.Subscription{
  275. user: build(:user),
  276. token: build(:oauth_token),
  277. endpoint: "https://example.com/example/1234",
  278. key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
  279. key_p256dh:
  280. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
  281. data: %{}
  282. }
  283. end
  284. def notification_factory do
  285. %Pleroma.Notification{
  286. user: build(:user)
  287. }
  288. end
  289. def scheduled_activity_factory do
  290. %Pleroma.ScheduledActivity{
  291. user: build(:user),
  292. scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
  293. params: build(:note) |> Map.from_struct() |> Map.get(:data)
  294. }
  295. end
  296. def registration_factory do
  297. user = insert(:user)
  298. %Pleroma.Registration{
  299. user: user,
  300. provider: "twitter",
  301. uid: "171799000",
  302. info: %{
  303. "name" => "John Doe",
  304. "email" => "john@doe.com",
  305. "nickname" => "johndoe",
  306. "description" => "My bio"
  307. }
  308. }
  309. end
  310. def config_factory do
  311. %Pleroma.Web.AdminAPI.Config{
  312. key: sequence(:key, &"some_key_#{&1}"),
  313. group: "pleroma",
  314. value:
  315. sequence(
  316. :value,
  317. fn key ->
  318. :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
  319. end
  320. )
  321. }
  322. end
  323. def marker_factory do
  324. %Pleroma.Marker{
  325. user: build(:user),
  326. timeline: "notifications",
  327. lock_version: 0,
  328. last_read_id: "1"
  329. }
  330. end
  331. end