forked from kaverti/website
0.176-prerelease3
This commit is contained in:
parent
4d810b0eb9
commit
3335d84de5
|
@ -55,6 +55,7 @@ exports.create = async function (userIds, name) {
|
|||
searchString of usernames given by user
|
||||
*/
|
||||
exports.getFromUser = async function (userId, page, searchString) {
|
||||
console.log(userId)
|
||||
let usernames = (searchString || '').split(/\s+/);
|
||||
let replacementsObj = {
|
||||
offset: (page || 0) * 10,
|
||||
|
|
|
@ -226,7 +226,7 @@
|
|||
},
|
||||
createConversation () {
|
||||
let userIds = this.newConversationUsers.map(user => user.id);
|
||||
userIds.push(+this.$store.state.userId);
|
||||
userIds.push(+this.$store.state.UserId);
|
||||
|
||||
//If there is no message or only one user (themselves)
|
||||
//then do not create new conversation
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
const typing = require('../socket_routes/typing');
|
||||
const conversationRooms = require('../socket_routes/conversationRooms');
|
||||
module.exports = {
|
||||
init (app, server, session) {
|
||||
let io = require('socket.io')(server)
|
||||
|
@ -30,6 +32,13 @@ module.exports = {
|
|||
socket.on('accountEvent', _ => {
|
||||
socket.disconnect()
|
||||
})
|
||||
socket.on('disconnect', () => { removeIoUser(socket); });
|
||||
|
||||
socket.on('joinConversation', data => { conversationRooms.join(data, socket) });
|
||||
socket.on('leaveConversation', data => { conversationRooms.leave(data, socket) });
|
||||
|
||||
socket.on('startTyping', data => { typing('startTyping', data, socket) });
|
||||
socket.on('stopTyping', data => { typing('stopTyping', data, socket) });
|
||||
})
|
||||
|
||||
app.set('io', io)
|
||||
|
|
|
@ -414,6 +414,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||
User.hasMany(models.Post)
|
||||
User.hasMany(models.Thread)
|
||||
User.hasMany(models.userWall)
|
||||
User.belongsToMany(models.Conversation, { through: models.UserConversation })
|
||||
User.belongsToMany(models.Ip, { through: 'UserIp' })
|
||||
},
|
||||
includeOptions (from, limit) {
|
||||
|
|
|
@ -352,13 +352,11 @@ router.all('*', (req, res, next) => {
|
|||
}
|
||||
})
|
||||
|
||||
router.get('/conversations', async (req, res, next) => {
|
||||
router.get('/:userId/conversations', async (req, res, next) => {
|
||||
try {
|
||||
let user = await User.findOne({ where: {
|
||||
username: req.session.username
|
||||
}})
|
||||
let id = +req.params.userId;
|
||||
|
||||
let conversations = await conversationController.getFromUser(user.id, +req.query.page, req.query.search);
|
||||
let conversations = await conversationController.getFromUser(id, +req.query.page, req.query.search);
|
||||
res.json(conversations);
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
const socketErrorHandler = require('../lib/socketErrorHandler');
|
||||
const validateSchema = require('../lib/validation/validateSchema');
|
||||
|
||||
const conversationController = require('../controllers/conversation');
|
||||
|
||||
/*
|
||||
data:
|
||||
{
|
||||
conversationId: integer
|
||||
}
|
||||
*/
|
||||
async function join (data, socket) {
|
||||
try {
|
||||
validateSchema({
|
||||
conversationId: {
|
||||
type: 'integer',
|
||||
required: true
|
||||
}
|
||||
}, data);
|
||||
|
||||
const { conversationId } = data;
|
||||
const { userId } = socket.request.session;
|
||||
|
||||
const conversationUsers = await conversationController.getUserIds(conversationId);
|
||||
if(!conversationUsers.includes(userId)) {
|
||||
throw new Error('unauthorised');
|
||||
}
|
||||
|
||||
socket.join('conversation/' + conversationId);
|
||||
} catch (e) {
|
||||
socketErrorHandler(e, socket);
|
||||
}
|
||||
};
|
||||
|
||||
function leave (data, socket) {
|
||||
try {
|
||||
validateSchema({
|
||||
conversationId: {
|
||||
type: 'integer',
|
||||
required: true
|
||||
}
|
||||
}, data);
|
||||
|
||||
socket.leave('conversation/' + data.conversationId)
|
||||
} catch (e) {
|
||||
socketErrorHandler(e, socket);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { join, leave };
|
|
@ -0,0 +1,37 @@
|
|||
const socketErrorHandler = require('../lib/socketErrorHandler');
|
||||
const validateSchema = require('../lib/validation/validateSchema');
|
||||
|
||||
const conversationController = require('../controllers/conversation');
|
||||
|
||||
/*
|
||||
data:
|
||||
{
|
||||
conversationId: integer
|
||||
}
|
||||
*/
|
||||
async function setTyping (event, data, socket) {
|
||||
try {
|
||||
validateSchema({
|
||||
conversationId: {
|
||||
type: 'integer',
|
||||
required: true
|
||||
}
|
||||
}, data);
|
||||
|
||||
const { conversationId } = data;
|
||||
const { userId } = socket.request.session;
|
||||
|
||||
const conversationUsers = await conversationController.getUserIds(conversationId);
|
||||
if(!conversationUsers.includes(userId)) {
|
||||
throw new Error('unauthorised');
|
||||
}
|
||||
|
||||
socket
|
||||
.to('conversation/' + conversationId)
|
||||
.emit(event, { userId });
|
||||
} catch (e) {
|
||||
socketErrorHandler(e, socket);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = setTyping;
|
Loading…
Reference in New Issue