Skip to content

Commit

Permalink
Merge pull request #139 from Hassan950/dev
Browse files Browse the repository at this point in the history
Chat feat (#138)
  • Loading branch information
Hassan950 authored Jun 9, 2020
2 parents a48a0f4 + 73150d4 commit 80c2eef
Show file tree
Hide file tree
Showing 21 changed files with 638 additions and 16 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"moment": "^2.24.0",
"mongoose": "^5.7.7",
"mongoose-fuzzy-searching": "^1.3.1",
"mongoose-lean-defaults": "^0.4.1",
"mongoose-lean-virtuals": "^0.6.1",
"mongoose-type-url": "^1.0.6",
"morgan": "^1.9.1",
Expand Down
42 changes: 42 additions & 0 deletions src/controllers/chat.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { chatService } = require('../services');
const AppError = require('../utils/AppError');
const httpStatus = require('http-status');

exports.getChat = async (req, res, next) => {
const result = await chatService.getChat(req.user._id, req.query);
if (result instanceof AppError) return next(result);
res.status(httpStatus.OK).json({
items: result.data,
offset: req.query.offset,
limit: req.query.limit,
total: result.total
});
};

exports.getThread = async (req, res, next) => {
const result = await chatService.getThread(req.user._id, req.params.id, req.query);
if (result instanceof AppError) return next(result);
res.status(httpStatus.OK).json({
result
});
};

exports.sendMessage = async (req, res, next) => {
const result = await chatService.sendMessage(
req.user._id,
req.params.id,
req.body.message
);
if (result instanceof AppError) return next(result);
res.sendStatus(httpStatus.CREATED);
};

exports.deleteMessage = async (req, res, next) => {
const result = await chatService.deleteMessage(
req.user._id,
req.params.id,
req.params.messageId
);
if (result instanceof AppError) return next(result);
res.sendStatus(httpStatus.NO_CONTENT);
};
3 changes: 2 additions & 1 deletion src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ exports.genreController = require('./genre.controller');
exports.followController = require('./follow.controller');
exports.searchController = require('./search.controller');
exports.premiumController = require('./premium.controller');
exports.commentController = require('./comment.controller');
exports.commentController = require('./comment.controller');
exports.chatController = require('./chat.controller');
1 change: 1 addition & 0 deletions src/models/ad.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const adSchema = new mongoose.Schema(
}
);

/* istanbul ignore next */
adSchema.virtual('type').get(function () {
return 'ad';
});
Expand Down
2 changes: 2 additions & 0 deletions src/models/album.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ const albumSchema = new mongoose.Schema(
}
);

/* istanbul ignore next */
albumSchema.pre('save', async function(next) {
this.album_group = this.album_type;
next();
});

/* istanbul ignore next */
albumSchema.virtual('type').get(function() {
return 'album';
});
Expand Down
1 change: 1 addition & 0 deletions src/models/category.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const categorySchema = new mongoose.Schema(
}
);

/* istanbul ignore next */
categorySchema.virtual('type').get(function() {
return 'category';
},
Expand Down
77 changes: 77 additions & 0 deletions src/models/chat.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const mongoose = require('mongoose');
const mongooseLeanDefaults = require('mongoose-lean-defaults')

const messageSchema = new mongoose.Schema(
{
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
message: {
type: String,
trim: true,
required: true
}
},
{
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
},
timestamps: { createdAt: 'sentAt' }
}
);

const threadSchema = new mongoose.Schema(
{
from: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
to: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
read: {
type: Boolean,
default: false,
},
messages: [messageSchema]
},
{
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
},
timestamps: { updatedAt: 'updatedAt' }
}
);

/* istanbul ignore next */
threadSchema.virtual('type').get(
function() {
return 'message';
},
{
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
}
}
);

threadSchema.index({ from: 1, to: 1 }, { unique: true });
threadSchema.plugin(mongooseLeanDefaults);
const Message = mongoose.model('Message', messageSchema);
const Thread = mongoose.model('Thread', threadSchema);

module.exports = { Thread, threadSchema, Message, messageSchema };
1 change: 1 addition & 0 deletions src/models/genre.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const genreSchema = new mongoose.Schema(
}
);

/* istanbul ignore next */
genreSchema.virtual('type').get(function() {
return 'genre';
});
Expand Down
28 changes: 24 additions & 4 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const { Category, categorySchema } = require('./category.model');
const { Playlist, playlistSchema } = require('./playlist.model');
const { Genre, genreSchema } = require('./genre.model');
const { Device, deviceSchema } = require('./device.model');
const { likedTracks, likedTracksSchema, likedAlbums, likedAlbumsSchema } = require('./library.model');
const {
likedTracks,
likedTracksSchema,
likedAlbums,
likedAlbumsSchema
} = require('./library.model');
const { Player, playerSchema } = require('./player.model');
const { PlayHistory, playHistorySchema } = require('./playHistory.model');
const { Queue, queueSchema } = require('./queue.model');
Expand All @@ -17,11 +22,22 @@ const {
playlistFollowingsSchema,
PlaylistFollowings
} = require('./follow.model');
const { Recent,recentSchema } = require('./recent.model');
const { Recent, recentSchema } = require('./recent.model');
const { Request, requestSchema } = require('./request.model');
const { Coupon, couponSchema } = require('./coupon.model');
const { Ad, adSchema } = require('./ad.model');
const { PlaylistComments, playlistCommentsSchema, AlbumComments, albumCommentsSchema } = require('./comments.model');
const {
PlaylistComments,
playlistCommentsSchema,
AlbumComments,
albumCommentsSchema
} = require('./comments.model');
const {
Thread,
threadSchema,
Message,
messageSchema
} = require('./chat.model');

module.exports = {
User,
Expand Down Expand Up @@ -69,5 +85,9 @@ module.exports = {
playlistCommentsSchema,
PlaylistComments,
albumCommentsSchema,
AlbumComments
AlbumComments,
Thread,
threadSchema,
Message,
messageSchema
};
1 change: 1 addition & 0 deletions src/models/playHistory.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const playHistorySchema = mongoose.Schema({
playHistorySchema.index({ user: 1, playedAt: -1 });
playHistorySchema.index({ user: 1, "context.type": 1, "context.item": 1 }, { unique: 1 });

/* istanbul ignore next */
playHistorySchema.virtual('context.id').get(function () {
if (this.context.item && this.context.item._id)
return this.context.item._id;
Expand Down
1 change: 1 addition & 0 deletions src/models/search.plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore next */
module.exports = function (schema, options) {
schema.statics = {
...schema.statics,
Expand Down
5 changes: 5 additions & 0 deletions src/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ const userSchema = mongoose.Schema(
}
);

/* istanbul ignore next */
userSchema.pre('save', async function(next) {
if (!this.password || !this.isModified('password')) return next();

Expand All @@ -162,18 +163,21 @@ userSchema.pre('save', async function(next) {
next();
});

/* istanbul ignore next */
userSchema.pre('save', function(next) {
if (!this.isModified('password') || this.isNew) return next();

this.passwordChangedAt = Date.now();
next();
});

/* istanbul ignore next */
userSchema.pre('save', function(next) {
if (this.isNew) this.newUser = true; // if the user is new make newUser to true
next();
});

/* istanbul ignore next */
userSchema.post('save', async function(doc) {
if (doc.newUser) {
const { Player } = require('../models/player.model');
Expand Down Expand Up @@ -201,6 +205,7 @@ userSchema.post('save', async function(doc) {
}
});

/* istanbul ignore next */
userSchema.methods.changedPasswordAfter = function(user, JWTTimestamp) {
if (this.passwordChangedAt) {
const changedTimestamp = parseInt(
Expand Down
28 changes: 28 additions & 0 deletions src/routes/v1/chat.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require('express');
const validate = require('../../middlewares/validate');
const { chatValidation } = require('../../validations');
const catchAsync = require('../../utils/catchAsync');
const { chatController } = require('../../controllers');

const router = express.Router();

router
.route('/')
.get(validate(chatValidation.getChat), catchAsync(chatController.getChat));

router
.route('/:id')
.get(validate(chatValidation.getThread), catchAsync(chatController.getThread))
.post(
validate(chatValidation.sendMessage),
catchAsync(chatController.sendMessage)
);

router
.route('/:id/:messageId')
.delete(
validate(chatValidation.deleteMessage),
catchAsync(chatController.deleteMessage)
);

module.exports = router;
26 changes: 16 additions & 10 deletions src/routes/v1/userMe.route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const { authController , userController } = require('../../controllers');
const { authController, userController } = require('../../controllers');
const authMiddleware = require('../../middlewares/auth');
const validate = require('../../middlewares/validate');
const { authValidation, userValidation } = require('../../validations');
Expand All @@ -12,10 +12,10 @@ const artistRoute = require('./artist.route');
const premiumRouter = require('./premium.route');
const playlistRouter = require('./playlist.route');
const searchRouter = require('./search.route');
const chatRoute = require('./chat.route');

const router = express.Router();


// /me/artist router
router.use('/artists', artistRoute);

Expand All @@ -30,23 +30,27 @@ router.use('/albums', libraryRouter);
// /me/playlists router
router.use('/playlists', playlistRouter);
// /me/recentSearch
router.use('/search',searchRouter);
router.use('/search', searchRouter);
// /me/artist router
router.use('/artists', artistRoute);

// /me/chat router
router.use('/chat', chatRoute);

// /me/notifications
router.route('/notifications').put(
validate(userValidation.notification),
catchAsync(userController.setToken)
)
router
.route('/notifications')
.put(
validate(userValidation.notification),
catchAsync(userController.setToken)
);

// /me/queue
router.use('/queue', queueRouter);

// /me/premium
router.use('/premium', premiumRouter);


router
.route('/updatePassword')
.patch(
Expand All @@ -71,7 +75,9 @@ router

router
.route('/privateSession')
.put(validate(userValidation.setPrivateSession), catchAsync(userController.setPrivateSession));

.put(
validate(userValidation.setPrivateSession),
catchAsync(userController.setPrivateSession)
);

module.exports = router;
Loading

0 comments on commit 80c2eef

Please sign in to comment.