44const cookie = require ( 'cookie' )
55const cookieParser = require ( 'cookie-parser' )
66const url = require ( 'url' )
7- const async = require ( 'async' )
87const randomcolor = require ( 'randomcolor' )
98const Chance = require ( 'chance' )
109const chance = new Chance ( )
@@ -44,14 +43,17 @@ const updateDirtyNoteJob = new UpdateDirtyNoteJob(realtime)
4443const cleanDanglingUserJob = new CleanDanglingUserJob ( realtime )
4544const saveRevisionJob = new SaveRevisionJob ( realtime )
4645
46+ // TODO: test it
4747function onAuthorizeSuccess ( data , accept ) {
4848 accept ( )
4949}
5050
51+ // TODO: test it
5152function onAuthorizeFail ( data , message , error , accept ) {
5253 accept ( ) // accept whether authorize or not to allow anonymous usage
5354}
5455
56+ // TODO: test it
5557// secure the origin by the cookie
5658function secure ( socket , next ) {
5759 try {
@@ -78,6 +80,7 @@ function secure (socket, next) {
7880}
7981
8082// TODO: only use in `updateDirtyNote`
83+ // TODO: test it
8184function emitCheck ( note ) {
8285 var out = {
8386 title : note . title ,
@@ -202,6 +205,7 @@ async function _updateNoteAsync (note) {
202205 return noteModel
203206}
204207
208+ // TODO: test it
205209function getStatus ( callback ) {
206210 models . Note . count ( ) . then ( function ( notecount ) {
207211 var distinctaddresses = [ ]
@@ -257,6 +261,7 @@ function getStatus (callback) {
257261 } )
258262}
259263
264+ // TODO: test it
260265function isReady ( ) {
261266 return realtime . io &&
262267 Object . keys ( notes ) . length === 0 && Object . keys ( users ) . length === 0 &&
@@ -322,6 +327,7 @@ function parseNoteIdFromSocket (socket, callback) {
322327 } )
323328}
324329
330+ // TODO: test it
325331function emitOnlineUsers ( socket ) {
326332 var noteId = socket . noteId
327333 if ( ! noteId || ! notes [ noteId ] ) return
@@ -338,6 +344,7 @@ function emitOnlineUsers (socket) {
338344 realtime . io . to ( noteId ) . emit ( 'online users' , out )
339345}
340346
347+ // TODO: test it
341348function emitUserStatus ( socket ) {
342349 var noteId = socket . noteId
343350 var user = users [ socket . id ]
@@ -346,6 +353,7 @@ function emitUserStatus (socket) {
346353 socket . broadcast . to ( noteId ) . emit ( 'user status' , out )
347354}
348355
356+ // TODO: test it
349357function emitRefresh ( socket ) {
350358 var noteId = socket . noteId
351359 if ( ! noteId || ! notes [ noteId ] ) return
@@ -366,6 +374,7 @@ function emitRefresh (socket) {
366374 socket . emit ( 'refresh' , out )
367375}
368376
377+ // TODO: test it
369378function isDuplicatedInSocketQueue ( queue , socket ) {
370379 for ( var i = 0 ; i < queue . length ; i ++ ) {
371380 if ( queue [ i ] && queue [ i ] . id === socket . id ) {
@@ -375,6 +384,7 @@ function isDuplicatedInSocketQueue (queue, socket) {
375384 return false
376385}
377386
387+ // TODO: test it
378388function clearSocketQueue ( queue , socket ) {
379389 for ( var i = 0 ; i < queue . length ; i ++ ) {
380390 if ( ! queue [ i ] || queue [ i ] . id === socket . id ) {
@@ -384,6 +394,7 @@ function clearSocketQueue (queue, socket) {
384394 }
385395}
386396
397+ // TODO: test it
387398function connectNextSocket ( ) {
388399 setTimeout ( function ( ) {
389400 isConnectionBusy = false
@@ -393,6 +404,7 @@ function connectNextSocket () {
393404 } , 1 )
394405}
395406
407+ // TODO: test it
396408function interruptConnection ( socket , noteId , socketId ) {
397409 if ( notes [ noteId ] ) delete notes [ noteId ]
398410 if ( users [ socketId ] ) delete users [ socketId ]
@@ -425,6 +437,7 @@ function checkViewPermission (req, note) {
425437var isConnectionBusy = false
426438var connectionSocketQueue = [ ]
427439
440+ // TODO: test it
428441function finishConnection ( socket , noteId , socketId ) {
429442 // if no valid info provided will drop the client
430443 if ( ! socket || ! notes [ noteId ] || ! users [ socketId ] ) {
@@ -469,6 +482,7 @@ function finishConnection (socket, noteId, socketId) {
469482 }
470483}
471484
485+ // TODO: test it
472486function startConnection ( socket ) {
473487 if ( isConnectionBusy ) return
474488 isConnectionBusy = true
@@ -556,6 +570,7 @@ function startConnection (socket) {
556570 }
557571}
558572
573+ // TODO: test it
559574function failConnection ( code , err , socket ) {
560575 logger . error ( err )
561576 // clear error socket in queue
@@ -624,6 +639,7 @@ function buildUserOutData (user) {
624639 return out
625640}
626641
642+ // TODO: test it
627643function updateUserData ( socket , user ) {
628644 // retrieve user data from passport
629645 if ( socket . request . user && socket . request . user . logged_in ) {
@@ -639,31 +655,26 @@ function updateUserData (socket, user) {
639655 }
640656}
641657
642- function ifMayEdit ( socket , callback ) {
643- var noteId = socket . noteId
644- if ( ! noteId || ! notes [ noteId ] ) return
645- var note = notes [ noteId ]
646- var mayEdit = true
647- switch ( note . permission ) {
658+ function canEditNote ( notePermission , noteOwnerId , currentUserId ) {
659+ switch ( notePermission ) {
648660 case 'freely' :
649- // not blocking anyone
650- break
661+ return true
651662 case 'editable' :
652663 case 'limited' :
653664 // only login user can change
654- if ( ! socket . request . user || ! socket . request . user . logged_in ) {
655- mayEdit = false
656- }
657- break
665+ return ! ! currentUserId
658666 case 'locked' :
659667 case 'private' :
660668 case 'protected' :
661669 // only owner can change
662- if ( ! note . owner || note . owner !== socket . request . user . id ) {
663- mayEdit = false
664- }
665- break
670+ return noteOwnerId === currentUserId
666671 }
672+ }
673+
674+ function ifMayEdit ( socket , callback ) {
675+ const note = getNoteFromNotePool ( socket . noteId )
676+ if ( ! note ) return
677+ const mayEdit = canEditNote ( note . permission , note . owner , socket . request . user . id )
667678 // if user may edit and this is a text operation
668679 if ( socket . origin === 'operation' && mayEdit ) {
669680 // save for the last change user id
@@ -676,6 +687,7 @@ function ifMayEdit (socket, callback) {
676687 return callback ( mayEdit )
677688}
678689
690+ // TODO: test it
679691function operationCallback ( socket , operation ) {
680692 var noteId = socket . noteId
681693 if ( ! noteId || ! notes [ noteId ] ) return
@@ -718,6 +730,7 @@ function operationCallback (socket, operation) {
718730 } )
719731}
720732
733+ // TODO: test it
721734function updateHistory ( userId , note , time ) {
722735 var noteId = note . alias ? note . alias : models . Note . encodeNoteId ( note . id )
723736 if ( note . server ) history . updateHistory ( userId , noteId , note . server . document , time )
@@ -739,6 +752,7 @@ function getNoteFromNotePool (noteId) {
739752 return notes [ noteId ]
740753}
741754
755+ // TODO: test it
742756function connection ( socket ) {
743757 if ( realtime . maintenance ) return
744758 exports . parseNoteIdFromSocket ( socket , function ( err , noteId ) {
@@ -798,6 +812,7 @@ function connection (socket) {
798812 socketClient . registerEventHandler ( )
799813}
800814
815+ // TODO: test it
801816function terminate ( ) {
802817 disconnectProcessQueue . stop ( )
803818 updateDirtyNoteJob . stop ( )
@@ -825,6 +840,7 @@ exports.queueForDisconnect = queueForDisconnect
825840exports . terminate = terminate
826841exports . getUserPool = getUserPool
827842exports . updateHistory = updateHistory
843+ exports . ifMayEdit = ifMayEdit
828844exports . disconnectProcessQueue = disconnectProcessQueue
829845exports . notes = notes
830846exports . users = users
0 commit comments