logo

pleroma

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

status_view_test.exs (31446B)


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