4
4
const cookie = require ( 'cookie' )
5
5
const cookieParser = require ( 'cookie-parser' )
6
6
const url = require ( 'url' )
7
- const async = require ( 'async' )
8
7
const randomcolor = require ( 'randomcolor' )
9
8
const Chance = require ( 'chance' )
10
9
const chance = new Chance ( )
@@ -44,14 +43,17 @@ const updateDirtyNoteJob = new UpdateDirtyNoteJob(realtime)
44
43
const cleanDanglingUserJob = new CleanDanglingUserJob ( realtime )
45
44
const saveRevisionJob = new SaveRevisionJob ( realtime )
46
45
46
+ // TODO: test it
47
47
function onAuthorizeSuccess ( data , accept ) {
48
48
accept ( )
49
49
}
50
50
51
+ // TODO: test it
51
52
function onAuthorizeFail ( data , message , error , accept ) {
52
53
accept ( ) // accept whether authorize or not to allow anonymous usage
53
54
}
54
55
56
+ // TODO: test it
55
57
// secure the origin by the cookie
56
58
function secure ( socket , next ) {
57
59
try {
@@ -78,6 +80,7 @@ function secure (socket, next) {
78
80
}
79
81
80
82
// TODO: only use in `updateDirtyNote`
83
+ // TODO: test it
81
84
function emitCheck ( note ) {
82
85
var out = {
83
86
title : note . title ,
@@ -202,6 +205,7 @@ async function _updateNoteAsync (note) {
202
205
return noteModel
203
206
}
204
207
208
+ // TODO: test it
205
209
function getStatus ( callback ) {
206
210
models . Note . count ( ) . then ( function ( notecount ) {
207
211
var distinctaddresses = [ ]
@@ -257,6 +261,7 @@ function getStatus (callback) {
257
261
} )
258
262
}
259
263
264
+ // TODO: test it
260
265
function isReady ( ) {
261
266
return realtime . io &&
262
267
Object . keys ( notes ) . length === 0 && Object . keys ( users ) . length === 0 &&
@@ -322,6 +327,7 @@ function parseNoteIdFromSocket (socket, callback) {
322
327
} )
323
328
}
324
329
330
+ // TODO: test it
325
331
function emitOnlineUsers ( socket ) {
326
332
var noteId = socket . noteId
327
333
if ( ! noteId || ! notes [ noteId ] ) return
@@ -338,6 +344,7 @@ function emitOnlineUsers (socket) {
338
344
realtime . io . to ( noteId ) . emit ( 'online users' , out )
339
345
}
340
346
347
+ // TODO: test it
341
348
function emitUserStatus ( socket ) {
342
349
var noteId = socket . noteId
343
350
var user = users [ socket . id ]
@@ -346,6 +353,7 @@ function emitUserStatus (socket) {
346
353
socket . broadcast . to ( noteId ) . emit ( 'user status' , out )
347
354
}
348
355
356
+ // TODO: test it
349
357
function emitRefresh ( socket ) {
350
358
var noteId = socket . noteId
351
359
if ( ! noteId || ! notes [ noteId ] ) return
@@ -366,6 +374,7 @@ function emitRefresh (socket) {
366
374
socket . emit ( 'refresh' , out )
367
375
}
368
376
377
+ // TODO: test it
369
378
function isDuplicatedInSocketQueue ( queue , socket ) {
370
379
for ( var i = 0 ; i < queue . length ; i ++ ) {
371
380
if ( queue [ i ] && queue [ i ] . id === socket . id ) {
@@ -375,6 +384,7 @@ function isDuplicatedInSocketQueue (queue, socket) {
375
384
return false
376
385
}
377
386
387
+ // TODO: test it
378
388
function clearSocketQueue ( queue , socket ) {
379
389
for ( var i = 0 ; i < queue . length ; i ++ ) {
380
390
if ( ! queue [ i ] || queue [ i ] . id === socket . id ) {
@@ -384,6 +394,7 @@ function clearSocketQueue (queue, socket) {
384
394
}
385
395
}
386
396
397
+ // TODO: test it
387
398
function connectNextSocket ( ) {
388
399
setTimeout ( function ( ) {
389
400
isConnectionBusy = false
@@ -393,6 +404,7 @@ function connectNextSocket () {
393
404
} , 1 )
394
405
}
395
406
407
+ // TODO: test it
396
408
function interruptConnection ( socket , noteId , socketId ) {
397
409
if ( notes [ noteId ] ) delete notes [ noteId ]
398
410
if ( users [ socketId ] ) delete users [ socketId ]
@@ -425,6 +437,7 @@ function checkViewPermission (req, note) {
425
437
var isConnectionBusy = false
426
438
var connectionSocketQueue = [ ]
427
439
440
+ // TODO: test it
428
441
function finishConnection ( socket , noteId , socketId ) {
429
442
// if no valid info provided will drop the client
430
443
if ( ! socket || ! notes [ noteId ] || ! users [ socketId ] ) {
@@ -469,6 +482,7 @@ function finishConnection (socket, noteId, socketId) {
469
482
}
470
483
}
471
484
485
+ // TODO: test it
472
486
function startConnection ( socket ) {
473
487
if ( isConnectionBusy ) return
474
488
isConnectionBusy = true
@@ -556,6 +570,7 @@ function startConnection (socket) {
556
570
}
557
571
}
558
572
573
+ // TODO: test it
559
574
function failConnection ( code , err , socket ) {
560
575
logger . error ( err )
561
576
// clear error socket in queue
@@ -624,6 +639,7 @@ function buildUserOutData (user) {
624
639
return out
625
640
}
626
641
642
+ // TODO: test it
627
643
function updateUserData ( socket , user ) {
628
644
// retrieve user data from passport
629
645
if ( socket . request . user && socket . request . user . logged_in ) {
@@ -639,31 +655,26 @@ function updateUserData (socket, user) {
639
655
}
640
656
}
641
657
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 ) {
648
660
case 'freely' :
649
- // not blocking anyone
650
- break
661
+ return true
651
662
case 'editable' :
652
663
case 'limited' :
653
664
// only login user can change
654
- if ( ! socket . request . user || ! socket . request . user . logged_in ) {
655
- mayEdit = false
656
- }
657
- break
665
+ return ! ! currentUserId
658
666
case 'locked' :
659
667
case 'private' :
660
668
case 'protected' :
661
669
// only owner can change
662
- if ( ! note . owner || note . owner !== socket . request . user . id ) {
663
- mayEdit = false
664
- }
665
- break
670
+ return noteOwnerId === currentUserId
666
671
}
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 )
667
678
// if user may edit and this is a text operation
668
679
if ( socket . origin === 'operation' && mayEdit ) {
669
680
// save for the last change user id
@@ -676,6 +687,7 @@ function ifMayEdit (socket, callback) {
676
687
return callback ( mayEdit )
677
688
}
678
689
690
+ // TODO: test it
679
691
function operationCallback ( socket , operation ) {
680
692
var noteId = socket . noteId
681
693
if ( ! noteId || ! notes [ noteId ] ) return
@@ -718,6 +730,7 @@ function operationCallback (socket, operation) {
718
730
} )
719
731
}
720
732
733
+ // TODO: test it
721
734
function updateHistory ( userId , note , time ) {
722
735
var noteId = note . alias ? note . alias : models . Note . encodeNoteId ( note . id )
723
736
if ( note . server ) history . updateHistory ( userId , noteId , note . server . document , time )
@@ -739,6 +752,7 @@ function getNoteFromNotePool (noteId) {
739
752
return notes [ noteId ]
740
753
}
741
754
755
+ // TODO: test it
742
756
function connection ( socket ) {
743
757
if ( realtime . maintenance ) return
744
758
exports . parseNoteIdFromSocket ( socket , function ( err , noteId ) {
@@ -798,6 +812,7 @@ function connection (socket) {
798
812
socketClient . registerEventHandler ( )
799
813
}
800
814
815
+ // TODO: test it
801
816
function terminate ( ) {
802
817
disconnectProcessQueue . stop ( )
803
818
updateDirtyNoteJob . stop ( )
@@ -825,6 +840,7 @@ exports.queueForDisconnect = queueForDisconnect
825
840
exports . terminate = terminate
826
841
exports . getUserPool = getUserPool
827
842
exports . updateHistory = updateHistory
843
+ exports . ifMayEdit = ifMayEdit
828
844
exports . disconnectProcessQueue = disconnectProcessQueue
829
845
exports . notes = notes
830
846
exports . users = users
0 commit comments