logo

pleroma

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

search_operation.ex (6079B)


  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.SearchOperation do
  5. alias OpenApiSpex.Operation
  6. alias OpenApiSpex.Schema
  7. alias Pleroma.Web.ApiSpec.AccountOperation
  8. alias Pleroma.Web.ApiSpec.Schemas.Account
  9. alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
  10. alias Pleroma.Web.ApiSpec.Schemas.FlakeID
  11. alias Pleroma.Web.ApiSpec.Schemas.Status
  12. alias Pleroma.Web.ApiSpec.Schemas.Tag
  13. import Pleroma.Web.ApiSpec.Helpers
  14. def open_api_operation(action) do
  15. operation = String.to_existing_atom("#{action}_operation")
  16. apply(__MODULE__, operation, [])
  17. end
  18. # Note: `with_relationships` param is not supported (PleromaFE uses this op for autocomplete)
  19. def account_search_operation do
  20. %Operation{
  21. tags: ["Search"],
  22. summary: "Search for matching accounts by username or display name",
  23. operationId: "SearchController.account_search",
  24. parameters: [
  25. Operation.parameter(:q, :query, %Schema{type: :string}, "What to search for",
  26. required: true
  27. ),
  28. Operation.parameter(
  29. :limit,
  30. :query,
  31. %Schema{type: :integer, default: 40},
  32. "Maximum number of results"
  33. ),
  34. Operation.parameter(
  35. :resolve,
  36. :query,
  37. %Schema{allOf: [BooleanLike], default: false},
  38. "Attempt WebFinger lookup. Use this when `q` is an exact address."
  39. ),
  40. Operation.parameter(
  41. :following,
  42. :query,
  43. %Schema{allOf: [BooleanLike], default: false},
  44. "Only include accounts that the user is following"
  45. )
  46. ],
  47. responses: %{
  48. 200 =>
  49. Operation.response(
  50. "Array of Account",
  51. "application/json",
  52. AccountOperation.array_of_accounts()
  53. )
  54. }
  55. }
  56. end
  57. def search_operation do
  58. %Operation{
  59. tags: ["Search"],
  60. summary: "Search results",
  61. security: [%{"oAuth" => ["read:search"]}],
  62. operationId: "SearchController.search",
  63. deprecated: true,
  64. parameters: [
  65. Operation.parameter(
  66. :account_id,
  67. :query,
  68. FlakeID.schema(),
  69. "If provided, statuses returned will be authored only by this account"
  70. ),
  71. Operation.parameter(
  72. :type,
  73. :query,
  74. %Schema{type: :string, enum: ["accounts", "hashtags", "statuses"]},
  75. "Search type"
  76. ),
  77. Operation.parameter(:q, :query, %Schema{type: :string}, "The search query", required: true),
  78. Operation.parameter(
  79. :resolve,
  80. :query,
  81. %Schema{allOf: [BooleanLike], default: false},
  82. "Attempt WebFinger lookup"
  83. ),
  84. Operation.parameter(
  85. :following,
  86. :query,
  87. %Schema{allOf: [BooleanLike], default: false},
  88. "Only include accounts that the user is following"
  89. ),
  90. Operation.parameter(
  91. :offset,
  92. :query,
  93. %Schema{type: :integer},
  94. "Offset"
  95. ),
  96. with_relationships_param() | pagination_params()
  97. ],
  98. responses: %{
  99. 200 => Operation.response("Results", "application/json", results())
  100. }
  101. }
  102. end
  103. def search2_operation do
  104. %Operation{
  105. tags: ["Search"],
  106. summary: "Search results",
  107. security: [%{"oAuth" => ["read:search"]}],
  108. operationId: "SearchController.search2",
  109. parameters: [
  110. Operation.parameter(
  111. :account_id,
  112. :query,
  113. FlakeID.schema(),
  114. "If provided, statuses returned will be authored only by this account"
  115. ),
  116. Operation.parameter(
  117. :type,
  118. :query,
  119. %Schema{type: :string, enum: ["accounts", "hashtags", "statuses"]},
  120. "Search type"
  121. ),
  122. Operation.parameter(:q, :query, %Schema{type: :string}, "What to search for",
  123. required: true
  124. ),
  125. Operation.parameter(
  126. :resolve,
  127. :query,
  128. %Schema{allOf: [BooleanLike], default: false},
  129. "Attempt WebFinger lookup"
  130. ),
  131. Operation.parameter(
  132. :following,
  133. :query,
  134. %Schema{allOf: [BooleanLike], default: false},
  135. "Only include accounts that the user is following"
  136. ),
  137. with_relationships_param() | pagination_params()
  138. ],
  139. responses: %{
  140. 200 => Operation.response("Results", "application/json", results2())
  141. }
  142. }
  143. end
  144. defp results2 do
  145. %Schema{
  146. title: "SearchResults",
  147. type: :object,
  148. properties: %{
  149. accounts: %Schema{
  150. type: :array,
  151. items: Account,
  152. description: "Accounts which match the given query"
  153. },
  154. statuses: %Schema{
  155. type: :array,
  156. items: Status,
  157. description: "Statuses which match the given query"
  158. },
  159. hashtags: %Schema{
  160. type: :array,
  161. items: Tag,
  162. description: "Hashtags which match the given query"
  163. }
  164. },
  165. example: %{
  166. "accounts" => [Account.schema().example],
  167. "statuses" => [Status.schema().example],
  168. "hashtags" => [Tag.schema().example]
  169. }
  170. }
  171. end
  172. defp results do
  173. %Schema{
  174. title: "SearchResults",
  175. type: :object,
  176. properties: %{
  177. accounts: %Schema{
  178. type: :array,
  179. items: Account,
  180. description: "Accounts which match the given query"
  181. },
  182. statuses: %Schema{
  183. type: :array,
  184. items: Status,
  185. description: "Statuses which match the given query"
  186. },
  187. hashtags: %Schema{
  188. type: :array,
  189. items: %Schema{type: :string},
  190. description: "Hashtags which match the given query"
  191. }
  192. },
  193. example: %{
  194. "accounts" => [Account.schema().example],
  195. "statuses" => [Status.schema().example],
  196. "hashtags" => ["cofe"]
  197. }
  198. }
  199. end
  200. end