logo

pleroma

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

status_view_test.exs (30857B)


  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.Web.MastodonAPI.StatusViewTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Activity
  7. alias Pleroma.Bookmark
  8. alias Pleroma.Conversation.Participation
  9. alias Pleroma.HTML
  10. alias Pleroma.Object
  11. alias Pleroma.Repo
  12. alias Pleroma.UnstubbedConfigMock, as: ConfigMock
  13. alias Pleroma.User
  14. alias Pleroma.UserRelationship
  15. alias Pleroma.Web.CommonAPI
  16. alias Pleroma.Web.MastodonAPI.AccountView
  17. alias Pleroma.Web.MastodonAPI.StatusView
  18. require Bitwise
  19. import Mox
  20. import OpenApiSpex.TestAssertions
  21. import Pleroma.Factory
  22. import Tesla.Mock
  23. setup do
  24. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  25. :ok
  26. end
  27. test "has an emoji reaction list" do
  28. user = insert(:user)
  29. other_user = insert(:user)
  30. third_user = insert(:user)
  31. {:ok, activity} = CommonAPI.post(user, %{status: "dae cofe??"})
  32. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "☕")
  33. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, ":dinosaur:")
  34. {:ok, _} = CommonAPI.react_with_emoji(activity.id, third_user, "🍵")
  35. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  36. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
  37. activity = Repo.get(Activity, activity.id)
  38. status = StatusView.render("show.json", activity: activity)
  39. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  40. assert status[:pleroma][:emoji_reactions] == [
  41. %{name: "☕", count: 2, me: false, url: nil, account_ids: [other_user.id, user.id]},
  42. %{
  43. count: 2,
  44. me: false,
  45. name: "dinosaur",
  46. url: "http://localhost:4001/emoji/dino walking.gif",
  47. account_ids: [other_user.id, user.id]
  48. },
  49. %{name: "🍵", count: 1, me: false, url: nil, account_ids: [third_user.id]}
  50. ]
  51. status = StatusView.render("show.json", activity: activity, for: user)
  52. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  53. assert status[:pleroma][:emoji_reactions] == [
  54. %{name: "☕", count: 2, me: true, url: nil, account_ids: [other_user.id, user.id]},
  55. %{
  56. count: 2,
  57. me: true,
  58. name: "dinosaur",
  59. url: "http://localhost:4001/emoji/dino walking.gif",
  60. account_ids: [other_user.id, user.id]
  61. },
  62. %{name: "🍵", count: 1, me: false, url: nil, account_ids: [third_user.id]}
  63. ]
  64. end
  65. test "works with legacy-formatted reactions" do
  66. user = insert(:user)
  67. other_user = insert(:user)
  68. note =
  69. insert(:note,
  70. user: user,
  71. data: %{
  72. "reactions" => [["😿", [other_user.ap_id]]]
  73. }
  74. )
  75. activity = insert(:note_activity, user: user, note: note)
  76. status = StatusView.render("show.json", activity: activity, for: user)
  77. assert status[:pleroma][:emoji_reactions] == [
  78. %{name: "😿", count: 1, me: false, url: nil, account_ids: [other_user.id]}
  79. ]
  80. end
  81. test "works correctly with badly formatted emojis" do
  82. user = insert(:user)
  83. {:ok, activity} = CommonAPI.post(user, %{status: "yo"})
  84. activity
  85. |> Object.normalize(fetch: false)
  86. |> Object.update_data(%{"reactions" => %{"☕" => [user.ap_id], "x" => 1}})
  87. activity = Activity.get_by_id(activity.id)
  88. status = StatusView.render("show.json", activity: activity, for: user)
  89. assert status[:pleroma][:emoji_reactions] == [
  90. %{name: "☕", count: 1, me: true, url: nil, account_ids: [user.id]}
  91. ]
  92. end
  93. test "doesn't show reactions from muted and blocked users" do
  94. user = insert(:user)
  95. other_user = insert(:user)
  96. third_user = insert(:user)
  97. {:ok, activity} = CommonAPI.post(user, %{status: "dae cofe??"})
  98. {:ok, _} = User.mute(user, other_user)
  99. {:ok, _} = User.block(other_user, third_user)
  100. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  101. activity = Repo.get(Activity, activity.id)
  102. status = StatusView.render("show.json", activity: activity)
  103. assert status[:pleroma][:emoji_reactions] == [
  104. %{name: "☕", count: 1, me: false, url: nil, account_ids: [other_user.id]}
  105. ]
  106. status = StatusView.render("show.json", activity: activity, for: user)
  107. assert status[:pleroma][:emoji_reactions] == []
  108. {:ok, _} = CommonAPI.react_with_emoji(activity.id, third_user, "☕")
  109. status = StatusView.render("show.json", activity: activity)
  110. assert status[:pleroma][:emoji_reactions] == [
  111. %{
  112. name: "☕",
  113. count: 2,
  114. me: false,
  115. url: nil,
  116. account_ids: [third_user.id, other_user.id]
  117. }
  118. ]
  119. status = StatusView.render("show.json", activity: activity, for: user)
  120. assert status[:pleroma][:emoji_reactions] == [
  121. %{name: "☕", count: 1, me: false, url: nil, account_ids: [third_user.id]}
  122. ]
  123. status = StatusView.render("show.json", activity: activity, for: other_user)
  124. assert status[:pleroma][:emoji_reactions] == [
  125. %{name: "☕", count: 1, me: true, url: nil, account_ids: [other_user.id]}
  126. ]
  127. end
  128. test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
  129. user = insert(:user)
  130. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  131. [participation] = Participation.for_user(user)
  132. status =
  133. StatusView.render("show.json",
  134. activity: activity,
  135. with_direct_conversation_id: true,
  136. for: user
  137. )
  138. assert status[:pleroma][:direct_conversation_id] == participation.id
  139. status = StatusView.render("show.json", activity: activity, for: user)
  140. assert status[:pleroma][:direct_conversation_id] == nil
  141. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  142. end
  143. test "returns the direct conversation id when given the `direct_conversation_id` option" do
  144. user = insert(:user)
  145. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  146. [participation] = Participation.for_user(user)
  147. status =
  148. StatusView.render("show.json",
  149. activity: activity,
  150. direct_conversation_id: participation.id,
  151. for: user
  152. )
  153. assert status[:pleroma][:direct_conversation_id] == participation.id
  154. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  155. end
  156. @tag capture_log: true
  157. test "returns a temporary ap_id based user for activities missing db users" do
  158. user = insert(:user)
  159. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  160. Repo.delete(user)
  161. User.invalidate_cache(user)
  162. finger_url =
  163. "https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost"
  164. Tesla.Mock.mock_global(fn
  165. %{method: :get, url: "http://localhost/.well-known/host-meta"} ->
  166. %Tesla.Env{status: 404, body: ""}
  167. %{method: :get, url: "https://localhost/.well-known/host-meta"} ->
  168. %Tesla.Env{status: 404, body: ""}
  169. %{
  170. method: :get,
  171. url: ^finger_url
  172. } ->
  173. %Tesla.Env{status: 404, body: ""}
  174. end)
  175. %{account: ms_user} = StatusView.render("show.json", activity: activity)
  176. assert ms_user.acct == "erroruser@example.com"
  177. end
  178. test "tries to get a user by nickname if fetching by ap_id doesn't work" do
  179. user = insert(:user)
  180. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  181. {:ok, user} =
  182. user
  183. |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
  184. |> Repo.update()
  185. User.invalidate_cache(user)
  186. result = StatusView.render("show.json", activity: activity)
  187. assert result[:account][:id] == to_string(user.id)
  188. assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
  189. end
  190. test "a note with null content" do
  191. note = insert(:note_activity)
  192. note_object = Object.normalize(note, fetch: false)
  193. data =
  194. note_object.data
  195. |> Map.put("content", nil)
  196. Object.change(note_object, %{data: data})
  197. |> Object.update_and_set_cache()
  198. User.get_cached_by_ap_id(note.data["actor"])
  199. status = StatusView.render("show.json", %{activity: note})
  200. assert status.content == ""
  201. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  202. end
  203. test "a note activity" do
  204. note = insert(:note_activity)
  205. object_data = Object.normalize(note, fetch: false).data
  206. user = User.get_cached_by_ap_id(note.data["actor"])
  207. convo_id = :erlang.crc32(object_data["context"]) |> Bitwise.band(Bitwise.bnot(0x8000_0000))
  208. status = StatusView.render("show.json", %{activity: note})
  209. created_at =
  210. (object_data["published"] || "")
  211. |> String.replace(~r/\.\d+Z/, ".000Z")
  212. expected = %{
  213. id: to_string(note.id),
  214. uri: object_data["id"],
  215. url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
  216. account: AccountView.render("show.json", %{user: user, skip_visibility_check: true}),
  217. in_reply_to_id: nil,
  218. in_reply_to_account_id: nil,
  219. card: nil,
  220. reblog: nil,
  221. content: HTML.filter_tags(object_data["content"]),
  222. text: nil,
  223. created_at: created_at,
  224. edited_at: nil,
  225. reblogs_count: 0,
  226. replies_count: 0,
  227. favourites_count: 0,
  228. reblogged: false,
  229. bookmarked: false,
  230. favourited: false,
  231. muted: false,
  232. pinned: false,
  233. sensitive: false,
  234. poll: nil,
  235. spoiler_text: HTML.filter_tags(object_data["summary"]),
  236. visibility: "public",
  237. media_attachments: [],
  238. mentions: [],
  239. tags: [
  240. %{
  241. name: "#{hd(object_data["tag"])}",
  242. url: "http://localhost:4001/tag/#{hd(object_data["tag"])}"
  243. }
  244. ],
  245. application: nil,
  246. language: nil,
  247. emojis: [
  248. %{
  249. shortcode: "2hu",
  250. url: "corndog.png",
  251. static_url: "corndog.png",
  252. visible_in_picker: false
  253. }
  254. ],
  255. pleroma: %{
  256. local: true,
  257. conversation_id: convo_id,
  258. context: object_data["context"],
  259. in_reply_to_account_acct: nil,
  260. quote: nil,
  261. quote_id: nil,
  262. quote_url: nil,
  263. quote_visible: false,
  264. content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
  265. spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
  266. expires_at: nil,
  267. direct_conversation_id: nil,
  268. thread_muted: false,
  269. emoji_reactions: [],
  270. parent_visible: false,
  271. pinned_at: nil,
  272. quotes_count: 0
  273. }
  274. }
  275. assert status == expected
  276. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  277. end
  278. test "tells if the message is muted for some reason" do
  279. user = insert(:user)
  280. other_user = insert(:user)
  281. {:ok, _user_relationships} = User.mute(user, other_user)
  282. {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
  283. relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
  284. opts = %{activity: activity}
  285. status = StatusView.render("show.json", opts)
  286. assert status.muted == false
  287. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  288. status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt))
  289. assert status.muted == false
  290. for_opts = %{activity: activity, for: user}
  291. status = StatusView.render("show.json", for_opts)
  292. assert status.muted == true
  293. status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))
  294. assert status.muted == true
  295. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  296. end
  297. test "tells if the message is thread muted" do
  298. user = insert(:user)
  299. other_user = insert(:user)
  300. {:ok, _user_relationships} = User.mute(user, other_user)
  301. {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
  302. status = StatusView.render("show.json", %{activity: activity, for: user})
  303. assert status.pleroma.thread_muted == false
  304. {:ok, activity} = CommonAPI.add_mute(user, activity)
  305. status = StatusView.render("show.json", %{activity: activity, for: user})
  306. assert status.pleroma.thread_muted == true
  307. end
  308. test "tells if the status is bookmarked" do
  309. user = insert(:user)
  310. {:ok, activity} = CommonAPI.post(user, %{status: "Cute girls doing cute things"})
  311. status = StatusView.render("show.json", %{activity: activity})
  312. assert status.bookmarked == false
  313. status = StatusView.render("show.json", %{activity: activity, for: user})
  314. assert status.bookmarked == false
  315. {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
  316. activity = Activity.get_by_id_with_object(activity.id)
  317. status = StatusView.render("show.json", %{activity: activity, for: user})
  318. assert status.bookmarked == true
  319. end
  320. test "a reply" do
  321. note = insert(:note_activity)
  322. user = insert(:user)
  323. {:ok, activity} = CommonAPI.post(user, %{status: "he", in_reply_to_status_id: note.id})
  324. status = StatusView.render("show.json", %{activity: activity})
  325. assert status.in_reply_to_id == to_string(note.id)
  326. [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
  327. assert status.in_reply_to_id == to_string(note.id)
  328. end
  329. test "a quote post" do
  330. post = insert(:note_activity)
  331. user = insert(:user)
  332. {:ok, quote_post} = CommonAPI.post(user, %{status: "he", quote_id: post.id})
  333. {:ok, quoted_quote_post} = CommonAPI.post(user, %{status: "yo", quote_id: quote_post.id})
  334. status = StatusView.render("show.json", %{activity: quoted_quote_post})
  335. assert status.pleroma.quote.id == to_string(quote_post.id)
  336. assert status.pleroma.quote_id == to_string(quote_post.id)
  337. assert status.pleroma.quote_url == Object.normalize(quote_post).data["id"]
  338. assert status.pleroma.quote_visible
  339. # Quotes don't go more than one level deep
  340. refute status.pleroma.quote.pleroma.quote
  341. assert status.pleroma.quote.pleroma.quote_id == to_string(post.id)
  342. assert status.pleroma.quote.pleroma.quote_url == Object.normalize(post).data["id"]
  343. assert status.pleroma.quote.pleroma.quote_visible
  344. # In an index
  345. [status] = StatusView.render("index.json", %{activities: [quoted_quote_post], as: :activity})
  346. assert status.pleroma.quote.id == to_string(quote_post.id)
  347. end
  348. test "quoted private post" do
  349. user = insert(:user)
  350. # Insert a private post
  351. private = insert(:followers_only_note_activity, user: user)
  352. private_object = Object.normalize(private)
  353. # Create a public post quoting the private post
  354. quote_private =
  355. insert(:note_activity, note: insert(:note, data: %{"quoteUrl" => private_object.data["id"]}))
  356. status = StatusView.render("show.json", %{activity: quote_private})
  357. # The quote isn't rendered
  358. refute status.pleroma.quote
  359. assert status.pleroma.quote_url == private_object.data["id"]
  360. refute status.pleroma.quote_visible
  361. # After following the user, the quote is rendered
  362. follower = insert(:user)
  363. CommonAPI.follow(follower, user)
  364. status = StatusView.render("show.json", %{activity: quote_private, for: follower})
  365. assert status.pleroma.quote.id == to_string(private.id)
  366. assert status.pleroma.quote_visible
  367. end
  368. test "quoted direct message" do
  369. # Insert a direct message
  370. direct = insert(:direct_note_activity)
  371. direct_object = Object.normalize(direct)
  372. # Create a public post quoting the direct message
  373. quote_direct =
  374. insert(:note_activity, note: insert(:note, data: %{"quoteUrl" => direct_object.data["id"]}))
  375. status = StatusView.render("show.json", %{activity: quote_direct})
  376. # The quote isn't rendered
  377. refute status.pleroma.quote
  378. assert status.pleroma.quote_url == direct_object.data["id"]
  379. refute status.pleroma.quote_visible
  380. end
  381. test "repost of quote post" do
  382. post = insert(:note_activity)
  383. user = insert(:user)
  384. {:ok, quote_post} = CommonAPI.post(user, %{status: "he", quote_id: post.id})
  385. {:ok, repost} = CommonAPI.repeat(quote_post.id, user)
  386. [status] = StatusView.render("index.json", %{activities: [repost], as: :activity})
  387. assert status.reblog.pleroma.quote.id == to_string(post.id)
  388. end
  389. test "contains mentions" do
  390. user = insert(:user)
  391. mentioned = insert(:user)
  392. {:ok, activity} = CommonAPI.post(user, %{status: "hi @#{mentioned.nickname}"})
  393. status = StatusView.render("show.json", %{activity: activity})
  394. assert status.mentions ==
  395. Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
  396. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  397. end
  398. test "create mentions from the 'to' field" do
  399. %User{ap_id: recipient_ap_id} = insert(:user)
  400. cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
  401. object =
  402. insert(:note, %{
  403. data: %{
  404. "to" => [recipient_ap_id],
  405. "cc" => cc
  406. }
  407. })
  408. activity =
  409. insert(:note_activity, %{
  410. note: object,
  411. recipients: [recipient_ap_id | cc]
  412. })
  413. assert length(activity.recipients) == 3
  414. %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
  415. assert length(mentions) == 1
  416. assert mention.url == recipient_ap_id
  417. end
  418. test "create mentions from the 'tag' field" do
  419. recipient = insert(:user)
  420. cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
  421. object =
  422. insert(:note, %{
  423. data: %{
  424. "cc" => cc,
  425. "tag" => [
  426. %{
  427. "href" => recipient.ap_id,
  428. "name" => recipient.nickname,
  429. "type" => "Mention"
  430. },
  431. %{
  432. "href" => "https://example.com/search?tag=test",
  433. "name" => "#test",
  434. "type" => "Hashtag"
  435. }
  436. ]
  437. }
  438. })
  439. activity =
  440. insert(:note_activity, %{
  441. note: object,
  442. recipients: [recipient.ap_id | cc]
  443. })
  444. assert length(activity.recipients) == 3
  445. %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
  446. assert length(mentions) == 1
  447. assert mention.url == recipient.ap_id
  448. end
  449. test "attachments" do
  450. object = %{
  451. "type" => "Image",
  452. "url" => [
  453. %{
  454. "mediaType" => "image/png",
  455. "href" => "someurl",
  456. "width" => 200,
  457. "height" => 100
  458. }
  459. ],
  460. "blurhash" => "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn",
  461. "uuid" => 6
  462. }
  463. expected = %{
  464. id: "1638338801",
  465. type: "image",
  466. url: "someurl",
  467. remote_url: "someurl",
  468. preview_url: "someurl",
  469. text_url: "someurl",
  470. description: nil,
  471. pleroma: %{mime_type: "image/png"},
  472. meta: %{original: %{width: 200, height: 100, aspect: 2}},
  473. blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn"
  474. }
  475. api_spec = Pleroma.Web.ApiSpec.spec()
  476. assert expected == StatusView.render("attachment.json", %{attachment: object})
  477. assert_schema(expected, "Attachment", api_spec)
  478. # If theres a "id", use that instead of the generated one
  479. object = Map.put(object, "id", 2)
  480. result = StatusView.render("attachment.json", %{attachment: object})
  481. assert %{id: "2"} = result
  482. assert_schema(result, "Attachment", api_spec)
  483. end
  484. test "put the url advertised in the Activity in to the url attribute" do
  485. id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
  486. [activity] = Activity.search(nil, id)
  487. status = StatusView.render("show.json", %{activity: activity})
  488. assert status.uri == id
  489. assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
  490. end
  491. test "a reblog" do
  492. user = insert(:user)
  493. activity = insert(:note_activity)
  494. {:ok, reblog} = CommonAPI.repeat(activity.id, user)
  495. represented = StatusView.render("show.json", %{for: user, activity: reblog})
  496. assert represented[:id] == to_string(reblog.id)
  497. assert represented[:reblog][:id] == to_string(activity.id)
  498. assert represented[:emojis] == []
  499. assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
  500. end
  501. test "a peertube video" do
  502. user = insert(:user)
  503. {:ok, object} =
  504. Pleroma.Object.Fetcher.fetch_object_from_id(
  505. "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
  506. )
  507. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  508. represented = StatusView.render("show.json", %{for: user, activity: activity})
  509. assert represented[:id] == to_string(activity.id)
  510. assert length(represented[:media_attachments]) == 1
  511. assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
  512. end
  513. test "funkwhale audio" do
  514. user = insert(:user)
  515. {:ok, object} =
  516. Pleroma.Object.Fetcher.fetch_object_from_id(
  517. "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
  518. )
  519. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  520. represented = StatusView.render("show.json", %{for: user, activity: activity})
  521. assert represented[:id] == to_string(activity.id)
  522. assert length(represented[:media_attachments]) == 1
  523. end
  524. test "a Mobilizon event" do
  525. user = insert(:user)
  526. {:ok, object} =
  527. Pleroma.Object.Fetcher.fetch_object_from_id(
  528. "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
  529. )
  530. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  531. represented = StatusView.render("show.json", %{for: user, activity: activity})
  532. assert represented[:id] == to_string(activity.id)
  533. assert represented[:url] ==
  534. "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
  535. assert represented[:content] ==
  536. "<p><a href=\"https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39\">Mobilizon Launching Party</a></p><p>Mobilizon is now federated! 🎉</p><p></p><p>You can view this event from other instances if they are subscribed to mobilizon.org, and soon directly from Mastodon and Pleroma. It is possible that you may see some comments from other instances, including Mastodon ones, just below.</p><p></p><p>With a Mobilizon account on an instance, you may <strong>participate</strong> at events from other instances and <strong>add comments</strong> on events.</p><p></p><p>Of course, it&#39;s still <u>a work in progress</u>: if reports made from an instance on events and comments can be federated, you can&#39;t block people right now, and moderators actions are rather limited, but this <strong>will definitely get fixed over time</strong> until first stable version next year.</p><p></p><p>Anyway, if you want to come up with some feedback, head over to our forum or - if you feel you have technical skills and are familiar with it - on our Gitlab repository.</p><p></p><p>Also, to people that want to set Mobilizon themselves even though we really don&#39;t advise to do that for now, we have a little documentation but it&#39;s quite the early days and you&#39;ll probably need some help. No worries, you can chat with us on our Forum or though our Matrix channel.</p><p></p><p>Check our website for more informations and follow us on Twitter or Mastodon.</p>"
  537. end
  538. test "a Honk event" do
  539. user = insert(:user)
  540. {:ok, object} =
  541. Pleroma.Object.Fetcher.fetch_object_from_id(
  542. "https://honk.tedunangst.com/u/tedu/h/8dkPX284T8286Mm9HD"
  543. )
  544. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  545. represented = StatusView.render("show.json", %{for: user, activity: activity})
  546. assert represented[:id] == to_string(activity.id)
  547. end
  548. describe "build_tags/1" do
  549. test "it returns a a dictionary tags" do
  550. object_tags = [
  551. "fediverse",
  552. "mastodon",
  553. "nextcloud",
  554. %{
  555. "href" => "https://kawen.space/users/lain",
  556. "name" => "@lain@kawen.space",
  557. "type" => "Mention"
  558. }
  559. ]
  560. assert StatusView.build_tags(object_tags) == [
  561. %{name: "fediverse", url: "http://localhost:4001/tag/fediverse"},
  562. %{name: "mastodon", url: "http://localhost:4001/tag/mastodon"},
  563. %{name: "nextcloud", url: "http://localhost:4001/tag/nextcloud"}
  564. ]
  565. end
  566. end
  567. describe "rich media cards" do
  568. test "a rich media card without a site name renders correctly" do
  569. page_url = "http://example.com"
  570. card = %{
  571. url: page_url,
  572. image: page_url <> "/example.jpg",
  573. title: "Example website"
  574. }
  575. %{provider_name: "example.com"} =
  576. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  577. end
  578. test "a rich media card without a site name or image renders correctly" do
  579. page_url = "http://example.com"
  580. card = %{
  581. url: page_url,
  582. title: "Example website"
  583. }
  584. %{provider_name: "example.com"} =
  585. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  586. end
  587. test "a rich media card without an image renders correctly" do
  588. page_url = "http://example.com"
  589. card = %{
  590. url: page_url,
  591. site_name: "Example site name",
  592. title: "Example website"
  593. }
  594. %{provider_name: "example.com"} =
  595. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  596. end
  597. test "a rich media card with all relevant data renders correctly" do
  598. page_url = "http://example.com"
  599. card = %{
  600. url: page_url,
  601. site_name: "Example site name",
  602. title: "Example website",
  603. image: page_url <> "/example.jpg",
  604. description: "Example description"
  605. }
  606. %{provider_name: "example.com"} =
  607. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  608. end
  609. test "a rich media card has all media proxied" do
  610. clear_config([:media_proxy, :enabled], true)
  611. clear_config([:media_preview_proxy, :enabled])
  612. ConfigMock
  613. |> stub_with(Pleroma.Test.StaticConfig)
  614. page_url = "http://example.com"
  615. card = %{
  616. url: page_url,
  617. site_name: "Example site name",
  618. title: "Example website",
  619. image: page_url <> "/example.jpg",
  620. audio: page_url <> "/example.ogg",
  621. video: page_url <> "/example.mp4",
  622. description: "Example description"
  623. }
  624. strcard = for {k, v} <- card, into: %{}, do: {to_string(k), v}
  625. %{
  626. provider_name: "example.com",
  627. image: image,
  628. pleroma: %{opengraph: og}
  629. } = StatusView.render("card.json", %{page_url: page_url, rich_media: strcard})
  630. assert String.match?(image, ~r/\/proxy\//)
  631. assert String.match?(og["image"], ~r/\/proxy\//)
  632. assert String.match?(og["audio"], ~r/\/proxy\//)
  633. assert String.match?(og["video"], ~r/\/proxy\//)
  634. end
  635. end
  636. test "does not embed a relationship in the account" do
  637. user = insert(:user)
  638. other_user = insert(:user)
  639. {:ok, activity} =
  640. CommonAPI.post(user, %{
  641. status: "drink more water"
  642. })
  643. result = StatusView.render("show.json", %{activity: activity, for: other_user})
  644. assert result[:account][:pleroma][:relationship] == %{}
  645. assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
  646. end
  647. test "does not embed a relationship in the account in reposts" do
  648. user = insert(:user)
  649. other_user = insert(:user)
  650. {:ok, activity} =
  651. CommonAPI.post(user, %{
  652. status: "˙˙ɐʎns"
  653. })
  654. {:ok, activity} = CommonAPI.repeat(activity.id, other_user)
  655. result = StatusView.render("show.json", %{activity: activity, for: user})
  656. assert result[:account][:pleroma][:relationship] == %{}
  657. assert result[:reblog][:account][:pleroma][:relationship] == %{}
  658. assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
  659. end
  660. test "visibility/list" do
  661. user = insert(:user)
  662. {:ok, list} = Pleroma.List.create("foo", user)
  663. {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
  664. status = StatusView.render("show.json", activity: activity)
  665. assert status.visibility == "list"
  666. end
  667. test "has a field for parent visibility" do
  668. user = insert(:user)
  669. poster = insert(:user)
  670. {:ok, invisible} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
  671. {:ok, visible} =
  672. CommonAPI.post(poster, %{status: "hey", visibility: "private", in_reply_to_id: invisible.id})
  673. status = StatusView.render("show.json", activity: visible, for: user)
  674. refute status.pleroma.parent_visible
  675. status = StatusView.render("show.json", activity: visible, for: poster)
  676. assert status.pleroma.parent_visible
  677. end
  678. test "it shows edited_at" do
  679. poster = insert(:user)
  680. {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
  681. status = StatusView.render("show.json", activity: post)
  682. refute status.edited_at
  683. {:ok, _} = CommonAPI.update(poster, post, %{status: "mew mew"})
  684. edited = Pleroma.Activity.normalize(post)
  685. status = StatusView.render("show.json", activity: edited)
  686. assert status.edited_at
  687. end
  688. test "with a source object" do
  689. note =
  690. insert(:note,
  691. data: %{"source" => %{"content" => "object source", "mediaType" => "text/markdown"}}
  692. )
  693. activity = insert(:note_activity, note: note)
  694. status = StatusView.render("show.json", activity: activity, with_source: true)
  695. assert status.text == "object source"
  696. end
  697. describe "source.json" do
  698. test "with a source object, renders both source and content type" do
  699. note =
  700. insert(:note,
  701. data: %{"source" => %{"content" => "object source", "mediaType" => "text/markdown"}}
  702. )
  703. activity = insert(:note_activity, note: note)
  704. status = StatusView.render("source.json", activity: activity)
  705. assert status.text == "object source"
  706. assert status.content_type == "text/markdown"
  707. end
  708. test "with a source string, renders source and put text/plain as the content type" do
  709. note = insert(:note, data: %{"source" => "string source"})
  710. activity = insert(:note_activity, note: note)
  711. status = StatusView.render("source.json", activity: activity)
  712. assert status.text == "string source"
  713. assert status.content_type == "text/plain"
  714. end
  715. end
  716. end