EventBase: add the concept of sticky_duration

This commit is contained in:
Olivier 'reivilibre
2026-01-16 08:59:42 +00:00
parent a2b0da6c0a
commit 0c53a1e83a
+23 -1
View File
@@ -36,7 +36,12 @@ from typing import (
import attr
from unpaddedbase64 import encode_base64
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
from synapse.api.constants import (
EventContentFields,
EventTypes,
RelationTypes,
StickyEvent,
)
from synapse.api.room_versions import EventFormatVersions, RoomVersion, RoomVersions
from synapse.synapse_rust.events import EventInternalMetadata
from synapse.types import (
@@ -318,6 +323,23 @@ class EventBase(metaclass=abc.ABCMeta):
# this will be a no-op if the event dict is already frozen.
self._dict = freeze(self._dict)
def sticky_duration(self) -> int | None:
"""
Returns the effective sticky duration of this event, or None
if the event does not have a sticky duration.
(Sticky Events are a MSC4354 feature.)
Clamps the sticky duration to the maximum allowed duration.
"""
sticky_obj = self.get_dict().get(StickyEvent.FIELD_NAME, None)
if type(sticky_obj) is not dict:
return None
sticky_duration_ms = sticky_obj.get("duration_ms", None)
# MSC: Valid values are the integer range 0-MAX_DURATION_MS
if type(sticky_duration_ms) is int and sticky_duration_ms >= 0:
return min(sticky_duration_ms, StickyEvent.MAX_DURATION_MS)
return None
def __str__(self) -> str:
return self.__repr__()