mirror of
https://github.com/status-im/codimd.git
synced 2025-01-11 18:04:11 +00:00
refactor(realtime: extract user event "delete" to SocketClient
Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
parent
79666aeec3
commit
968c12ff6c
129
lib/realtime.js
129
lib/realtime.js
@ -49,9 +49,11 @@ function secure (socket, next) {
|
||||
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie)
|
||||
handshakeData.sessionID = cookieParser.signedCookie(handshakeData.cookie[config.sessionName], config.sessionSecret)
|
||||
if (handshakeData.sessionID &&
|
||||
handshakeData.cookie[config.sessionName] &&
|
||||
handshakeData.cookie[config.sessionName] !== handshakeData.sessionID) {
|
||||
if (config.debug) { logger.info('AUTH success cookie: ' + handshakeData.sessionID) }
|
||||
handshakeData.cookie[config.sessionName] &&
|
||||
handshakeData.cookie[config.sessionName] !== handshakeData.sessionID) {
|
||||
if (config.debug) {
|
||||
logger.info('AUTH success cookie: ' + handshakeData.sessionID)
|
||||
}
|
||||
return next()
|
||||
} else {
|
||||
next(new Error('AUTH failed: Cookie is invalid.'))
|
||||
@ -184,7 +186,9 @@ setInterval(function () {
|
||||
var socket = realtime.io.sockets.connected[key]
|
||||
if ((!socket && users[key]) ||
|
||||
(socket && (!socket.rooms || socket.rooms.length <= 0))) {
|
||||
if (config.debug) { logger.info('cleaner found redundant user: ' + key) }
|
||||
if (config.debug) {
|
||||
logger.info('cleaner found redundant user: ' + key)
|
||||
}
|
||||
if (!socket) {
|
||||
socket = {
|
||||
id: key
|
||||
@ -337,7 +341,9 @@ function emitOnlineUsers (socket) {
|
||||
var users = []
|
||||
Object.keys(notes[noteId].users).forEach(function (key) {
|
||||
var user = notes[noteId].users[key]
|
||||
if (user) { users.push(buildUserOutData(user)) }
|
||||
if (user) {
|
||||
users.push(buildUserOutData(user))
|
||||
}
|
||||
})
|
||||
var out = {
|
||||
users: users
|
||||
@ -403,15 +409,27 @@ function connectNextSocket () {
|
||||
function interruptConnection (socket, noteId, socketId) {
|
||||
if (notes[noteId]) delete notes[noteId]
|
||||
if (users[socketId]) delete users[socketId]
|
||||
if (socket) { clearSocketQueue(connectionSocketQueue, socket) } else { connectionSocketQueue.shift() }
|
||||
if (socket) {
|
||||
clearSocketQueue(connectionSocketQueue, socket)
|
||||
} else {
|
||||
connectionSocketQueue.shift()
|
||||
}
|
||||
connectNextSocket()
|
||||
}
|
||||
|
||||
function checkViewPermission (req, note) {
|
||||
if (note.permission === 'private') {
|
||||
if (req.user && req.user.logged_in && req.user.id === note.owner) { return true } else { return false }
|
||||
if (req.user && req.user.logged_in && req.user.id === note.owner) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
||||
if (req.user && req.user.logged_in) { return true } else { return false }
|
||||
if (req.user && req.user.logged_in) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
@ -620,7 +638,9 @@ function disconnect (socket) {
|
||||
clearSocketQueue(disconnectSocketQueue, socket)
|
||||
// seek for next socket
|
||||
isDisconnectBusy = false
|
||||
if (disconnectSocketQueue.length > 0) { disconnect(disconnectSocketQueue[0]) }
|
||||
if (disconnectSocketQueue.length > 0) {
|
||||
disconnect(disconnectSocketQueue[0])
|
||||
}
|
||||
|
||||
if (config.debug) {
|
||||
// logger.info(notes);
|
||||
@ -669,13 +689,20 @@ function ifMayEdit (socket, callback) {
|
||||
case 'freely':
|
||||
// not blocking anyone
|
||||
break
|
||||
case 'editable': case 'limited':
|
||||
case 'editable':
|
||||
case 'limited':
|
||||
// only login user can change
|
||||
if (!socket.request.user || !socket.request.user.logged_in) { mayEdit = false }
|
||||
if (!socket.request.user || !socket.request.user.logged_in) {
|
||||
mayEdit = false
|
||||
}
|
||||
break
|
||||
case 'locked': case 'private': case 'protected':
|
||||
case 'locked':
|
||||
case 'private':
|
||||
case 'protected':
|
||||
// only owner can change
|
||||
if (!note.owner || note.owner !== socket.request.user.id) { mayEdit = false }
|
||||
if (!note.owner || note.owner !== socket.request.user.id) {
|
||||
mayEdit = false
|
||||
}
|
||||
break
|
||||
}
|
||||
// if user may edit and this is a text operation
|
||||
@ -777,6 +804,51 @@ class SocketClient {
|
||||
this.socket.on('online users', this.onlineUsersEventHandler.bind(this))
|
||||
// reveiced when user logout or changed
|
||||
this.socket.on('user changed', this.userChangedEventHandler.bind(this))
|
||||
// delete a note
|
||||
this.socket.on('delete', this.deleteNote.bind(this))
|
||||
}
|
||||
|
||||
isUserLoggedIn () {
|
||||
return this.socket.request.user && this.socket.request.user.logged_in
|
||||
}
|
||||
|
||||
getCurrentLoggedInUserId () {
|
||||
return get(this.socket, 'request.user.id')
|
||||
}
|
||||
|
||||
disconnectSocketOnNote (note) {
|
||||
note.socks.forEach((sock) => {
|
||||
if (sock) {
|
||||
sock.emit('delete')
|
||||
setImmediate(() => {
|
||||
sock.disconnect(true)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async destroyNote (id) {
|
||||
return models.Note.destroy({
|
||||
where: { id: id }
|
||||
})
|
||||
}
|
||||
|
||||
deleteNote () {
|
||||
// need login to do more actions
|
||||
if (this.isUserLoggedIn() && this.isNoteAndUserExists()) {
|
||||
const note = this.getCurrentNote()
|
||||
// Only owner can delete note
|
||||
if (note.owner && note.owner === this.getCurrentLoggedInUserId()) {
|
||||
this.destroyNote(note.id)
|
||||
.then((successRows) => {
|
||||
if (!successRows) return
|
||||
this.disconnectSocketOnNote(note)
|
||||
})
|
||||
.catch(function (err) {
|
||||
return logger.error('delete note failed: ' + err)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
userChangedEventHandler () {
|
||||
@ -983,37 +1055,6 @@ function connection (socket) {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// delete a note
|
||||
socket.on('delete', function () {
|
||||
// need login to do more actions
|
||||
if (socket.request.user && socket.request.user.logged_in) {
|
||||
var noteId = socket.noteId
|
||||
if (!noteId || !notes[noteId]) return
|
||||
var note = notes[noteId]
|
||||
// Only owner can delete note
|
||||
if (note.owner && note.owner === socket.request.user.id) {
|
||||
models.Note.destroy({
|
||||
where: {
|
||||
id: noteId
|
||||
}
|
||||
}).then(function (count) {
|
||||
if (!count) return
|
||||
for (var i = 0, l = note.socks.length; i < l; i++) {
|
||||
var sock = note.socks[i]
|
||||
if (typeof sock !== 'undefined' && sock) {
|
||||
sock.emit('delete')
|
||||
setTimeout(function () {
|
||||
sock.disconnect(true)
|
||||
}, 0)
|
||||
}
|
||||
}
|
||||
}).catch(function (err) {
|
||||
return logger.error('delete note failed: ' + err)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
exports = module.exports = realtime
|
||||
|
@ -267,4 +267,120 @@ describe('realtime#socket event', function () {
|
||||
assert(emitOnlineUsersStub.called === false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('delete', function () {
|
||||
it('should delete note when owner request', function (done) {
|
||||
const currentUserId = 'user1_id'
|
||||
const noteOwnerId = 'user1_id'
|
||||
const otherClient = makeMockSocket()
|
||||
clientSocket.request = {
|
||||
user: {
|
||||
logged_in: true,
|
||||
id: currentUserId
|
||||
}
|
||||
}
|
||||
realtime.notes[noteId] = {
|
||||
owner: noteOwnerId,
|
||||
socks: [clientSocket, undefined, otherClient]
|
||||
}
|
||||
const deleteFunc = eventFuncMap.get('delete')
|
||||
deleteFunc()
|
||||
setTimeout(() => {
|
||||
assert(otherClient.disconnect.calledOnce)
|
||||
assert(otherClient.emit.calledOnce)
|
||||
assert(otherClient.emit.lastCall.args[0] === 'delete')
|
||||
assert(clientSocket.disconnect.calledOnce)
|
||||
assert(clientSocket.emit.calledOnce)
|
||||
assert(clientSocket.emit.lastCall.args[0] === 'delete')
|
||||
assert(modelsMock.Note.destroy.calledOnce)
|
||||
done()
|
||||
}, 10)
|
||||
})
|
||||
|
||||
it('should not do anything when user not login', function (done) {
|
||||
const noteOwnerId = 'user1_id'
|
||||
clientSocket.request = {}
|
||||
realtime.notes[noteId] = {
|
||||
owner: noteOwnerId,
|
||||
socks: [clientSocket]
|
||||
}
|
||||
const deleteFunc = eventFuncMap.get('delete')
|
||||
deleteFunc()
|
||||
setTimeout(() => {
|
||||
assert(modelsMock.Note.destroy.called === false)
|
||||
assert(clientSocket.disconnect.called === false)
|
||||
done()
|
||||
}, 10)
|
||||
})
|
||||
|
||||
it('should not do anything when note not exists', function (done) {
|
||||
const currentUserId = 'user1_id'
|
||||
clientSocket.request = {
|
||||
user: {
|
||||
logged_in: true,
|
||||
id: currentUserId
|
||||
}
|
||||
}
|
||||
const deleteFunc = eventFuncMap.get('delete')
|
||||
deleteFunc()
|
||||
setTimeout(() => {
|
||||
assert(modelsMock.Note.destroy.called === false)
|
||||
assert(clientSocket.disconnect.called === false)
|
||||
done()
|
||||
}, 10)
|
||||
})
|
||||
|
||||
it('should not do anything when note owner is not me', function (done) {
|
||||
const currentUserId = 'user1_id'
|
||||
const noteOwnerId = 'user2_id'
|
||||
const otherClient = makeMockSocket()
|
||||
clientSocket.request = {
|
||||
user: {
|
||||
logged_in: true,
|
||||
id: currentUserId
|
||||
}
|
||||
}
|
||||
realtime.notes[noteId] = {
|
||||
owner: noteOwnerId,
|
||||
socks: [clientSocket, otherClient]
|
||||
}
|
||||
const deleteFunc = eventFuncMap.get('delete')
|
||||
deleteFunc()
|
||||
setTimeout(() => {
|
||||
assert(clientSocket.disconnect.called === false)
|
||||
assert(modelsMock.Note.destroy.called === false)
|
||||
done()
|
||||
}, 10)
|
||||
})
|
||||
|
||||
it('should not do anything when note destroy fail', function (done) {
|
||||
const currentUserId = 'user1_id'
|
||||
const noteOwnerId = 'user1_id'
|
||||
modelsMock.Note.destroy.withArgs({
|
||||
where: {
|
||||
id: noteId
|
||||
}
|
||||
}).returns(Promise.resolve(0))
|
||||
|
||||
const otherClient = makeMockSocket()
|
||||
clientSocket.request = {
|
||||
user: {
|
||||
logged_in: true,
|
||||
id: currentUserId
|
||||
}
|
||||
}
|
||||
realtime.notes[noteId] = {
|
||||
id: noteId,
|
||||
owner: noteOwnerId,
|
||||
socks: [clientSocket, otherClient]
|
||||
}
|
||||
const deleteFunc = eventFuncMap.get('delete')
|
||||
deleteFunc()
|
||||
setTimeout(() => {
|
||||
assert(modelsMock.Note.destroy.calledOnce)
|
||||
assert(clientSocket.disconnect.called === false)
|
||||
done()
|
||||
}, 10)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user