Commit 75815fdb authored by les's avatar les
Browse files

upgrade sequelize

parent a5683973
const { announcement: Announcement } = require('../models')
const Announcement = require('../models/announcement')
const debug = require('debug')('announcement:controller')
const announceController = {
......
const { ap_user: APUser } = require('../models')
const APUser = require('../models/ap_user')
const apUserController = {
async toggleBlock (req, res) {
......
......@@ -9,14 +9,13 @@ const helpers = require('../../helpers')
const linkifyHtml = require('linkifyjs/html')
const Sequelize = require('sequelize')
const {
event: Event,
resource: Resource,
tag: Tag,
place: Place,
notification: Notification,
ap_user: APUser
} = require('../models')
const Event = require('../models/event')
const Resource = require('../models/resource')
const Tag = require('../models/tag')
const Place = require('../models/place')
const Notification = require('../models/notification')
const APUser = require('../models/ap_user')
const exportController = require('./export')
const debug = require('debug')('controller:event')
......@@ -301,15 +300,14 @@ const eventController = {
await event.setUser(req.user)
}
// return created event to the client
res.json(event)
// create recurrent instances of event if needed
// without waiting for the task manager
if (event.recurrent) {
eventController._createRecurrent()
}
// return created event to the client
res.json(event)
// send notifications (mastodon / email)
const notifier = require('../../notifier')
notifier.notifyEvent('Create', event.id)
......
const { event: Event, place: Place, tag: Tag } = require('../models')
const Event = require('../models/event')
const Place = require('../models/place')
const Tag = require('../models/tag')
const { Op } = require('sequelize')
const moment = require('moment-timezone')
const ics = require('ics')
......
const {
ap_user: APUser,
instance: Instance,
resource: Resource
// event: Event
} = require('../models')
const APUser = require('../models/ap_user')
const Instance = require('../models/instance')
const Resource = require('../models/resource')
// const { Op } = require('sequelize')
const instancesController = {
......
const crypto = require('crypto')
const { promisify } = require('util')
const randomBytes = promisify(crypto.randomBytes)
const {
oauth_client: OAuthClient, oauth_token: OAuthToken,
oauth_code: OAuthCode, user: User
} = require('../models')
const OAuthClient = require('../models/oauth_client')
const OAuthToken = require('../models/oauth_token')
const OAuthCode = require('../models/oauth_code')
const User = require('../models/user')
const debug = require('debug')('oauth')
const moment = require('moment')
......
const { resource: Resource } = require('../models')
const Resource = require('../models/resource')
const resourceController = {
async hide (req, res) {
......
const { setting: Setting } = require('../models')
const Setting = require('../models/setting')
const config = require('config')
const consola = require('consola')
const path = require('path')
......
......@@ -2,7 +2,7 @@ const crypto = require('crypto')
const { Op } = require('sequelize')
const config = require('config')
const mail = require('../mail')
const { user: User } = require('../models')
const User = require('../models/user')
const settingsController = require('./settings')
const debug = require('debug')('user:controller')
const linkify = require('linkifyjs')
......
module.exports = (sequelize, DataTypes) => {
const announcement = sequelize.define('announcement', {
title: DataTypes.STRING,
announcement: DataTypes.STRING,
visible: DataTypes.BOOLEAN
}, {})
const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize')
return announcement
}
class Announcement extends Model {}
Announcement.init({
title: DataTypes.STRING,
announcement: DataTypes.STRING,
visible: DataTypes.BOOLEAN
}, { sequelize, modelName: 'announcement' })
module.exports = Announcement
module.exports = (sequelize, DataTypes) => {
const APUser = sequelize.define('ap_user', {
ap_id: {
type: DataTypes.STRING,
primaryKey: true
},
follower: DataTypes.BOOLEAN,
blocked: DataTypes.BOOLEAN,
object: DataTypes.JSON
})
const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize')
APUser.associate = function (models) {
APUser.belongsTo(models.instance)
APUser.hasMany(models.resource)
}
class APUser extends Model {}
return APUser
}
APUser.init({
ap_id: {
type: DataTypes.STRING,
primaryKey: true
},
follower: DataTypes.BOOLEAN,
blocked: DataTypes.BOOLEAN,
object: DataTypes.JSON
}, { sequelize, modelName: 'ap_user' })
module.exports = APUser
......@@ -2,92 +2,105 @@ const config = require('config')
const moment = require('moment-timezone')
const htmlToText = require('html-to-text')
module.exports = (sequelize, DataTypes) => {
const Event = sequelize.define('event', {
id: {
allowNull: false,
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: DataTypes.STRING,
slug: DataTypes.STRING,
description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN,
start_datetime: {
type: DataTypes.INTEGER,
index: true
},
end_datetime: {
type: DataTypes.INTEGER,
index: true
},
image_path: DataTypes.STRING,
is_visible: DataTypes.BOOLEAN,
recurrent: DataTypes.JSON,
likes: { type: DataTypes.JSON, defaultValue: [] },
boost: { type: DataTypes.JSON, defaultValue: [] }
}, {})
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.resource)
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(models.event, { as: 'parent' })
}
const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index')
const Resource = require('./resource')
const Notification = require('./notification')
const Place = require('./place')
const User = require('./user')
const Tag = require('./tag')
class Event extends Model {}
Event.init({
id: {
allowNull: false,
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: DataTypes.STRING,
slug: DataTypes.STRING,
description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN,
start_datetime: {
type: DataTypes.INTEGER,
index: true
},
end_datetime: {
type: DataTypes.INTEGER,
index: true
},
image_path: DataTypes.STRING,
is_visible: DataTypes.BOOLEAN,
recurrent: DataTypes.JSON,
likes: { type: DataTypes.JSON, defaultValue: [] },
boost: { type: DataTypes.JSON, defaultValue: [] }
}, { sequelize, modelName: 'event' })
Event.belongsTo(Place)
Place.hasMany(Event)
Event.belongsTo(User)
Event.belongsToMany(Tag, { through: 'event_tags' })
Event.belongsToMany(Notification, { through: 'event_notification' })
Notification.belongsToMany(Event, { through: 'event_notification' })
Event.prototype.toAP = function (username, locale, follower = []) {
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
const plainDescription = htmlToText.fromString(this.description.replace('\n', '').slice(0, 1000))
const summary = `
📍 ${this.place.name}
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
${plainDescription}
${tags.map(t => `#${t}`)}
`
const attachment = []
if (this.image_path) {
attachment.push({
type: 'Document',
mediaType: 'image/webp',
url: `${config.baseurl}/media/${this.image_path}`,
name: null,
blurHash: null
})
}
return {
id: `${config.baseurl}/federation/m/${this.id}`,
name: this.title,
url: `${config.baseurl}/event/${this.id}`,
type: 'Event',
startTime: moment.unix(this.start_datetime).locale(locale).format(),
endTime: moment.unix(this.end_datetime).locale(locale).format(),
location: {
name: this.place.name
},
attachment,
tag: tags.map(tag => ({
type: 'Hashtag',
name: '#' + tag,
href: '/tags/' + tag
})),
published: this.createdAt,
attributedTo: `${config.baseurl}/federation/u/${username}`,
to: follower || [],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
summary,
sensitive: false
}
Event.hasMany(Resource)
Resource.belongsTo(Event)
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(Event, { as: 'parent' })
Event.prototype.toAP = function (username, locale, follower = []) {
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
const plainDescription = htmlToText.fromString(this.description.replace('\n', '').slice(0, 1000))
const summary = `
📍 ${this.place.name}
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
${plainDescription}
${tags.map(t => `#${t}`)}
`
const attachment = []
if (this.image_path) {
attachment.push({
type: 'Document',
mediaType: 'image/webp',
url: `${config.baseurl}/media/${this.image_path}`,
name: null,
blurHash: null
})
}
return Event
return {
id: `${config.baseurl}/federation/m/${this.id}`,
name: this.title,
url: `${config.baseurl}/event/${this.id}`,
type: 'Event',
startTime: moment.unix(this.start_datetime).locale(locale).format(),
endTime: moment.unix(this.end_datetime).locale(locale).format(),
location: {
name: this.place.name
},
attachment,
tag: tags.map(tag => ({
type: 'Hashtag',
name: '#' + tag,
href: '/tags/' + tag
})),
published: this.createdAt,
attributedTo: `${config.baseurl}/federation/u/${username}`,
to: follower || [],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
summary,
sensitive: false
}
}
module.exports = Event
'use strict'
module.exports = (sequelize, DataTypes) => {
const event_notification = sequelize.define('event_notification', {
status: {
type: DataTypes.ENUM,
values: ['new', 'sent', 'error'],
defaultValue: 'new',
index: true
}
}, {})
const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize')
return event_notification
}
class EventNotification extends Model {}
EventNotification.init({
status: {
type: DataTypes.ENUM,
values: ['new', 'sent', 'error'],
defaultValue: 'new',
index: true
}
}, { sequelize, modelName: 'event_notification' })
module.exports = EventNotification
const fs = require('fs')
const path = require('path')
// const fs = require('fs')
// const path = require('path')
const Sequelize = require('sequelize')
const basename = path.basename(__filename)
// const basename = path.basename(__filename)
const config = require('config')
const consola = require('consola')
const db = {}
// const db = {}
let sequelize = null
try {
......@@ -19,23 +19,23 @@ sequelize.authenticate().catch(e => {
process.exit(-1)
})
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
})
.forEach(file => {
const model = sequelize.import(path.join(__dirname, file))
db[model.name] = model
})
// fs
// .readdirSync(__dirname)
// .filter(file => {
// return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
// })
// .forEach(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)
}
})
// Object.keys(db).forEach(modelName => {
// if (db[modelName].associate) {
// db[modelName].associate(db)
// }
// })
db.sequelize = sequelize
db.Sequelize = Sequelize
// db.sequelize = sequelize
// db.Sequelize = Sequelize
module.exports = db
module.exports = sequelize
'use strict'
module.exports = (sequelize, DataTypes) => {
const Instance = sequelize.define('instance', {
domain: {
primaryKey: true,
allowNull: false,
type: DataTypes.STRING
},
name: DataTypes.STRING,
blocked: DataTypes.BOOLEAN,
data: DataTypes.JSON
}, {})
Instance.associate = function (models) {
Instance.hasMany(models.ap_user)
}
const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize')
const APUser = require('./ap_user')
return Instance
}
class Instance extends Model {}
Instance.init({
domain: {
primaryKey: true,
allowNull: false,
type: DataTypes.STRING
},
name: DataTypes.STRING,
blocked: DataTypes.BOOLEAN,
data: DataTypes.JSON
}, { sequelize, modelName: 'instance' })
Instance.hasMany(APUser)
APUser.belongsTo(Instance)
module.exports = Instance
module.exports = (sequelize, DataTypes) => {
const Notification = sequelize.define('notification', {
filters: DataTypes.JSON,
email: DataTypes.STRING,
remove_code: DataTypes.STRING,
action: {
type: DataTypes.ENUM,
values: ['Create', 'Update', 'Delete']
},
type: {
type: DataTypes.ENUM,
values: ['mail', 'admin_email', 'ap']
}
}, {
indexes: [{
unique: true,
fields: ['action', 'type']
}]
})
const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize')
// const Event = require('./event')
Notification.associate = function (models) {
Notification.belongsToMany(models.event, { through: 'event_notification' })
class Notification extends Model {}
Notification.init({
filters: DataTypes.JSON,
email: DataTypes.STRING,
remove_code: DataTypes.STRING,
action: {
type: DataTypes.ENUM,
values: ['Create', 'Update', 'Delete']
},
type: {
type: DataTypes.ENUM,
values: ['mail', 'admin_email', 'ap']
}
return Notification
}
},
{
sequelize,
modelName: 'notification',
indexes: [{
unique: true,
fields: ['action', 'type']
}]
})
// Notification.belongsToMany(Event, { through: 'event_notification' })
module.exports = Notification
module.exports = (sequelize, DataTypes) => {
const OAuthClient = sequelize.define('oauth_client', {
id: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
},
name: DataTypes.STRING,
client_secret: DataTypes.STRING,
scopes: DataTypes.STRING,
redirectUris: DataTypes.STRING,
website: DataTypes.STRING
}, {})
const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize')
return OAuthClient
}
class OAuthClient extends Model {}
OAuthClient.init({
id: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
},
name: DataTypes.STRING,
client_secret: DataTypes.STRING,
scopes: DataTypes.STRING,
redirectUris: DataTypes.STRING,
website: DataTypes.STRING
}, { sequelize, modelName: 'oauth_client' })
module.exports = OAuthClient
module.exports = (sequelize, DataTypes) => {
const OAuthCode = sequelize.define('oauth_code', {
authorizationCode: {
type: DataTypes.STRING,