logo

pleroma

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

chat_operation.ex (10675B)


  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.ApiSpec.ChatOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.Schemas.ApiError
  8. alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
  9. alias Pleroma.Web.ApiSpec.Schemas.Chat
  10. alias Pleroma.Web.ApiSpec.Schemas.ChatMessage
  11. import Pleroma.Web.ApiSpec.Helpers
  12. @spec open_api_operation(atom) :: Operation.t()
  13. def open_api_operation(action) do
  14. operation = String.to_existing_atom("#{action}_operation")
  15. apply(__MODULE__, operation, [])
  16. end
  17. def mark_as_read_operation do
  18. %Operation{
  19. tags: ["Chats"],
  20. summary: "Mark all messages in the chat as read",
  21. operationId: "ChatController.mark_as_read",
  22. parameters: [Operation.parameter(:id, :path, :string, "The ID of the Chat")],
  23. requestBody: request_body("Parameters", mark_as_read()),
  24. responses: %{
  25. 200 =>
  26. Operation.response(
  27. "The updated chat",
  28. "application/json",
  29. Chat
  30. )
  31. },
  32. security: [
  33. %{
  34. "oAuth" => ["write:chats"]
  35. }
  36. ]
  37. }
  38. end
  39. def mark_message_as_read_operation do
  40. %Operation{
  41. tags: ["Chats"],
  42. summary: "Mark a message as read",
  43. operationId: "ChatController.mark_message_as_read",
  44. parameters: [
  45. Operation.parameter(:id, :path, :string, "The ID of the Chat"),
  46. Operation.parameter(:message_id, :path, :string, "The ID of the message")
  47. ],
  48. responses: %{
  49. 200 =>
  50. Operation.response(
  51. "The read ChatMessage",
  52. "application/json",
  53. ChatMessage
  54. )
  55. },
  56. security: [
  57. %{
  58. "oAuth" => ["write:chats"]
  59. }
  60. ]
  61. }
  62. end
  63. def show_operation do
  64. %Operation{
  65. tags: ["Chats"],
  66. summary: "Retrieve a chat",
  67. operationId: "ChatController.show",
  68. parameters: [
  69. Operation.parameter(
  70. :id,
  71. :path,
  72. :string,
  73. "The id of the chat",
  74. required: true,
  75. example: "1234"
  76. )
  77. ],
  78. responses: %{
  79. 200 =>
  80. Operation.response(
  81. "The existing chat",
  82. "application/json",
  83. Chat
  84. )
  85. },
  86. security: [
  87. %{
  88. "oAuth" => ["read"]
  89. }
  90. ]
  91. }
  92. end
  93. def create_operation do
  94. %Operation{
  95. tags: ["Chats"],
  96. summary: "Create a chat",
  97. operationId: "ChatController.create",
  98. parameters: [
  99. Operation.parameter(
  100. :id,
  101. :path,
  102. :string,
  103. "The account id of the recipient of this chat",
  104. required: true,
  105. example: "someflakeid"
  106. )
  107. ],
  108. responses: %{
  109. 200 =>
  110. Operation.response(
  111. "The created or existing chat",
  112. "application/json",
  113. Chat
  114. )
  115. },
  116. security: [
  117. %{
  118. "oAuth" => ["write:chats"]
  119. }
  120. ]
  121. }
  122. end
  123. def index_operation do
  124. %Operation{
  125. tags: ["Chats"],
  126. summary: "Retrieve list of chats (unpaginated)",
  127. deprecated: true,
  128. description:
  129. "Deprecated due to no support for pagination. Using [/api/v2/pleroma/chats](#operation/ChatController.index2) instead is recommended.",
  130. operationId: "ChatController.index",
  131. parameters: [
  132. Operation.parameter(
  133. :with_muted,
  134. :query,
  135. BooleanLike.schema(),
  136. "Include chats from muted users"
  137. )
  138. ],
  139. responses: %{
  140. 200 => Operation.response("The chats of the user", "application/json", chats_response())
  141. },
  142. security: [
  143. %{
  144. "oAuth" => ["read:chats"]
  145. }
  146. ]
  147. }
  148. end
  149. def index2_operation do
  150. %Operation{
  151. tags: ["Chats"],
  152. summary: "Retrieve list of chats",
  153. operationId: "ChatController.index2",
  154. parameters: [
  155. Operation.parameter(
  156. :with_muted,
  157. :query,
  158. BooleanLike.schema(),
  159. "Include chats from muted users"
  160. )
  161. | pagination_params()
  162. ],
  163. responses: %{
  164. 200 => Operation.response("The chats of the user", "application/json", chats_response())
  165. },
  166. security: [
  167. %{
  168. "oAuth" => ["read:chats"]
  169. }
  170. ]
  171. }
  172. end
  173. def messages_operation do
  174. %Operation{
  175. tags: ["Chats"],
  176. summary: "Retrieve chat's messages",
  177. operationId: "ChatController.messages",
  178. parameters:
  179. [Operation.parameter(:id, :path, :string, "The ID of the Chat")] ++
  180. pagination_params(),
  181. responses: %{
  182. 200 =>
  183. Operation.response(
  184. "The messages in the chat",
  185. "application/json",
  186. chat_messages_response()
  187. ),
  188. 404 => Operation.response("Not Found", "application/json", ApiError)
  189. },
  190. security: [
  191. %{
  192. "oAuth" => ["read:chats"]
  193. }
  194. ]
  195. }
  196. end
  197. def post_chat_message_operation do
  198. %Operation{
  199. tags: ["Chats"],
  200. summary: "Post a message to the chat",
  201. operationId: "ChatController.post_chat_message",
  202. parameters: [
  203. Operation.parameter(:id, :path, :string, "The ID of the Chat")
  204. ],
  205. requestBody: request_body("Parameters", chat_message_create()),
  206. responses: %{
  207. 200 =>
  208. Operation.response(
  209. "The newly created ChatMessage",
  210. "application/json",
  211. ChatMessage
  212. ),
  213. 400 => Operation.response("Bad Request", "application/json", ApiError),
  214. 422 => Operation.response("MRF Rejection", "application/json", ApiError)
  215. },
  216. security: [
  217. %{
  218. "oAuth" => ["write:chats"]
  219. }
  220. ]
  221. }
  222. end
  223. def delete_message_operation do
  224. %Operation{
  225. tags: ["Chats"],
  226. summary: "Delete message",
  227. operationId: "ChatController.delete_message",
  228. parameters: [
  229. Operation.parameter(:id, :path, :string, "The ID of the Chat"),
  230. Operation.parameter(:message_id, :path, :string, "The ID of the message")
  231. ],
  232. responses: %{
  233. 200 =>
  234. Operation.response(
  235. "The deleted ChatMessage",
  236. "application/json",
  237. ChatMessage
  238. )
  239. },
  240. security: [
  241. %{
  242. "oAuth" => ["write:chats"]
  243. }
  244. ]
  245. }
  246. end
  247. def chats_response do
  248. %Schema{
  249. title: "ChatsResponse",
  250. description: "Response schema for multiple Chats",
  251. type: :array,
  252. items: Chat,
  253. example: [
  254. %{
  255. "account" => %{
  256. "pleroma" => %{
  257. "is_admin" => false,
  258. "is_confirmed" => true,
  259. "hide_followers_count" => false,
  260. "is_moderator" => false,
  261. "hide_favorites" => true,
  262. "ap_id" => "https://dontbulling.me/users/lain",
  263. "hide_follows_count" => false,
  264. "hide_follows" => false,
  265. "background_image" => nil,
  266. "skip_thread_containment" => false,
  267. "hide_followers" => false,
  268. "relationship" => %{},
  269. "tags" => []
  270. },
  271. "avatar" =>
  272. "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
  273. "following_count" => 0,
  274. "header_static" => "https://originalpatchou.li/images/banner.png",
  275. "source" => %{
  276. "sensitive" => false,
  277. "note" => "lain",
  278. "pleroma" => %{
  279. "discoverable" => false,
  280. "actor_type" => "Person"
  281. },
  282. "fields" => []
  283. },
  284. "statuses_count" => 1,
  285. "locked" => false,
  286. "created_at" => "2020-04-16T13:40:15.000Z",
  287. "display_name" => "lain",
  288. "fields" => [],
  289. "acct" => "lain@dontbulling.me",
  290. "id" => "9u6Qw6TAZANpqokMkK",
  291. "emojis" => [],
  292. "avatar_static" =>
  293. "https://dontbulling.me/media/065a4dd3c6740dab13ff9c71ec7d240bb9f8be9205c9e7467fb2202117da1e32.jpg",
  294. "username" => "lain",
  295. "followers_count" => 0,
  296. "header" => "https://originalpatchou.li/images/banner.png",
  297. "bot" => false,
  298. "note" => "lain",
  299. "url" => "https://dontbulling.me/users/lain"
  300. },
  301. "id" => "1",
  302. "unread" => 2
  303. }
  304. ]
  305. }
  306. end
  307. def chat_messages_response do
  308. %Schema{
  309. title: "ChatMessagesResponse",
  310. description: "Response schema for multiple ChatMessages",
  311. type: :array,
  312. items: ChatMessage,
  313. example: [
  314. %{
  315. "emojis" => [
  316. %{
  317. "static_url" => "https://dontbulling.me/emoji/Firefox.gif",
  318. "visible_in_picker" => false,
  319. "shortcode" => "firefox",
  320. "url" => "https://dontbulling.me/emoji/Firefox.gif"
  321. }
  322. ],
  323. "created_at" => "2020-04-21T15:11:46.000Z",
  324. "content" => "Check this out :firefox:",
  325. "id" => "13",
  326. "chat_id" => "1",
  327. "account_id" => "someflakeid",
  328. "unread" => false
  329. },
  330. %{
  331. "account_id" => "someflakeid",
  332. "content" => "Whats' up?",
  333. "id" => "12",
  334. "chat_id" => "1",
  335. "emojis" => [],
  336. "created_at" => "2020-04-21T15:06:45.000Z",
  337. "unread" => false
  338. }
  339. ]
  340. }
  341. end
  342. def chat_message_create do
  343. %Schema{
  344. title: "ChatMessageCreateRequest",
  345. description: "POST body for creating an chat message",
  346. type: :object,
  347. properties: %{
  348. content: %Schema{
  349. type: :string,
  350. description: "The content of your message. Optional if media_id is present"
  351. },
  352. media_id: %Schema{type: :string, description: "The id of an upload"}
  353. },
  354. example: %{
  355. "content" => "Hey wanna buy feet pics?",
  356. "media_id" => "134234"
  357. }
  358. }
  359. end
  360. def mark_as_read do
  361. %Schema{
  362. title: "MarkAsReadRequest",
  363. description: "POST body for marking a number of chat messages as read",
  364. type: :object,
  365. required: [:last_read_id],
  366. properties: %{
  367. last_read_id: %Schema{
  368. type: :string,
  369. description: "The content of your message."
  370. }
  371. },
  372. example: %{
  373. "last_read_id" => "abcdef12456"
  374. }
  375. }
  376. end
  377. end