mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
v4.5.2 (#439)
This commit is contained in:
45
packages/service/support/user/inform/controller.ts
Normal file
45
packages/service/support/user/inform/controller.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { MongoUserInform } from './schema';
|
||||
import { MongoUser } from '../schema';
|
||||
import { InformTypeEnum } from '@fastgpt/global/support/user/constant';
|
||||
|
||||
export type SendInformProps = {
|
||||
type: `${InformTypeEnum}`;
|
||||
title: string;
|
||||
content: string;
|
||||
};
|
||||
|
||||
export async function sendInform2AllUser({ type, title, content }: SendInformProps) {
|
||||
const users = await MongoUser.find({}, '_id');
|
||||
await MongoUserInform.insertMany(
|
||||
users.map(({ _id }) => ({
|
||||
type,
|
||||
title,
|
||||
content,
|
||||
userId: _id
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
export async function sendInform2OneUser({
|
||||
type,
|
||||
title,
|
||||
content,
|
||||
userId
|
||||
}: SendInformProps & { userId: string }) {
|
||||
const inform = await MongoUserInform.findOne({
|
||||
type,
|
||||
title,
|
||||
content,
|
||||
userId,
|
||||
time: { $gte: new Date(Date.now() - 5 * 60 * 1000) }
|
||||
});
|
||||
|
||||
if (inform) return;
|
||||
|
||||
await MongoUserInform.create({
|
||||
type,
|
||||
title,
|
||||
content,
|
||||
userId
|
||||
});
|
||||
}
|
42
packages/service/support/user/inform/schema.ts
Normal file
42
packages/service/support/user/inform/schema.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { connectionMongo, type Model } from '../../../common/mongo';
|
||||
const { Schema, model, models } = connectionMongo;
|
||||
import type { UserInformSchema } from '@fastgpt/global/support/user/type.d';
|
||||
import { InformTypeMap } from '@fastgpt/global/support/user/constant';
|
||||
|
||||
const InformSchema = new Schema({
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
required: true
|
||||
},
|
||||
time: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: Object.keys(InformTypeMap)
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
content: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
read: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
InformSchema.index({ time: -1 });
|
||||
InformSchema.index({ userId: 1 });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
export const MongoUserInform: Model<UserInformSchema> =
|
||||
models['inform'] || model('inform', InformSchema);
|
Reference in New Issue
Block a user