@@ -805,15 +805,29 @@ class SocketClient {
805
805
// reveiced when user logout or changed
806
806
this . socket . on ( 'user changed' , this . userChangedEventHandler . bind ( this ) )
807
807
// delete a note
808
- this . socket . on ( 'delete' , this . deleteNote . bind ( this ) )
808
+ this . socket . on ( 'delete' , this . deleteNoteEventHandler . bind ( this ) )
809
+ // received note permission change request
810
+ this . socket . on ( 'permission' , this . permissionChangeEventHandler . bind ( this ) )
809
811
}
810
812
811
813
isUserLoggedIn ( ) {
812
814
return this . socket . request . user && this . socket . request . user . logged_in
813
815
}
814
816
815
- getCurrentLoggedInUserId ( ) {
816
- return get ( this . socket , 'request.user.id' )
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
817
831
}
818
832
819
833
disconnectSocketOnNote ( note ) {
@@ -827,28 +841,83 @@ class SocketClient {
827
841
} )
828
842
}
829
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
+
830
862
async destroyNote ( id ) {
831
863
return models . Note . destroy ( {
832
864
where : { id : id }
833
865
} )
834
866
}
835
867
836
- deleteNote ( ) {
837
- // need login to do more actions
838
- if ( this . isUserLoggedIn ( ) && this . isNoteAndUserExists ( ) ) {
839
- const note = this . getCurrentNote ( )
840
- // Only owner can delete note
841
- if ( note . owner && note . owner === this . getCurrentLoggedInUserId ( ) ) {
842
- this . destroyNote ( note . id )
843
- . then ( ( successRows ) => {
844
- if ( ! successRows ) return
845
- this . disconnectSocketOnNote ( note )
846
- } )
847
- . catch ( function ( err ) {
848
- return logger . error ( 'delete note failed: ' + err )
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
849
890
} )
891
+ setTimeout ( function ( ) {
892
+ sock . disconnect ( true )
893
+ } , 0 )
894
+ }
850
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 ) )
851
915
}
916
+ if ( data ) {
917
+ user . idle = data . idle
918
+ user . type = data . type
919
+ }
920
+ exports . emitUserStatus ( this . socket )
852
921
}
853
922
854
923
userChangedEventHandler ( ) {
@@ -863,26 +932,6 @@ class SocketClient {
863
932
exports . emitOnlineUsers ( this . socket )
864
933
}
865
934
866
- getCurrentUser ( ) {
867
- if ( ! this . socket . id ) return
868
- return getUserFromUserPool ( this . socket . id )
869
- }
870
-
871
- getCurrentNote ( ) {
872
- if ( ! this . socket . noteId ) return
873
- return getNoteFromNotePool ( this . socket . noteId )
874
- }
875
-
876
- getNoteChannel ( ) {
877
- return this . socket . broadcast . to ( this . socket . noteId )
878
- }
879
-
880
- isNoteAndUserExists ( ) {
881
- const note = getNoteFromNotePool ( this . socket . noteId )
882
- const user = getUserFromUserPool ( this . socket . id )
883
- return note && user
884
- }
885
-
886
935
onlineUsersEventHandler ( ) {
887
936
if ( ! this . isNoteAndUserExists ( ) ) return
888
937
@@ -921,28 +970,40 @@ class SocketClient {
921
970
} )
922
971
}
923
972
924
- checkVersionEventHandler ( ) {
925
- this . socket . emit ( 'version' , {
926
- version : config . fullversion ,
927
- minimumCompatibleVersion : config . minimumCompatibleVersion
928
- } )
929
- }
930
-
931
- refreshEventHandler ( ) {
932
- exports . emitRefresh ( this . socket )
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
+ }
933
989
}
934
990
935
- userStatusEventHandler ( data ) {
991
+ permissionChangeEventHandler ( permission ) {
992
+ if ( ! this . isUserLoggedIn ( ) ) return
936
993
if ( ! this . isNoteAndUserExists ( ) ) return
937
- const user = this . getCurrentUser ( )
938
- if ( config . debug ) {
939
- logger . info ( 'SERVER received [' + this . socket . noteId + '] user status from [' + this . socket . id + ']: ' + JSON . stringify ( data ) )
940
- }
941
- if ( data ) {
942
- user . idle = data . idle
943
- user . type = data . type
944
- }
945
- exports . emitUserStatus ( this . socket )
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 ) )
946
1007
}
947
1008
948
1009
disconnectEventHandler ( ) {
@@ -1009,52 +1070,6 @@ function connection (socket) {
1009
1070
1010
1071
const socketClient = new SocketClient ( socket )
1011
1072
socketClient . registerEventHandler ( )
1012
-
1013
- // received note permission change request
1014
- socket . on ( 'permission' , function ( permission ) {
1015
- // need login to do more actions
1016
- if ( socket . request . user && socket . request . user . logged_in ) {
1017
- var noteId = socket . noteId
1018
- if ( ! noteId || ! notes [ noteId ] ) return
1019
- var note = notes [ noteId ]
1020
- // Only owner can change permission
1021
- if ( note . owner && note . owner === socket . request . user . id ) {
1022
- if ( permission === 'freely' && ! config . allowAnonymous && ! config . allowAnonymousEdits ) return
1023
- note . permission = permission
1024
- models . Note . update ( {
1025
- permission : permission
1026
- } , {
1027
- where : {
1028
- id : noteId
1029
- }
1030
- } ) . then ( function ( count ) {
1031
- if ( ! count ) {
1032
- return
1033
- }
1034
- var out = {
1035
- permission : permission
1036
- }
1037
- realtime . io . to ( note . id ) . emit ( 'permission' , out )
1038
- for ( var i = 0 , l = note . socks . length ; i < l ; i ++ ) {
1039
- var sock = note . socks [ i ]
1040
- if ( typeof sock !== 'undefined' && sock ) {
1041
- // check view permission
1042
- if ( ! checkViewPermission ( sock . request , note ) ) {
1043
- sock . emit ( 'info' , {
1044
- code : 403
1045
- } )
1046
- setTimeout ( function ( ) {
1047
- sock . disconnect ( true )
1048
- } , 0 )
1049
- }
1050
- }
1051
- }
1052
- } ) . catch ( function ( err ) {
1053
- return logger . error ( 'update note permission failed: ' + err )
1054
- } )
1055
- }
1056
- }
1057
- } )
1058
1073
}
1059
1074
1060
1075
exports = module . exports = realtime
@@ -1070,6 +1085,7 @@ exports.emitRefresh = emitRefresh
1070
1085
exports . emitUserStatus = emitUserStatus
1071
1086
exports . disconnect = disconnect
1072
1087
exports . emitOnlineUsers = emitOnlineUsers
1088
+ exports . checkViewPermission = checkViewPermission
1073
1089
exports . notes = notes
1074
1090
exports . users = users
1075
1091
exports . disconnectSocketQueue = disconnectSocketQueue
0 commit comments