better config / install from cli / allow_registration

This commit is contained in:
lesion 2019-06-21 23:52:18 +02:00
parent 4c3c7ee324
commit cf81a73f2f
38 changed files with 530 additions and 272 deletions

View file

@ -1,7 +1,7 @@
const path = require('path') const path = require('path')
module.exports = { module.exports = {
'config': path.resolve('config.js'), 'config': path.resolve('server', 'dbconfig.js'),
'migrations-path': path.resolve('server', 'migrations'), 'migrations-path': path.resolve('server', 'migrations'),
'models-path': path.resolve('server', 'api', 'models') 'models-path': path.resolve('server', 'api', 'models')
} }

View file

@ -2,7 +2,7 @@
#calendar #calendar
v-calendar( v-calendar(
title-position='left' title-position='left'
locale='it' :locale='$i18n.locale'
is-dark is-dark
:attributes='attributes' :attributes='attributes'
:from-page.sync='page' :from-page.sync='page'
@ -41,25 +41,19 @@ export default {
computed: { computed: {
...mapGetters(['filteredEvents']), ...mapGetters(['filteredEvents']),
attributes () { attributes () {
return [ let attributes = []
{ key: 'today', dates: new Date(), attributes.push ({ key: 'today', dates: new Date(), highlight: { color: 'yellow' }})
highlight: { color: 'red' },
}, attributes = attributes.concat(this.filteredEvents
{
key: 'event',
dates: this.filteredEvents
.filter(e => !e.multidate) .filter(e => !e.multidate)
.map(e => e.start_datetime ), .map(e => ({ key: e.id, dot: {}, dates: new Date(e.start_datetime*1000)})))
dot: { }
}, attributes = attributes.concat(this.filteredEvents
{
key: 'multidays',
dates: this.filteredEvents
.filter(e => e.multidate) .filter(e => e.multidate)
.map( e => ({ start: e.start_datetime, end: e.end_datetime })), .map( e => ({ key: e.id, highlight: {}, dates: {
highlight: { color: 'green' } start: new Date(e.start_datetime*1000), end: new Date(e.end_datetime*1000) }})))
}
] return attributes
} }
} }
} }

View file

@ -21,7 +21,7 @@
</template> </template>
<script> <script>
import { mapGetters } from 'vuex' import { mapGetters, mapState } from 'vuex'
import Event from '@/components/Event' import Event from '@/components/Event'
import Calendar from '@/components/Calendar' import Calendar from '@/components/Calendar'
@ -31,7 +31,10 @@ export default {
return { } return { }
}, },
components: { Calendar, Event }, components: { Calendar, Event },
computed: mapGetters(['filteredEvents']), computed: {
...mapGetters(['filteredEvents']),
...mapState(['events', 'settings'])
}
} }
</script> </script>
<style lang="less"> <style lang="less">

19
config/default.json Normal file
View file

@ -0,0 +1,19 @@
{
"title": "Gancio",
"description": "A shared agenda for local communities",
"baseurl": "http://localhost:3000",
"server": {
"host": "localhost",
"port": 3000
},
"db": {
"dialect": "sqlite",
"storage": "./db.sqlite"
},
"upload_path": "./",
"admin": {
"email": "les",
"password": "les"
},
"secret": "za34yz70mmwlervesgxor"
}

12
config/production.js Normal file
View file

@ -0,0 +1,12 @@
const argv = require('yargs').argv
const path = require('path')
const fs = require('fs')
const config_path = path.resolve(argv.config || '/etc/gancio_config.json')
let config = {}
if (fs.existsSync(config_path)) {
config = require(config_path)
}
module.exports = config

View file

@ -1,3 +1,3 @@
{ module.exports = {
"registration_email": "registration_email" "registration_email": "registration_email"
} }

View file

@ -44,7 +44,9 @@ const it = {
new_password: 'Nuova password', new_password: 'Nuova password',
new_user: 'Nuovo utente', new_user: 'Nuovo utente',
ok: 'Ok', ok: 'Ok',
cancel: 'Annulla' cancel: 'Annulla',
enable: 'Abilita',
disable: 'Disabilita'
}, },
login: { login: {
@ -120,7 +122,9 @@ const it = {
delete_user: 'Elimina', delete_user: 'Elimina',
remove_admin: 'Rimuovi admin', remove_admin: 'Rimuovi admin',
delete_user_confirm: 'Sicura di rimuovere questo utente?', delete_user_confirm: 'Sicura di rimuovere questo utente?',
user_remove_ok: 'Utente eliminato' user_remove_ok: 'Utente eliminato',
user_create_ok: 'Utente creato',
allow_registration_description : 'Puoi decidere se abilitare la registrazione',
}, },
auth: { auth: {
@ -130,7 +134,10 @@ const it = {
settings: { settings: {
change_password: 'Cambia password', change_password: 'Cambia password',
password_updated: 'Password modificata' password_updated: 'Password modificata',
danger_section: 'Sezione pericolosa',
remove_account: 'Premendo il seguente tasto il tuo utente verrà eliminato. Gli eventi da te pubblicati invece no.',
remove_account_confirm: 'Stai per eliminare definitivamente il tuo account',
}, },
err: { err: {
@ -138,12 +145,6 @@ const it = {
} }
// firstrun: {
// basic: `Inserisci titolo e descrizione della tua istanza di gancio.`,
// database: `Gancio ha bisogno di un database postgresql!`,
// smtp: `Inserisci un account SMTP relativo a questa istanza di gancio.`
// },
} }
export default it module.exports = it

View file

@ -1,20 +1,12 @@
const argv = require('yargs').argv
const path = require('path')
const config_path = path.resolve(argv.config || './config.js')
const config = require(config_path)
module.exports = { module.exports = {
mode: 'universal', mode: 'universal',
/* /*
** Headers of the page ** Headers of the page
*/ */
head: { head: {
title: config.title,
meta: [ meta: [
{ charset: 'utf-8' }, { charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: config.description }
], ],
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }] link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
}, },
@ -35,20 +27,15 @@ module.exports = {
'bootstrap/dist/css/bootstrap.css', 'bootstrap/dist/css/bootstrap.css',
'element-ui/lib/theme-chalk/index.css' 'element-ui/lib/theme-chalk/index.css'
], ],
env: {
baseurl: config.baseurl,
title: config.title,
description: config.description,
locale: config.locale
},
/* /*
** Plugins to load before mounting the App ** Plugins to load before mounting the App
*/ */
plugins: [ plugins: [
'@/plugins/element-ui', // UI library -> https://element.eleme.io/#/en-US/ '@/plugins/element-ui', // UI library -> https://element.eleme.io/#/en-US/
'@/plugins/filters', // text filters, datetime, etc. '@/plugins/filters', // text filters, datetime, etc.
'@/plugins/i18n', // localization plugin
'@/plugins/vue-awesome', // icon '@/plugins/vue-awesome', // icon
'@/plugins/axios', // icon
{ src: '@/plugins/v-calendar', ssr: false } // calendar, TO-REDO { src: '@/plugins/v-calendar', ssr: false } // calendar, TO-REDO
], ],
@ -58,17 +45,28 @@ module.exports = {
modules: [ modules: [
// Doc: https://axios.nuxtjs.org/usage // Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios', '@nuxtjs/axios',
'@nuxtjs/auth' '@nuxtjs/auth',
['nuxt-i18n', {
locales: [
{ code: 'en', iso: 'en-US', file: './locales/en.js' },
{ code: 'it', iso: 'it-IT', file: './locales/it.js' },
],
defaultLocale: 'it',
vueI18n: {
fallbackLocale: 'it',
messages: {
it: require('./locales/it'),
en: require('./locales/en'),
}
}
}]
], ],
/* /*
** Axios module configuration ** Axios module configuration
* See https://github.com/nuxt-community/axios-module#options
*/ */
axios: { axios: {
baseURL: config.baseurl + '/api',
browserBaseURL: config.baseurl + '/api',
prefix: '/api' prefix: '/api'
// credentials: true
// See https://github.com/nuxt-community/axios-module#options
}, },
auth: { auth: {
strategies: { strategies: {
@ -102,6 +100,5 @@ module.exports = {
layouts: true layouts: true
}, },
cache: true, cache: true,
// parallel: true
} }
} }

View file

@ -1,12 +1,12 @@
{ {
"name": "gancio", "name": "gancio",
"version": "0.9.4", "version": "0.9.4",
"description": "My well-made Nuxt.js project", "description": "A shared agenda for local communities",
"author": "lesion", "author": "lesion",
"scripts": { "scripts": {
"dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server", "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",
"build": "nuxt build", "build": "nuxt build",
"start": "cross-env NODE_ENV=production node server/index.js", "start": "cross-env NODE_ENV=production node server/cli.js",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .", "lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
"precommit": "npm run lint", "precommit": "npm run lint",
"migrate:dev": "sequelize db:migrate", "migrate:dev": "sequelize db:migrate",
@ -22,7 +22,7 @@
"url": "https://git.lattuga.net/cisti/gancio.git" "url": "https://git.lattuga.net/cisti/gancio.git"
}, },
"bin": { "bin": {
"gancio": "server/index.js" "gancio": "server/cli.js"
}, },
"files": [ "files": [
"server/*", "server/*",
@ -32,10 +32,12 @@
"dependencies": { "dependencies": {
"@nuxtjs/auth": "^4.6.5", "@nuxtjs/auth": "^4.6.5",
"@nuxtjs/axios": "^5.5.3", "@nuxtjs/axios": "^5.5.3",
"arg": "^4.1.0",
"axios": "^0.19.0", "axios": "^0.19.0",
"bcrypt": "^3.0.5", "bcrypt": "^3.0.5",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"bootstrap": "4.3.1", "bootstrap": "4.3.1",
"config": "^3.1.0",
"cookie-parser": "^1.4.4", "cookie-parser": "^1.4.4",
"cors": "^2.8.5", "cors": "^2.8.5",
"cross-env": "^5.2.0", "cross-env": "^5.2.0",
@ -45,12 +47,15 @@
"express": "^4.17.1", "express": "^4.17.1",
"express-jwt": "^5.3.1", "express-jwt": "^5.3.1",
"ics": "^2.13.2", "ics": "^2.13.2",
"inquirer": "^6.3.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"less": "^3.9.0", "less": "^3.9.0",
"mastodon-api": "^1.3.0", "mastodon-api": "^1.3.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"multer": "^1.4.1", "multer": "^1.4.1",
"nuxt": "^2.8.1", "nuxt": "^2.8.1",
"nuxt-env": "^0.1.0",
"nuxt-i18n": "^5.12.4",
"pg": "^7.11.0", "pg": "^7.11.0",
"sequelize": "^5.8.7", "sequelize": "^5.8.7",
"sequelize-cli": "^5.4.0", "sequelize-cli": "^5.4.0",
@ -58,7 +63,6 @@
"sqlite3": "^4.0.8", "sqlite3": "^4.0.8",
"v-calendar": "^1.0.0-beta.14", "v-calendar": "^1.0.0-beta.14",
"vue-awesome": "^3.5.3", "vue-awesome": "^3.5.3",
"vue-i18n": "^8.10.0",
"yargs": "^13.2.4" "yargs": "^13.2.4"
}, },
"devDependencies": { "devDependencies": {

View file

@ -42,6 +42,7 @@
:mode='event.multidate ? "range" : "single"' :mode='event.multidate ? "range" : "single"'
:attributes='attributes' :attributes='attributes'
v-model='date' v-model='date'
:locale='$i18n.locale'
:from-page.sync='page' :from-page.sync='page'
is-inline is-inline
is-expanded is-expanded
@ -161,12 +162,12 @@ export default {
data.event.place.address = event.place.address || '' data.event.place.address = event.place.address || ''
data.event.multidate = event.multidate data.event.multidate = event.multidate
if (event.multidate) { if (event.multidate) {
data.date = { start: new Date(event.start_datetime), end: new Date(event.end_datetime) } data.date = { start: new Date(event.start_datetime*1000), end: new Date(event.end_datetime*1000) }
} else { } else {
data.date = new Date(event.start_datetime) data.date = new Date(event.start_datetime*1000)
} }
data.time.start = moment(event.start_datetime).format('HH:mm') data.time.start = moment(event.start_datetime*1000).format('HH:mm')
data.time.end = moment(event.end_datetime).format('HH:mm') data.time.end = moment(event.end_datetime*1000).format('HH:mm')
data.event.title = event.title data.event.title = event.title
data.event.description = event.description.replace(/(<([^>]+)>)/ig, '') data.event.description = event.description.replace(/(<([^>]+)>)/ig, '')
data.event.id = event.id data.event.id = event.id
@ -193,41 +194,33 @@ export default {
const date_end = moment(this.date.end) const date_end = moment(this.date.end)
return this.events.filter(e => return this.events.filter(e =>
!e.multidate ? !e.multidate ?
date_start.isSame(e.start_datetime, 'day') || date_start.isSame(e.start_datetime*1000, 'day') ||
date_start.isBefore(e.start_datime) && date_end.isAfter(e.start_datetime) : date_start.isBefore(e.start_datime*1000) && date_end.isAfter(e.start_datetime*1000) :
date_start.isSame(e.start_datetime, 'day') || date_start.isSame(e.end_datetime) || date_start.isSame(e.start_datetime*1000, 'day') || date_start.isSame(e.end_datetime*1000) ||
date_start.isAfter(e.start_datetime) && date_start.isBefore(e.end_datetime)) date_start.isAfter(e.start_datetime*1000) && date_start.isBefore(e.end_datetime*1000))
} else { } else {
const date = moment(this.date) const date = moment(this.date)
return this.events.filter(e => return this.events.filter(e =>
!e.multidate ? !e.multidate ?
date.isSame(moment(e.start_datetime), 'day') : date.isSame(moment(e.start_datetime*1000), 'day') :
moment(e.start_datetime).isSame(date, 'day') || moment(e.start_datetime*1000).isSame(date, 'day') ||
moment(e.start_datetime).isBefore(date) && moment(e.end_datetime).isAfter(date) moment(e.start_datetime*1000).isBefore(date) && moment(e.end_datetime*1000).isAfter(date)
) )
} }
}, },
...mapGetters(['filteredEvents']), ...mapGetters(['filteredEvents']),
attributes () { attributes () {
return [ let attributes = []
{ key: 'today', dates: new Date(), attributes.push ({ key: 'today', dates: new Date(), highlight: { color: 'yellow' }})
highlight: { color: 'red' },
}, attributes = attributes.concat(this.filteredEvents
{
key: 'event',
dates: this.filteredEvents
.filter(e => !e.multidate) .filter(e => !e.multidate)
.map(e => e.start_datetime ), .map(e => ({ key: e.id, dot: {}, dates: new Date(e.start_datetime*1000)})))
dot: { }
}, attributes = attributes.concat(this.filteredEvents
{
key: 'multidays',
dates: this.filteredEvents
.filter(e => e.multidate) .filter(e => e.multidate)
.map( e => ({ start: e.start_datetime, end: e.end_datetime })), .map( e => ({ key: e.id, highlight: {}, dates: {
highlight: { color: 'green' } start: new Date(e.start_datetime*1000), end: new Date(e.end_datetime*1000) }})))
}
]
}, },
disableAddress () { disableAddress () {
return this.places_name.find(p => p.name === this.event.place.name) return this.places_name.find(p => p.name === this.event.place.name)
@ -279,7 +272,7 @@ export default {
let start_datetime, end_datetime let start_datetime, end_datetime
const [ start_hour, start_minute ] = this.time.start.split(':') const [ start_hour, start_minute ] = this.time.start.split(':')
if (!this.time.end) { if (!this.time.end) {
this.time.end = this.time.start this.time.end = (Number(start_hour)+2) + ':' + start_minute
} }
const [ end_hour, end_minute ] = this.time.end.split(':') const [ end_hour, end_minute ] = this.time.end.split(':')
if (this.event.multidate) { if (this.event.multidate) {
@ -301,8 +294,8 @@ export default {
formData.append('place_address', this.event.place.address) formData.append('place_address', this.event.place.address)
formData.append('description', this.event.description) formData.append('description', this.event.description)
formData.append('multidate', this.event.multidate) formData.append('multidate', this.event.multidate)
formData.append('start_datetime', start_datetime) formData.append('start_datetime', start_datetime.unix())
formData.append('end_datetime', end_datetime) formData.append('end_datetime', end_datetime.unix())
if (this.edit) { if (this.edit) {
formData.append('id', this.event.id) formData.append('id', this.event.id)
} }

View file

@ -80,51 +80,27 @@
el-pagination(:page-size='perPage' :currentPage.sync='eventPage' :total='events.length') el-pagination(:page-size='perPage' :currentPage.sync='eventPage' :total='events.length')
//- TAGS
//- el-tab-pane.pt-1
//- template(slot='label')
//- v-icon(name='tags')
//- span {{$t('common.tags')}}
//- p {{$t('admin.tag_description')}}
//- el-tag(v-if='tag.tag' :color='tag.color' size='mini') {{tag.tag}}
//- el-form(:inline='true' label-width='120px')
//- el-form-item(:label="$t('common.color')")
//- el-color-picker(v-model='tag.color' @change='updateColor')
//- el-table(:data='paginatedTags' striped small hover
//- highlight-current-row @current-change="tagSelected")
//- el-table-column(:label="$t('common.tag')")
//- template(slot-scope='data')
//- el-tag(:color='data.row.color' size='mini') {{data.row.tag}}
//- el-pagination(:page-size='perPage' :currentPage.sync='tagPage' :total='tags.length')
//- SETTINGS //- SETTINGS
el-tab-pane.pt-1 el-tab-pane.pt-1
template(slot='label') template(slot='label')
v-icon(name='cog') v-icon(name='cog')
span {{$t('common.settings')}} span {{$t('common.settings')}}
//- el-form(inline @submit.prevent.stop='save_settings' label-width='140px')
//- p {{$t('settings.name_description')}}
//- el-form-item(:label="$t('settings.name')")
//- el-input(v-model="settings.title")
//- el-form-item(:label="$t('settings.description')")
//- el-input(v-model="settings.description")
//- el-button(slot='append' @click='associate' :disabled='!mastodon_instance.length') {{$t('common.associate')}}
el-form(inline @submit.native.prevent='associate_mastondon_instance' label-width='140px') el-form(inline @submit.native.prevent='associate_mastondon_instance' label-width='140px')
p {{$t('admin.mastodon_description')}} p {{$t('admin.mastodon_description')}}
el-form-item {{$t('admin.mastodon_instance')}} el-form-item(:label='$t("admin.mastodon_instance")')
el-input(v-model="settings.mastodon_instance") el-input(v-model="mastodon_instance")
el-button(slot='append' native-type='submit' type='success' :disabled='!settings.mastodon_instance') {{$t('common.associate')}} el-form-item
el-button(native-type='submit' type='success' :disabled='!mastodon_instance') {{$t('common.associate')}}
hr
p {{$t('admin.allow_registration_description')}} p {{$t('admin.allow_registration_description')}}
el-form-item {{$t('admin.allow_registration')}} el-form-item(:label="allow_registration?$t('common.disable'):$t('common.enable')")
el-switch(v-model='settings.allow_registration') el-switch(v-model='allow_registration')
</template> </template>
<script> <script>
import { mapState } from 'vuex' import { mapState, mapActions } from 'vuex'
import { Message, MessageBox } from 'element-ui' import { Message, MessageBox } from 'element-ui'
export default { export default {
@ -146,10 +122,6 @@ export default {
tag: {name: '', color: ''}, tag: {name: '', color: ''},
events: [], events: [],
loading: false, loading: false,
settings: {
allow_registration: true,
mastodon_instance: ''
},
new_user: { new_user: {
email: '', email: '',
password: '', password: '',
@ -169,17 +141,24 @@ export default {
}, },
async asyncData ({ $axios, params, store }) { async asyncData ({ $axios, params, store }) {
try { try {
console.error(store)
const users = await $axios.$get('/users') const users = await $axios.$get('/users')
const events = await $axios.$get('/event/unconfirmed') const events = await $axios.$get('/event/unconfirmed')
const settings = await $axios.$get('/settings') return { users, events, mastodon_instance: store.state.settings.mastodon_instance }
return { users, events, settings}
} catch ( e ) { } catch ( e ) {
console.error(e) console.error(e)
} }
}, },
computed: { computed: {
...mapState(['tags', 'places']), ...mapState(['tags', 'places', 'settings']),
allow_registration: {
get () {
return this.settings.allow_registration
},
set (value) {
this.setSetting({ key: 'allow_registration', value })
}
},
paginatedEvents () { paginatedEvents () {
return this.events.slice((this.eventPage-1) * this.perPage, return this.events.slice((this.eventPage-1) * this.perPage,
this.eventPage * this.perPage) this.eventPage * this.perPage)
@ -198,6 +177,7 @@ export default {
}, },
}, },
methods: { methods: {
...mapActions(['setSetting']),
placeSelected (items) { placeSelected (items) {
if (items.length === 0 ) { if (items.length === 0 ) {
this.place.name = this.place.address = '' this.place.name = this.place.address = ''
@ -217,24 +197,21 @@ export default {
async toggle(user) { async toggle(user) {
user.is_active = !user.is_active user.is_active = !user.is_active
this.$axios.$put('/user', user) this.$axios.$put('/user', user)
// const newuser = await api.updateUser(user)
}, },
async toggleAdmin(user) { async toggleAdmin(user) {
user.is_admin = !user.is_admin user.is_admin = !user.is_admin
this.$axios.$put('/user', user) this.$axios.$put('/user', user)
// const newuser = await api.updateUser(user)
}, },
preview (id) { preview (id) {
this.$router.push(`/event/${id}`) this.$router.push(`/event/${id}`)
}, },
async associate_mastondon_instance () { async associate_mastondon_instance () {
if (!this.settings.mastodon_instance) return false if (!this.mastodon_instance) return false
const url = await this.$axios.$post('/settings/getauthurl', {instance: this.settings.mastodon_instance}) const url = await this.$axios.$post('/settings/getauthurl', { instance: this.mastodon_instance })
setTimeout( () => window.location.href=url, 100); setTimeout( () => window.location.href=url, 100);
}, },
async delete_user (user) { async delete_user (user) {
console.error('dentro delete user', user)
MessageBox.confirm(this.$t('admin.delete_user_confirm'), MessageBox.confirm(this.$t('admin.delete_user_confirm'),
this.$t('common.confirm'), { this.$t('common.confirm'), {
confirmButtonText: this.$t('common.ok'), confirmButtonText: this.$t('common.ok'),
@ -257,7 +234,7 @@ export default {
this.new_user = { email: '', password: '', is_admin: false } this.new_user = { email: '', password: '', is_admin: false }
Message({ Message({
type: 'success', type: 'success',
message: this.$t('user.create_ok') message: this.$t('admin.user_create_ok')
}) })
} catch (e) { } catch (e) {
Message({ Message({

View file

@ -108,7 +108,7 @@ export default {
params.push(`tags=${this.filters.tags}`) params.push(`tags=${this.filters.tags}`)
} }
return `<iframe src="${process.env.baseurl}/embed/list?${params.join('&')}"></iframe>` return `<iframe src="${this.$axios.defaults.baseURL}/embed/list?${params.join('&')}"></iframe>`
}, },
link () { link () {
const tags = this.filters.tags.join(',') const tags = this.filters.tags.join(',')
@ -124,7 +124,7 @@ export default {
} }
} }
return `${process.env.baseurl}/api/export/${this.type}${query}` return `${this.$axios.defaults.baseURL}/api/export/${this.type}${query}`
}, },
showLink () { showLink () {
return (['feed', 'ics'].indexOf(this.type)>-1) return (['feed', 'ics'].indexOf(this.type)>-1)

View file

@ -5,7 +5,6 @@
nuxt-link.float-right(to='/') nuxt-link.float-right(to='/')
v-icon(name='times' color='red') v-icon(name='times' color='red')
h5 {{$t('common.login')}} h5 {{$t('common.login')}}
el-form(v-loading='loading' method='POST' action='/api/auth/login') el-form(v-loading='loading' method='POST' action='/api/auth/login')
p(v-html="$t('login.description')") p(v-html="$t('login.description')")
@ -20,14 +19,14 @@
el-button.mr-1(plain type="success" native-type='submit' el-button.mr-1(plain type="success" native-type='submit'
:disabled='disabled' @click='submit') {{$t('common.login')}} :disabled='disabled' @click='submit') {{$t('common.login')}}
nuxt-link(to='/register') nuxt-link(to='/register' v-if='settings.allow_registration')
el-button.mt-1(plain type="primary") {{$t('login.not_registered')}} el-button.mt-1(plain type="primary") {{$t('login.not_registered')}}
a.float-right(href='#' @click='forgot') {{$t('login.forgot_password')}} a.float-right(href='#' @click='forgot') {{$t('login.forgot_password')}}
</template> </template>
<script> <script>
import { mapActions } from 'vuex' import { mapActions, mapState } from 'vuex'
import { Message } from 'element-ui' import { Message } from 'element-ui'
import get from 'lodash/get' import get from 'lodash/get'
@ -41,6 +40,7 @@ export default {
} }
}, },
computed: { computed: {
...mapState(['settings']),
disabled () { disabled () {
if (process.server) return false if (process.server) return false
return !this.email || !this.password return !this.email || !this.password

View file

@ -22,7 +22,7 @@
</template> </template>
<script> <script>
import { mapActions } from 'vuex' import { mapActions, mapState } from 'vuex'
import { Message } from 'element-ui' import { Message } from 'element-ui'
import get from 'lodash/get' import get from 'lodash/get'
@ -34,7 +34,11 @@ export default {
user: { } user: { }
} }
}, },
validate ({store}) {
return store.state.settings.allow_registration
},
computed: { computed: {
...mapState(['settings']),
disabled () { disabled () {
if (process.server) return false if (process.server) return false
return !this.user.password || !this.user.email || !this.user.description return !this.user.password || !this.user.email || !this.user.description

View file

@ -5,14 +5,18 @@
v-icon(name='times' color='red') v-icon(name='times' color='red')
h5 {{$t('common.settings')}} h5 {{$t('common.settings')}}
el-form(action='/api/user' method='PUT' @submit.native.prevent='change') el-form(action='/api/user' method='PUT' @submit.native.prevent='change_password')
el-form-item {{$t('settings.change_password')}} el-form-item {{$t('settings.change_password')}}
el-input(v-model='password' type='password') el-input(v-model='password' type='password')
el-button(type='success' native-type='submit') {{$t('common.send')}} el-button(type='success' native-type='submit') {{$t('common.send')}}
el-divider {{$t('settings.danger_section')}}
p {{$t('settings.remove_account')}}
el-button(type='danger' @click='remove_account') {{$t('common.remove')}}
</template> </template>
<script> <script>
import { mapState, mapActions } from 'vuex' import { mapState, mapActions } from 'vuex'
import { Message } from 'element-ui' import { Message, MessageBox } from 'element-ui'
export default { export default {
data () { data () {
@ -20,13 +24,13 @@ export default {
password: '', password: '',
} }
}, },
// async asyncData ({ $axios, params }) { async asyncData ({ $axios, params }) {
// const user = await $axios.$get('/auth/user') const user = await $axios.$get('/auth/user')
// user.mastodon_auth = '' user.mastodon_auth = ''
// return { user } return { user }
// }, },
methods: { methods: {
async change () { async change_password () {
if (!this.password) return if (!this.password) return
const user_data = { id : this.$auth.user.id, password: this.password } const user_data = { id : this.$auth.user.id, password: this.password }
try { try {
@ -37,6 +41,15 @@ export default {
console.log(e) console.log(e)
} }
}, },
async remove_account () {
MessageBox.confirm(this.$t('settings.remove_account_confirm'), this.$t('common.confirm'), {
confirmButtonText: this.$t('common.ok'),
cancelButtonText: this.$t('common.cancel'),
type: 'error'
}).then( () => {
this.$axios.$delete('/user')
})
}
} }
} }
</script> </script>

13
plugins/axios.js Normal file
View file

@ -0,0 +1,13 @@
export default function({ $axios, store }) {
if (process.server) {
console.error('dentro il server ', store.settings )
// $axios.defaults.baseurl =
} else {
// const protocol = window.location.protocol
// const hostname = window.location.hostname
// const port = 8000
// const url = `${protocol}//${hostname}:${port}`
$axios.defaults.baseURL = window.location.origin + '/api'
console.error('dentro il client !')
}
}

View file

@ -9,9 +9,9 @@ const locales = {
it: require('element-ui/lib/locale/lang/it'), it: require('element-ui/lib/locale/lang/it'),
en: require('element-ui/lib/locale/lang/en') en: require('element-ui/lib/locale/lang/en')
} }
locale.use(locales[process.env.locale])
export default () => { export default ({ app }) => {
locale.use(locales[app.i18n.locale])
Vue.use(Button) Vue.use(Button)
Vue.use(Collapse) Vue.use(Collapse)
Vue.use(CollapseItem) Vue.use(CollapseItem)

View file

@ -1,31 +1,24 @@
import Vue from 'vue' import Vue from 'vue'
import moment from 'dayjs' import moment from 'dayjs'
import 'dayjs/locale/it' import 'dayjs/locale/it'
moment.locale(process.env.locale)
function short_hour(datetime) { export default ({ app }) => {
if (datetime.minute() === 0) { moment.locale(app.i18n.locale)
return datetime.format('HH')
} else {
return datetime.format('HH:mm')
}
}
export default (a) => {
Vue.filter('linkify', value => value.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1">$1</a>')) Vue.filter('linkify', value => value.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1">$1</a>'))
Vue.filter('datetime', value => moment(value).format('ddd, D MMMM HH:mm')) Vue.filter('datetime', value => moment(value).format('ddd, D MMMM HH:mm'))
Vue.filter('short_datetime', value => moment(value).format('D/MM HH:mm')) Vue.filter('short_datetime', value => moment(value).format('D/MM HH:mm'))
Vue.filter('hour', value => moment(value).format('HH:mm')) Vue.filter('hour', value => moment(value).format('HH:mm'))
Vue.filter('day', value => moment(value).format('dddd, D MMMM')) Vue.filter('day', value => moment(value*1000).format('dddd, D MMMM'))
Vue.filter('month', value => moment(value).format('MMM')) Vue.filter('month', value => moment(value).format('MMM'))
Vue.filter('event_when', event => { Vue.filter('event_when', event => {
const start = moment(event.start_datetime)
const end = moment(event.end_datetime) const start = moment(event.start_datetime*1000)
const end = moment(event.end_datetime*1000)
if (event.multidate) { if (event.multidate) {
return `${start.format('ddd, D MMMM')} (${short_hour(start)}) - ${end.format('ddd, D MMMM')} (${short_hour(end)})` return `${start.format('ddd, D MMMM (HH:mm)')} - ${end.format('ddd, D MMMM (HH:mm)')}`
} else if (event.end_datetime && event.end_datetime !== event.start_datetime) } else if (event.end_datetime && event.end_datetime !== event.start_datetime)
return `${start.format('ddd, D MMMM')} (${short_hour(start)}-${short_hour(end)})` return `${start.format('ddd, D MMMM (HH:mm-')}${end.format('HH:mm)')}`
else else
return `${start.format('dddd, D MMMM')} (${short_hour(start)})` return start.format('dddd, D MMMM (HH:mm)')
}) })
} }

View file

@ -1,6 +1,5 @@
const { Op } = require('sequelize') const { Op } = require('sequelize')
const { user: User } = require('./models') const { user: User } = require('./models')
const Settings = require('./controller/settings')
const Auth = { const Auth = {
async fillUser(req, res, next) { async fillUser(req, res, next) {
@ -26,7 +25,7 @@ const Auth = {
if (!req.user) { if (!req.user) {
return res return res
.status(403) .status(403)
.send({ message: 'Failed to authenticate token ' + err }) .send({ message: 'Failed to authenticate token ' })
} }
next() next()
}, },

View file

@ -2,15 +2,13 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const moment = require('moment') const moment = require('moment')
const { event: Event, comment: Comment } = require('../models') const { event: Event, comment: Comment } = require('../models')
const config = require('../../config') const config = require('config')
const Mastodon = require('mastodon-api') const Mastodon = require('mastodon-api')
const settingsController = require('./settings') const settingsController = require('./settings')
moment.locale(process.env.locale)
const botController = { const botController = {
bot: null, bot: null,
async initialize() { async initialize() {
const settings = await settingsController.settings()
if (!settings.mastodon_auth || !settings.mastodon_auth.access_token) return if (!settings.mastodon_auth || !settings.mastodon_auth.access_token) return
const mastodon_auth = settings.mastodon_auth const mastodon_auth = settings.mastodon_auth
botController.bot = new Mastodon({ botController.bot = new Mastodon({

View file

@ -169,17 +169,19 @@ const eventController = {
async getAll(req, res) { async getAll(req, res) {
// this is due how v-calendar shows dates // this is due how v-calendar shows dates
const start = moment().year(req.params.year).month(req.params.month) const start = moment().year(req.params.year).month(req.params.month)
.startOf('month').startOf('isoWeek') .startOf('month').startOf('isoWeek').unix()
let end = moment().year(req.params.year).month(req.params.month).endOf('month') let end = moment().utc().year(req.params.year).month(req.params.month).endOf('month')
const shownDays = end.diff(start, 'days') const shownDays = end.diff(start, 'days')
if (shownDays <= 34) end = end.add(1, 'week') if (shownDays <= 34) end = end.add(1, 'week')
end = end.endOf('isoWeek') end = end.endOf('isoWeek').unix()
const events = await Event.findAll({ const events = await Event.findAll({
where: { where: {
is_visible: true, is_visible: true,
[Op.and]: [ [Op.and]: [
{ start_datetime: { [Op.gte]: start } }, Sequelize.literal(`start_datetime >= ${start}`),
{ start_datetime: { [Op.lte]: end } } Sequelize.literal(`start_datetime <= ${end}`)
// { start_datetime: { [Op.gte]: start } },
// { start_datetime: { [Op.lte]: end } }
] ]
}, },
order: [ order: [

View file

@ -1,47 +1,77 @@
const Mastodon = require('mastodon-api') const Mastodon = require('mastodon-api')
const { setting: Setting } = require('../models') const { setting: Setting } = require('../models')
const config = require('config')
const settingsController = { const settingsController = {
settings: null,
secretSettings: null,
async setAdminSetting(key, value) { // initialize instance settings from db
await Setting.findOrCreate({ where: { key }, async init (req, res, next) {
defaults: { value } }) if (!settingsController.settings) {
.spread((settings, created) => { const settings = await Setting.findAll()
if (!created) return settings.update({ value }) settingsController.settings = {}
}) settingsController.secretSettings = {}
settings.forEach( s => settingsController[s.is_secret?'secretSettings':'settings'][s.key] = s.value)
}
next()
}, },
async getAdminSettings(req, res) { async set(key, value, is_secret=false) {
const settings = await settingsController.settings() try {
res.json(settings) await Setting.findOrCreate({
where: { key },
defaults: { value, is_secret }
}).spread((settings, created) => {
if (!created) return settings.update({ value, is_secret })
})
settingsController[is_secret?'secretSettings':'settings'][key]=value
console.error('settings ', settingsController.settings)
console.error('settings controller ', settingsController.secretSettings)
return true
} catch(e) {
console.error(e)
return false
}
},
async setRequest(req, res) {
const { key, value, is_secret } = req.body
const ret = await settingsController.set(key, value, is_secret)
if (ret) res.sendStatus(200)
else res.sendStatus(400)
},
getAllRequest(req, res) {
res.json(settingsController.settings)
}, },
async getAuthURL(req, res) { async getAuthURL(req, res) {
const instance = req.body.instance const instance = req.body.instance
const callback = `${process.env.baseurl}/api/settings/oauth` console.error('DENTRO GET AUTH URL ', instance)
const callback = `${config.baseurl}/api/settings/oauth`
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`, const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
'gancio', 'read write', callback) 'gancio', 'read write', callback)
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret, const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
`https://${instance}`, 'read write', callback) `https://${instance}`, 'read write', callback)
await settingsController.setAdminSetting('mastodon_auth', { client_id, client_secret, instance }) await settingsController.set('mastodon_instance', instance )
await settingsController.set('mastodon_auth', { client_id, client_secret }, true)
res.json(url) res.json(url)
}, },
async code(req, res) { async code(req, res) {
const code = req.query.code const code = req.query.code
let client_id, client_secret, instance const callback = `${config.baseurl}/api/settings/oauth`
const callback = `${process.env.baseurl}/api/settings/oauth` const client_id = settingsController.secretSettings.mastodon_auth.client_id
const client_secret = settingsController.secretSettings.mastodon_auth.client_secret
const settings = await settingsController.settings() const instance = settingsController.settings.mastodon_instance
({ client_id, client_secret, instance } = settings.mastodon_auth)
try { try {
const token = await Mastodon.getAccessToken(client_id, client_secret, code, const access_token = await Mastodon.getAccessToken(client_id, client_secret, code,
`https://${instance}`, callback) `https://${instance}`, callback)
const mastodon_auth = { client_id, client_secret, access_token: token, instance } const mastodon_auth = { client_id, client_secret, access_token }
await settingsController.setAdminSetting('mastodon_auth', mastodon_auth) await settingsController.set('mastodon_auth', mastodon_auth, true)
res.redirect('/admin') res.redirect('/admin')
} catch (e) { } catch (e) {
@ -49,11 +79,6 @@ const settingsController = {
} }
}, },
async settings() {
const settings = await Setting.findAll()
return settings
}
} }
module.exports = settingsController module.exports = settingsController

View file

@ -4,10 +4,11 @@ const crypto = require('crypto')
const jwt = require('jsonwebtoken') const jwt = require('jsonwebtoken')
const { Op } = require('sequelize') const { Op } = require('sequelize')
const jsonwebtoken = require('jsonwebtoken') const jsonwebtoken = require('jsonwebtoken')
const config = require('config')
const mail = require('../mail') const mail = require('../mail')
const { user: User, event: Event, tag: Tag, place: Place } = require('../models') const { user: User, event: Event, tag: Tag, place: Place } = require('../models')
const eventController = require('./event') const eventController = require('./event')
const config = require('../../config') const settingsController = require('./settings')
const userController = { const userController = {
async login(req, res) { async login(req, res) {
@ -219,6 +220,7 @@ const userController = {
async register(req, res) { async register(req, res) {
if (!settingsController.settings.allow_registration) return res.sendStatus(404)
const n_users = await User.count() const n_users = await User.count()
try { try {
// the first registered user will be an active admin // the first registered user will be an active admin

View file

@ -3,7 +3,7 @@ const multer = require('multer')
const cookieParser = require('cookie-parser') const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const expressJwt = require('express-jwt') const expressJwt = require('express-jwt')
const config = require('../config') const config = require('config')
const { fillUser, isAuth, isAdmin } = require('./auth') const { fillUser, isAuth, isAdmin } = require('./auth')
const eventController = require('./controller/event') const eventController = require('./controller/event')
@ -18,10 +18,20 @@ const api = express.Router()
api.use(cookieParser()) api.use(cookieParser())
api.use(bodyParser.urlencoded({ extended: false })) api.use(bodyParser.urlencoded({ extended: false }))
api.use(bodyParser.json()) api.use(bodyParser.json())
api.use(settingsController.init)
const jwt = expressJwt({ const jwt = expressJwt({
secret: config.secret, secret: config.secret,
credentialsRequired: false credentialsRequired: false,
getToken: function fromHeaderOrQuerystring (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.cookies && req.cookies['auth._token.local']) {
const [ prefix, token ] = req.cookies['auth._token.local'].split(' ')
if (prefix === 'Bearer') return token
}
return null
}
}) })
// AUTH // AUTH
@ -74,8 +84,8 @@ api.get('/event/unconfirmed', jwt, isAuth, isAdmin, eventController.getUnconfirm
api.post('/event/notification', eventController.addNotification) api.post('/event/notification', eventController.addNotification)
api.delete('/event/notification/:code', eventController.delNotification) api.delete('/event/notification/:code', eventController.delNotification)
api.get('/settings', jwt, fillUser, isAdmin, settingsController.getAdminSettings) api.get('/settings', settingsController.getAllRequest)
api.post('/settings', jwt, fillUser, isAdmin, settingsController.setAdminSetting) api.post('/settings', jwt, fillUser, isAdmin, settingsController.setRequest)
// get event // get event
api.get('/event/:event_id', eventController.get) api.get('/event/:event_id', eventController.get)

View file

@ -1,7 +1,7 @@
const Email = require('email-templates') const Email = require('email-templates')
const path = require('path') const path = require('path')
const moment = require('moment') const moment = require('moment')
const config = require('../config') const config = require('config')
moment.locale(config.locale) moment.locale(config.locale)
const mail = { const mail = {

View file

@ -6,10 +6,13 @@ module.exports = (sequelize, DataTypes) => {
description: DataTypes.TEXT, description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN, multidate: DataTypes.BOOLEAN,
start_datetime: { start_datetime: {
type: DataTypes.DATE, type: DataTypes.INTEGER,
index: true
},
end_datetime: {
type: DataTypes.INTEGER,
index: true index: true
}, },
end_datetime: DataTypes.DATE,
image_path: DataTypes.STRING, image_path: DataTypes.STRING,
is_visible: DataTypes.BOOLEAN, is_visible: DataTypes.BOOLEAN,
activitypub_id: { activitypub_id: {

View file

@ -5,12 +5,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.ENUM, type: DataTypes.ENUM,
values: ['new', 'sent', 'error'], values: ['new', 'sent', 'error'],
defaultValue: 'new', defaultValue: 'new',
errorMessage: DataTypes.TEXT,
index: true index: true
} }
}, {}) }, {})
eventNotification.associate = function (models) {
// associations can be defined here
}
return eventNotification return eventNotification
} }

View file

@ -2,9 +2,8 @@ const argv = require('yargs').argv
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const Sequelize = require('sequelize') const Sequelize = require('sequelize')
const config_path = path.resolve(argv.config || './config.js')
const basename = path.basename(__filename) const basename = path.basename(__filename)
const config = require(config_path) const config = require('config')
const db = {} const db = {}
const sequelize = new Sequelize(config.db) const sequelize = new Sequelize(config.db)

View file

@ -7,7 +7,8 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false, allowNull: false,
index: true index: true
}, },
value: DataTypes.JSON value: DataTypes.JSON,
is_secret: DataTypes.BOOLEAN
}, {}) }, {})
return setting return setting

View file

@ -4,7 +4,7 @@ const crypto = require('crypto')
const mkdirp = require('mkdirp') const mkdirp = require('mkdirp')
const sharp = require('sharp') const sharp = require('sharp')
const consola = require('consola') const consola = require('consola')
const config = require('../config') const config = require('config')
mkdirp.sync(config.upload_path + '/thumb') mkdirp.sync(config.upload_path + '/thumb')

162
server/cli.js Executable file
View file

@ -0,0 +1,162 @@
#!/usr/bin/env node
const arg = require('arg')
const inquirer = require('inquirer')
const package = require('../package.json')
const consola = require('consola')
const firstrun = require('./firstrun')
const path = require('path')
const fs = require('fs')
const sequelize = require('sequelize')
/**
* initial setup:
* - check first run
* - ask title, description, baseurl
* - ask and create upload path and thumb dir
* - ask and inizialite db
* - create first admin account
* - enable open registration?
* - enable anon event?
* - enable email export?
* - enable email notification?
* - enable notifier?
* - enable pm2
*
* start gancio:
*
* update gancio:
* - yarn/npm global update...
* - sequelize migrate !
*/
function parseArguments(rawArgs) {
const args = arg({
'--config': String,
'--install': Boolean,
'--upgrade': Boolean
}, {
argv: rawArgs.slice(2),
});
return {
config: path.resolve(args['--config'] || '/etc/gancio_config.json') ,
install: args['--install'] || false,
upgrade: args['--upgrade'] || false
};
}
async function setupQuestionnaire() {
const questions = []
questions.push({
message: 'Specify a baseurl for this gancio installation! (eg. http://gancio.cisti.org)',
name: 'baseurl',
default: 'http://localhost:3000',
validate: baseurl => baseurl.length>0
})
questions.push({
name: 'db.dialect',
message: 'DB dialect',
type: 'list',
choices: ['sqlite', 'postgres']
})
questions.push({
name: 'db.storage',
message: 'sqlite db path',
default: '/var/gancio/db.sqlite',
when: answers => answers.db.dialect === 'sqlite',
validate: db_path => db_path.length>0 && fs.existsSync(path.dirname(db_path))
})
questions.push({
name: 'db.user',
message: 'Postgres user',
default: 'gancio',
when: answers => answers.db.dialect === 'postgres',
validate: user => user.length>0
})
questions.push({
name: 'db.pass',
type: 'password',
message: 'Postgres password',
default: 'gancio',
when: answers => answers.db.dialect === 'postgres',
validate: async (password, options) => {
try {
const db = new sequelize({host: 'localhost', dialect: 'postgres', database: 'gancio', username: options.db.user, password })
return db.authenticate().then( () => {
consola.info(`DB connected`)
return true
})
} catch(e) {
consola.error(e)
return false
}
}
})
questions.push({
name: 'upload_path',
message: 'Where gancio has to store media?',
default: '/var/gancio/',
validate: (p) => {
const exists = fs.existsSync(p)
if (!exists) consola.warn(`"${p}" does not exists, please create it`)
return exists
}
})
questions.push({
name: 'admin.email',
message: `Admin email (a first user with this username will be created)`,
validate: email => email.length>0
})
questions.push({
name: 'admin.password',
message: 'Admin password',
type: 'password',
validate: password => password.length>0
})
const answers = await inquirer.prompt(questions)
return answers
}
async function cli(args) {
const options = parseArguments(args)
consola.info(`${package.name} - v${package.version} - ${package.description}`)
// install flag specified?
if (options.install) {
consola.info(`Cool! You're going to setup gancio on this machine.`)
const config = await setupQuestionnaire()
await firstrun.setup(config, options.config)
consola.info(`This is your configuration, run "gancio --install" or edit ${options.config} to modify it: `)
consola.info(JSON.stringify(config, null, 2))
process.exit(0)
}
// upgrade gancio / TODO npm/yarn global upgrade gancio ?
if (options.upgrade) {
consola.warn('Not implemented yet but should be an easy task! PR welcome!')
process.exit(-1)
}
// is first run?
if (firstrun.check(options.config)) {
consola.error(`Configuration file "${options.config}" not found!
This is your first run? You could create it using --install flag`)
process.exit(-1)
} else {
require('./index')
}
}
cli(process.argv)

View file

@ -1,5 +1,24 @@
const argv = require('yargs').argv // const argv = require('yargs').argv
const path = require('path') // const path = require('path')
const config_path = path.resolve(argv.config || './config.js') // const fs = require('fs')
module.exports = require(config_path) // const config_path = path.resolve(argv.config || './config.js')
// let user_config
// if (fs.existsSync(config_path)) {
// user_config = require(config_path)
// }
// const config = {
// baseurl: 'PLEASE CONFIGURE YOUR BASEURL!',
// server: {
// host: 'localhost',
// port: 3000
// },
// secret: '',
// db: {
// dialect: 'sqlite'
// }
// }
// module.exports = Object.assign( config, user_config )

View file

@ -3,4 +3,4 @@ p= t('email.register')
hr hr
small #{config.title} / #{config.description} small #{config.title} / #{config.description}
br br
small #{config.baseurl} a(href='#{config.baseurl}') #{config.baseurl}

View file

@ -1,41 +1,43 @@
// check config.js existance // check config.js existance
const fs = require('fs') const fs = require('fs')
const path = require('path') const consola = require('consola')
const argv = require('yargs').argv
const config_path = path.resolve(argv.config || './config.js') module.exports = {
check (config_path) {
return !fs.existsSync(config_path)
},
if (!fs.existsSync(config_path)) { async setup (config, config_path) {
console.error(`Configuration file not found at '${config_path}. Please copy 'config.example.js' and modify it.`) // generate a random salt
process.exit(1) consola.info('Generate random salt')
} config.secret = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const config = require(config_path) consola.info(`Save configuration into ${config_path}`)
if (!config.secret) { fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
console.error(`Please specify a random 'secret' in '${config_path}'!`)
process.exit(1)
}
const Sequelize = require('sequelize') // sync db (TODO, check if there's something in db and ask to backup)
let db const db = require('./api/models')
try { try {
db = new Sequelize(config.db) consola.info(`Create tables..`)
await db.sequelize.sync({force: true})
} catch(e) { } catch(e) {
console.error(`DB Error: check '${config.env}' configuration.\n (sequelize error -> ${e})`) consola.error('Error creating tables', e)
process.exit(1) return -1
} }
// return db existence // create admin user
module.exports = db.authenticate() consola.info('Create admin user')
.then(() => { await db.user.create({
require('./api/models') email: config.admin.email,
if (config.env === 'development') { password: config.admin.password,
console.error('DB Force sync') is_admin: true,
return db.sync({ force: true }) is_active: true
})
const settings = require('./api/controller/settings')
settings.set('enable_registration', true)
settings.set('allow_anon_event', true)
settings.set('allow_mastodon_association', true)
}
} }
})
.catch(e => {
console.error(e)
console.error(`DB Error: check '${config.env}' configuration\n (sequelize error -> ${e})`)
process.exit(1)
})

View file

@ -7,10 +7,11 @@ const { Nuxt, Builder } = require('nuxt')
const firstRun = require('./firstrun') const firstRun = require('./firstrun')
// Import and Set Nuxt.js options // Import and Set Nuxt.js options
const nuxt_config = require('../nuxt.config.js') const nuxt_config = require('../nuxt.config.js')
const config = require('./config') const config = require('config')
const app = express() const app = express()
async function start() { async function start() {
nuxt_config.server = config.server
// Init Nuxt.js // Init Nuxt.js
const nuxt = new Nuxt(nuxt_config) const nuxt = new Nuxt(nuxt_config)
@ -31,7 +32,7 @@ async function start() {
app.use(nuxt.render) app.use(nuxt.render)
// Listen the server // Listen the server
const server = app.listen(config.server) const server = app.listen(nuxt_config.server)
// close connections/port/unix socket // close connections/port/unix socket
function shutdown() { function shutdown() {
@ -54,4 +55,4 @@ async function start() {
}) })
} }
firstRun.then(start) start()

View file

@ -22,10 +22,12 @@ module.exports = {
type: Sequelize.BOOLEAN type: Sequelize.BOOLEAN
}, },
start_datetime: { start_datetime: {
type: Sequelize.DATE type: Sequelize.INTEGER,
index: true
}, },
end_datetime: { end_datetime: {
type: Sequelize.DATE type: Sequelize.INTEGER,
index: true
}, },
image_path: { image_path: {
type: Sequelize.STRING type: Sequelize.STRING

View file

@ -11,6 +11,7 @@ module.exports = {
value: { value: {
type: Sequelize.JSON type: Sequelize.JSON
}, },
is_secret: Sequelize.BOOLEAN,
createdAt: { createdAt: {
allowNull: false, allowNull: false,
type: Sequelize.DATE type: Sequelize.DATE

View file

@ -46,7 +46,7 @@ export const getters = {
let lastDay = null let lastDay = null
events = map(events, e => { events = map(events, e => {
const currentDay = moment(e.start_datetime).date() const currentDay = moment(e.start_datetime*1000).date()
e.newDay = (!lastDay || lastDay !== currentDay) && currentDay e.newDay = (!lastDay || lastDay !== currentDay) && currentDay
lastDay = currentDay lastDay = currentDay
return e return e
@ -60,8 +60,8 @@ export const mutations = {
setEvents(state, events) { setEvents(state, events) {
// set a `past` flag // set a `past` flag
state.events = events.map((e) => { state.events = events.map((e) => {
const end_datetime = e.end_datetime || moment(e.start_datetime).add('3', 'hour') const end_datetime = e.end_datetime || e.start_datetime+3600*2
const past = (moment().diff(end_datetime, 'minutes') > 0) const past = (moment().unix() - end_datetime) > 0
e.past = !!past e.past = !!past
return e return e
}) })
@ -96,12 +96,22 @@ export const mutations = {
setSettings(state, settings) { setSettings(state, settings) {
state.settings = settings state.settings = settings
}, },
setSetting(state, setting) {
state.settings[setting.key] = setting.value
},
setConfig(state, config) { setConfig(state, config) {
state.config = config state.config = config
} }
} }
export const actions = { export const actions = {
// this method is called server side only for each request
// we use it to get configuration from db
async nuxtServerInit ({ commit }, { app } ) {
const settings = await app.$axios.$get('/settings')
console.error('dentro NUXST SERVER INIT', settings)
return commit('setSettings', settings)
},
async updateEvents({ commit }, page) { async updateEvents({ commit }, page) {
const events = await this.$axios.$get(`/event/${page.month - 1}/${page.year}`) const events = await this.$axios.$get(`/event/${page.month - 1}/${page.year}`)
commit('setEvents', events) commit('setEvents', events)
@ -134,8 +144,9 @@ export const actions = {
showPastEvents({ commit }, show) { showPastEvents({ commit }, show) {
commit('showPastEvents', show) commit('showPastEvents', show)
}, },
setSettings({ commit }, settings) { async setSetting({ commit }, setting) {
commit('setSettings', settings) await this.$axios.$post('/settings', setting )
commit('setSetting', setting)
}, },
setConfig({ commit }, config) { setConfig({ commit }, config) {
commit('setConfig', config) commit('setConfig', config)