This commit is contained in:
lesion
2019-06-06 23:54:32 +02:00
parent 745b9247c9
commit 3ca818f016
66 changed files with 989 additions and 532 deletions

View File

@@ -0,0 +1,13 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const comment = sequelize.define('comment', {
activitypub_id: DataTypes.BIGINT,
data: DataTypes.JSON
}, {});
comment.associate = function(models) {
comment.belongsTo(models.event)
// Event.hasMany(Comment)
// associations can be defined here
};
return comment;
};

View File

@@ -1,81 +1,31 @@
const Sequelize = require('sequelize')
const db = require('../db')
const User = require('./user')
const Event = db.define('event', {
title: Sequelize.STRING,
description: Sequelize.TEXT,
multidate: Sequelize.BOOLEAN,
start_datetime: { type: Sequelize.DATE, index: true },
end_datetime: { type: Sequelize.DATE, index: true },
image_path: Sequelize.STRING,
is_visible: Sequelize.BOOLEAN,
activitypub_id: { type: Sequelize.BIGINT, index: true },
activitypub_ids: {
type: Sequelize.ARRAY(Sequelize.BIGINT),
index: true,
defaultValue: []
}
})
const Tag = db.define('tag', {
tag: { type: Sequelize.STRING, index: true, unique: true, primaryKey: true },
weigth: { type: Sequelize.INTEGER, defaultValue: 0 },
color: { type: Sequelize.STRING }
})
const Comment = db.define('comment', {
activitypub_id: { type: Sequelize.BIGINT, index: true },
data: Sequelize.JSON,
// url: Sequelize.STRING,
author: Sequelize.STRING,
text: Sequelize.STRING
})
const Notification = db.define('notification', {
filters: Sequelize.JSON,
email: Sequelize.STRING,
remove_code: Sequelize.STRING,
type: {
type: Sequelize.ENUM,
values: ['mail', 'admin_email', 'mastodon']
}
})
const Place = db.define('place', {
name: { type: Sequelize.STRING, unique: true, index: true },
weigth: { type: Sequelize.INTEGER, defaultValue: 0 },
address: { type: Sequelize.STRING }
})
Comment.belongsTo(Event)
Event.hasMany(Comment)
Event.belongsToMany(Tag, { through: 'tagEvent' })
Tag.belongsToMany(Event, { through: 'tagEvent' })
const EventNotification = db.define('EventNotification', {
status: {
type: Sequelize.ENUM,
values: ['new', 'sent', 'error'],
defaultValue: 'new',
index: true
}
})
Event.belongsToMany(Notification, { through: EventNotification })
Notification.belongsToMany(Event, { through: EventNotification })
Event.belongsTo(User)
Event.belongsTo(Place)
User.hasMany(Event)
Place.hasMany(Event)
async function init() {
await Notification.findOrCreate({ where: { type: 'mastodon', filters: { is_visible: true } } })
// await Notification.findOrCreate({ where: { type: 'admin_email', filters: { is_visible: false } } })
}
init()
module.exports = { Event, Comment, Tag, Place, Notification, EventNotification }
'use strict';
module.exports = (sequelize, DataTypes) => {
const event = sequelize.define('event', {
title: DataTypes.STRING,
slug: DataTypes.STRING,
description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN,
start_datetime: {
type: DataTypes.DATE,
index: true
},
end_datetime: DataTypes.DATE,
image_path: DataTypes.STRING,
is_visible: DataTypes.BOOLEAN,
activitypub_id: {
type: DataTypes.BIGINT,
index: true
}
}, {});
event.associate = function(models) {
event.belongsTo(models.place)
event.belongsTo(models.user)
event.belongsToMany(models.tag, { through: 'event_tags' })
event.belongsToMany(models.notification, { through: 'event_notification' })
event.hasMany(models.comment)
// Tag.belongsToMany(Event, { through: 'tagEvent' })
// Event.hasMany(models.Tag)
// associations can be defined here
};
return event;
};

View File

@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const eventNotification = sequelize.define('eventNotification', {
status: {
type: DataTypes.ENUM,
values: ['new', 'sent', 'error'],
defaultValue: 'new',
index: true
}
}, {});
eventNotification.associate = function(models) {
// associations can be defined here
};
return eventNotification;
};

View File

@@ -1,29 +1,31 @@
const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const basename = path.basename(__filename)
const config = require('../../../config')
const db = {}
'use strict';
const sequelize = new Sequelize(config.db)
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = require(__dirname + '/../../config.js').SECRET_CONF.db
const db = {};
let sequelize = new Sequelize(config);
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file))
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db)
db[modelName].associate(db);
}
})
});
db.sequelize = sequelize
db.Sequelize = Sequelize
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

View File

@@ -0,0 +1,17 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const notification = sequelize.define('notification', {
filters: DataTypes.JSON,
email: DataTypes.STRING,
remove_code: DataTypes.STRING,
type: {
type: DataTypes.ENUM,
values: ['mail', 'admin_email', 'mastodon']
}
}, {});
notification.associate = function(models) {
notification.belongsToMany(models.event, { through: 'event_notification' })
// associations can be defined here
};
return notification;
};

View File

@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const place = sequelize.define('place', {
name: DataTypes.STRING,
address: DataTypes.STRING,
weigth: DataTypes.INTEGER
}, {});
place.associate = function(models) {
// associations can be defined here
place.hasMany(models.event)
};
return place;
};

View File

@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const setting = sequelize.define('setting', {
key: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
index: true,
},
value: DataTypes.JSON
}, {});
return setting;
};

View File

@@ -1,9 +0,0 @@
const db = require('../db')
const Sequelize = require('sequelize')
const Settings = db.define('settings', {
key: { type: Sequelize.STRING, primaryKey: true, index: true },
value: Sequelize.JSON
})
module.exports = Settings

19
server/api/models/tag.js Normal file
View File

@@ -0,0 +1,19 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const tag = sequelize.define('tag', {
tag: {
type: DataTypes.STRING,
index: true,
primaryKey: true
},
weigth: DataTypes.INTEGER,
color: DataTypes.STRING
}, {});
tag.associate = function(models) {
tag.belongsToMany(models.event, { through: 'event_tags' })
// associations can be defined here
};
return tag;
};

View File

@@ -1,34 +1,39 @@
const Sequelize = require('sequelize')
'use strict';
const bcrypt = require('bcrypt')
const db = require('../db')
const User = db.define('user', {
email: {
type: Sequelize.STRING,
unique: { msg: 'err.register_error' },
index: true,
allowNull: false
},
description: Sequelize.TEXT,
password: Sequelize.STRING,
recover_code: Sequelize.STRING,
is_admin: Sequelize.BOOLEAN,
is_active: Sequelize.BOOLEAN,
mastodon_auth: Sequelize.JSON
})
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define('user', {
email: {
type: DataTypes.STRING,
unique: { msg: 'err.register_error' },
index: true,
allowNull: false
},
description: DataTypes.TEXT,
password: DataTypes.STRING,
recover_code: DataTypes.STRING,
is_admin: DataTypes.BOOLEAN,
is_active: DataTypes.BOOLEAN
}, {});
User.prototype.comparePassword = async function (pwd) {
if (!this.password) return false
const ret = await bcrypt.compare(pwd, this.password)
return ret
}
user.associate = function(models) {
// associations can be defined here
user.hasMany(models.event)
};
User.beforeSave(async (user, options) => {
if (user.changed('password')) {
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(user.password, salt)
user.password = hash
user.prototype.comparePassword = async function (pwd) {
if (!this.password) return false
const ret = await bcrypt.compare(pwd, this.password)
return ret
}
})
user.beforeSave(async (user, options) => {
if (user.changed('password')) {
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(user.password, salt)
user.password = hash
}
})
module.exports = User
return user;
};