Colubrina/backend/models/messages.js

94 lines
2 KiB
JavaScript
Raw Normal View History

2022-06-05 22:58:18 +10:00
"use strict"
const { Model } = require("sequelize")
module.exports = (sequelize, DataTypes) => {
class Message extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
// eslint-disable-next-line no-unused-vars
static associate(models) {
Message.belongsTo(models.User, {
as: "user"
})
Message.belongsTo(models.Chat, {
as: "chat"
})
Message.belongsTo(models.Message, {
as: "reply"
})
Message.hasMany(models.Attachment, {
as: "attachments",
foreignKey: "messageId"
})
2022-07-31 18:42:36 +10:00
Message.hasMany(models.ChatAssociation, {
as: "readReceipts",
foreignKey: "lastRead"
})
2022-08-14 22:06:56 +10:00
Message.hasOne(models.Poll, {
as: "poll",
foreignKey: "messageId"
})
2022-06-05 22:58:18 +10:00
}
}
Message.init(
{
chatId: {
type: DataTypes.BIGINT,
allowNull: false
},
userId: {
type: DataTypes.BIGINT,
allowNull: false
},
content: {
type: DataTypes.TEXT,
allowNull: false
},
type: {
type: DataTypes.ENUM([
"message",
"leave",
"join",
"pin",
"administrator",
"rename",
"system"
])
},
embeds: {
type: DataTypes.JSON,
defaultValue: [],
allowNull: false
},
edited: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false
},
editedAt: {
type: DataTypes.DATE,
allowNull: true
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
replyId: {
type: DataTypes.BIGINT,
allowNull: true
}
},
{
sequelize,
modelName: "Message"
}
)
return Message
}