-
Notifications
You must be signed in to change notification settings - Fork 14
feat(publish-quota): per user publishArticle quota #3862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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') | ||
| }) | ||
| } |
| 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: 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| import type { GQLMutationResolvers } from 'definitions' | ||
|
|
||
| import { PUBLISH_STATE, USER_STATE } from 'common/enums' | ||
| import { | ||
| PUBLISH_ARTICLE_RATE_LIMIT, | ||
| PUBLISH_ARTICLE_RATE_PERIOD, | ||
| PUBLISH_STATE, | ||
| USER_STATE, | ||
| } from 'common/enums' | ||
| import { | ||
| ActionLimitExceededError, | ||
| DraftNotFoundError, | ||
| ForbiddenByStateError, | ||
| ForbiddenError, | ||
| UserInputError, | ||
| } from 'common/errors' | ||
| import { fromGlobalId } from 'common/utils' | ||
| import { checkOperationLimit, fromGlobalId } from 'common/utils' | ||
|
|
||
| const resolver: GQLMutationResolvers['publishArticle'] = async ( | ||
| _, | ||
|
|
@@ -18,6 +24,7 @@ const resolver: GQLMutationResolvers['publishArticle'] = async ( | |
| draftService, | ||
| atomService, | ||
| queues: { publicationQueue }, | ||
| connections: { redis }, | ||
| }, | ||
| } | ||
| ) => { | ||
|
|
@@ -50,6 +57,21 @@ const resolver: GQLMutationResolvers['publishArticle'] = async ( | |
| throw new UserInputError('content is required') | ||
| } | ||
|
|
||
| const fieldName = 'publishArticle' | ||
| const pass = await checkOperationLimit({ | ||
| user: viewer.id || viewer.ip, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. viewer.ip will never be used as only logged-in user can publish articles by now?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And as opercation limit is checked here, rateLimitDirective (@ratelimit) on |
||
| operation: fieldName, | ||
| limit: viewer?.publishRate?.limit ?? PUBLISH_ARTICLE_RATE_LIMIT, | ||
| period: viewer?.publishRate?.period ?? PUBLISH_ARTICLE_RATE_PERIOD, | ||
| redis, // : connections.redis, | ||
| }) | ||
|
|
||
| if (!pass) { | ||
| throw new ActionLimitExceededError( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add unit test for this mutation changes?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added in |
||
| `rate exceeded for operation ${fieldName}` | ||
| ) | ||
| } | ||
|
|
||
| if ( | ||
| draft.publishState === PUBLISH_STATE.pending || | ||
| (draft.archived && isPublished) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import type { GQLMutationResolvers } from 'definitions' | ||
|
|
||
| import _isEmpty from 'lodash/isEmpty' | ||
|
|
||
| import { UserInputError } from 'common/errors' | ||
| import { fromGlobalId } from 'common/utils' | ||
|
|
||
| const resolver: GQLMutationResolvers['updateUserPublishRate'] = async ( | ||
| _, | ||
| { input: { id, limit, period } }, | ||
| { dataSources: { atomService } } | ||
| ) => { | ||
| const { id: dbId } = fromGlobalId(id) | ||
|
|
||
| // validate data ranges? | ||
| const publishRate = { | ||
| limit, | ||
| period, | ||
| } | ||
|
|
||
| if (_isEmpty(publishRate)) { | ||
| throw new UserInputError('bad request') | ||
| } | ||
|
|
||
| const user = await atomService.update({ | ||
| table: 'user', | ||
| where: { id: dbId }, | ||
| data: { publishRate }, | ||
| }) | ||
|
|
||
| return user | ||
| } | ||
|
|
||
| export default resolver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this rate confirmed with the CC team?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was the default before:
"hotfix(publish-rate): reduce rate of publishArticle"
This reverts commit 2717c88.