wikijs-fork/server/controllers/ws.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-02-09 01:52:37 +00:00
'use strict'
/* global appconfig, entries, rights, search, upl */
2017-04-05 02:04:17 +00:00
/* eslint-disable standard/no-callback-literal */
2017-02-09 01:52:37 +00:00
const _ = require('lodash')
module.exports = (socket) => {
// Check if Guest
if (!socket.request.user.logged_in) {
socket.request.user = _.assign(rights.guest, socket.request.user)
}
2017-02-09 01:52:37 +00:00
// -----------------------------------------
// SEARCH
2017-02-09 01:52:37 +00:00
// -----------------------------------------
if (appconfig.public || socket.request.user.logged_in) {
socket.on('search', (data, cb) => {
cb = cb || _.noop
search.find(data.terms).then((results) => {
return cb(results) || true
})
2017-02-09 01:52:37 +00:00
})
}
2017-04-18 02:44:04 +00:00
// -----------------------------------------
// TREE VIEW (LIST ALL PAGES)
// -----------------------------------------
if (appconfig.public || socket.request.user.logged_in) {
2017-04-18 02:44:04 +00:00
socket.on('treeFetch', (data, cb) => {
cb = cb || _.noop
entries.getFromTree(data.basePath, socket.request.user).then((f) => {
2017-04-18 02:44:04 +00:00
return cb(f) || true
})
})
}
2017-02-09 01:52:37 +00:00
// -----------------------------------------
// UPLOADS
2017-02-09 01:52:37 +00:00
// -----------------------------------------
if (socket.request.user.logged_in) {
socket.on('uploadsGetFolders', (data, cb) => {
cb = cb || _.noop
upl.getUploadsFolders().then((f) => {
return cb(f) || true
})
2017-02-09 01:52:37 +00:00
})
socket.on('uploadsCreateFolder', (data, cb) => {
cb = cb || _.noop
upl.createUploadsFolder(data.foldername).then((f) => {
return cb(f) || true
})
2017-02-09 01:52:37 +00:00
})
socket.on('uploadsGetImages', (data, cb) => {
cb = cb || _.noop
upl.getUploadsFiles('image', data.folder).then((f) => {
return cb(f) || true
})
2017-02-09 01:52:37 +00:00
})
socket.on('uploadsGetFiles', (data, cb) => {
cb = cb || _.noop
upl.getUploadsFiles('binary', data.folder).then((f) => {
return cb(f) || true
})
2017-02-09 01:52:37 +00:00
})
socket.on('uploadsDeleteFile', (data, cb) => {
cb = cb || _.noop
upl.deleteUploadsFile(data.uid).then((f) => {
return cb(f) || true
})
2017-02-09 01:52:37 +00:00
})
socket.on('uploadsFetchFileFromURL', (data, cb) => {
cb = cb || _.noop
upl.downloadFromUrl(data.folder, data.fetchUrl).then((f) => {
return cb({ ok: true }) || true
}).catch((err) => {
return cb({
ok: false,
msg: err.message
}) || true
})
2017-02-09 01:52:37 +00:00
})
socket.on('uploadsRenameFile', (data, cb) => {
cb = cb || _.noop
upl.moveUploadsFile(data.uid, data.folder, data.filename).then((f) => {
return cb({ ok: true }) || true
}).catch((err) => {
return cb({
ok: false,
msg: err.message
}) || true
})
2017-02-09 01:52:37 +00:00
})
2016-10-17 03:24:19 +00:00
socket.on('uploadsMoveFile', (data, cb) => {
cb = cb || _.noop
upl.moveUploadsFile(data.uid, data.folder).then((f) => {
return cb({ ok: true }) || true
}).catch((err) => {
return cb({
ok: false,
msg: err.message
}) || true
})
2017-02-09 01:52:37 +00:00
})
}
2017-02-09 01:52:37 +00:00
}