models initialization refactored, better dev experience as backend hmr is working

This commit is contained in:
lesion
2022-12-23 01:08:14 +01:00
parent 380eaa87ca
commit cbed0288fe
43 changed files with 624 additions and 707 deletions

View File

@@ -1,4 +1,5 @@
const Announcement = require('../models/announcement')
const { Announcement } = require('../models/models')
const log = require('../../log')
const announceController = {

View File

@@ -1,4 +1,4 @@
const APUser = require('../models/ap_user')
const { APUser } = require('../models/models')
const apUserController = {
async toggleBlock (req, res) {

View File

@@ -1,8 +1,5 @@
const Collection = require('../models/collection')
const Filter = require('../models/filter')
const Event = require('../models/event')
const Tag = require('../models/tag')
const Place = require('../models/place')
const { Collection, Filter, Event, Tag, Place } = require('../models/models')
const log = require('../../log')
const dayjs = require('dayjs')
const { col: Col } = require('../../helpers')
@@ -114,7 +111,7 @@ const collectionController = {
res.json(collection)
} catch (e) {
log.error(`Create collection failed ${e}`)
res.sendStatus(400)
res.status(400).send(e)
}
},
@@ -138,15 +135,14 @@ const collectionController = {
},
async addFilter (req, res) {
const collectionId = req.body.collectionId
const tags = req.body.tags
const places = req.body.places
const { collectionId, tags, places } = req.body
try {
const filter = await Filter.create({ collectionId, tags, places })
filter = await Filter.create({ collectionId, tags, places })
return res.json(filter)
} catch (e) {
log.error(String(e))
return res.status(500)
return res.sendStatus(400)
}
},
@@ -170,6 +166,4 @@ const collectionController = {
}
module.exports = collectionController

View File

@@ -9,12 +9,10 @@ const Sequelize = require('sequelize')
const dayjs = require('dayjs')
const helpers = require('../../helpers')
const Col = helpers.col
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 notifier = require('../../notifier')
const { Event, Resource, Tag, Place, Notification, APUser } = require('../models/models')
const exportController = require('./export')
const tagController = require('./tag')
@@ -155,34 +153,6 @@ const eventController = {
},
async getNotifications(event, action) {
log.debug(`getNotifications ${event.title} ${action}`)
function match(event, filters) {
// matches if no filter specified
if (!filters) { return true }
// check for visibility
if (typeof filters.is_visible !== 'undefined' && filters.is_visible !== event.is_visible) { return false }
if (!filters.tags && !filters.places) { return true }
if (!filters.tags.length && !filters.places.length) { return true }
if (filters.tags.length) {
const m = intersection(event.tags.map(t => t.tag), filters.tags)
if (m.length > 0) { return true }
}
if (filters.places.length) {
if (filters.places.find(p => p === event.place.name)) {
return true
}
}
}
const notifications = await Notification.findAll({ where: { action }, include: [Event] })
// get notification that matches with selected event
return notifications.filter(notification => match(event, notification.filters))
},
async _get(slug) {
// retrocompatibility, old events URL does not use slug, use id as fallback
const id = Number(slug) || -1
@@ -317,7 +287,6 @@ const eventController = {
res.sendStatus(200)
// send notification
const notifier = require('../../notifier')
notifier.notifyEvent('Create', event.id)
} catch (e) {
log.error('[EVENT]', e)

View File

@@ -1,6 +1,4 @@
const Event = require('../models/event')
const Place = require('../models/place')
const Tag = require('../models/tag')
const { Event, Place, Tag } = require('../models/models')
const { htmlToText } = require('html-to-text')
const { Op, literal } = require('sequelize')

View File

@@ -1,6 +1,5 @@
const APUser = require('../models/ap_user')
const Instance = require('../models/instance')
const Resource = require('../models/resource')
const { APUser, Instance, Resource } = require('../models/models')
const Sequelize = require('sequelize')
const instancesController = {

View File

@@ -1,4 +1,4 @@
const User = require('../models/user')
const User = require('../models/modles')
const metrics = {

View File

@@ -2,12 +2,9 @@ const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('cookie-session')
const OAuthClient = require('../models/oauth_client')
const OAuthToken = require('../models/oauth_token')
const OAuthCode = require('../models/oauth_code')
const { OAuthClient, OAuthToken, OAuthCode, User } = require('../models/models')
const helpers = require('../../helpers.js')
const User = require('../models/user')
const passport = require('passport')
const get = require('lodash/get')

View File

@@ -1,5 +1,5 @@
const Place = require('../models/place')
const Event = require('../models/event')
const { Place, Event } = require('../models/models')
const eventController = require('./event')
const exportController = require('./export')

View File

@@ -2,11 +2,12 @@ const path = require('path')
const fs = require('fs')
const log = require('../../log')
const config = require('../../config')
const settingsController = require('./settings')
const notifier = require('../../notifier')
const pluginController = {
plugins: [],
getAll(_req, res) {
const settingsController = require('./settings')
// return plugins and inner settings
const plugins = pluginController.plugins.map( ({ configuration }) => {
if (settingsController.settings['plugin_' + configuration.name]) {
@@ -18,7 +19,6 @@ const pluginController = {
},
togglePlugin(req, res) {
const settingsController = require('./settings')
const pluginName = req.params.plugin
const pluginSettings = settingsController.settings['plugin_' + pluginName]
if (!pluginSettings) { return res.sendStatus(404) }
@@ -33,7 +33,6 @@ const pluginController = {
},
unloadPlugin(pluginName) {
const settingsController = require('./settings')
const plugin = pluginController.plugins.find(p => p.configuration.name === pluginName)
const settings = settingsController.settings['plugin_' + pluginName]
if (!plugin) {
@@ -59,14 +58,12 @@ const pluginController = {
},
loadPlugin(pluginName) {
const settingsController = require('./settings')
const plugin = pluginController.plugins.find(p => p.configuration.name === pluginName)
const settings = settingsController.settings['plugin_' + pluginName]
if (!plugin) {
log.warn(`Plugin ${pluginName} not found`)
return
}
const notifier = require('../../notifier')
log.info('Load plugin ' + pluginName)
if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.on('Create', plugin.onEventCreate)
@@ -88,7 +85,6 @@ const pluginController = {
},
_load() {
const settingsController = require('./settings')
// load custom plugins
const plugins_path = config.plugins_path || path.resolve(process.env.cwd || '', 'plugins')
log.info(`Loading plugin ${plugins_path}`)

View File

@@ -1,6 +1,4 @@
const Resource = require('../models/resource')
const APUser = require('../models/ap_user')
const Event = require('../models/event')
const { Resource, APUser, Event } = require('../models/models')
const get = require('lodash/get')
const resourceController = {

View File

@@ -1,6 +1,5 @@
const path = require('path')
const URL = require('url')
const fs = require('fs')
const crypto = require('crypto')
const { promisify } = require('util')
const sharp = require('sharp')
@@ -9,7 +8,7 @@ const generateKeyPair = promisify(crypto.generateKeyPair)
const log = require('../../log')
// const locales = require('../../../locales/index')
const escape = require('lodash/escape')
const pluginController = require('./plugins')
const DB = require('../models/models')
let defaultHostname
try {
@@ -30,7 +29,7 @@ const defaultSettings = {
allow_multidate_event: true,
allow_recurrent_event: false,
recurrent_event_visible: false,
allow_geolocation: true,
allow_geolocation: false,
geocoding_provider_type: 'Nominatim',
geocoding_provider: 'https://nominatim.openstreetmap.org/search',
geocoding_countrycodes: [],
@@ -74,8 +73,7 @@ const settingsController = {
// initialize instance settings from db
// note that this is done only once when the server starts
// and not for each request
const Setting = require('../models/setting')
const settings = await Setting.findAll()
const settings = await DB.Setting.findAll()
settingsController.settings = defaultSettings
settings.forEach(s => {
if (s.is_secret) {
@@ -117,15 +115,14 @@ const settingsController = {
// }
// })
// }
const pluginController = require('./plugins')
pluginController._load()
},
async set (key, value, is_secret = false) {
const Setting = require('../models/setting')
log.info(`SET ${key} ${is_secret ? '*****' : value}`)
try {
const [setting, created] = await Setting.findOrCreate({
const [setting, created] = await DB.Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
})

View File

@@ -7,6 +7,8 @@ const settingsController = require('./settings')
const path = require('path')
const escape = require('lodash/escape')
const DB = require('../models/models')
const setupController = {
async _setupDb (dbConf) {
@@ -23,7 +25,10 @@ const setupController = {
// try to connect
dbConf.logging = false
await db.connect(dbConf)
db.connect(dbConf)
db.loadModels()
db.associates()
await db.sequelize.authenticate()
// is empty ?
const isEmpty = await db.isEmpty()
@@ -69,8 +74,7 @@ const setupController = {
// create admin
const password = helpers.randomString()
const email = `admin`
const User = require('../models/user')
await User.create({
await DB.User.create({
email,
password,
is_admin: true,

View File

@@ -1,5 +1,4 @@
const Tag = require('../models/tag')
const Event = require('../models/event')
const { Tag, Event } = require('../models/models')
const uniq = require('lodash/uniq')
const log = require('../../log')
@@ -82,29 +81,35 @@ module.exports = {
return res.json(tags.map(t => t.tag))
},
async updateTag (req, res) {
const tag = await Tag.findByPk(req.body.tag)
await tag.update(req.body)
res.json(place)
},
// async updateTag (req, res) {
// const tag = await Tag.findByPk(req.body.tag)
// await tag.update(req.body)
// res.json(place)
// },
async updateTag (req, res) {
const oldtag = await Tag.findByPk(req.body.tag)
const newtag = await Tag.findByPk(req.body.newTag)
try {
const oldtag = await Tag.findByPk(req.body.tag)
const newtag = await Tag.findByPk(req.body.newTag)
// if the new tag does not exists, just rename the old one
if (!newtag) {
oldtag.tag = req.body.newTag
await oldtag.update({ tag: req.body.newTag })
} else {
// in case it exists:
// - search for events with old tag
const events = await oldtag.getEvents()
// - substitute it with the new one
await oldtag.removeEvents(events)
await newtag.addEvents(events)
// if the new tag does not exists, just rename the old one
if (!newtag) {
log.info(`Rename tag ${oldtag.tag} to ${req.body.newTag}`)
await Tag.update({ tag: req.body.newTag }, { where: { tag: req.body.tag }, raw: true })
} else {
// in case it exists:
// - search for events with old tag
const events = await oldtag.getEvents()
// - substitute it with the new one
await oldtag.removeEvents(events)
await newtag.addEvents(events)
}
res.sendStatus(200)
} catch (e) {
console.error(e)
res.sendStatus(400)
}
res.sendStatus(200)
},
async remove (req, res) {

View File

@@ -2,7 +2,7 @@ const crypto = require('crypto')
const { Op } = require('sequelize')
const config = require('../../config')
const mail = require('../mail')
const User = require('../models/user')
const { User } = require('../models/models')
const settingsController = require('./settings')
const log = require('../../log')
const linkify = require('linkifyjs')