text-dataset-tiny-code-script-py-format / ysnrfd_messenger /BACKUP /backend /src /models /Conversation.js
| const mongoose = require('mongoose'); | |
| const conversationSchema = new mongoose.Schema({ | |
| type: { | |
| type: String, | |
| enum: ['direct', 'group', 'channel'], | |
| required: true | |
| }, | |
| name: { | |
| type: String, | |
| required: function() { return this.type !== 'direct'; } | |
| }, | |
| description: { | |
| type: String, | |
| maxlength: 500 | |
| }, | |
| avatar: { | |
| type: String | |
| }, | |
| createdBy: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| required: true | |
| }, | |
| participants: [{ | |
| user: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| required: true | |
| }, | |
| role: { | |
| type: String, | |
| enum: ['member', 'admin', 'owner'], | |
| default: 'member' | |
| }, | |
| joinedAt: { | |
| type: Date, | |
| default: Date.now | |
| }, | |
| lastRead: { | |
| type: Date, | |
| default: Date.now | |
| } | |
| }], | |
| settings: { | |
| isPublic: { | |
| type: Boolean, | |
| default: false | |
| }, | |
| allowInvites: { | |
| type: Boolean, | |
| default: true | |
| }, | |
| adminOnlyMessages: { | |
| type: Boolean, | |
| default: false | |
| } | |
| }, | |
| inviteLinks: [{ | |
| code: String, | |
| createdBy: mongoose.Schema.Types.ObjectId, | |
| expiresAt: Date, | |
| maxUses: Number, | |
| uses: { type: Number, default: 0 }, | |
| isActive: { type: Boolean, default: true } | |
| }] | |
| }, { | |
| timestamps: true | |
| }); | |
| conversationSchema.index({ 'participants.user': 1 }); | |
| conversationSchema.index({ type: 1, updatedAt: -1 }); | |
| module.exports = mongoose.model('Conversation', conversationSchema); |