check if there are more than five items "created" within a minute instead of "saved"

This commit is contained in:
Hongbo Wu 2024-07-24 15:59:22 +08:00
parent 31fe4b65a0
commit 6776cdf1cd
2 changed files with 10 additions and 5 deletions

View file

@ -8,11 +8,10 @@ import {
PageType,
} from '../generated/graphql'
import { createPubSubClient, PubsubClient } from '../pubsub'
import { Merge } from '../util'
import { enqueueParseRequest } from '../utils/createTask'
import { cleanUrl, generateSlug } from '../utils/helpers'
import { logger } from '../utils/logger'
import { countBySavedAt, createOrUpdateLibraryItem } from './library_item'
import { countByCreatedAt, createOrUpdateLibraryItem } from './library_item'
interface PageSaveRequest {
user: User
@ -39,7 +38,13 @@ const isPrivateIP = privateIpLib.default
const getPriorityByRateLimit = async (
userId: string
): Promise<'low' | 'high'> => {
const count = await countBySavedAt(userId, new Date(Date.now() - 60 * 1000))
const now = new Date()
const count = await countByCreatedAt(
userId,
new Date(now.getTime() - 60 * 1000), // 1 minute ago
now
)
return count >= 5 ? 'low' : 'high'
}

View file

@ -1192,7 +1192,7 @@ export const findLibraryItemsByPrefix = async (
)
}
export const countBySavedAt = async (
export const countByCreatedAt = async (
userId: string,
startDate = new Date(0),
endDate = new Date()
@ -1202,7 +1202,7 @@ export const countBySavedAt = async (
tx
.createQueryBuilder(LibraryItem, 'library_item')
.where('library_item.user_id = :userId', { userId })
.andWhere('library_item.saved_at between :startDate and :endDate', {
.andWhere('library_item.created_at between :startDate and :endDate', {
startDate,
endDate,
})