|
@@ -21,6 +21,54 @@ const listMessages = async (req, res, next) => {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+const removeMessage = async (req, res, next) => {
|
|
|
|
+ try {
|
|
|
|
+ const id = req.params.id;
|
|
|
|
+ const userId = req.user.id;
|
|
|
|
+ const DIR_IMAGES = process.env.DIR_IMAGES;
|
|
|
|
+ const DIR_AUDIOS = process.env.DIR_AUDIOS;
|
|
|
|
+ const DIR_VIDEOS = process.env.DIR_VIDEOS;
|
|
|
|
+ const DIR_FILES = process.env.DIR_FILES;
|
|
|
|
+ const userMessage = await MessageModel.remove(id, userId);
|
|
|
|
+ const companionMessage = await MessageModel.removeByFields(
|
|
|
|
+ userId,
|
|
|
|
+ userMessage.idTime,
|
|
|
|
+ userMessage.companionId
|
|
|
|
+ );
|
|
|
|
+ if (userMessage.type !== 'text') {
|
|
|
|
+ switch (userMessage.type) {
|
|
|
|
+ case 'image':
|
|
|
|
+ await fs.unlink(path.join(DIR_IMAGES, userMessage.message));
|
|
|
|
+ break;
|
|
|
|
+ case 'audio':
|
|
|
|
+ await fs.unlink(path.join(DIR_AUDIOS, userMessage.message));
|
|
|
|
+ break;
|
|
|
|
+ case 'video':
|
|
|
|
+ await fs.unlink(path.join(DIR_VIDEOS, userMessage.message));
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ await fs.unlink(path.join(DIR_FILES, userMessage.message));
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (userMessage && companionMessage) {
|
|
|
|
+ return res.json({
|
|
|
|
+ status: 'success',
|
|
|
|
+ code: 200,
|
|
|
|
+ data: {},
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ return res.status(404).json({
|
|
|
|
+ status: 'error',
|
|
|
|
+ code: 404,
|
|
|
|
+ data: 'Not Found',
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ } catch (e) {
|
|
|
|
+ next(e);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
const listMessagesById = async (req, res, next) => {
|
|
const listMessagesById = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const userId = req.user.id;
|
|
const userId = req.user.id;
|
|
@@ -42,6 +90,7 @@ const listMessagesById = async (req, res, next) => {
|
|
const sentMessage = async (req, res, next) => {
|
|
const sentMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const { id, message } = req.body;
|
|
const { id, message } = req.body;
|
|
|
|
+ const idTime = Math.round(Date.now() / 1000);
|
|
const user = req.user;
|
|
const user = req.user;
|
|
const userId = user.id;
|
|
const userId = user.id;
|
|
const companion = await UserModel.findById(id);
|
|
const companion = await UserModel.findById(id);
|
|
@@ -58,6 +107,7 @@ const sentMessage = async (req, res, next) => {
|
|
color,
|
|
color,
|
|
number,
|
|
number,
|
|
type: 'text',
|
|
type: 'text',
|
|
|
|
+ idTime,
|
|
companionId: id,
|
|
companionId: id,
|
|
owner: userId,
|
|
owner: userId,
|
|
});
|
|
});
|
|
@@ -69,6 +119,7 @@ const sentMessage = async (req, res, next) => {
|
|
color,
|
|
color,
|
|
number,
|
|
number,
|
|
type: 'text',
|
|
type: 'text',
|
|
|
|
+ idTime,
|
|
companionId: userId,
|
|
companionId: userId,
|
|
owner: id,
|
|
owner: id,
|
|
});
|
|
});
|
|
@@ -107,11 +158,13 @@ const imageMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const userId = req.user.id;
|
|
const userId = req.user.id;
|
|
const id = req.params.companionId;
|
|
const id = req.params.companionId;
|
|
|
|
+ const idTime = Math.round(Date.now() / 1000);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const DIR_IMAGES = process.env.DIR_IMAGES;
|
|
const DIR_IMAGES = process.env.DIR_IMAGES;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameImg = req.file.originalname;
|
|
|
|
|
|
+ const originalName = req.file.originalname;
|
|
|
|
+ const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
await Jimp.read(pathToFile);
|
|
await Jimp.read(pathToFile);
|
|
await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
@@ -128,6 +181,7 @@ const imageMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type: 'image',
|
|
type: 'image',
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: id,
|
|
companionId: id,
|
|
owner: userId,
|
|
owner: userId,
|
|
});
|
|
});
|
|
@@ -140,6 +194,7 @@ const imageMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type: 'image',
|
|
type: 'image',
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: userId,
|
|
companionId: userId,
|
|
owner: id,
|
|
owner: id,
|
|
});
|
|
});
|
|
@@ -178,11 +233,13 @@ const audioMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const userId = req.user.id;
|
|
const userId = req.user.id;
|
|
const id = req.params.companionId;
|
|
const id = req.params.companionId;
|
|
|
|
+ const idTime = Math.round(Date.now() / 1000);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const DIR_AUDIOS = process.env.DIR_AUDIOS;
|
|
const DIR_AUDIOS = process.env.DIR_AUDIOS;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameAudio = req.file.originalname;
|
|
|
|
|
|
+ const originalName = req.file.originalname;
|
|
|
|
+ const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
await createFolderIsExist(path.join(DIR_AUDIOS, userId));
|
|
await createFolderIsExist(path.join(DIR_AUDIOS, userId));
|
|
await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
|
|
await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
|
|
@@ -198,6 +255,7 @@ const audioMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type: 'audio',
|
|
type: 'audio',
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: id,
|
|
companionId: id,
|
|
owner: userId,
|
|
owner: userId,
|
|
});
|
|
});
|
|
@@ -210,6 +268,7 @@ const audioMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type: 'audio',
|
|
type: 'audio',
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: userId,
|
|
companionId: userId,
|
|
owner: id,
|
|
owner: id,
|
|
});
|
|
});
|
|
@@ -248,11 +307,13 @@ const videoMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const userId = req.user.id;
|
|
const userId = req.user.id;
|
|
const id = req.params.companionId;
|
|
const id = req.params.companionId;
|
|
|
|
+ const idTime = Math.round(Date.now() / 1000);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const DIR_VIDEOS = process.env.DIR_VIDEOS;
|
|
const DIR_VIDEOS = process.env.DIR_VIDEOS;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameVideo = req.file.originalname;
|
|
|
|
|
|
+ const originalName = req.file.originalname;
|
|
|
|
+ const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
await createFolderIsExist(path.join(DIR_VIDEOS, userId));
|
|
await createFolderIsExist(path.join(DIR_VIDEOS, userId));
|
|
await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
|
|
await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
|
|
@@ -268,6 +329,7 @@ const videoMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type: 'video',
|
|
type: 'video',
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: id,
|
|
companionId: id,
|
|
owner: userId,
|
|
owner: userId,
|
|
});
|
|
});
|
|
@@ -280,6 +342,7 @@ const videoMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type: 'video',
|
|
type: 'video',
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: userId,
|
|
companionId: userId,
|
|
owner: id,
|
|
owner: id,
|
|
});
|
|
});
|
|
@@ -318,11 +381,11 @@ const fileMessage = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const userId = req.user.id;
|
|
const userId = req.user.id;
|
|
const id = req.params.companionId;
|
|
const id = req.params.companionId;
|
|
|
|
+ const idTime = Math.round(Date.now() / 1000);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isChat = await ChatModel.getByField(id, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
const DIR_FILES = process.env.DIR_FILES;
|
|
const DIR_FILES = process.env.DIR_FILES;
|
|
const pathToFile = req.file.path;
|
|
const pathToFile = req.file.path;
|
|
- const newNameFile = req.file.originalname;
|
|
|
|
const fullType = req.file.mimetype;
|
|
const fullType = req.file.mimetype;
|
|
let type;
|
|
let type;
|
|
switch (fullType) {
|
|
switch (fullType) {
|
|
@@ -338,6 +401,7 @@ const fileMessage = async (req, res, next) => {
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
+ const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
|
|
await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
|
|
await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
|
|
const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
@@ -352,6 +416,7 @@ const fileMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type,
|
|
type,
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: id,
|
|
companionId: id,
|
|
owner: userId,
|
|
owner: userId,
|
|
});
|
|
});
|
|
@@ -364,6 +429,7 @@ const fileMessage = async (req, res, next) => {
|
|
number,
|
|
number,
|
|
type,
|
|
type,
|
|
fullType,
|
|
fullType,
|
|
|
|
+ idTime,
|
|
companionId: userId,
|
|
companionId: userId,
|
|
owner: id,
|
|
owner: id,
|
|
});
|
|
});
|
|
@@ -400,6 +466,7 @@ const fileMessage = async (req, res, next) => {
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|
|
listMessages,
|
|
listMessages,
|
|
|
|
+ removeMessage,
|
|
listMessagesById,
|
|
listMessagesById,
|
|
sentMessage,
|
|
sentMessage,
|
|
imageMessage,
|
|
imageMessage,
|