diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/user.ex | 14 | ||||
| -rw-r--r-- | lib/pleroma/user/query.ex | 4 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 37 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/operations/app_operation.ex | 60 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex | 40 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/operations/domain_block_operation.ex | 31 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/schemas/app_create_request.ex | 33 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/schemas/app_create_response.ex | 33 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex | 42 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/schemas/domain_block_request.ex | 20 | ||||
| -rw-r--r-- | lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex | 16 | ||||
| -rw-r--r-- | lib/pleroma/web/common_api/activity_draft.ex | 16 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex | 2 | ||||
| -rw-r--r-- | lib/pleroma/web/oauth/scopes.ex | 6 | 
14 files changed, 185 insertions, 169 deletions
| diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 477237756..b451202b2 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -832,6 +832,7 @@ defmodule Pleroma.User do    def set_cache(%User{} = user) do      Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)      Cachex.put(:user_cache, "nickname:#{user.nickname}", user) +    Cachex.put(:user_cache, "friends_ap_ids:#{user.nickname}", get_user_friends_ap_ids(user))      {:ok, user}    end @@ -847,9 +848,22 @@ defmodule Pleroma.User do      end    end +  def get_user_friends_ap_ids(user) do +    from(u in User.get_friends_query(user), select: u.ap_id) +    |> Repo.all() +  end + +  @spec get_cached_user_friends_ap_ids(User.t()) :: [String.t()] +  def get_cached_user_friends_ap_ids(user) do +    Cachex.fetch!(:user_cache, "friends_ap_ids:#{user.ap_id}", fn _ -> +      get_user_friends_ap_ids(user) +    end) +  end +    def invalidate_cache(user) do      Cachex.del(:user_cache, "ap_id:#{user.ap_id}")      Cachex.del(:user_cache, "nickname:#{user.nickname}") +    Cachex.del(:user_cache, "friends_ap_ids:#{user.ap_id}")    end    @spec get_cached_by_ap_id(String.t()) :: User.t() | nil diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index ec88088cf..ac77aab71 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -54,13 +54,13 @@ defmodule Pleroma.User.Query do              select: term(),              limit: pos_integer()            } -          | %{} +          | map()    @ilike_criteria [:nickname, :name, :query]    @equal_criteria [:email]    @contains_criteria [:ap_id, :nickname] -  @spec build(criteria()) :: Query.t() +  @spec build(Query.t(), criteria()) :: Query.t()    def build(query \\ base_query(), criteria) do      prepare_query(query, criteria)    end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4cce4f13c..61a4960a0 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1048,6 +1048,41 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      )    end +  defp restrict_replies(query, %{ +         "reply_filtering_user" => user, +         "reply_visibility" => "self" +       }) do +    from( +      [activity, object] in query, +      where: +        fragment( +          "?->>'inReplyTo' is null OR ? = ANY(?)", +          object.data, +          ^user.ap_id, +          activity.recipients +        ) +    ) +  end + +  defp restrict_replies(query, %{ +         "reply_filtering_user" => user, +         "reply_visibility" => "following" +       }) do +    from( +      [activity, object] in query, +      where: +        fragment( +          "?->>'inReplyTo' is null OR ? && array_remove(?, ?) OR ? = ?", +          object.data, +          ^[user.ap_id | User.get_cached_user_friends_ap_ids(user)], +          activity.recipients, +          activity.actor, +          activity.actor, +          ^user.ap_id +        ) +    ) +  end +    defp restrict_replies(query, _), do: query    defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do @@ -1262,6 +1297,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      |> maybe_set_thread_muted_field(opts)      |> maybe_order(opts)      |> restrict_recipients(recipients, opts["user"]) +    |> restrict_replies(opts)      |> restrict_tag(opts)      |> restrict_tag_reject(opts)      |> restrict_tag_all(opts) @@ -1276,7 +1312,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      |> restrict_media(opts)      |> restrict_visibility(opts)      |> restrict_thread_visibility(opts, config) -    |> restrict_replies(opts)      |> restrict_reblogs(opts)      |> restrict_pinned(opts)      |> restrict_muted_reblogs(restrict_muted_reblogs_opts) diff --git a/lib/pleroma/web/api_spec/operations/app_operation.ex b/lib/pleroma/web/api_spec/operations/app_operation.ex index 26d8dbd42..035ef2470 100644 --- a/lib/pleroma/web/api_spec/operations/app_operation.ex +++ b/lib/pleroma/web/api_spec/operations/app_operation.ex @@ -6,8 +6,6 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do    alias OpenApiSpex.Operation    alias OpenApiSpex.Schema    alias Pleroma.Web.ApiSpec.Helpers -  alias Pleroma.Web.ApiSpec.Schemas.AppCreateRequest -  alias Pleroma.Web.ApiSpec.Schemas.AppCreateResponse    @spec open_api_operation(atom) :: Operation.t()    def open_api_operation(action) do @@ -22,9 +20,9 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do        summary: "Create an application",        description: "Create a new application to obtain OAuth2 credentials",        operationId: "AppController.create", -      requestBody: Helpers.request_body("Parameters", AppCreateRequest, required: true), +      requestBody: Helpers.request_body("Parameters", create_request(), required: true),        responses: %{ -        200 => Operation.response("App", "application/json", AppCreateResponse), +        200 => Operation.response("App", "application/json", create_response()),          422 =>            Operation.response(              "Unprocessable Entity", @@ -93,4 +91,58 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do        }      }    end + +  defp create_request do +    %Schema{ +      title: "AppCreateRequest", +      description: "POST body for creating an app", +      type: :object, +      properties: %{ +        client_name: %Schema{type: :string, description: "A name for your application."}, +        redirect_uris: %Schema{ +          type: :string, +          description: +            "Where the user should be redirected after authorization. To display the authorization code to the user instead of redirecting to a web page, use `urn:ietf:wg:oauth:2.0:oob` in this parameter." +        }, +        scopes: %Schema{ +          type: :string, +          description: "Space separated list of scopes", +          default: "read" +        }, +        website: %Schema{type: :string, description: "A URL to the homepage of your app"} +      }, +      required: [:client_name, :redirect_uris], +      example: %{ +        "client_name" => "My App", +        "redirect_uris" => "https://myapp.com/auth/callback", +        "website" => "https://myapp.com/" +      } +    } +  end + +  defp create_response do +    %Schema{ +      title: "AppCreateResponse", +      description: "Response schema for an app", +      type: :object, +      properties: %{ +        id: %Schema{type: :string}, +        name: %Schema{type: :string}, +        client_id: %Schema{type: :string}, +        client_secret: %Schema{type: :string}, +        redirect_uri: %Schema{type: :string}, +        vapid_key: %Schema{type: :string}, +        website: %Schema{type: :string, nullable: true} +      }, +      example: %{ +        "id" => "123", +        "name" => "My App", +        "client_id" => "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM", +        "client_secret" => "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw", +        "vapid_key" => +          "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=", +        "website" => "https://myapp.com/" +      } +    } +  end  end diff --git a/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex b/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex index cf2215823..a117fe460 100644 --- a/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex +++ b/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex @@ -4,7 +4,8 @@  defmodule Pleroma.Web.ApiSpec.CustomEmojiOperation do    alias OpenApiSpex.Operation -  alias Pleroma.Web.ApiSpec.Schemas.CustomEmojisResponse +  alias OpenApiSpex.Schema +  alias Pleroma.Web.ApiSpec.Schemas.CustomEmoji    def open_api_operation(action) do      operation = String.to_existing_atom("#{action}_operation") @@ -18,8 +19,43 @@ defmodule Pleroma.Web.ApiSpec.CustomEmojiOperation do        description: "Returns custom emojis that are available on the server.",        operationId: "CustomEmojiController.index",        responses: %{ -        200 => Operation.response("Custom Emojis", "application/json", CustomEmojisResponse) +        200 => Operation.response("Custom Emojis", "application/json", custom_emojis_resposnse())        }      }    end + +  defp custom_emojis_resposnse do +    %Schema{ +      title: "CustomEmojisResponse", +      description: "Response schema for custom emojis", +      type: :array, +      items: CustomEmoji, +      example: [ +        %{ +          "category" => "Fun", +          "shortcode" => "blank", +          "static_url" => "https://lain.com/emoji/blank.png", +          "tags" => ["Fun"], +          "url" => "https://lain.com/emoji/blank.png", +          "visible_in_picker" => false +        }, +        %{ +          "category" => "Gif,Fun", +          "shortcode" => "firefox", +          "static_url" => "https://lain.com/emoji/Firefox.gif", +          "tags" => ["Gif", "Fun"], +          "url" => "https://lain.com/emoji/Firefox.gif", +          "visible_in_picker" => true +        }, +        %{ +          "category" => "pack:mixed", +          "shortcode" => "sadcat", +          "static_url" => "https://lain.com/emoji/mixed/sadcat.png", +          "tags" => ["pack:mixed"], +          "url" => "https://lain.com/emoji/mixed/sadcat.png", +          "visible_in_picker" => true +        } +      ] +    } +  end  end diff --git a/lib/pleroma/web/api_spec/operations/domain_block_operation.ex b/lib/pleroma/web/api_spec/operations/domain_block_operation.ex index dd14837c3..3b7f51ceb 100644 --- a/lib/pleroma/web/api_spec/operations/domain_block_operation.ex +++ b/lib/pleroma/web/api_spec/operations/domain_block_operation.ex @@ -6,8 +6,6 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do    alias OpenApiSpex.Operation    alias OpenApiSpex.Schema    alias Pleroma.Web.ApiSpec.Helpers -  alias Pleroma.Web.ApiSpec.Schemas.DomainBlockRequest -  alias Pleroma.Web.ApiSpec.Schemas.DomainBlocksResponse    def open_api_operation(action) do      operation = String.to_existing_atom("#{action}_operation") @@ -22,7 +20,13 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do        security: [%{"oAuth" => ["follow", "read:blocks"]}],        operationId: "DomainBlockController.index",        responses: %{ -        200 => Operation.response("Domain blocks", "application/json", DomainBlocksResponse) +        200 => +          Operation.response("Domain blocks", "application/json", %Schema{ +            description: "Response schema for domain blocks", +            type: :array, +            items: %Schema{type: :string}, +            example: ["google.com", "facebook.com"] +          })        }      }    end @@ -40,7 +44,7 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do        - prevent following new users from it (but does not remove existing follows)        """,        operationId: "DomainBlockController.create", -      requestBody: Helpers.request_body("Parameters", DomainBlockRequest, required: true), +      requestBody: domain_block_request(),        security: [%{"oAuth" => ["follow", "write:blocks"]}],        responses: %{          200 => Operation.response("Empty object", "application/json", %Schema{type: :object}) @@ -54,11 +58,28 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do        summary: "Unblock a domain",        description: "Remove a domain block, if it exists in the user's array of blocked domains.",        operationId: "DomainBlockController.delete", -      requestBody: Helpers.request_body("Parameters", DomainBlockRequest, required: true), +      requestBody: domain_block_request(),        security: [%{"oAuth" => ["follow", "write:blocks"]}],        responses: %{          200 => Operation.response("Empty object", "application/json", %Schema{type: :object})        }      }    end + +  defp domain_block_request do +    Helpers.request_body( +      "Parameters", +      %Schema{ +        type: :object, +        properties: %{ +          domain: %Schema{type: :string} +        }, +        required: [:domain] +      }, +      required: true, +      example: %{ +        "domain" => "facebook.com" +      } +    ) +  end  end diff --git a/lib/pleroma/web/api_spec/schemas/app_create_request.ex b/lib/pleroma/web/api_spec/schemas/app_create_request.ex deleted file mode 100644 index 8a83abef3..000000000 --- a/lib/pleroma/web/api_spec/schemas/app_create_request.ex +++ /dev/null @@ -1,33 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.AppCreateRequest do -  alias OpenApiSpex.Schema -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "AppCreateRequest", -    description: "POST body for creating an app", -    type: :object, -    properties: %{ -      client_name: %Schema{type: :string, description: "A name for your application."}, -      redirect_uris: %Schema{ -        type: :string, -        description: -          "Where the user should be redirected after authorization. To display the authorization code to the user instead of redirecting to a web page, use `urn:ietf:wg:oauth:2.0:oob` in this parameter." -      }, -      scopes: %Schema{ -        type: :string, -        description: "Space separated list of scopes. If none is provided, defaults to `read`." -      }, -      website: %Schema{type: :string, description: "A URL to the homepage of your app"} -    }, -    required: [:client_name, :redirect_uris], -    example: %{ -      "client_name" => "My App", -      "redirect_uris" => "https://myapp.com/auth/callback", -      "website" => "https://myapp.com/" -    } -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/app_create_response.ex b/lib/pleroma/web/api_spec/schemas/app_create_response.ex deleted file mode 100644 index f290fb031..000000000 --- a/lib/pleroma/web/api_spec/schemas/app_create_response.ex +++ /dev/null @@ -1,33 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.AppCreateResponse do -  alias OpenApiSpex.Schema - -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "AppCreateResponse", -    description: "Response schema for an app", -    type: :object, -    properties: %{ -      id: %Schema{type: :string}, -      name: %Schema{type: :string}, -      client_id: %Schema{type: :string}, -      client_secret: %Schema{type: :string}, -      redirect_uri: %Schema{type: :string}, -      vapid_key: %Schema{type: :string}, -      website: %Schema{type: :string, nullable: true} -    }, -    example: %{ -      "id" => "123", -      "name" => "My App", -      "client_id" => "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM", -      "client_secret" => "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw", -      "vapid_key" => -        "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=", -      "website" => "https://myapp.com/" -    } -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex b/lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex deleted file mode 100644 index 01582a63d..000000000 --- a/lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex +++ /dev/null @@ -1,42 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.CustomEmojisResponse do -  alias Pleroma.Web.ApiSpec.Schemas.CustomEmoji - -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "CustomEmojisResponse", -    description: "Response schema for custom emojis", -    type: :array, -    items: CustomEmoji, -    example: [ -      %{ -        "category" => "Fun", -        "shortcode" => "blank", -        "static_url" => "https://lain.com/emoji/blank.png", -        "tags" => ["Fun"], -        "url" => "https://lain.com/emoji/blank.png", -        "visible_in_picker" => true -      }, -      %{ -        "category" => "Gif,Fun", -        "shortcode" => "firefox", -        "static_url" => "https://lain.com/emoji/Firefox.gif", -        "tags" => ["Gif", "Fun"], -        "url" => "https://lain.com/emoji/Firefox.gif", -        "visible_in_picker" => true -      }, -      %{ -        "category" => "pack:mixed", -        "shortcode" => "sadcat", -        "static_url" => "https://lain.com/emoji/mixed/sadcat.png", -        "tags" => ["pack:mixed"], -        "url" => "https://lain.com/emoji/mixed/sadcat.png", -        "visible_in_picker" => true -      } -    ] -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/domain_block_request.ex b/lib/pleroma/web/api_spec/schemas/domain_block_request.ex deleted file mode 100644 index ee9238361..000000000 --- a/lib/pleroma/web/api_spec/schemas/domain_block_request.ex +++ /dev/null @@ -1,20 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.DomainBlockRequest do -  alias OpenApiSpex.Schema -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "DomainBlockRequest", -    type: :object, -    properties: %{ -      domain: %Schema{type: :string} -    }, -    required: [:domain], -    example: %{ -      "domain" => "facebook.com" -    } -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex b/lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex deleted file mode 100644 index d895aca4e..000000000 --- a/lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex +++ /dev/null @@ -1,16 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.DomainBlocksResponse do -  require OpenApiSpex -  alias OpenApiSpex.Schema - -  OpenApiSpex.schema(%{ -    title: "DomainBlocksResponse", -    description: "Response schema for domain blocks", -    type: :array, -    items: %Schema{type: :string}, -    example: ["google.com", "facebook.com"] -  }) -end diff --git a/lib/pleroma/web/common_api/activity_draft.ex b/lib/pleroma/web/common_api/activity_draft.ex index c1cd15bb2..244cf2be5 100644 --- a/lib/pleroma/web/common_api/activity_draft.ex +++ b/lib/pleroma/web/common_api/activity_draft.ex @@ -84,14 +84,18 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do      %__MODULE__{draft | attachments: attachments}    end -  defp in_reply_to(draft) do -    case Map.get(draft.params, "in_reply_to_status_id") do -      "" -> draft -      nil -> draft -      id -> %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)} -    end +  defp in_reply_to(%{params: %{"in_reply_to_status_id" => ""}} = draft), do: draft + +  defp in_reply_to(%{params: %{"in_reply_to_status_id" => id}} = draft) when is_binary(id) do +    %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}    end +  defp in_reply_to(%{params: %{"in_reply_to_status_id" => %Activity{} = in_reply_to}} = draft) do +    %__MODULE__{draft | in_reply_to: in_reply_to} +  end + +  defp in_reply_to(draft), do: draft +    defp in_reply_to_conversation(draft) do      in_reply_to_conversation = Participation.get(draft.params["in_reply_to_conversation_id"])      %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation} diff --git a/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex b/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex index b3c58005e..403d500e0 100644 --- a/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex @@ -37,6 +37,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do        |> Map.put("type", ["Create", "Announce"])        |> Map.put("blocking_user", user)        |> Map.put("muting_user", user) +      |> Map.put("reply_filtering_user", user)        |> Map.put("user", user)      recipients = [user.ap_id | User.following(user)] @@ -100,6 +101,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do          |> Map.put("local_only", local_only)          |> Map.put("blocking_user", user)          |> Map.put("muting_user", user) +        |> Map.put("reply_filtering_user", user)          |> ActivityPub.fetch_public_activities()        conn diff --git a/lib/pleroma/web/oauth/scopes.ex b/lib/pleroma/web/oauth/scopes.ex index 1023f16d4..6f06f1431 100644 --- a/lib/pleroma/web/oauth/scopes.ex +++ b/lib/pleroma/web/oauth/scopes.ex @@ -17,12 +17,8 @@ defmodule Pleroma.Web.OAuth.Scopes do    """    @spec fetch_scopes(map() | struct(), list()) :: list() -  def fetch_scopes(%Pleroma.Web.ApiSpec.Schemas.AppCreateRequest{scopes: scopes}, default) do -    parse_scopes(scopes, default) -  end -    def fetch_scopes(params, default) do -    parse_scopes(params["scope"] || params["scopes"], default) +    parse_scopes(params["scope"] || params["scopes"] || params[:scopes], default)    end    def parse_scopes(scopes, _default) when is_list(scopes) do | 
