{#
Copyright 2024, 2025 New Vector Ltd.
Copyright 2023, 2024 The Matrix.org Foundation C.I.C.

SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
-#}

{# Macro to remove 'safe' scope from a scope list. Usage:

    {% call(scopes) scope.unsafe_scopes(scopes=["openid", "urn:matrix:client:api:*", "urn:synapse:admin:*", "urn:mas:admin"]) %}
      `scopes` only has unsafe scopes: ["urn:synapse:admin:*", "urn:mas:admin"]

      <ul>
        {% for scope in scopes %}
          <li>{{ scope }}</li>
        {% endfor %}
      </ul>
    {% endcall %}
#}
{% macro unsafe_scopes(scopes) -%}
  {% set ns = namespace(unsafe_scopes=[]) %}
  {% set safe_scope_prefixes = ["openid", "urn:matrix:client:api:", "urn:matrix:org.matrix.msc2967.client:api:", "urn:matrix:client:device:", "urn:matrix:org.matrix.msc2967.client:device:"] %}
  {% for scope in scopes %}
    {% set ns.is_safe = False %}
    {% for safe_scope_prefix in safe_scope_prefixes %}
      {% if scope.startswith(safe_scope_prefix) %}
        {% set ns.is_safe = True %}
      {% endif %}
    {% endfor %}
    {% if not ns.is_safe %}
      {% set ns.unsafe_scopes = ns.unsafe_scopes + [scope] %}
    {% endif %}
  {% endfor %}
  {{ caller(ns.unsafe_scopes) }}
{%- endmacro %}

{% macro list(scopes) %}
  <ul>
    {% for scope in scopes %}
      {% if scope == "openid" %}
        <li>{{ icon.user_profile() }}<p>{{ _("mas.scope.view_profile") }}</p></li>
      {% elif scope == "urn:mas:graphql:*" %}
        <li>{{ icon.info() }}<p>{{ _("mas.scope.edit_profile") }}</p></li>
        <li>{{ icon.computer() }}<p>{{ _("mas.scope.manage_sessions") }}</p></li>
      {% elif scope == "urn:matrix:client:api:*" or scope == "urn:matrix:org.matrix.msc2967.client:api:*" %}
        <li>{{ icon.chat() }}<p>{{ _("mas.scope.view_messages") }}</p></li>
        <li>{{ icon.send() }}<p>{{ _("mas.scope.send_messages") }}</p></li>
      {% elif scope == "urn:synapse:admin:*" %}
        <li class="dangerous">{{ icon.room() }}<p>{{ _("mas.scope.synapse_admin", scope=scope) }}</p></li>
      {% elif scope == "urn:mas:admin" %}
        <li class="dangerous">{{ icon.admin() }}<p>{{ _("mas.scope.mas_admin", scope=scope) }}</p></li>
      {% elif scope is startingwith("urn:matrix:client:device:") or scope is startingwith("urn:matrix:org.matrix.msc2967.client:device:") %}
        {# We hide this scope #}
      {% else %}
        <li>{{ icon.info() }}<p>{{ scope }}</p></li>
      {% endif %}
    {% endfor %}
  </ul>
{% endmacro %}
