first commit backend

This commit is contained in:
lesion
2019-02-26 00:02:42 +01:00
commit 887157f2a9
27 changed files with 819 additions and 0 deletions

52
app/models/event.js Normal file
View File

@@ -0,0 +1,52 @@
const db = require('../db')
const Sequelize = require('sequelize')
const User = require('./user')
const Event = db.define('event', {
title: Sequelize.STRING,
description: Sequelize.STRING,
multidate: Sequelize.BOOLEAN,
start_datetime: { type: Sequelize.DATE, index: true},
end_datetime: {type: Sequelize.DATE, index: true},
image_path: Sequelize.STRING,
activitypub_id: { type: Sequelize.INTEGER, index: true },
})
const Tag = db.define('tag', {
tag: { type: Sequelize.STRING, index: true, unique: true, primaryKey: true},
color: { type: Sequelize.STRING }
})
const Comment = db.define('comment', {
activitypub_id: { type: Sequelize.INTEGER, index: true },
author: Sequelize.STRING,
text: Sequelize.STRING,
})
const MailSubscription = db.define('subscription' , {
filters: Sequelize.JSON,
mail: Sequelize.TEXT,
send_on_add: Sequelize.BOOLEAN,
send_reminder: Sequelize.INTEGER,
})
const Place = db.define('place', {
name: { type: Sequelize.STRING, unique: true, index: true },
address: { type: Sequelize.STRING }
})
Comment.belongsTo(Event)
Event.hasMany(Comment)
Event.belongsToMany(Tag, {through: 'tagEvent'})
Tag.belongsToMany(Event, {through: 'tagEvent'})
Event.belongsToMany(User, {through: 'boost'})
Event.belongsTo(User)
Event.belongsTo(Place)
User.hasMany(Event)
Place.hasMany(Event)
User.belongsToMany(User, {through: 'userFollower', as: 'follower'})
module.exports = { Event, Comment, Tag, Place }

33
app/models/user.js Normal file
View File

@@ -0,0 +1,33 @@
const bcrypt = require('bcrypt')
const db = require('../db')
const Sequelize = require('sequelize')
const User = db.define('user', {
email: {
type: Sequelize.STRING,
unique: {msg: 'Email already exists'},
index: true, allowNull: false },
description: Sequelize.TEXT,
password: Sequelize.STRING,
is_admin: Sequelize.BOOLEAN,
is_active: Sequelize.BOOLEAN,
instance: Sequelize.STRING,
mastodon_auth: Sequelize.JSON
})
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