-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat: notice #6013
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
Merged
Merged
feat: notice #6013
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af9d14d
feat: record user's language
FinleyGe 7d3d660
feat: notice points/dataset indexes; support count limit; update dock…
FinleyGe 248f72c
fix: ts error
FinleyGe ee57dc1
feat: send auth code i18n
FinleyGe fc96cbc
chore: dataset notice limit
FinleyGe 444fc05
chore: adjust
FinleyGe b9e575c
fix: ts
c121914yu a50ae7d
fix: countLimit race condition; i18n en-prefix locale fallback to en
FinleyGe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| import z from 'zod'; | ||
| import { CountLimitTypeEnum } from './type'; | ||
|
|
||
| export const CountLimitConfigType = z.record( | ||
| CountLimitTypeEnum, | ||
| z.object({ maxCount: z.number() }) | ||
| ); | ||
|
|
||
| export const CountLimitConfig = { | ||
| [CountLimitTypeEnum.enum['notice:30PercentPoints']]: { | ||
| maxCount: 3 | ||
| }, | ||
| [CountLimitTypeEnum.enum['notice:10PercentPoints']]: { | ||
| maxCount: 5 | ||
| }, | ||
| [CountLimitTypeEnum.enum['notice:LackOfPoints']]: { | ||
| maxCount: 5 | ||
| }, | ||
| [CountLimitTypeEnum.enum['notice:30PercentDatasetIndexes']]: { | ||
| maxCount: 3 | ||
| }, | ||
| [CountLimitTypeEnum.enum['notice:10PercentDatasetIndexes']]: { | ||
| maxCount: 5 | ||
| }, | ||
| [CountLimitTypeEnum.enum['notice:NoDatasetIndexes']]: { | ||
| maxCount: 5 | ||
| } | ||
| } satisfies z.infer<typeof CountLimitConfigType>; |
This file contains hidden or 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,78 @@ | ||
| import type z from 'zod'; | ||
| import type { CountLimitTypeEnum } from './type'; | ||
| import { CountLimitConfig } from './const'; | ||
| import { MongoCountLimit } from './schema'; | ||
| import { mongoSessionRun } from 'common/mongo/sessionRun'; | ||
|
|
||
| /** | ||
| * Update the count limit for a specific type and key. | ||
| * @param param0 - The type, key, and update value. | ||
| * @returns The updated count limit information. | ||
| */ | ||
| export const updateCountLimit = async ({ | ||
| type, | ||
| key, | ||
| update | ||
| }: { | ||
| type: z.infer<typeof CountLimitTypeEnum>; | ||
| key: string; | ||
| update: number; | ||
| }) => | ||
| mongoSessionRun(async (session) => { | ||
| const maxCount = CountLimitConfig[type].maxCount; | ||
| const countLimit = await MongoCountLimit.findOne( | ||
| { | ||
| type, | ||
| key | ||
| }, | ||
| undefined, | ||
| { | ||
| session | ||
| } | ||
| ).lean(); | ||
| if (!countLimit) { | ||
| // do not exist, create a new one | ||
| await MongoCountLimit.create( | ||
| { | ||
| type, | ||
| key, | ||
| count: update // 0 + update | ||
| }, | ||
| { | ||
| session | ||
| } | ||
| ); | ||
| return { | ||
| maxCount, | ||
| nowCount: update, | ||
| remain: maxCount - update | ||
| }; | ||
| } | ||
| if (countLimit && countLimit.count >= maxCount) { | ||
| return Promise.reject(`Max Count Reached, type: ${type}, key: ${key}`); | ||
| } | ||
|
|
||
| await MongoCountLimit.updateOne( | ||
| { | ||
| type, | ||
| key | ||
| }, | ||
| { | ||
| $inc: { count: update } | ||
| }, | ||
| { session } | ||
| ); | ||
|
|
||
| return { | ||
| maxCount, | ||
| nowCount: countLimit.count + update, | ||
| remain: maxCount - (countLimit.count + update) | ||
| }; | ||
| }); | ||
|
|
||
| /** Clean the Count limit, if no key provided, clean all the type */ | ||
| export const cleanCountLimit = async ({ type, key }: { type: CountLimitTypeEnum; key?: string }) => | ||
| MongoCountLimit.deleteMany({ | ||
| type, | ||
| ...(key ? { key } : {}) | ||
| }); | ||
This file contains hidden or 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,29 @@ | ||
| import { connectionMongo, getMongoModel } from '../../../common/mongo'; | ||
| import type { CountLimitType } from './type'; | ||
|
|
||
| const { Schema } = connectionMongo; | ||
|
|
||
| const collectionName = 'count_limits'; | ||
| const CountLimitSchema = new Schema({ | ||
| key: { | ||
| type: String, | ||
| required: true | ||
| }, | ||
| type: { | ||
| type: String, | ||
| required: true | ||
| }, | ||
| count: { | ||
| type: Number, | ||
| required: true, | ||
| default: 0 | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| CountLimitSchema.index({ type: 1, key: 1 }, { unique: true }); | ||
FinleyGe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
|
|
||
| export const MongoCountLimit = getMongoModel<CountLimitType>(collectionName, CountLimitSchema); | ||
This file contains hidden or 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,19 @@ | ||
| import z from 'zod'; | ||
|
|
||
| export const CountLimitTypeEnum = z.enum([ | ||
| 'notice:30PercentPoints', | ||
| 'notice:10PercentPoints', | ||
| 'notice:LackOfPoints', | ||
| 'notice:30PercentDatasetIndexes', | ||
| 'notice:10PercentDatasetIndexes', | ||
| 'notice:NoDatasetIndexes' | ||
| ]); | ||
|
|
||
| export const CountLimitType = z.object({ | ||
| type: CountLimitTypeEnum, | ||
| key: z.string(), | ||
| count: z.number() | ||
| }); | ||
|
|
||
| export type CountLimitType = z.infer<typeof CountLimitType>; | ||
| export type CountLimitTypeEnum = z.infer<typeof CountLimitTypeEnum>; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,58 @@ | ||
| import { type I18nKeyFunction } from './i18next'; | ||
| import { LangEnum } from '@fastgpt/global/common/i18n/type'; | ||
| import Cookies from 'js-cookie'; | ||
|
|
||
| const LANG_KEY = 'NEXT_LOCALE'; | ||
|
|
||
| const isInIframe = () => { | ||
| try { | ||
| return window.self !== window.top; | ||
| } catch (e) { | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| export const setLangToStorage = (value: string) => { | ||
| if (isInIframe()) { | ||
| localStorage.setItem(LANG_KEY, value); | ||
| } else { | ||
| // 不在 iframe 中,同时使用 Cookie 和 localStorage | ||
| Cookies.set(LANG_KEY, value, { expires: 30 }); | ||
| localStorage.setItem(LANG_KEY, value); | ||
| } | ||
| }; | ||
|
|
||
| export const getLangFromStorage = () => { | ||
| return localStorage.getItem(LANG_KEY) || Cookies.get(LANG_KEY); | ||
| }; | ||
|
|
||
| export const getLangMapping = (lng: string): string => { | ||
| const languageMap: Record<string, string> = { | ||
| zh: LangEnum.zh_CN, | ||
| 'zh-CN': LangEnum.zh_CN, | ||
| 'zh-Hans': LangEnum.zh_CN, | ||
| 'zh-HK': LangEnum.zh_Hant, | ||
| 'zh-TW': LangEnum.zh_Hant, | ||
| 'zh-Hant': LangEnum.zh_Hant, | ||
| en: LangEnum.en, | ||
| 'en-US': LangEnum.en | ||
| }; | ||
|
|
||
| let lang = languageMap[lng]; | ||
|
|
||
| // 如果没有直接映射,尝试智能回退 | ||
| if (!lang) { | ||
| const langPrefix = lng.split('-')[0]; | ||
| // 中文相关语言优先回退到简体中文 | ||
| if (langPrefix === 'zh') { | ||
| lang = LangEnum.zh_CN; | ||
| } | ||
| if (langPrefix === 'en') { | ||
| lang = LangEnum.en; | ||
| } | ||
| } | ||
FinleyGe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return lang || LangEnum.zh_CN; | ||
| }; | ||
|
|
||
| export const i18nT: I18nKeyFunction = (key) => key; | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.