@@ -35,7 +35,8 @@ module.exports = function (
3535 isPreVerified ,
3636 checkPassword ,
3737 push ,
38- metricsContext
38+ metricsContext ,
39+ devices
3940 ) {
4041
4142 // Loads and compiles a json validator for the payloads received
@@ -803,7 +804,7 @@ module.exports = function (
803804 schema : isA . object ( {
804805 id : isA . string ( ) . length ( 32 ) . regex ( HEX_STRING ) . required ( ) ,
805806 createdAt : isA . number ( ) . positive ( ) . optional ( ) ,
806- // We previously allowed devices to register with arbitrry unicode names,
807+ // We previously allowed devices to register with arbitrary unicode names,
807808 // so we can't assert DISPLAY_SAFE_UNICODE in the response schema.
808809 name : isA . string ( ) . max ( 255 ) . optional ( ) ,
809810 type : isA . string ( ) . max ( 16 ) . optional ( ) ,
@@ -829,17 +830,19 @@ module.exports = function (
829830 if ( config . deviceUpdatesEnabled === false ) {
830831 throw error . featureNotEnabled ( )
831832 }
833+ } else if ( sessionToken . deviceId ) {
834+ // Keep the old id, which is probably from a synthesized device record
835+ payload . id = sessionToken . deviceId . toString ( 'hex' )
832836 }
833837
834838 if ( payload . pushCallback && ( ! payload . pushPublicKey || ! payload . pushAuthKey ) ) {
835839 payload . pushPublicKey = ''
836840 payload . pushAuthKey = ''
837841 }
838842
839- upsertDevice ( ) . then (
843+ devices . upsert ( request , sessionToken , payload ) . then (
840844 function ( device ) {
841845 reply ( butil . unbuffer ( device ) )
842- push . notifyDeviceConnected ( sessionToken . uid , device . name , device . id . toString ( 'hex' ) )
843846 } ,
844847 reply
845848 )
@@ -871,45 +874,6 @@ module.exports = function (
871874 }
872875 return spurious
873876 }
874-
875- function upsertDevice ( ) {
876- var operation , event , result
877- if ( payload . id ) {
878- operation = 'updateDevice'
879- event = 'device.updated'
880- } else {
881- operation = 'createDevice'
882- event = 'device.created'
883- }
884-
885- return db [ operation ] ( sessionToken . uid , sessionToken . tokenId , payload )
886- . then (
887- function ( res ) {
888- result = res
889- return log . activityEvent ( event , request , {
890- uid : sessionToken . uid . toString ( 'hex' ) ,
891- device_id : result . id . toString ( 'hex' )
892- } )
893- }
894- )
895- . then (
896- function ( ) {
897- if ( operation === 'createDevice' ) {
898- return log . notifyAttachedServices ( 'device:create' , request , {
899- uid : sessionToken . uid ,
900- id : result . id ,
901- type : result . type ,
902- timestamp : result . createdAt
903- } )
904- }
905- }
906- )
907- . then (
908- function ( ) {
909- return result
910- }
911- )
912- }
913877 }
914878 } ,
915879 {
@@ -996,9 +960,9 @@ module.exports = function (
996960 id : isA . string ( ) . length ( 32 ) . regex ( HEX_STRING ) . required ( ) ,
997961 isCurrentDevice : isA . boolean ( ) . required ( ) ,
998962 lastAccessTime : isA . number ( ) . min ( 0 ) . required ( ) . allow ( null ) ,
999- // We previously allowed devices to register with arbitrry unicode names,
963+ // We previously allowed devices to register with arbitrary unicode names,
1000964 // so we can't assert DISPLAY_SAFE_UNICODE in the response schema.
1001- name : isA . string ( ) . max ( 255 ) . required ( ) ,
965+ name : isA . string ( ) . max ( 255 ) . required ( ) . allow ( '' ) ,
1002966 type : isA . string ( ) . max ( 16 ) . required ( ) ,
1003967 pushCallback : isA . string ( ) . uri ( { scheme : 'https' } ) . max ( 255 ) . optional ( ) . allow ( '' ) . allow ( null ) ,
1004968 pushPublicKey : isA . string ( ) . max ( 88 ) . regex ( URLSAFEBASE64 ) . optional ( ) . allow ( '' ) . allow ( null ) ,
@@ -1011,11 +975,26 @@ module.exports = function (
1011975 var sessionToken = request . auth . credentials
1012976 var uid = sessionToken . uid
1013977 db . devices ( uid ) . then (
1014- function ( devices ) {
1015- reply ( devices . map ( function ( device ) {
978+ function ( deviceArray ) {
979+ reply ( deviceArray . map ( function ( device ) {
980+ if ( ! device . name ) {
981+ device . name = devices . synthesizeName ( device )
982+ }
983+
984+ if ( ! device . type ) {
985+ device . type = device . uaDeviceType || 'desktop'
986+ }
987+
1016988 device . isCurrentDevice =
1017989 device . sessionToken . toString ( 'hex' ) === sessionToken . tokenId . toString ( 'hex' )
990+
1018991 delete device . sessionToken
992+ delete device . uaBrowser
993+ delete device . uaBrowserVersion
994+ delete device . uaOS
995+ delete device . uaOSVersion
996+ delete device . uaDeviceType
997+
1019998 return butil . unbuffer ( device )
1020999 } ) )
10211000 } ,
0 commit comments