-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user-chat): Enable users to chat
- Loading branch information
1 parent
cc1cacd
commit cbab7f6
Showing
11 changed files
with
436 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import sequelize from 'sequelize'; | ||
import db from '../../sequelize/models'; | ||
|
||
const { Chat, User, follows } = db; | ||
const { Op } = sequelize; | ||
/** | ||
* @author Rukundo Eric | ||
* @class chatController | ||
* @description this class performs chat | ||
*/ | ||
class chatController { | ||
/** | ||
* | ||
* @param {Object} req - Request object | ||
* @param {Object} res - Response object | ||
* @return {Object} - Response object | ||
*/ | ||
static async getUsers(req, res) { | ||
const { id } = req.user; | ||
follows | ||
.findAll({ | ||
where: { | ||
[Op.or]: [{ userId: id }, { followerId: id }] | ||
}, | ||
include: [ | ||
{ | ||
model: User, | ||
as: 'follower', | ||
attributes: ['id', 'firstName', 'lastName', 'email', 'username'] | ||
}, | ||
{ | ||
model: User, | ||
as: 'followedUser', | ||
attributes: ['id', 'firstName', 'lastName', 'email', 'username'] | ||
} | ||
] | ||
}) | ||
.then((data) => { | ||
if (!data[0]) { | ||
return res.status(200).json({ error: 'you do not have any followers currently' }); | ||
} | ||
return res.status(200).json({ followers: data, me: req.user }); | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param {Object} req - Request object | ||
* @param {Object} res - Response object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async getMessages(req, res) { | ||
const myId = req.user.id; | ||
const { username } = req.params; | ||
const { dataValues: { id } } = await User.findOne({ where: { username } }); | ||
Chat.findAll({ | ||
where: { | ||
[Op.or]: [{ | ||
senderId: myId, | ||
recieverId: id | ||
}, | ||
{ | ||
senderId: id, | ||
recieverId: myId | ||
}] | ||
}, | ||
include: [ | ||
{ | ||
model: User, | ||
as: 'sender', | ||
attributes: ['id', 'firstName', 'lastName', 'email', 'username'] | ||
}, | ||
{ | ||
model: User, | ||
as: 'receiver', | ||
attributes: ['id', 'firstName', 'lastName', 'email', 'username'] | ||
} | ||
] | ||
}).then((messages) => { | ||
res.status(200).json({ | ||
messages | ||
}); | ||
}); | ||
} | ||
} | ||
export default chatController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Router } from 'express'; | ||
import chat from '../controllers/chatController'; | ||
import auth from '../../middleware/auth'; | ||
|
||
const { getUsers, getMessages } = chat; | ||
const { verifyToken } = auth; | ||
const chatRouter = Router(); | ||
|
||
chatRouter.get('/users', verifyToken, getUsers); | ||
chatRouter.get('/:username', verifyToken, getMessages); | ||
|
||
export default chatRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import db from '../../sequelize/models'; | ||
import findUser from '../FindUser'; | ||
|
||
const { Chat } = db; | ||
|
||
/** | ||
* @author Elie Mugenzi | ||
* @class ChatHelper | ||
* @description this class performs the whole authentication | ||
*/ | ||
class ChatHelper { | ||
/** | ||
* | ||
* @param {Object} message - Request object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async saveMessage(message) { | ||
const { | ||
sender, receiver, message: chatMessage, read | ||
} = message; | ||
const { id: senderId } = await findUser(sender); | ||
const infos = await findUser(receiver); | ||
|
||
const newChat = await Chat.create({ | ||
senderId, | ||
recieverId: infos.id, | ||
message: chatMessage, | ||
read | ||
}); | ||
return newChat; | ||
} | ||
|
||
/** | ||
* | ||
* @param {String} username - Request object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async updateReadMessages(username) { | ||
const { id } = await findUser(username); | ||
const result = await Chat.update({ | ||
read: true, | ||
}, { | ||
where: { | ||
recieverId: id, | ||
read: false, | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
/** | ||
* | ||
* @param {String} username - Request object | ||
* @returns {Number} - Response object | ||
*/ | ||
static async getUnreadMessageCount(username) { | ||
const { id } = await findUser(username); | ||
const result = await Chat.count({ | ||
where: { recieverId: id, read: false } | ||
}); | ||
return result; | ||
} | ||
} | ||
|
||
|
||
export default ChatHelper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Chats', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
senderId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
message: { | ||
type: Sequelize.STRING | ||
}, | ||
recieverId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
read: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Chats') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable func-names */ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Chat = sequelize.define('Chat', { | ||
senderId: DataTypes.INTEGER, | ||
message: DataTypes.STRING, | ||
recieverId: DataTypes.INTEGER, | ||
read: DataTypes.BOOLEAN | ||
}, {}); | ||
Chat.associate = function (models) { | ||
// associations can be defined here | ||
Chat.belongsTo(models.User, { | ||
as: 'sender', | ||
foreignKey: 'senderId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
Chat.belongsTo(models.User, { | ||
as: 'receiver', | ||
foreignKey: 'recieverId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}); | ||
}; | ||
return Chat; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('follows', [{ | ||
userId: 5, | ||
followerId: 10, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
userId: 10, | ||
followerId: 5, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
userId: 3, | ||
followerId: 5, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
} | ||
], {}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Chats', [{ | ||
senderId: 10, | ||
recieverId: 5, | ||
message: 'hello tyhere', | ||
read: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
senderId: 5, | ||
recieverId: 10, | ||
message: 'How are you?', | ||
read: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
{ | ||
senderId: 10, | ||
recieverId: 5, | ||
message: 'Im fine', | ||
read: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
} | ||
], {}), | ||
}; |
Oops, something went wrong.