1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 07:39:44 +02:00

Initial commit

This commit is contained in:
Maksim Eltyshev 2019-08-31 04:07:25 +05:00
commit 5ffef61fe7
613 changed files with 91659 additions and 0 deletions

View file

@ -0,0 +1,41 @@
const Errors = {
USER_EXIST: {
conflict: 'User is already exist'
}
};
module.exports = {
inputs: {
email: {
type: 'string',
isEmail: true,
required: true
},
password: {
type: 'string',
required: true
},
name: {
type: 'string',
required: true
}
},
exits: {
conflict: {
responseType: 'conflict'
}
},
fn: async function(inputs, exits) {
const values = _.pick(inputs, ['email', 'password', 'name']);
const user = await sails.helpers
.createUser(values, this.req)
.intercept('conflict', () => Errors.USER_EXIST);
return exits.success({
item: user
});
}
};

View file

@ -0,0 +1,38 @@
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found'
}
};
module.exports = {
inputs: {
id: {
type: 'number',
required: true
}
},
exits: {
notFound: {
responseType: 'notFound'
}
},
fn: async function(inputs, exits) {
let user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
user = await sails.helpers.deleteUser(user, this.req);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user
});
}
};

View file

@ -0,0 +1,9 @@
module.exports = {
fn: async function(inputs, exits) {
const users = await sails.helpers.getUsers();
return exits.success({
items: users
});
}
};

View file

@ -0,0 +1,16 @@
module.exports = {
fn: async function(inputs, exits) {
// TODO: allow over HTTP without subscription
if (!this.req.isSocket) {
return this.res.badRequest();
}
const { currentUser } = this.req;
sails.sockets.join(this.req, `user:${currentUser.id}`); // TODO: only when subscription needed
return exits.success({
item: currentUser
});
}
};

View file

@ -0,0 +1,61 @@
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found'
}
};
module.exports = {
inputs: {
id: {
type: 'number',
required: true
},
isAdmin: {
type: 'boolean'
},
name: {
type: 'string',
isNotEmptyString: true
},
avatar: {
type: 'json',
custom: value => _.isNull(value)
}
},
exits: {
notFound: {
responseType: 'notFound'
}
},
fn: async function(inputs, exits) {
const { currentUser } = this.req;
if (!currentUser.isAdmin) {
if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
}
delete inputs.isAdmin;
}
let user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
const values = _.pick(inputs, ['isAdmin', 'name', 'avatar']);
user = await sails.helpers.updateUser(user, values, this.req);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user
});
}
};

View file

@ -0,0 +1,124 @@
const stream = require('stream');
const fs = require('fs');
const path = require('path');
const uuid = require('uuid/v4');
const sharp = require('sharp');
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found'
}
};
const createReceiver = () => {
const receiver = require('stream').Writable({ objectMode: true });
let firstFileHandled = false;
receiver._write = (file, encoding, done) => {
if (firstFileHandled) {
file.pipe(
new stream.Writable({
write(chunk, encoding, callback) {
callback();
}
})
);
return done();
}
firstFileHandled = true;
const resize = sharp()
.resize(36, 36)
.jpeg();
const transform = new stream.Transform({
transform(chunk, encoding, callback) {
callback(null, chunk);
}
});
stream.pipeline(file, resize, transform, error => {
if (error) {
return done(error.message);
}
file.fd = `${uuid()}.jpg`;
const output = fs.createWriteStream(
path.join(sails.config.custom.uploadsPath, file.fd)
);
stream.pipeline(transform, output, error => {
if (error) {
return done(error);
}
done();
});
});
};
return receiver;
};
module.exports = {
inputs: {
id: {
type: 'number',
required: true
}
},
exits: {
notFound: {
responseType: 'notFound'
},
unprocessableEntity: {
responseType: 'unprocessableEntity'
}
},
fn: async function(inputs, exits) {
const { currentUser } = this.req;
let user;
if (currentUser.isAdmin) {
user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
} else if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
} else {
user = currentUser;
}
this.req.file('file').upload(createReceiver(), async (error, files) => {
if (error) {
return exits.unprocessableEntity(error);
}
if (files.length === 0) {
return exits.unprocessableEntity('No file was uploaded');
}
user = await sails.helpers.updateUser(
user,
{
avatar: files[0].fd
},
this.req
);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return this.res.json({
item: user
});
});
}
};