-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish-quota): per user publishArticle quota
resolves #3842
- Loading branch information
49659410+tx0c
committed
Apr 16, 2024
1 parent
6f15387
commit 7beda71
Showing
6 changed files
with
108 additions
and
66 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
db/migrations/20240417164251_alter_user_add_publish_rate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const table = 'user' | ||
|
||
exports.up = async (knex) => { | ||
await knex.schema.table(table, (t) => { | ||
t.jsonb('publish_rate') | ||
}) | ||
} | ||
|
||
exports.down = async (knex) => { | ||
await knex.schema.table(table, (t) => { | ||
t.dropColumn('publish_rate') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// import type { Redis } from 'ioredis' | ||
|
||
import { CACHE_PREFIX } from 'common/enums' | ||
import { genCacheKey } from 'connectors' | ||
|
||
export const checkOperationLimit = async ({ | ||
user, | ||
operation, | ||
limit, | ||
period, | ||
redis, | ||
}: { | ||
user: string | ||
operation: string | ||
limit: number | ||
period: number | ||
redis: any // Redis | ||
}) => { | ||
const cacheKey = genCacheKey({ | ||
id: user, | ||
field: operation, | ||
prefix: CACHE_PREFIX.OPERATION_LOG, | ||
}) | ||
|
||
const operationLog = await redis.lrange(cacheKey, 0, -1) | ||
|
||
// timestamp in seconds | ||
const current = Math.floor(Date.now() / 1000) | ||
|
||
// no record | ||
if (!operationLog) { | ||
// create | ||
redis.lpush(cacheKey, current).then(() => { | ||
redis.expire(cacheKey, period) | ||
}) | ||
|
||
// pass | ||
return true | ||
} | ||
|
||
// count times within period | ||
const cutoff = current - period | ||
let times = 0 | ||
for (const timestamp of operationLog) { | ||
if (parseInt(timestamp, 10) >= cutoff) { | ||
times += 1 | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
// over limit | ||
if (times >= limit) { | ||
return false | ||
} | ||
|
||
// add, trim, update expiration | ||
redis.lpush(cacheKey, current) | ||
redis.ltrim(cacheKey, 0, times) | ||
redis.expire(cacheKey, period) | ||
|
||
// pass | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters