Skip to content

Commit aa348ce

Browse files
committed
Update for MSC4335 landing in the spec
1 parent 0417805 commit aa348ce

9 files changed

Lines changed: 115 additions & 159 deletions

File tree

changelog.d/18876.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add support for experimental [MSC4335](https://github.com/matrix-org/matrix-spec-proposals/pull/4335) M_USER_LIMIT_EXCEEDED error code for media upload limits.
1+
Return M_USER_LIMIT_EXCEEDED error code for media upload limits from [MSC4335](https://github.com/matrix-org/matrix-spec-proposals/pull/4335).

docs/usage/configuration/config_documentation.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,19 +2215,20 @@ Options for each entry include:
22152215

22162216
* `max_size` (byte size): Amount of data that can be uploaded in the time period by the user. Required.
22172217

2218-
* `msc4335_info_uri` (string): Experimental MSC4335 URI to where the user can find information about the upload limit. Optional.
2218+
* `info_uri` (string): URI return to the client for where the user can find information about the upload limit. Optional. If not set then a static `data:text/html` URI is returned with a simple message. It is recommended to provide an `info_uri` that points to a page with more information about the upload limit and how users can reduce their upload usage or request an upload limit increase.
22192219

2220-
* `msc4335_can_upgrade` (boolean): Experimental MSC4335 value to say if the limit can be increased. Optional.
2220+
* `can_upgrade` (boolean): Value returned to the client for whether the limit can be increased. Optional default `false`. Defaults to `false`.
22212221

22222222
Example configuration:
22232223
```yaml
22242224
media_upload_limits:
22252225
- time_period: 1h
22262226
max_size: 100M
2227+
info_uri: https://example.com/quota#hour
22272228
- time_period: 1w
22282229
max_size: 500M
2229-
msc4335_info_uri: https://example.com/quota
2230-
msc4335_can_upgrade: true
2230+
info_uri: https://example.com/quota
2231+
can_upgrade: true
22312232
```
22322233
---
22332234
### `max_image_pixels`

schema/synapse-config.schema.yaml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,21 +2475,29 @@ properties:
24752475
description: >-
24762476
Amount of data that can be uploaded in the time period by the user.
24772477
Required.
2478-
msc4335_info_uri:
2478+
info_uri:
24792479
type: string
24802480
description: >-
2481-
Experimental MSC4335 URI to where the user can find information about the upload limit. Optional.
2482-
msc4335_can_upgrade:
2481+
URI return to the client for where the user can find information
2482+
about the upload limit. Optional.
2483+
If not set then a static `data:text/html` URI is returned with a
2484+
simple message.
2485+
It is recommended to provide an `info_uri` that points to a page
2486+
with more information about the upload limit and how users can
2487+
reduce their upload usage or request an upload limit increase.
2488+
can_upgrade:
24832489
type: boolean
24842490
description: >-
2485-
Experimental MSC4335 value to say if the limit can be increased. Optional.
2491+
Value returned to the client for whether the limit can be increased. Optional default `false`.
2492+
default: false
24862493
examples:
24872494
- - time_period: 1h
24882495
max_size: 100M
2496+
info_uri: https://example.com/quota#hour
24892497
- time_period: 1w
24902498
max_size: 500M
2491-
msc4335_info_uri: https://example.com/quota
2492-
msc4335_can_upgrade: true
2499+
info_uri: https://example.com/quota
2500+
can_upgrade: true
24932501
max_image_pixels:
24942502
$ref: "#/$defs/bytes"
24952503
description: Maximum number of pixels that will be thumbnailed.

synapse/api/errors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class Codes(str, Enum):
152152
# Part of MSC4326
153153
UNKNOWN_DEVICE = "ORG.MATRIX.MSC4326.M_UNKNOWN_DEVICE"
154154

155-
MSC4335_USER_LIMIT_EXCEEDED = "ORG.MATRIX.MSC4335_USER_LIMIT_EXCEEDED"
155+
USER_LIMIT_EXCEEDED = "M_USER_LIMIT_EXCEEDED"
156156

157157

158158
class CodeMessageException(RuntimeError):
@@ -515,9 +515,9 @@ def error_dict(self, config: Optional["HomeServerConfig"]) -> "JsonDict":
515515
)
516516

517517

518-
class MSC4335UserLimitExceededError(SynapseError):
518+
class UserLimitExceededError(SynapseError):
519519
"""
520-
Experimental implementation of MSC4335 M_USER_LIMIT_EXCEEDED error
520+
Implementation of M_USER_LIMIT_EXCEEDED error
521521
"""
522522

523523
def __init__(
@@ -528,16 +528,16 @@ def __init__(
528528
can_upgrade: bool = False,
529529
):
530530
additional_fields: dict[str, Union[str, bool]] = {
531-
"org.matrix.msc4335.info_uri": info_uri,
531+
"info_uri": info_uri,
532532
}
533533

534534
if can_upgrade:
535-
additional_fields["org.matrix.msc4335.can_upgrade"] = can_upgrade
535+
additional_fields["can_upgrade"] = can_upgrade
536536

537537
super().__init__(
538538
code,
539539
msg,
540-
Codes.MSC4335_USER_LIMIT_EXCEEDED,
540+
Codes.USER_LIMIT_EXCEEDED,
541541
additional_fields=additional_fields,
542542
)
543543

synapse/config/repository.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import logging
2323
import os
24-
from typing import Any, Optional
24+
from typing import Any
2525

2626
import attr
2727

@@ -134,11 +134,11 @@ class MediaUploadLimit:
134134
time_period_ms: int
135135
"""The time period in milliseconds."""
136136

137-
msc4335_info_uri: Optional[str] = None
138-
"""Used for experimental MSC4335 error code feature"""
137+
info_uri: str
138+
"""The URI to return with the M_USER_LIMIT_EXCEEDED error."""
139139

140-
msc4335_can_upgrade: Optional[bool] = None
141-
"""Used for experimental MSC4335 error code feature"""
140+
can_upgrade: bool = False
141+
"""Whether the user can upgrade their plan to increase the limit. This is returned in the M_USER_LIMIT_EXCEEDED error."""
142142

143143

144144
class ContentRepositoryConfig(Config):
@@ -314,23 +314,21 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
314314
for limit_config in config.get("media_upload_limits", []):
315315
time_period_ms = self.parse_duration(limit_config["time_period"])
316316
max_bytes = self.parse_size(limit_config["max_size"])
317-
msc4335_info_uri = limit_config.get("msc4335_info_uri", None)
318-
msc4335_can_upgrade = limit_config.get("msc4335_can_upgrade", None)
317+
info_uri = limit_config.get("info_uri", "")
318+
can_upgrade = bool(limit_config.get("can_upgrade", False))
319319

320-
if (msc4335_info_uri is not None or msc4335_can_upgrade is not None) and (
321-
not (msc4335_info_uri and msc4335_can_upgrade is not None)
322-
):
323-
raise ConfigError(
324-
"If any of msc4335_info_uri or msc4335_can_upgrade are set, then both msc4335_info_uri and "
325-
"msc4335_can_upgrade must be set."
320+
if info_uri == "":
321+
logger.warning(
322+
"Empty info_uri provided for media upload limit, using static fallback value instead. You should specify an info_uri that points to more information about the upload limits imposed."
326323
)
324+
info_uri = "data:text/html,<p>You have exceeded a media upload limit. Ask your server administrator for more information.</p>"
327325

328326
self.media_upload_limits.append(
329327
MediaUploadLimit(
330328
max_bytes,
331329
time_period_ms,
332-
msc4335_info_uri,
333-
msc4335_can_upgrade,
330+
info_uri,
331+
can_upgrade,
334332
)
335333
)
336334

synapse/media/media_repository.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
Codes,
3838
FederationDeniedError,
3939
HttpResponseException,
40-
MSC4335UserLimitExceededError,
4140
NotFoundError,
4241
RequestSendFailed,
4342
SynapseError,
43+
UserLimitExceededError,
4444
cs_error,
4545
)
4646
from synapse.api.ratelimiting import Ratelimiter
@@ -71,7 +71,6 @@
7171
)
7272
from synapse.media.thumbnailer import Thumbnailer, ThumbnailError
7373
from synapse.media.url_previewer import UrlPreviewer
74-
from synapse.rest.admin.experimental_features import ExperimentalFeature
7574
from synapse.storage.databases.main.media_repository import LocalMedia, RemoteMedia
7675
from synapse.types import UserID
7776
from synapse.util.async_helpers import Linearizer
@@ -398,26 +397,12 @@ async def create_or_update_content(
398397
sent_bytes=uploaded_media_size,
399398
attempted_bytes=content_length,
400399
)
401-
# If the MSC4335 experimental feature is enabled and the media limit
402-
# has the info_uri configured then we raise the MSC4335 error
403-
msc4335_enabled = await self.store.is_feature_enabled(
404-
auth_user.to_string(), ExperimentalFeature.MSC4335
405-
)
406-
if (
407-
msc4335_enabled
408-
and limit.msc4335_info_uri
409-
and limit.msc4335_can_upgrade is not None
410-
):
411-
raise MSC4335UserLimitExceededError(
412-
403,
413-
"Media upload limit exceeded",
414-
limit.msc4335_info_uri,
415-
limit.msc4335_can_upgrade,
416-
)
417-
# Otherwise we use the current behaviour albeit not spec compliant
418-
# See: https://github.com/element-hq/synapse/issues/18749
419-
raise SynapseError(
420-
400, "Media upload limit exceeded", Codes.RESOURCE_LIMIT_EXCEEDED
400+
401+
raise UserLimitExceededError(
402+
403,
403+
"Media upload limit exceeded",
404+
limit.info_uri,
405+
limit.can_upgrade,
421406
)
422407

423408
if is_new_media:

synapse/module_api/callbacks/media_repository_callbacks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ async def on_media_upload_limit_exceeded(
150150
):
151151
# Use a copy of the data in case the module modifies it
152152
limit_copy = MediaUploadLimit(
153-
max_bytes=limit.max_bytes, time_period_ms=limit.time_period_ms
153+
max_bytes=limit.max_bytes,
154+
time_period_ms=limit.time_period_ms,
155+
info_uri=limit.info_uri,
156+
can_upgrade=limit.can_upgrade,
154157
)
155158
await delay_cancellation(
156159
callback(user_id, limit_copy, sent_bytes, attempted_bytes)

synapse/rest/admin/experimental_features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class ExperimentalFeature(str, Enum):
4444
MSC3881 = "msc3881"
4545
MSC3575 = "msc3575"
4646
MSC4222 = "msc4222"
47-
MSC4335 = "msc4335"
4847

4948
def is_globally_enabled(self, config: "HomeServerConfig") -> bool:
5049
if self is ExperimentalFeature.MSC3881:
@@ -53,8 +52,6 @@ def is_globally_enabled(self, config: "HomeServerConfig") -> bool:
5352
return config.experimental.msc3575_enabled
5453
if self is ExperimentalFeature.MSC4222:
5554
return config.experimental.msc4222_enabled
56-
if self is ExperimentalFeature.MSC4335:
57-
return config.experimental.msc4335_enabled
5855

5956
assert_never(self)
6057

0 commit comments

Comments
 (0)