logo

pleroma

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

emoji_reaction_controller_test.exs (11554B)


  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.PleromaAPI.EmojiReactionControllerTest do
  5. use Oban.Testing, repo: Pleroma.Repo
  6. use Pleroma.Web.ConnCase
  7. alias Pleroma.Object
  8. alias Pleroma.Tests.ObanHelpers
  9. alias Pleroma.User
  10. alias Pleroma.Web.CommonAPI
  11. import Pleroma.Factory
  12. setup do
  13. Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
  14. :ok
  15. end
  16. test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
  17. user = insert(:user)
  18. other_user = insert(:user)
  19. note = insert(:note, user: user, data: %{"reactions" => [["👍", [other_user.ap_id], nil]]})
  20. activity = insert(:note_activity, note: note, user: user)
  21. result =
  22. conn
  23. |> assign(:user, other_user)
  24. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  25. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/\u26A0")
  26. |> json_response_and_validate_schema(200)
  27. assert %{"id" => id} = result
  28. assert to_string(activity.id) == id
  29. assert result["pleroma"]["emoji_reactions"] == [
  30. %{
  31. "name" => "👍",
  32. "count" => 1,
  33. "me" => true,
  34. "url" => nil,
  35. "account_ids" => [other_user.id]
  36. },
  37. %{
  38. "name" => "\u26A0\uFE0F",
  39. "count" => 1,
  40. "me" => true,
  41. "url" => nil,
  42. "account_ids" => [other_user.id]
  43. }
  44. ]
  45. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  46. ObanHelpers.perform_all()
  47. # Reacting with a custom emoji
  48. result =
  49. conn
  50. |> assign(:user, other_user)
  51. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  52. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
  53. |> json_response_and_validate_schema(200)
  54. assert %{"id" => id} = result
  55. assert to_string(activity.id) == id
  56. assert result["pleroma"]["emoji_reactions"] == [
  57. %{
  58. "name" => "dinosaur",
  59. "count" => 1,
  60. "me" => true,
  61. "url" => "http://localhost:4001/emoji/dino walking.gif",
  62. "account_ids" => [other_user.id]
  63. }
  64. ]
  65. # Reacting with a remote emoji
  66. note =
  67. insert(:note,
  68. user: user,
  69. data: %{
  70. "reactions" => [
  71. ["👍", [other_user.ap_id], nil],
  72. ["wow", [other_user.ap_id], "https://remote/emoji/wow"]
  73. ]
  74. }
  75. )
  76. activity = insert(:note_activity, note: note, user: user)
  77. result =
  78. conn
  79. |> assign(:user, user)
  80. |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
  81. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
  82. |> json_response(200)
  83. assert result["pleroma"]["emoji_reactions"] == [
  84. %{
  85. "account_ids" => [other_user.id],
  86. "count" => 1,
  87. "me" => false,
  88. "name" => "👍",
  89. "url" => nil
  90. },
  91. %{
  92. "name" => "wow@remote",
  93. "count" => 2,
  94. "me" => true,
  95. "url" => "https://remote/emoji/wow",
  96. "account_ids" => [user.id, other_user.id]
  97. }
  98. ]
  99. # Reacting with a remote custom emoji that hasn't been reacted with yet
  100. note =
  101. insert(:note,
  102. user: user
  103. )
  104. activity = insert(:note_activity, note: note, user: user)
  105. assert conn
  106. |> assign(:user, user)
  107. |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
  108. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
  109. |> json_response(400)
  110. # Reacting with a non-emoji
  111. assert conn
  112. |> assign(:user, other_user)
  113. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  114. |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
  115. |> json_response_and_validate_schema(400)
  116. end
  117. test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
  118. user = insert(:user)
  119. other_user = insert(:user)
  120. note =
  121. insert(:note,
  122. user: user,
  123. data: %{"reactions" => [["wow", [user.ap_id], "https://remote/emoji/wow"]]}
  124. )
  125. activity = insert(:note_activity, note: note, user: user)
  126. ObanHelpers.perform_all()
  127. {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  128. {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
  129. {:ok, _reaction_activity} =
  130. CommonAPI.react_with_emoji(activity.id, other_user, ":wow@remote:")
  131. ObanHelpers.perform_all()
  132. result =
  133. conn
  134. |> assign(:user, other_user)
  135. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  136. |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
  137. assert %{"id" => id} = json_response_and_validate_schema(result, 200)
  138. assert to_string(activity.id) == id
  139. # Remove custom emoji
  140. result =
  141. conn
  142. |> assign(:user, other_user)
  143. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  144. |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
  145. assert %{"id" => id} = json_response_and_validate_schema(result, 200)
  146. assert to_string(activity.id) == id
  147. ObanHelpers.perform_all()
  148. object = Object.get_by_ap_id(activity.data["object"])
  149. assert object.data["reaction_count"] == 2
  150. # Remove custom remote emoji
  151. result =
  152. conn
  153. |> assign(:user, other_user)
  154. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  155. |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
  156. |> json_response(200)
  157. assert result["pleroma"]["emoji_reactions"] == [
  158. %{
  159. "name" => "wow@remote",
  160. "count" => 1,
  161. "me" => false,
  162. "url" => "https://remote/emoji/wow",
  163. "account_ids" => [user.id]
  164. }
  165. ]
  166. # Remove custom remote emoji that hasn't been reacted with yet
  167. assert conn
  168. |> assign(:user, other_user)
  169. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
  170. |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:zoop@remote:")
  171. |> json_response(400)
  172. end
  173. test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
  174. user = insert(:user)
  175. other_user = insert(:user)
  176. doomed_user = insert(:user)
  177. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  178. result =
  179. conn
  180. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  181. |> json_response_and_validate_schema(200)
  182. assert result == []
  183. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
  184. {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
  185. User.perform(:delete, doomed_user)
  186. result =
  187. conn
  188. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  189. |> json_response_and_validate_schema(200)
  190. [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
  191. assert represented_user["id"] == other_user.id
  192. result =
  193. conn
  194. |> assign(:user, other_user)
  195. |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
  196. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  197. |> json_response_and_validate_schema(200)
  198. assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
  199. result
  200. end
  201. test "GET /api/v1/pleroma/statuses/:id/reactions with legacy format", %{conn: conn} do
  202. user = insert(:user)
  203. other_user = insert(:user)
  204. note =
  205. insert(:note,
  206. user: user,
  207. data: %{
  208. "reactions" => [["😿", [other_user.ap_id]]]
  209. }
  210. )
  211. activity = insert(:note_activity, user: user, note: note)
  212. result =
  213. conn
  214. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  215. |> json_response_and_validate_schema(200)
  216. other_user_id = other_user.id
  217. assert [
  218. %{
  219. "name" => "😿",
  220. "count" => 1,
  221. "me" => false,
  222. "url" => nil,
  223. "accounts" => [%{"id" => ^other_user_id}]
  224. }
  225. ] = result
  226. end
  227. test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
  228. user = insert(:user)
  229. user2 = insert(:user)
  230. user3 = insert(:user)
  231. token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
  232. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  233. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
  234. {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
  235. result =
  236. conn
  237. |> assign(:user, user)
  238. |> assign(:token, token)
  239. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  240. |> json_response_and_validate_schema(200)
  241. assert [%{"name" => "🎅", "count" => 2}] = result
  242. User.mute(user, user3)
  243. result =
  244. conn
  245. |> assign(:user, user)
  246. |> assign(:token, token)
  247. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  248. |> json_response_and_validate_schema(200)
  249. assert [%{"name" => "🎅", "count" => 1}] = result
  250. result =
  251. conn
  252. |> assign(:user, user)
  253. |> assign(:token, token)
  254. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
  255. |> json_response_and_validate_schema(200)
  256. assert [%{"name" => "🎅", "count" => 2}] = result
  257. end
  258. test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
  259. clear_config([:instance, :show_reactions], false)
  260. user = insert(:user)
  261. other_user = insert(:user)
  262. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  263. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
  264. result =
  265. conn
  266. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
  267. |> json_response_and_validate_schema(200)
  268. assert result == []
  269. end
  270. test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
  271. user = insert(:user)
  272. other_user = insert(:user)
  273. {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
  274. result =
  275. conn
  276. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
  277. |> json_response_and_validate_schema(200)
  278. assert result == []
  279. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
  280. {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
  281. assert [
  282. %{
  283. "name" => "🎅",
  284. "count" => 1,
  285. "accounts" => [represented_user],
  286. "me" => false,
  287. "url" => nil
  288. }
  289. ] =
  290. conn
  291. |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
  292. |> json_response_and_validate_schema(200)
  293. assert represented_user["id"] == other_user.id
  294. end
  295. end