245 lines
3.2 KiB
JavaScript
245 lines
3.2 KiB
JavaScript
const config = require('../../config');
|
|
const providers = config.providers;
|
|
const crypto = require('crypto');
|
|
const bcrypt = require('bcrypt');
|
|
const moment = require('moment');
|
|
|
|
module.exports = function(sequelize, DataTypes) {
|
|
const posts = sequelize.define(
|
|
'posts',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
post_type: {
|
|
type: DataTypes.ENUM,
|
|
|
|
|
|
|
|
values: [
|
|
|
|
"text",
|
|
|
|
|
|
"image",
|
|
|
|
|
|
"video",
|
|
|
|
|
|
"link"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
caption: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
visibility_level: {
|
|
type: DataTypes.ENUM,
|
|
|
|
|
|
|
|
values: [
|
|
|
|
"public",
|
|
|
|
|
|
"friends_only",
|
|
|
|
|
|
"private"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
moderation_status: {
|
|
type: DataTypes.ENUM,
|
|
|
|
|
|
|
|
values: [
|
|
|
|
"pending",
|
|
|
|
|
|
"approved",
|
|
|
|
|
|
"rejected",
|
|
|
|
|
|
"removed"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
moderated_at: {
|
|
type: DataTypes.DATE,
|
|
|
|
|
|
|
|
},
|
|
|
|
moderation_note: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
comments_enabled: {
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
|
|
|
|
|
|
},
|
|
|
|
is_pinned: {
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
|
|
|
|
|
|
},
|
|
|
|
published_at: {
|
|
type: DataTypes.DATE,
|
|
|
|
|
|
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
},
|
|
);
|
|
|
|
posts.associate = (db) => {
|
|
|
|
|
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.posts.hasMany(db.comments, {
|
|
as: 'comments_post',
|
|
foreignKey: {
|
|
name: 'postId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
db.posts.hasMany(db.reactions, {
|
|
as: 'reactions_post',
|
|
foreignKey: {
|
|
name: 'postId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
db.posts.hasMany(db.moderation_reports, {
|
|
as: 'moderation_reports_reported_post',
|
|
foreignKey: {
|
|
name: 'reported_postId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//end loop
|
|
|
|
|
|
|
|
db.posts.belongsTo(db.users, {
|
|
as: 'author_user',
|
|
foreignKey: {
|
|
name: 'author_userId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.posts.belongsTo(db.users, {
|
|
as: 'moderated_by_user',
|
|
foreignKey: {
|
|
name: 'moderated_by_userId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
db.posts.hasMany(db.file, {
|
|
as: 'media_images',
|
|
foreignKey: 'belongsToId',
|
|
constraints: false,
|
|
scope: {
|
|
belongsTo: db.posts.getTableName(),
|
|
belongsToColumn: 'media_images',
|
|
},
|
|
});
|
|
|
|
db.posts.hasMany(db.file, {
|
|
as: 'media_files',
|
|
foreignKey: 'belongsToId',
|
|
constraints: false,
|
|
scope: {
|
|
belongsTo: db.posts.getTableName(),
|
|
belongsToColumn: 'media_files',
|
|
},
|
|
});
|
|
|
|
|
|
db.posts.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.posts.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
|
|
|
|
return posts;
|
|
};
|
|
|
|
|