2018-02-11 13:18:59 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const imageService = require('../../services/image');
|
2018-02-11 13:18:59 +08:00
|
|
|
const utils = require('../../services/utils');
|
2018-04-02 09:27:46 +08:00
|
|
|
const dateNoteService = require('../../services/date_notes');
|
2018-02-11 13:18:59 +08:00
|
|
|
const sql = require('../../services/sql');
|
2018-04-02 09:27:46 +08:00
|
|
|
const noteService = require('../../services/notes');
|
|
|
|
const passwordEncryptionService = require('../../services/password_encryption');
|
|
|
|
const optionService = require('../../services/options');
|
2018-04-02 05:38:24 +08:00
|
|
|
const ApiToken = require('../../entities/api_token');
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function login(req) {
|
2018-02-11 13:18:59 +08:00
|
|
|
const username = req.body.username;
|
|
|
|
const password = req.body.password;
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const isUsernameValid = username === await optionService.getOption('username');
|
|
|
|
const isPasswordValid = await passwordEncryptionService.verifyPassword(password);
|
2018-02-11 13:18:59 +08:00
|
|
|
|
|
|
|
if (!isUsernameValid || !isPasswordValid) {
|
2018-03-31 05:29:13 +08:00
|
|
|
return [401, "Incorrect username/password"];
|
2018-02-11 13:18:59 +08:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:53:01 +08:00
|
|
|
const apiToken = await new ApiToken({
|
|
|
|
token: utils.randomSecureToken()
|
|
|
|
}).save();
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
return {
|
2018-04-02 05:38:24 +08:00
|
|
|
token: apiToken.token
|
2018-03-31 05:29:13 +08:00
|
|
|
};
|
2018-02-11 13:18:59 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function uploadImage(req) {
|
2018-02-11 13:18:59 +08:00
|
|
|
const file = req.file;
|
|
|
|
|
|
|
|
if (!["image/png", "image/jpeg", "image/gif"].includes(file.mimetype)) {
|
2018-03-31 05:29:13 +08:00
|
|
|
return [400, "Unknown image type: " + file.mimetype];
|
2018-02-11 13:18:59 +08:00
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
const parentNote = await dateNoteService.getDateNote(req.headers['x-local-date']);
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2019-02-20 04:24:35 +08:00
|
|
|
const {noteId} = await imageService.saveImage(file.buffer, "Sender image", parentNote.noteId);
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2019-02-20 04:24:35 +08:00
|
|
|
return {
|
|
|
|
noteId: noteId
|
|
|
|
};
|
2018-03-31 05:29:13 +08:00
|
|
|
}
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function saveNote(req) {
|
2018-04-08 01:03:16 +08:00
|
|
|
const parentNote = await dateNoteService.getDateNote(req.headers['x-local-date']);
|
2018-02-11 23:54:56 +08:00
|
|
|
|
2019-02-20 04:24:35 +08:00
|
|
|
const {note, branch} = await noteService.createNewNote(parentNote.noteId, {
|
2018-02-11 23:54:56 +08:00
|
|
|
title: req.body.title,
|
|
|
|
content: req.body.content,
|
|
|
|
target: 'into',
|
|
|
|
isProtected: false,
|
|
|
|
type: 'text',
|
|
|
|
mime: 'text/html'
|
|
|
|
});
|
2019-02-20 04:24:35 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
noteId: note.noteId,
|
|
|
|
branchId: branch.branchId
|
|
|
|
};
|
2018-03-31 05:29:13 +08:00
|
|
|
}
|
2018-02-11 23:54:56 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
module.exports = {
|
|
|
|
login,
|
|
|
|
uploadImage,
|
|
|
|
saveNote
|
|
|
|
};
|