mirror of
https://github.com/block/buzz.git
synced 2026-11-08 10:21:46 +00:00
Renew TTL when unarchiving ephemeral channels (#902)
Signed-off-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
@@ -1054,7 +1054,11 @@ pub async fn unarchive_channel(pool: &PgPool, channel_id: Uuid) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"UPDATE channels SET archived_at = NULL \
|
"UPDATE channels SET archived_at = NULL, \
|
||||||
|
ttl_deadline = CASE \
|
||||||
|
WHEN ttl_seconds IS NOT NULL THEN NOW() + (ttl_seconds || ' seconds')::interval \
|
||||||
|
ELSE ttl_deadline \
|
||||||
|
END \
|
||||||
WHERE id = $1 AND deleted_at IS NULL AND archived_at IS NOT NULL",
|
WHERE id = $1 AND deleted_at IS NULL AND archived_at IS NOT NULL",
|
||||||
)
|
)
|
||||||
.bind(channel_id)
|
.bind(channel_id)
|
||||||
@@ -1253,6 +1257,60 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Unarchiving an expired ephemeral channel renews its TTL lease so the
|
||||||
|
/// reaper does not immediately archive it again.
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires Postgres"]
|
||||||
|
async fn test_unarchive_expired_ephemeral_channel_renews_ttl_deadline() {
|
||||||
|
let pool = setup_pool().await;
|
||||||
|
let owner_pk = random_pubkey();
|
||||||
|
ensure_user(&pool, &owner_pk).await.expect("ensure owner");
|
||||||
|
|
||||||
|
let channel = create_channel(
|
||||||
|
&pool,
|
||||||
|
"test-unarchive-renews-ttl",
|
||||||
|
ChannelType::Stream,
|
||||||
|
ChannelVisibility::Open,
|
||||||
|
None,
|
||||||
|
&owner_pk,
|
||||||
|
Some(60),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("create ephemeral channel");
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
"UPDATE channels SET archived_at = NOW(), ttl_deadline = NOW() - interval '1 second' WHERE id = $1",
|
||||||
|
)
|
||||||
|
.bind(channel.id)
|
||||||
|
.execute(&pool)
|
||||||
|
.await
|
||||||
|
.expect("expire and archive channel");
|
||||||
|
|
||||||
|
unarchive_channel(&pool, channel.id)
|
||||||
|
.await
|
||||||
|
.expect("unarchive expired ephemeral channel");
|
||||||
|
|
||||||
|
let channel = get_channel(&pool, channel.id)
|
||||||
|
.await
|
||||||
|
.expect("reload channel");
|
||||||
|
assert!(
|
||||||
|
channel.archived_at.is_none(),
|
||||||
|
"channel should be unarchived"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
channel.ttl_deadline.expect("ttl deadline") > Utc::now(),
|
||||||
|
"unarchive should renew ttl_deadline into the future"
|
||||||
|
);
|
||||||
|
|
||||||
|
let reaped = reap_expired_ephemeral_channels(&pool)
|
||||||
|
.await
|
||||||
|
.expect("run reaper");
|
||||||
|
assert!(
|
||||||
|
!reaped.contains(&channel.id),
|
||||||
|
"reaper should not immediately rearchive renewed channel"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// A random non-admin, non-owner user cannot remove someone else's bot.
|
/// A random non-admin, non-owner user cannot remove someone else's bot.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[ignore = "requires Postgres"]
|
#[ignore = "requires Postgres"]
|
||||||
|
|||||||
Reference in New Issue
Block a user