Skip to content

Commit 702fc48

Browse files
committed
refactor(realtime): extract "SocketClient" to separate class "RealtimeClientConnection"
Signed-off-by: BoHong Li <[email protected]>
1 parent f317171 commit 702fc48

File tree

4 files changed

+260
-239
lines changed

4 files changed

+260
-239
lines changed

lib/realtime.js

Lines changed: 6 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var models = require('./models')
2121
// ot
2222
var ot = require('./ot')
2323

24+
const {RealtimeClientConnection} = require('./realtimeClientConnection')
25+
2426
// public
2527
var realtime = {
2628
io: null,
@@ -780,239 +782,6 @@ function getNoteFromNotePool (noteId) {
780782
return notes[noteId]
781783
}
782784

783-
class SocketClient {
784-
constructor (socket) {
785-
this.socket = socket
786-
}
787-
788-
registerEventHandler () {
789-
// received client refresh request
790-
this.socket.on('refresh', this.refreshEventHandler.bind(this))
791-
// received user status
792-
this.socket.on('user status', this.userStatusEventHandler.bind(this))
793-
// when a new client disconnect
794-
this.socket.on('disconnect', this.disconnectEventHandler.bind(this))
795-
// received cursor focus
796-
this.socket.on('cursor focus', this.cursorFocusEventHandler.bind(this))
797-
// received cursor activity
798-
this.socket.on('cursor activity', this.cursorActivityEventHandler.bind(this))
799-
// received cursor blur
800-
this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this))
801-
// check version
802-
this.socket.on('version', this.checkVersionEventHandler.bind(this))
803-
// received sync of online users request
804-
this.socket.on('online users', this.onlineUsersEventHandler.bind(this))
805-
// reveiced when user logout or changed
806-
this.socket.on('user changed', this.userChangedEventHandler.bind(this))
807-
// delete a note
808-
this.socket.on('delete', this.deleteNoteEventHandler.bind(this))
809-
// received note permission change request
810-
this.socket.on('permission', this.permissionChangeEventHandler.bind(this))
811-
}
812-
813-
isUserLoggedIn () {
814-
return this.socket.request.user && this.socket.request.user.logged_in
815-
}
816-
817-
isNoteAndUserExists () {
818-
const note = getNoteFromNotePool(this.socket.noteId)
819-
const user = getUserFromUserPool(this.socket.id)
820-
return note && user
821-
}
822-
823-
isNoteOwner () {
824-
const note = this.getCurrentNote()
825-
return get(note, 'owner') === this.getCurrentLoggedInUserId()
826-
}
827-
828-
isAnonymousEnable () {
829-
//TODO: move this method to config module
830-
return config.allowAnonymous || config.allowAnonymousEdits
831-
}
832-
833-
disconnectSocketOnNote (note) {
834-
note.socks.forEach((sock) => {
835-
if (sock) {
836-
sock.emit('delete')
837-
setImmediate(() => {
838-
sock.disconnect(true)
839-
})
840-
}
841-
})
842-
}
843-
844-
getCurrentUser () {
845-
if (!this.socket.id) return
846-
return getUserFromUserPool(this.socket.id)
847-
}
848-
849-
getCurrentLoggedInUserId () {
850-
return get(this.socket, 'request.user.id')
851-
}
852-
853-
getCurrentNote () {
854-
if (!this.socket.noteId) return
855-
return getNoteFromNotePool(this.socket.noteId)
856-
}
857-
858-
getNoteChannel () {
859-
return this.socket.broadcast.to(this.socket.noteId)
860-
}
861-
862-
async destroyNote (id) {
863-
return models.Note.destroy({
864-
where: { id: id }
865-
})
866-
}
867-
868-
async changeNotePermission (newPermission) {
869-
const changedRows = await models.Note.update({
870-
permission: newPermission
871-
}, {
872-
where: {
873-
id: this.getCurrentNote().id
874-
}
875-
})
876-
if (changedRows !== 1) {
877-
throw new Error(`update database failed, cannot set permission ${newPermission} to note ${this.getCurrentNote().id}`)
878-
}
879-
}
880-
881-
notifyPermissionChanged () {
882-
realtime.io.to(this.getCurrentNote().id).emit('permission', {
883-
permission: this.getCurrentNote().permission
884-
})
885-
this.getCurrentNote().socks.forEach((sock) => {
886-
if (sock) {
887-
if (!exports.checkViewPermission(sock.request, this.getCurrentNote())) {
888-
sock.emit('info', {
889-
code: 403
890-
})
891-
setTimeout(function () {
892-
sock.disconnect(true)
893-
}, 0)
894-
}
895-
}
896-
})
897-
}
898-
899-
refreshEventHandler () {
900-
exports.emitRefresh(this.socket)
901-
}
902-
903-
checkVersionEventHandler () {
904-
this.socket.emit('version', {
905-
version: config.fullversion,
906-
minimumCompatibleVersion: config.minimumCompatibleVersion
907-
})
908-
}
909-
910-
userStatusEventHandler (data) {
911-
if (!this.isNoteAndUserExists()) return
912-
const user = this.getCurrentUser()
913-
if (config.debug) {
914-
logger.info('SERVER received [' + this.socket.noteId + '] user status from [' + this.socket.id + ']: ' + JSON.stringify(data))
915-
}
916-
if (data) {
917-
user.idle = data.idle
918-
user.type = data.type
919-
}
920-
exports.emitUserStatus(this.socket)
921-
}
922-
923-
userChangedEventHandler () {
924-
logger.info('user changed')
925-
926-
const note = this.getCurrentNote()
927-
if (!note) return
928-
const user = note.users[this.socket.id]
929-
if (!user) return
930-
931-
exports.updateUserData(this.socket, user)
932-
exports.emitOnlineUsers(this.socket)
933-
}
934-
935-
onlineUsersEventHandler () {
936-
if (!this.isNoteAndUserExists()) return
937-
938-
const currentNote = this.getCurrentNote()
939-
940-
const currentNoteOnlineUserList = Object.keys(currentNote.users)
941-
.map(key => buildUserOutData(currentNote.users[key]))
942-
943-
this.socket.emit('online users', {
944-
users: currentNoteOnlineUserList
945-
})
946-
}
947-
948-
cursorFocusEventHandler (data) {
949-
if (!this.isNoteAndUserExists()) return
950-
const user = this.getCurrentUser()
951-
user.cursor = data
952-
const out = buildUserOutData(user)
953-
this.getNoteChannel().emit('cursor focus', out)
954-
}
955-
956-
cursorActivityEventHandler (data) {
957-
if (!this.isNoteAndUserExists()) return
958-
const user = this.getCurrentUser()
959-
user.cursor = data
960-
const out = buildUserOutData(user)
961-
this.getNoteChannel().emit('cursor activity', out)
962-
}
963-
964-
cursorBlurEventHandlder () {
965-
if (!this.isNoteAndUserExists()) return
966-
const user = this.getCurrentUser()
967-
user.cursor = null
968-
this.getNoteChannel().emit('cursor blur', {
969-
id: this.socket.id
970-
})
971-
}
972-
973-
deleteNoteEventHandler () {
974-
// need login to do more actions
975-
if (this.isUserLoggedIn() && this.isNoteAndUserExists()) {
976-
const note = this.getCurrentNote()
977-
// Only owner can delete note
978-
if (note.owner && note.owner === this.getCurrentLoggedInUserId()) {
979-
this.destroyNote(note.id)
980-
.then((successRows) => {
981-
if (!successRows) return
982-
this.disconnectSocketOnNote(note)
983-
})
984-
.catch(function (err) {
985-
return logger.error('delete note failed: ' + err)
986-
})
987-
}
988-
}
989-
}
990-
991-
permissionChangeEventHandler (permission) {
992-
if (!this.isUserLoggedIn()) return
993-
if (!this.isNoteAndUserExists()) return
994-
995-
const note = this.getCurrentNote()
996-
// Only owner can change permission
997-
if (!this.isNoteOwner()) return
998-
if (!this.isAnonymousEnable() && permission === 'freely') return
999-
1000-
this.changeNotePermission(permission)
1001-
.then(() => {
1002-
console.log('---')
1003-
note.permission = permission
1004-
this.notifyPermissionChanged()
1005-
})
1006-
.catch(err => logger.error('update note permission failed: ' + err))
1007-
}
1008-
1009-
disconnectEventHandler () {
1010-
if (isDuplicatedInSocketQueue(disconnectSocketQueue, this.socket)) return
1011-
disconnectSocketQueue.push(this.socket)
1012-
exports.disconnect(this.socket)
1013-
}
1014-
}
1015-
1016785
function connection (socket) {
1017786
if (realtime.maintenance) return
1018787
exports.parseNoteIdFromSocket(socket, function (err, noteId) {
@@ -1068,7 +837,7 @@ function connection (socket) {
1068837
exports.startConnection(socket)
1069838
})
1070839

1071-
const socketClient = new SocketClient(socket)
840+
const socketClient = new RealtimeClientConnection(socket)
1072841
socketClient.registerEventHandler()
1073842
}
1074843

@@ -1086,6 +855,9 @@ exports.emitUserStatus = emitUserStatus
1086855
exports.disconnect = disconnect
1087856
exports.emitOnlineUsers = emitOnlineUsers
1088857
exports.checkViewPermission = checkViewPermission
858+
exports.getNoteFromNotePool = getNoteFromNotePool
859+
exports.getUserFromUserPool = getUserFromUserPool
860+
exports.buildUserOutData = buildUserOutData
1089861
exports.notes = notes
1090862
exports.users = users
1091863
exports.disconnectSocketQueue = disconnectSocketQueue

0 commit comments

Comments
 (0)