logo

pleroma

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

streamer_test.exs (27715B)


  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.StreamerTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Chat
  8. alias Pleroma.Chat.MessageReference
  9. alias Pleroma.Conversation.Participation
  10. alias Pleroma.List
  11. alias Pleroma.Object
  12. alias Pleroma.User
  13. alias Pleroma.Web.CommonAPI
  14. alias Pleroma.Web.Streamer
  15. alias Pleroma.Web.StreamerView
  16. @moduletag needs_streamer: true, capture_log: true
  17. setup do: clear_config([:instance, :skip_thread_containment])
  18. describe "get_topic/_ (unauthenticated)" do
  19. test "allows public" do
  20. assert {:ok, "public"} = Streamer.get_topic("public", nil, nil)
  21. assert {:ok, "public:local"} = Streamer.get_topic("public:local", nil, nil)
  22. assert {:ok, "public:media"} = Streamer.get_topic("public:media", nil, nil)
  23. assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil, nil)
  24. end
  25. test "allows hashtag streams" do
  26. assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, nil, %{"tag" => "cofe"})
  27. end
  28. test "disallows user streams" do
  29. assert {:error, _} = Streamer.get_topic("user", nil, nil)
  30. assert {:error, _} = Streamer.get_topic("user:notification", nil, nil)
  31. assert {:error, _} = Streamer.get_topic("direct", nil, nil)
  32. end
  33. test "disallows list streams" do
  34. assert {:error, _} = Streamer.get_topic("list", nil, nil, %{"list" => 42})
  35. end
  36. end
  37. describe "get_topic/_ (authenticated)" do
  38. setup do: oauth_access(["read"])
  39. test "allows public streams (regardless of OAuth token scopes)", %{
  40. user: user,
  41. token: read_oauth_token
  42. } do
  43. with oauth_token <- [nil, read_oauth_token] do
  44. assert {:ok, "public"} = Streamer.get_topic("public", user, oauth_token)
  45. assert {:ok, "public:local"} = Streamer.get_topic("public:local", user, oauth_token)
  46. assert {:ok, "public:media"} = Streamer.get_topic("public:media", user, oauth_token)
  47. assert {:ok, "public:local:media"} =
  48. Streamer.get_topic("public:local:media", user, oauth_token)
  49. end
  50. end
  51. test "allows user streams (with proper OAuth token scopes)", %{
  52. user: user,
  53. token: read_oauth_token
  54. } do
  55. %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user)
  56. %{token: read_statuses_token} = oauth_access(["read:statuses"], user: user)
  57. %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user)
  58. expected_user_topic = "user:#{user.id}"
  59. expected_notification_topic = "user:notification:#{user.id}"
  60. expected_direct_topic = "direct:#{user.id}"
  61. expected_pleroma_chat_topic = "user:pleroma_chat:#{user.id}"
  62. for valid_user_token <- [read_oauth_token, read_statuses_token] do
  63. assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user, valid_user_token)
  64. assert {:ok, ^expected_direct_topic} =
  65. Streamer.get_topic("direct", user, valid_user_token)
  66. assert {:ok, ^expected_pleroma_chat_topic} =
  67. Streamer.get_topic("user:pleroma_chat", user, valid_user_token)
  68. end
  69. for invalid_user_token <- [read_notifications_token, badly_scoped_token],
  70. user_topic <- ["user", "direct", "user:pleroma_chat"] do
  71. assert {:error, :unauthorized} = Streamer.get_topic(user_topic, user, invalid_user_token)
  72. end
  73. for valid_notification_token <- [read_oauth_token, read_notifications_token] do
  74. assert {:ok, ^expected_notification_topic} =
  75. Streamer.get_topic("user:notification", user, valid_notification_token)
  76. end
  77. for invalid_notification_token <- [read_statuses_token, badly_scoped_token] do
  78. assert {:error, :unauthorized} =
  79. Streamer.get_topic("user:notification", user, invalid_notification_token)
  80. end
  81. end
  82. test "allows hashtag streams (regardless of OAuth token scopes)", %{
  83. user: user,
  84. token: read_oauth_token
  85. } do
  86. for oauth_token <- [nil, read_oauth_token] do
  87. assert {:ok, "hashtag:cofe"} =
  88. Streamer.get_topic("hashtag", user, oauth_token, %{"tag" => "cofe"})
  89. end
  90. end
  91. test "disallows registering to another user's stream", %{user: user, token: read_oauth_token} do
  92. another_user = insert(:user)
  93. assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user, read_oauth_token)
  94. assert {:error, _} =
  95. Streamer.get_topic("user:notification:#{another_user.id}", user, read_oauth_token)
  96. assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user, read_oauth_token)
  97. end
  98. test "allows list stream that are owned by the user (with `read` or `read:lists` scopes)", %{
  99. user: user,
  100. token: read_oauth_token
  101. } do
  102. %{token: read_lists_token} = oauth_access(["read:lists"], user: user)
  103. %{token: invalid_token} = oauth_access(["irrelevant:scope"], user: user)
  104. {:ok, list} = List.create("Test", user)
  105. assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, read_oauth_token)
  106. for valid_token <- [read_oauth_token, read_lists_token] do
  107. assert {:ok, _} = Streamer.get_topic("list", user, valid_token, %{"list" => list.id})
  108. end
  109. assert {:error, _} = Streamer.get_topic("list", user, invalid_token, %{"list" => list.id})
  110. end
  111. test "disallows list stream that are not owned by the user", %{user: user, token: oauth_token} do
  112. another_user = insert(:user)
  113. {:ok, list} = List.create("Test", another_user)
  114. assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, oauth_token)
  115. assert {:error, _} = Streamer.get_topic("list", user, oauth_token, %{"list" => list.id})
  116. end
  117. end
  118. describe "user streams" do
  119. setup do
  120. %{user: user, token: token} = oauth_access(["read"])
  121. notify = insert(:notification, user: user, activity: build(:note_activity))
  122. {:ok, %{user: user, notify: notify, token: token}}
  123. end
  124. test "it streams the user's post in the 'user' stream", %{user: user, token: oauth_token} do
  125. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  126. {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
  127. assert_receive {:render_with_user, _, _, ^activity}
  128. refute Streamer.filtered_by_user?(user, activity)
  129. end
  130. test "it streams boosts of the user in the 'user' stream", %{user: user, token: oauth_token} do
  131. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  132. other_user = insert(:user)
  133. {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
  134. {:ok, announce} = CommonAPI.repeat(activity.id, user)
  135. assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
  136. refute Streamer.filtered_by_user?(user, announce)
  137. end
  138. test "it does not stream announces of the user's own posts in the 'user' stream", %{
  139. user: user,
  140. token: oauth_token
  141. } do
  142. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  143. other_user = insert(:user)
  144. {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
  145. {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
  146. assert Streamer.filtered_by_user?(user, announce)
  147. end
  148. test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
  149. user: user,
  150. token: oauth_token
  151. } do
  152. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  153. other_user = insert(:user)
  154. {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
  155. {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
  156. notification =
  157. Pleroma.Notification
  158. |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
  159. |> Repo.preload(:activity)
  160. refute Streamer.filtered_by_user?(user, notification)
  161. end
  162. test "it streams boosts of mastodon user in the 'user' stream", %{
  163. user: user,
  164. token: oauth_token
  165. } do
  166. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  167. other_user = insert(:user)
  168. {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
  169. data =
  170. File.read!("test/fixtures/mastodon-announce.json")
  171. |> Poison.decode!()
  172. |> Map.put("object", activity.data["object"])
  173. |> Map.put("actor", user.ap_id)
  174. {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
  175. Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
  176. assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
  177. refute Streamer.filtered_by_user?(user, announce)
  178. end
  179. test "it sends notify to in the 'user' stream", %{
  180. user: user,
  181. token: oauth_token,
  182. notify: notify
  183. } do
  184. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  185. Streamer.stream("user", notify)
  186. assert_receive {:render_with_user, _, _, ^notify}
  187. refute Streamer.filtered_by_user?(user, notify)
  188. end
  189. test "it sends notify to in the 'user:notification' stream", %{
  190. user: user,
  191. token: oauth_token,
  192. notify: notify
  193. } do
  194. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  195. Streamer.stream("user:notification", notify)
  196. assert_receive {:render_with_user, _, _, ^notify}
  197. refute Streamer.filtered_by_user?(user, notify)
  198. end
  199. test "it sends chat messages to the 'user:pleroma_chat' stream", %{
  200. user: user,
  201. token: oauth_token
  202. } do
  203. other_user = insert(:user)
  204. {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
  205. object = Object.normalize(create_activity, false)
  206. chat = Chat.get(user.id, other_user.ap_id)
  207. cm_ref = MessageReference.for_chat_and_object(chat, object)
  208. cm_ref = %{cm_ref | chat: chat, object: object}
  209. Streamer.get_topic_and_add_socket("user:pleroma_chat", user, oauth_token)
  210. Streamer.stream("user:pleroma_chat", {user, cm_ref})
  211. text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
  212. assert text =~ "hey cirno"
  213. assert_receive {:text, ^text}
  214. end
  215. test "it sends chat messages to the 'user' stream", %{user: user, token: oauth_token} do
  216. other_user = insert(:user)
  217. {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
  218. object = Object.normalize(create_activity, false)
  219. chat = Chat.get(user.id, other_user.ap_id)
  220. cm_ref = MessageReference.for_chat_and_object(chat, object)
  221. cm_ref = %{cm_ref | chat: chat, object: object}
  222. Streamer.get_topic_and_add_socket("user", user, oauth_token)
  223. Streamer.stream("user", {user, cm_ref})
  224. text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
  225. assert text =~ "hey cirno"
  226. assert_receive {:text, ^text}
  227. end
  228. test "it sends chat message notifications to the 'user:notification' stream", %{
  229. user: user,
  230. token: oauth_token
  231. } do
  232. other_user = insert(:user)
  233. {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey")
  234. notify =
  235. Repo.get_by(Pleroma.Notification, user_id: user.id, activity_id: create_activity.id)
  236. |> Repo.preload(:activity)
  237. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  238. Streamer.stream("user:notification", notify)
  239. assert_receive {:render_with_user, _, _, ^notify}
  240. refute Streamer.filtered_by_user?(user, notify)
  241. end
  242. test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
  243. user: user,
  244. token: oauth_token
  245. } do
  246. blocked = insert(:user)
  247. {:ok, _user_relationship} = User.block(user, blocked)
  248. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  249. {:ok, activity} = CommonAPI.post(user, %{status: ":("})
  250. {:ok, _} = CommonAPI.favorite(blocked, activity.id)
  251. refute_receive _
  252. end
  253. test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
  254. user: user,
  255. token: oauth_token
  256. } do
  257. user2 = insert(:user)
  258. {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
  259. {:ok, _} = CommonAPI.add_mute(user, activity)
  260. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  261. {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
  262. refute_receive _
  263. assert Streamer.filtered_by_user?(user, favorite_activity)
  264. end
  265. test "it sends favorite to 'user:notification' stream'", %{
  266. user: user,
  267. token: oauth_token
  268. } do
  269. user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
  270. {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
  271. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  272. {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
  273. assert_receive {:render_with_user, _, "notification.json", notif}
  274. assert notif.activity.id == favorite_activity.id
  275. refute Streamer.filtered_by_user?(user, notif)
  276. end
  277. test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
  278. user: user,
  279. token: oauth_token
  280. } do
  281. user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
  282. {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
  283. {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
  284. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  285. {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
  286. refute_receive _
  287. assert Streamer.filtered_by_user?(user, favorite_activity)
  288. end
  289. test "it sends follow activities to the 'user:notification' stream", %{
  290. user: user,
  291. token: oauth_token
  292. } do
  293. user_url = user.ap_id
  294. user2 = insert(:user)
  295. body =
  296. File.read!("test/fixtures/users_mock/localhost.json")
  297. |> String.replace("{{nickname}}", user.nickname)
  298. |> Jason.encode!()
  299. Tesla.Mock.mock_global(fn
  300. %{method: :get, url: ^user_url} ->
  301. %Tesla.Env{status: 200, body: body}
  302. end)
  303. Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
  304. {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
  305. assert_receive {:render_with_user, _, "notification.json", notif}
  306. assert notif.activity.id == follow_activity.id
  307. refute Streamer.filtered_by_user?(user, notif)
  308. end
  309. end
  310. describe "public streams" do
  311. test "it sends to public (authenticated)" do
  312. %{user: user, token: oauth_token} = oauth_access(["read"])
  313. other_user = insert(:user)
  314. Streamer.get_topic_and_add_socket("public", user, oauth_token)
  315. {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
  316. assert_receive {:render_with_user, _, _, ^activity}
  317. refute Streamer.filtered_by_user?(other_user, activity)
  318. end
  319. test "it sends to public (unauthenticated)" do
  320. user = insert(:user)
  321. Streamer.get_topic_and_add_socket("public", nil, nil)
  322. {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
  323. activity_id = activity.id
  324. assert_receive {:text, event}
  325. assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
  326. assert %{"id" => ^activity_id} = Jason.decode!(payload)
  327. {:ok, _} = CommonAPI.delete(activity.id, user)
  328. assert_receive {:text, event}
  329. assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
  330. end
  331. test "handles deletions" do
  332. %{user: user, token: oauth_token} = oauth_access(["read"])
  333. other_user = insert(:user)
  334. {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
  335. Streamer.get_topic_and_add_socket("public", user, oauth_token)
  336. {:ok, _} = CommonAPI.delete(activity.id, other_user)
  337. activity_id = activity.id
  338. assert_receive {:text, event}
  339. assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
  340. end
  341. end
  342. describe "thread_containment/2" do
  343. test "it filters to user if recipients invalid and thread containment is enabled" do
  344. Pleroma.Config.put([:instance, :skip_thread_containment], false)
  345. author = insert(:user)
  346. %{user: user, token: oauth_token} = oauth_access(["read"])
  347. User.follow(user, author, :follow_accept)
  348. activity =
  349. insert(:note_activity,
  350. note:
  351. insert(:note,
  352. user: author,
  353. data: %{"to" => ["TEST-FFF"]}
  354. )
  355. )
  356. Streamer.get_topic_and_add_socket("public", user, oauth_token)
  357. Streamer.stream("public", activity)
  358. assert_receive {:render_with_user, _, _, ^activity}
  359. assert Streamer.filtered_by_user?(user, activity)
  360. end
  361. test "it sends message if recipients invalid and thread containment is disabled" do
  362. Pleroma.Config.put([:instance, :skip_thread_containment], true)
  363. author = insert(:user)
  364. %{user: user, token: oauth_token} = oauth_access(["read"])
  365. User.follow(user, author, :follow_accept)
  366. activity =
  367. insert(:note_activity,
  368. note:
  369. insert(:note,
  370. user: author,
  371. data: %{"to" => ["TEST-FFF"]}
  372. )
  373. )
  374. Streamer.get_topic_and_add_socket("public", user, oauth_token)
  375. Streamer.stream("public", activity)
  376. assert_receive {:render_with_user, _, _, ^activity}
  377. refute Streamer.filtered_by_user?(user, activity)
  378. end
  379. test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
  380. Pleroma.Config.put([:instance, :skip_thread_containment], false)
  381. author = insert(:user)
  382. user = insert(:user, skip_thread_containment: true)
  383. %{token: oauth_token} = oauth_access(["read"], user: user)
  384. User.follow(user, author, :follow_accept)
  385. activity =
  386. insert(:note_activity,
  387. note:
  388. insert(:note,
  389. user: author,
  390. data: %{"to" => ["TEST-FFF"]}
  391. )
  392. )
  393. Streamer.get_topic_and_add_socket("public", user, oauth_token)
  394. Streamer.stream("public", activity)
  395. assert_receive {:render_with_user, _, _, ^activity}
  396. refute Streamer.filtered_by_user?(user, activity)
  397. end
  398. end
  399. describe "blocks" do
  400. setup do: oauth_access(["read"])
  401. test "it filters messages involving blocked users", %{user: user, token: oauth_token} do
  402. blocked_user = insert(:user)
  403. {:ok, _user_relationship} = User.block(user, blocked_user)
  404. Streamer.get_topic_and_add_socket("public", user, oauth_token)
  405. {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
  406. assert_receive {:render_with_user, _, _, ^activity}
  407. assert Streamer.filtered_by_user?(user, activity)
  408. end
  409. test "it filters messages transitively involving blocked users", %{
  410. user: blocker,
  411. token: blocker_token
  412. } do
  413. blockee = insert(:user)
  414. friend = insert(:user)
  415. Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
  416. {:ok, _user_relationship} = User.block(blocker, blockee)
  417. {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
  418. assert_receive {:render_with_user, _, _, ^activity_one}
  419. assert Streamer.filtered_by_user?(blocker, activity_one)
  420. {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
  421. assert_receive {:render_with_user, _, _, ^activity_two}
  422. assert Streamer.filtered_by_user?(blocker, activity_two)
  423. {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
  424. assert_receive {:render_with_user, _, _, ^activity_three}
  425. assert Streamer.filtered_by_user?(blocker, activity_three)
  426. end
  427. end
  428. describe "lists" do
  429. setup do: oauth_access(["read"])
  430. test "it doesn't send unwanted DMs to list", %{user: user_a, token: user_a_token} do
  431. user_b = insert(:user)
  432. user_c = insert(:user)
  433. {:ok, user_a} = User.follow(user_a, user_b)
  434. {:ok, list} = List.create("Test", user_a)
  435. {:ok, list} = List.follow(list, user_b)
  436. Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
  437. {:ok, _activity} =
  438. CommonAPI.post(user_b, %{
  439. status: "@#{user_c.nickname} Test",
  440. visibility: "direct"
  441. })
  442. refute_receive _
  443. end
  444. test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
  445. user_b = insert(:user)
  446. {:ok, list} = List.create("Test", user_a)
  447. {:ok, list} = List.follow(list, user_b)
  448. Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
  449. {:ok, _activity} =
  450. CommonAPI.post(user_b, %{
  451. status: "Test",
  452. visibility: "private"
  453. })
  454. refute_receive _
  455. end
  456. test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
  457. user_b = insert(:user)
  458. {:ok, user_a} = User.follow(user_a, user_b)
  459. {:ok, list} = List.create("Test", user_a)
  460. {:ok, list} = List.follow(list, user_b)
  461. Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
  462. {:ok, activity} =
  463. CommonAPI.post(user_b, %{
  464. status: "Test",
  465. visibility: "private"
  466. })
  467. assert_receive {:render_with_user, _, _, ^activity}
  468. refute Streamer.filtered_by_user?(user_a, activity)
  469. end
  470. end
  471. describe "muted reblogs" do
  472. setup do: oauth_access(["read"])
  473. test "it filters muted reblogs", %{user: user1, token: user1_token} do
  474. user2 = insert(:user)
  475. user3 = insert(:user)
  476. CommonAPI.follow(user1, user2)
  477. CommonAPI.hide_reblogs(user1, user2)
  478. {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
  479. Streamer.get_topic_and_add_socket("user", user1, user1_token)
  480. {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
  481. assert_receive {:render_with_user, _, _, ^announce_activity}
  482. assert Streamer.filtered_by_user?(user1, announce_activity)
  483. end
  484. test "it filters reblog notification for reblog-muted actors", %{
  485. user: user1,
  486. token: user1_token
  487. } do
  488. user2 = insert(:user)
  489. CommonAPI.follow(user1, user2)
  490. CommonAPI.hide_reblogs(user1, user2)
  491. {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
  492. Streamer.get_topic_and_add_socket("user", user1, user1_token)
  493. {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
  494. assert_receive {:render_with_user, _, "notification.json", notif}
  495. assert Streamer.filtered_by_user?(user1, notif)
  496. end
  497. test "it send non-reblog notification for reblog-muted actors", %{
  498. user: user1,
  499. token: user1_token
  500. } do
  501. user2 = insert(:user)
  502. CommonAPI.follow(user1, user2)
  503. CommonAPI.hide_reblogs(user1, user2)
  504. {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
  505. Streamer.get_topic_and_add_socket("user", user1, user1_token)
  506. {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
  507. assert_receive {:render_with_user, _, "notification.json", notif}
  508. refute Streamer.filtered_by_user?(user1, notif)
  509. end
  510. end
  511. describe "muted threads" do
  512. test "it filters posts from muted threads" do
  513. user = insert(:user)
  514. %{user: user2, token: user2_token} = oauth_access(["read"])
  515. Streamer.get_topic_and_add_socket("user", user2, user2_token)
  516. {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
  517. {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
  518. {:ok, _} = CommonAPI.add_mute(user2, activity)
  519. assert_receive {:render_with_user, _, _, ^activity}
  520. assert Streamer.filtered_by_user?(user2, activity)
  521. end
  522. end
  523. describe "direct streams" do
  524. setup do: oauth_access(["read"])
  525. test "it sends conversation update to the 'direct' stream", %{user: user, token: oauth_token} do
  526. another_user = insert(:user)
  527. Streamer.get_topic_and_add_socket("direct", user, oauth_token)
  528. {:ok, _create_activity} =
  529. CommonAPI.post(another_user, %{
  530. status: "hey @#{user.nickname}",
  531. visibility: "direct"
  532. })
  533. assert_receive {:text, received_event}
  534. assert %{"event" => "conversation", "payload" => received_payload} =
  535. Jason.decode!(received_event)
  536. assert %{"last_status" => last_status} = Jason.decode!(received_payload)
  537. [participation] = Participation.for_user(user)
  538. assert last_status["pleroma"]["direct_conversation_id"] == participation.id
  539. end
  540. test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted",
  541. %{user: user, token: oauth_token} do
  542. another_user = insert(:user)
  543. Streamer.get_topic_and_add_socket("direct", user, oauth_token)
  544. {:ok, create_activity} =
  545. CommonAPI.post(another_user, %{
  546. status: "hi @#{user.nickname}",
  547. visibility: "direct"
  548. })
  549. create_activity_id = create_activity.id
  550. assert_receive {:render_with_user, _, _, ^create_activity}
  551. assert_receive {:text, received_conversation1}
  552. assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
  553. {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
  554. assert_receive {:text, received_event}
  555. assert %{"event" => "delete", "payload" => ^create_activity_id} =
  556. Jason.decode!(received_event)
  557. refute_receive _
  558. end
  559. test "it sends conversation update to the 'direct' stream when a message is deleted", %{
  560. user: user,
  561. token: oauth_token
  562. } do
  563. another_user = insert(:user)
  564. Streamer.get_topic_and_add_socket("direct", user, oauth_token)
  565. {:ok, create_activity} =
  566. CommonAPI.post(another_user, %{
  567. status: "hi @#{user.nickname}",
  568. visibility: "direct"
  569. })
  570. {:ok, create_activity2} =
  571. CommonAPI.post(another_user, %{
  572. status: "hi @#{user.nickname} 2",
  573. in_reply_to_status_id: create_activity.id,
  574. visibility: "direct"
  575. })
  576. assert_receive {:render_with_user, _, _, ^create_activity}
  577. assert_receive {:render_with_user, _, _, ^create_activity2}
  578. assert_receive {:text, received_conversation1}
  579. assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
  580. assert_receive {:text, received_conversation1}
  581. assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
  582. {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
  583. assert_receive {:text, received_event}
  584. assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
  585. assert_receive {:text, received_event}
  586. assert %{"event" => "conversation", "payload" => received_payload} =
  587. Jason.decode!(received_event)
  588. assert %{"last_status" => last_status} = Jason.decode!(received_payload)
  589. assert last_status["id"] == to_string(create_activity.id)
  590. end
  591. end
  592. end