collab: Adapt rate limits based on plan (#15548)

This PR updates the rate limits to adapt based on the user's current
plan.

For the free plan rate limits I just took one-tenth of the existing rate
limits (which are now the Pro limits). We can adjust, as needed.

Release Notes:

- N/A

---------

Co-authored-by: Max <max@zed.dev>
This commit is contained in:
Marshall Bowers 2024-07-31 14:27:28 -04:00 committed by GitHub
parent 7a0149f17c
commit 8c54a46202
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 195 additions and 106 deletions

View file

@ -110,13 +110,15 @@ impl Database {
.await
}
/// Returns all of the active billing subscriptions for the user with the specified ID.
pub async fn get_active_billing_subscriptions(
&self,
user_id: UserId,
) -> Result<Vec<billing_subscription::Model>> {
/// Returns whether the user has an active billing subscription.
pub async fn has_active_billing_subscription(&self, user_id: UserId) -> Result<bool> {
Ok(self.count_active_billing_subscriptions(user_id).await? > 0)
}
/// Returns the count of the active billing subscriptions for the user with the specified ID.
pub async fn count_active_billing_subscriptions(&self, user_id: UserId) -> Result<usize> {
self.transaction(|tx| async move {
let subscriptions = billing_subscription::Entity::find()
let count = billing_subscription::Entity::find()
.inner_join(billing_customer::Entity)
.filter(
billing_customer::Column::UserId.eq(user_id).and(
@ -124,11 +126,10 @@ impl Database {
.eq(StripeSubscriptionStatus::Active),
),
)
.order_by_asc(billing_subscription::Column::Id)
.all(&*tx)
.count(&*tx)
.await?;
Ok(subscriptions)
Ok(count as usize)
})
.await
}

View file

@ -17,9 +17,12 @@ async fn test_get_active_billing_subscriptions(db: &Arc<Database>) {
// A user with no subscription has no active billing subscriptions.
{
let user_id = new_test_user(db, "no-subscription-user@example.com").await;
let subscriptions = db.get_active_billing_subscriptions(user_id).await.unwrap();
let subscription_count = db
.count_active_billing_subscriptions(user_id)
.await
.unwrap();
assert_eq!(subscriptions.len(), 0);
assert_eq!(subscription_count, 0);
}
// A user with an active subscription has one active billing subscription.
@ -42,7 +45,7 @@ async fn test_get_active_billing_subscriptions(db: &Arc<Database>) {
.await
.unwrap();
let subscriptions = db.get_active_billing_subscriptions(user_id).await.unwrap();
let subscriptions = db.get_billing_subscriptions(user_id).await.unwrap();
assert_eq!(subscriptions.len(), 1);
let subscription = &subscriptions[0];
@ -76,7 +79,10 @@ async fn test_get_active_billing_subscriptions(db: &Arc<Database>) {
.await
.unwrap();
let subscriptions = db.get_active_billing_subscriptions(user_id).await.unwrap();
assert_eq!(subscriptions.len(), 0);
let subscription_count = db
.count_active_billing_subscriptions(user_id)
.await
.unwrap();
assert_eq!(subscription_count, 0);
}
}