Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
media/create: don't store unused expiration time in database
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Oct 24, 2023
1 parent 3d89f8f commit 972fd66
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
13 changes: 6 additions & 7 deletions synapse/media/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,12 @@ async def create_media_id(self, auth_user: UserID) -> Tuple[str, int]:
"""
media_id = random_string(24)
now = self.clock.time_msec()
unused_expires_at = now + self.unused_expiration_time
await self.store.store_local_media_id(
media_id=media_id,
time_now_ms=now,
user_id=auth_user,
unused_expires_at=unused_expires_at,
)
return f"mxc://{self.server_name}/{media_id}", unused_expires_at
return f"mxc://{self.server_name}/{media_id}", now + self.unused_expiration_time

@trace
async def reached_pending_media_limit(
Expand Down Expand Up @@ -262,7 +260,8 @@ async def verify_can_upload(self, media_id: str, auth_user: UserID) -> None:
errcode=Codes.CANNOT_OVERWRITE_MEDIA,
)

if media.get("unused_expires_at", 0) < self.clock.time_msec():
now = self.clock.time_msec()
if media.get("created_ts", now) < now - self.unused_expiration_time:
raise NotFoundError("Media ID has expired")

@trace
Expand Down Expand Up @@ -390,12 +389,12 @@ async def get_local_media_info(
return media_info

# Check if the media ID has expired and still hasn't been uploaded to.
unused_expires_at = media_info.get("unused_expires_at", 0)
if 0 < unused_expires_at < self.clock.time_msec():
now = self.clock.time_msec()
if media_info.get("created_ts", now) + self.unused_expiration_time < now:
respond_404(request)
return None

if self.clock.time_msec() >= wait_until:
if now >= wait_until:
break

await self.clock.sleep(0.5)
Expand Down
7 changes: 3 additions & 4 deletions synapse/storage/databases/main/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def __init__(
self._drop_media_index_without_method,
)

self.unused_expiration_time = hs.config.media.unused_expiration_time

async def _drop_media_index_without_method(
self, progress: JsonDict, batch_size: int
) -> int:
Expand Down Expand Up @@ -170,7 +172,6 @@ async def get_local_media(self, media_id: str) -> Optional[Dict[str, Any]]:
"url_cache",
"safe_from_quarantine",
"user_id",
"unused_expires_at",
),
allow_none=True,
desc="get_local_media",
Expand Down Expand Up @@ -339,15 +340,13 @@ async def store_local_media_id(
media_id: str,
time_now_ms: int,
user_id: UserID,
unused_expires_at: int,
) -> None:
await self.db_pool.simple_insert(
"local_media_repository",
{
"media_id": media_id,
"created_ts": time_now_ms,
"user_id": user_id.to_string(),
"unused_expires_at": unused_expires_at,
},
desc="store_local_media_id",
)
Expand Down Expand Up @@ -436,7 +435,7 @@ def get_pending_media_txn(txn: LoggingTransaction) -> Tuple[int, int]:
row = txn.fetchone()
if not row:
raise StoreError(404, "Failed to count pending media for user")
return row[0], row[1] or 0
return row[0], (row[1] + self.unused_expiration_time if row[1] else 0)

return await self.db_pool.runInteraction("get_url_cache", get_pending_media_txn)

Expand Down

This file was deleted.

0 comments on commit 972fd66

Please sign in to comment.