logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

status_view_test.exs (21449B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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.User
  13. alias Pleroma.UserRelationship
  14. alias Pleroma.Web.CommonAPI
  15. alias Pleroma.Web.CommonAPI.Utils
  16. alias Pleroma.Web.MastodonAPI.AccountView
  17. alias Pleroma.Web.MastodonAPI.StatusView
  18. import Pleroma.Factory
  19. import Tesla.Mock
  20. import OpenApiSpex.TestAssertions
  21. setup do
  22. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  23. :ok
  24. end
  25. test "has an emoji reaction list" do
  26. user = insert(:user)
  27. other_user = insert(:user)
  28. third_user = insert(:user)
  29. {:ok, activity} = CommonAPI.post(user, %{status: "dae cofe??"})
  30. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user, "☕")
  31. {:ok, _} = CommonAPI.react_with_emoji(activity.id, third_user, "🍵")
  32. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  33. activity = Repo.get(Activity, activity.id)
  34. status = StatusView.render("show.json", activity: activity)
  35. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  36. assert status[:pleroma][:emoji_reactions] == [
  37. %{name: "☕", count: 2, me: false},
  38. %{name: "🍵", count: 1, me: false}
  39. ]
  40. status = StatusView.render("show.json", activity: activity, for: user)
  41. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  42. assert status[:pleroma][:emoji_reactions] == [
  43. %{name: "☕", count: 2, me: true},
  44. %{name: "🍵", count: 1, me: false}
  45. ]
  46. end
  47. test "works correctly with badly formatted emojis" do
  48. user = insert(:user)
  49. {:ok, activity} = CommonAPI.post(user, %{status: "yo"})
  50. activity
  51. |> Object.normalize(false)
  52. |> Object.update_data(%{"reactions" => %{"☕" => [user.ap_id], "x" => 1}})
  53. activity = Activity.get_by_id(activity.id)
  54. status = StatusView.render("show.json", activity: activity, for: user)
  55. assert status[:pleroma][:emoji_reactions] == [
  56. %{name: "☕", count: 1, me: true}
  57. ]
  58. end
  59. test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
  60. user = insert(:user)
  61. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  62. [participation] = Participation.for_user(user)
  63. status =
  64. StatusView.render("show.json",
  65. activity: activity,
  66. with_direct_conversation_id: true,
  67. for: user
  68. )
  69. assert status[:pleroma][:direct_conversation_id] == participation.id
  70. status = StatusView.render("show.json", activity: activity, for: user)
  71. assert status[:pleroma][:direct_conversation_id] == nil
  72. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  73. end
  74. test "returns the direct conversation id when given the `direct_conversation_id` option" do
  75. user = insert(:user)
  76. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  77. [participation] = Participation.for_user(user)
  78. status =
  79. StatusView.render("show.json",
  80. activity: activity,
  81. direct_conversation_id: participation.id,
  82. for: user
  83. )
  84. assert status[:pleroma][:direct_conversation_id] == participation.id
  85. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  86. end
  87. test "returns a temporary ap_id based user for activities missing db users" do
  88. user = insert(:user)
  89. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  90. Repo.delete(user)
  91. Cachex.clear(:user_cache)
  92. finger_url =
  93. "https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost"
  94. Tesla.Mock.mock_global(fn
  95. %{method: :get, url: "http://localhost/.well-known/host-meta"} ->
  96. %Tesla.Env{status: 404, body: ""}
  97. %{method: :get, url: "https://localhost/.well-known/host-meta"} ->
  98. %Tesla.Env{status: 404, body: ""}
  99. %{
  100. method: :get,
  101. url: ^finger_url
  102. } ->
  103. %Tesla.Env{status: 404, body: ""}
  104. end)
  105. %{account: ms_user} = StatusView.render("show.json", activity: activity)
  106. assert ms_user.acct == "erroruser@example.com"
  107. end
  108. test "tries to get a user by nickname if fetching by ap_id doesn't work" do
  109. user = insert(:user)
  110. {:ok, activity} = CommonAPI.post(user, %{status: "Hey @shp!", visibility: "direct"})
  111. {:ok, user} =
  112. user
  113. |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
  114. |> Repo.update()
  115. Cachex.clear(:user_cache)
  116. result = StatusView.render("show.json", activity: activity)
  117. assert result[:account][:id] == to_string(user.id)
  118. assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
  119. end
  120. test "a note with null content" do
  121. note = insert(:note_activity)
  122. note_object = Object.normalize(note)
  123. data =
  124. note_object.data
  125. |> Map.put("content", nil)
  126. Object.change(note_object, %{data: data})
  127. |> Object.update_and_set_cache()
  128. User.get_cached_by_ap_id(note.data["actor"])
  129. status = StatusView.render("show.json", %{activity: note})
  130. assert status.content == ""
  131. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  132. end
  133. test "a note activity" do
  134. note = insert(:note_activity)
  135. object_data = Object.normalize(note).data
  136. user = User.get_cached_by_ap_id(note.data["actor"])
  137. convo_id = Utils.context_to_conversation_id(object_data["context"])
  138. status = StatusView.render("show.json", %{activity: note})
  139. created_at =
  140. (object_data["published"] || "")
  141. |> String.replace(~r/\.\d+Z/, ".000Z")
  142. expected = %{
  143. id: to_string(note.id),
  144. uri: object_data["id"],
  145. url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note),
  146. account: AccountView.render("show.json", %{user: user, skip_visibility_check: true}),
  147. in_reply_to_id: nil,
  148. in_reply_to_account_id: nil,
  149. card: nil,
  150. reblog: nil,
  151. content: HTML.filter_tags(object_data["content"]),
  152. text: nil,
  153. created_at: created_at,
  154. reblogs_count: 0,
  155. replies_count: 0,
  156. favourites_count: 0,
  157. reblogged: false,
  158. bookmarked: false,
  159. favourited: false,
  160. muted: false,
  161. pinned: false,
  162. sensitive: false,
  163. poll: nil,
  164. spoiler_text: HTML.filter_tags(object_data["summary"]),
  165. visibility: "public",
  166. media_attachments: [],
  167. mentions: [],
  168. tags: [],
  169. application: %{
  170. name: "Web",
  171. website: nil
  172. },
  173. language: nil,
  174. emojis: [
  175. %{
  176. shortcode: "2hu",
  177. url: "corndog.png",
  178. static_url: "corndog.png",
  179. visible_in_picker: false
  180. }
  181. ],
  182. pleroma: %{
  183. local: true,
  184. conversation_id: convo_id,
  185. in_reply_to_account_acct: nil,
  186. content: %{"text/plain" => HTML.strip_tags(object_data["content"])},
  187. spoiler_text: %{"text/plain" => HTML.strip_tags(object_data["summary"])},
  188. expires_at: nil,
  189. direct_conversation_id: nil,
  190. thread_muted: false,
  191. emoji_reactions: [],
  192. parent_visible: false
  193. }
  194. }
  195. assert status == expected
  196. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  197. end
  198. test "tells if the message is muted for some reason" do
  199. user = insert(:user)
  200. other_user = insert(:user)
  201. {:ok, _user_relationships} = User.mute(user, other_user)
  202. {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
  203. relationships_opt = UserRelationship.view_relationships_option(user, [other_user])
  204. opts = %{activity: activity}
  205. status = StatusView.render("show.json", opts)
  206. assert status.muted == false
  207. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  208. status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt))
  209. assert status.muted == false
  210. for_opts = %{activity: activity, for: user}
  211. status = StatusView.render("show.json", for_opts)
  212. assert status.muted == true
  213. status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))
  214. assert status.muted == true
  215. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  216. end
  217. test "tells if the message is thread muted" do
  218. user = insert(:user)
  219. other_user = insert(:user)
  220. {:ok, _user_relationships} = User.mute(user, other_user)
  221. {:ok, activity} = CommonAPI.post(other_user, %{status: "test"})
  222. status = StatusView.render("show.json", %{activity: activity, for: user})
  223. assert status.pleroma.thread_muted == false
  224. {:ok, activity} = CommonAPI.add_mute(user, activity)
  225. status = StatusView.render("show.json", %{activity: activity, for: user})
  226. assert status.pleroma.thread_muted == true
  227. end
  228. test "tells if the status is bookmarked" do
  229. user = insert(:user)
  230. {:ok, activity} = CommonAPI.post(user, %{status: "Cute girls doing cute things"})
  231. status = StatusView.render("show.json", %{activity: activity})
  232. assert status.bookmarked == false
  233. status = StatusView.render("show.json", %{activity: activity, for: user})
  234. assert status.bookmarked == false
  235. {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
  236. activity = Activity.get_by_id_with_object(activity.id)
  237. status = StatusView.render("show.json", %{activity: activity, for: user})
  238. assert status.bookmarked == true
  239. end
  240. test "a reply" do
  241. note = insert(:note_activity)
  242. user = insert(:user)
  243. {:ok, activity} = CommonAPI.post(user, %{status: "he", in_reply_to_status_id: note.id})
  244. status = StatusView.render("show.json", %{activity: activity})
  245. assert status.in_reply_to_id == to_string(note.id)
  246. [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
  247. assert status.in_reply_to_id == to_string(note.id)
  248. end
  249. test "contains mentions" do
  250. user = insert(:user)
  251. mentioned = insert(:user)
  252. {:ok, activity} = CommonAPI.post(user, %{status: "hi @#{mentioned.nickname}"})
  253. status = StatusView.render("show.json", %{activity: activity})
  254. assert status.mentions ==
  255. Enum.map([mentioned], fn u -> AccountView.render("mention.json", %{user: u}) end)
  256. assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
  257. end
  258. test "create mentions from the 'to' field" do
  259. %User{ap_id: recipient_ap_id} = insert(:user)
  260. cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
  261. object =
  262. insert(:note, %{
  263. data: %{
  264. "to" => [recipient_ap_id],
  265. "cc" => cc
  266. }
  267. })
  268. activity =
  269. insert(:note_activity, %{
  270. note: object,
  271. recipients: [recipient_ap_id | cc]
  272. })
  273. assert length(activity.recipients) == 3
  274. %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
  275. assert length(mentions) == 1
  276. assert mention.url == recipient_ap_id
  277. end
  278. test "create mentions from the 'tag' field" do
  279. recipient = insert(:user)
  280. cc = insert_pair(:user) |> Enum.map(& &1.ap_id)
  281. object =
  282. insert(:note, %{
  283. data: %{
  284. "cc" => cc,
  285. "tag" => [
  286. %{
  287. "href" => recipient.ap_id,
  288. "name" => recipient.nickname,
  289. "type" => "Mention"
  290. },
  291. %{
  292. "href" => "https://example.com/search?tag=test",
  293. "name" => "#test",
  294. "type" => "Hashtag"
  295. }
  296. ]
  297. }
  298. })
  299. activity =
  300. insert(:note_activity, %{
  301. note: object,
  302. recipients: [recipient.ap_id | cc]
  303. })
  304. assert length(activity.recipients) == 3
  305. %{mentions: [mention] = mentions} = StatusView.render("show.json", %{activity: activity})
  306. assert length(mentions) == 1
  307. assert mention.url == recipient.ap_id
  308. end
  309. test "attachments" do
  310. object = %{
  311. "type" => "Image",
  312. "url" => [
  313. %{
  314. "mediaType" => "image/png",
  315. "href" => "someurl"
  316. }
  317. ],
  318. "uuid" => 6
  319. }
  320. expected = %{
  321. id: "1638338801",
  322. type: "image",
  323. url: "someurl",
  324. remote_url: "someurl",
  325. preview_url: "someurl",
  326. text_url: "someurl",
  327. description: nil,
  328. pleroma: %{mime_type: "image/png"}
  329. }
  330. api_spec = Pleroma.Web.ApiSpec.spec()
  331. assert expected == StatusView.render("attachment.json", %{attachment: object})
  332. assert_schema(expected, "Attachment", api_spec)
  333. # If theres a "id", use that instead of the generated one
  334. object = Map.put(object, "id", 2)
  335. result = StatusView.render("attachment.json", %{attachment: object})
  336. assert %{id: "2"} = result
  337. assert_schema(result, "Attachment", api_spec)
  338. end
  339. test "put the url advertised in the Activity in to the url attribute" do
  340. id = "https://wedistribute.org/wp-json/pterotype/v1/object/85810"
  341. [activity] = Activity.search(nil, id)
  342. status = StatusView.render("show.json", %{activity: activity})
  343. assert status.uri == id
  344. assert status.url == "https://wedistribute.org/2019/07/mastodon-drops-ostatus/"
  345. end
  346. test "a reblog" do
  347. user = insert(:user)
  348. activity = insert(:note_activity)
  349. {:ok, reblog} = CommonAPI.repeat(activity.id, user)
  350. represented = StatusView.render("show.json", %{for: user, activity: reblog})
  351. assert represented[:id] == to_string(reblog.id)
  352. assert represented[:reblog][:id] == to_string(activity.id)
  353. assert represented[:emojis] == []
  354. assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
  355. end
  356. test "a peertube video" do
  357. user = insert(:user)
  358. {:ok, object} =
  359. Pleroma.Object.Fetcher.fetch_object_from_id(
  360. "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
  361. )
  362. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  363. represented = StatusView.render("show.json", %{for: user, activity: activity})
  364. assert represented[:id] == to_string(activity.id)
  365. assert length(represented[:media_attachments]) == 1
  366. assert_schema(represented, "Status", Pleroma.Web.ApiSpec.spec())
  367. end
  368. test "funkwhale audio" do
  369. user = insert(:user)
  370. {:ok, object} =
  371. Pleroma.Object.Fetcher.fetch_object_from_id(
  372. "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871"
  373. )
  374. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  375. represented = StatusView.render("show.json", %{for: user, activity: activity})
  376. assert represented[:id] == to_string(activity.id)
  377. assert length(represented[:media_attachments]) == 1
  378. end
  379. test "a Mobilizon event" do
  380. user = insert(:user)
  381. {:ok, object} =
  382. Pleroma.Object.Fetcher.fetch_object_from_id(
  383. "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
  384. )
  385. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  386. represented = StatusView.render("show.json", %{for: user, activity: activity})
  387. assert represented[:id] == to_string(activity.id)
  388. assert represented[:url] ==
  389. "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39"
  390. assert represented[:content] ==
  391. "<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>"
  392. end
  393. test "a Honk event" do
  394. user = insert(:user)
  395. {:ok, object} =
  396. Pleroma.Object.Fetcher.fetch_object_from_id(
  397. "https://honk.tedunangst.com/u/tedu/h/8dkPX284T8286Mm9HD"
  398. )
  399. %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
  400. represented = StatusView.render("show.json", %{for: user, activity: activity})
  401. assert represented[:id] == to_string(activity.id)
  402. end
  403. describe "build_tags/1" do
  404. test "it returns a a dictionary tags" do
  405. object_tags = [
  406. "fediverse",
  407. "mastodon",
  408. "nextcloud",
  409. %{
  410. "href" => "https://kawen.space/users/lain",
  411. "name" => "@lain@kawen.space",
  412. "type" => "Mention"
  413. }
  414. ]
  415. assert StatusView.build_tags(object_tags) == [
  416. %{name: "fediverse", url: "/tag/fediverse"},
  417. %{name: "mastodon", url: "/tag/mastodon"},
  418. %{name: "nextcloud", url: "/tag/nextcloud"}
  419. ]
  420. end
  421. end
  422. describe "rich media cards" do
  423. test "a rich media card without a site name renders correctly" do
  424. page_url = "http://example.com"
  425. card = %{
  426. url: page_url,
  427. image: page_url <> "/example.jpg",
  428. title: "Example website"
  429. }
  430. %{provider_name: "example.com"} =
  431. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  432. end
  433. test "a rich media card without a site name or image renders correctly" do
  434. page_url = "http://example.com"
  435. card = %{
  436. url: page_url,
  437. title: "Example website"
  438. }
  439. %{provider_name: "example.com"} =
  440. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  441. end
  442. test "a rich media card without an image renders correctly" do
  443. page_url = "http://example.com"
  444. card = %{
  445. url: page_url,
  446. site_name: "Example site name",
  447. title: "Example website"
  448. }
  449. %{provider_name: "example.com"} =
  450. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  451. end
  452. test "a rich media card with all relevant data renders correctly" do
  453. page_url = "http://example.com"
  454. card = %{
  455. url: page_url,
  456. site_name: "Example site name",
  457. title: "Example website",
  458. image: page_url <> "/example.jpg",
  459. description: "Example description"
  460. }
  461. %{provider_name: "example.com"} =
  462. StatusView.render("card.json", %{page_url: page_url, rich_media: card})
  463. end
  464. end
  465. test "does not embed a relationship in the account" do
  466. user = insert(:user)
  467. other_user = insert(:user)
  468. {:ok, activity} =
  469. CommonAPI.post(user, %{
  470. status: "drink more water"
  471. })
  472. result = StatusView.render("show.json", %{activity: activity, for: other_user})
  473. assert result[:account][:pleroma][:relationship] == %{}
  474. assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
  475. end
  476. test "does not embed a relationship in the account in reposts" do
  477. user = insert(:user)
  478. other_user = insert(:user)
  479. {:ok, activity} =
  480. CommonAPI.post(user, %{
  481. status: "˙˙ɐʎns"
  482. })
  483. {:ok, activity} = CommonAPI.repeat(activity.id, other_user)
  484. result = StatusView.render("show.json", %{activity: activity, for: user})
  485. assert result[:account][:pleroma][:relationship] == %{}
  486. assert result[:reblog][:account][:pleroma][:relationship] == %{}
  487. assert_schema(result, "Status", Pleroma.Web.ApiSpec.spec())
  488. end
  489. test "visibility/list" do
  490. user = insert(:user)
  491. {:ok, list} = Pleroma.List.create("foo", user)
  492. {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
  493. status = StatusView.render("show.json", activity: activity)
  494. assert status.visibility == "list"
  495. end
  496. test "has a field for parent visibility" do
  497. user = insert(:user)
  498. poster = insert(:user)
  499. {:ok, invisible} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
  500. {:ok, visible} =
  501. CommonAPI.post(poster, %{status: "hey", visibility: "private", in_reply_to_id: invisible.id})
  502. status = StatusView.render("show.json", activity: visible, for: user)
  503. refute status.pleroma.parent_visible
  504. status = StatusView.render("show.json", activity: visible, for: poster)
  505. assert status.pleroma.parent_visible
  506. end
  507. end