@ -42,6 +42,13 @@ define([
/ *
/ *
Tools
Tools
* /
* /
var _ownedByMe = function ( Env , owners ) {
return Array . isArray ( owners ) && owners . indexOf ( Env . edPublic ) !== - 1 ;
} ;
var _ownedByOther = function ( Env , owners ) {
return Array . isArray ( owners ) && owners . length &&
( ! Env . edPublic || owners . indexOf ( Env . edPublic ) === - 1 ) ;
} ;
var _getUserObjects = function ( Env ) {
var _getUserObjects = function ( Env ) {
var userObjects = [ Env . user . userObject ] ;
var userObjects = [ Env . user . userObject ] ;
@ -206,21 +213,36 @@ define([
} ;
} ;
// Get a copy of the elements located in the given paths, with their files data
// Get a copy of the elements located in the given paths, with their files data
var _getCopyFromPaths = function ( paths , userObject ) {
// Note: This function is only called to move files from a proxy to another
var _getCopyFromPaths = function ( Env , paths , userObject ) {
var data = [ ] ;
var data = [ ] ;
paths . forEach ( function ( path ) {
var toNotRemove = [ ] ;
paths . forEach ( function ( path , idx ) {
var el = userObject . find ( path ) ;
var el = userObject . find ( path ) ;
var files = [ ] ;
var files = [ ] ;
var key = path [ path . length - 1 ] ;
// Get the files ID from the current path (file or folder)
// Get the files ID from the current path (file or folder)
if ( userObject . isFile ( el ) ) {
if ( userObject . isFile ( el ) ) {
files . push ( el ) ;
files . push ( el ) ;
} else if ( userObject . isSharedFolder ( el ) ) {
files . push ( el ) ;
var obj = Env . folders [ el ] . proxy . metadata || { } ;
if ( obj ) { key = obj . title ; }
} else {
} else {
userObject . getFilesRecursively ( el , files ) ;
userObject . getFilesRecursively ( el , files ) ;
}
}
// Remove the shared folder from this list of files ID
// If the element is a folder and it contains a shared folder, abort!
files . filter ( function ( f ) { return ! userObject . isSharedFolder ( f ) ; } ) ;
// We don't want nested shared folders!
if ( files . some ( function ( f ) { return userObject . isSharedFolder ( f ) ; } ) ) {
if ( Env . cfg && Env . cfg . log ) {
Env . cfg . log ( Messages . _getKey ( 'fm_moveNestedSF' , [ key ] ) ) ;
}
toNotRemove . unshift ( idx ) ;
return ;
}
// Deduplicate
// Deduplicate
files = Util . deduplicateString ( files ) ;
files = Util . deduplicateString ( files ) ;
@ -237,9 +259,16 @@ define([
data . push ( {
data . push ( {
el : el ,
el : el ,
data : filesData
data : filesData ,
key : key
} ) ;
} ) ;
} ) ;
} ) ;
// Remove from the "paths" array the elements that we don't want to move
toNotRemove . forEach ( function ( idx ) {
paths . splice ( idx , 1 ) ;
} ) ;
return data ;
return data ;
} ;
} ;
@ -264,18 +293,19 @@ define([
// Move from the main drive to a shared folder
// Move from the main drive to a shared folder
// Copy the elements to the new location
// Copy the elements to the new location
var toCopy = _getCopyFromPaths ( resolved. main , Env . user . userObject ) ;
var toCopy = _getCopyFromPaths ( Env, resolved. main , Env . user . userObject ) ;
var newUserObject = newResolved . userObject ;
var newUserObject = newResolved . userObject ;
var ownedPads = [ ] ;
var ownedPads = [ ] ;
toCopy . forEach ( function ( obj ) {
toCopy . forEach ( function ( obj ) {
newUserObject . copyFromOtherDrive ( newResolved . path , obj . el , obj . data );
newUserObject . copyFromOtherDrive ( newResolved . path , obj . el , obj . data , obj . key );
var _owned = Object . keys ( obj . data ) . filter ( function ( id ) {
var _owned = Object . keys ( obj . data ) . filter ( function ( id ) {
var owners = obj . data [ id ] . owners ;
var owners = obj . data [ id ] . owners ;
return Array . isArray ( owners ) && owners . indexOf ( Env . edPublic ) !== - 1 ;
return _ownedByMe ( Env , owners ) ;
} ) ;
} ) ;
Array . prototype . push . apply ( ownedPads , _owned ) ;
Array . prototype . push . apply ( ownedPads , _owned ) ;
} ) ;
} ) ;
if ( resolved . main . length ) {
var rootPath = resolved . main [ 0 ] . slice ( ) ;
var rootPath = resolved . main [ 0 ] . slice ( ) ;
rootPath . pop ( ) ;
rootPath . pop ( ) ;
ownedPads = Util . deduplicateString ( ownedPads ) ;
ownedPads = Util . deduplicateString ( ownedPads ) ;
@ -287,6 +317,7 @@ define([
Env . user . userObject . delete ( resolved . main , waitFor ( ) ) ;
Env . user . userObject . delete ( resolved . main , waitFor ( ) ) ;
}
}
}
}
}
var folderIds = Object . keys ( resolved . folders ) ;
var folderIds = Object . keys ( resolved . folders ) ;
if ( folderIds . length ) {
if ( folderIds . length ) {
// Move from a shared folder
// Move from a shared folder
@ -302,9 +333,9 @@ define([
var uoTo = newResolved . userObject ;
var uoTo = newResolved . userObject ;
// Copy the elements to the new location
// Copy the elements to the new location
var toCopy = _getCopyFromPaths ( paths, uoFrom ) ;
var toCopy = _getCopyFromPaths ( Env, paths, uoFrom ) ;
toCopy . forEach ( function ( obj ) {
toCopy . forEach ( function ( obj ) {
uoTo . copyFromOtherDrive ( newResolved . path , obj . el , obj . data );
uoTo . copyFromOtherDrive ( newResolved . path , obj . el , obj . data , obj . key );
} ) ;
} ) ;
// Remove the elements from the old location (without unpinning)
// Remove the elements from the old location (without unpinning)
@ -415,7 +446,7 @@ define([
if ( ! Env . unpinPads || ! _toUnpin ) { return ; }
if ( ! Env . unpinPads || ! _toUnpin ) { return ; }
Array . prototype . push . apply ( toUnpin , _toUnpin ) ;
Array . prototype . push . apply ( toUnpin , _toUnpin ) ;
ownedRemoved = _ownedRemoved ;
ownedRemoved = _ownedRemoved ;
} ) , data . nocheck , data . isOwnPadRemoved );
} ) );
}
}
} ) . nThen ( function ( waitFor ) {
} ) . nThen ( function ( waitFor ) {
// Check if removed owned pads are duplicated is some shared folders
// Check if removed owned pads are duplicated is some shared folders
@ -440,7 +471,7 @@ define([
Env . folders [ id ] . userObject . delete ( resolved . folders [ id ] , waitFor ( function ( err , _toUnpin ) {
Env . folders [ id ] . userObject . delete ( resolved . folders [ id ] , waitFor ( function ( err , _toUnpin ) {
if ( ! Env . unpinPads || ! _toUnpin ) { return ; }
if ( ! Env . unpinPads || ! _toUnpin ) { return ; }
Array . prototype . push . apply ( toUnpin , _toUnpin ) ;
Array . prototype . push . apply ( toUnpin , _toUnpin ) ;
} ) , data . nocheck , data . isOwnPadRemoved );
} ) );
} ) ;
} ) ;
} ) . nThen ( function ( waitFor ) {
} ) . nThen ( function ( waitFor ) {
if ( ! Env . unpinPads ) { return ; }
if ( ! Env . unpinPads ) { return ; }
@ -589,8 +620,7 @@ define([
* /
* /
// Get the list of channels filtered by a type (expirable channels, owned channels, pin list)
// Get the list of channels filtered by a type (expirable channels, owned channels, pin list)
var getChannelsList = function ( Env , edPublic , type ) {
var getChannelsList = function ( Env , type ) {
//if (!edPublic) { return; }
var result = [ ] ;
var result = [ ] ;
var addChannel = function ( userObject ) {
var addChannel = function ( userObject ) {
if ( type === 'expirable' ) {
if ( type === 'expirable' ) {
@ -599,8 +629,7 @@ define([
// Don't push duplicates
// Don't push duplicates
if ( result . indexOf ( data . channel ) !== - 1 ) { return ; }
if ( result . indexOf ( data . channel ) !== - 1 ) { return ; }
// Return pads owned by someone else or expired by time
// Return pads owned by someone else or expired by time
if ( ( data . owners && data . owners . length && ( ! edPublic || data . owners . indexOf ( edPublic ) === - 1 ) )
if ( _ownedByOther ( Env , data . owners ) || ( data . expire && data . expire < ( + new Date ( ) ) ) ) {
|| ( data . expire && data . expire < ( + new Date ( ) ) ) ) {
result . push ( data . channel ) ;
result . push ( data . channel ) ;
}
}
} ;
} ;
@ -611,8 +640,7 @@ define([
// Don't push duplicates
// Don't push duplicates
if ( result . indexOf ( data . channel ) !== - 1 ) { return ; }
if ( result . indexOf ( data . channel ) !== - 1 ) { return ; }
// Return owned pads
// Return owned pads
if ( Array . isArray ( data . owners ) && data . owners . length &&
if ( _ownedByMe ( Env , data . owners ) ) {
data . owners . indexOf ( edPublic ) !== - 1 ) {
result . push ( data . channel ) ;
result . push ( data . channel ) ;
}
}
} ;
} ;
@ -621,8 +649,7 @@ define([
return function ( fileId ) {
return function ( fileId ) {
var data = userObject . getFileData ( fileId ) ;
var data = userObject . getFileData ( fileId ) ;
// Don't pin pads owned by someone else
// Don't pin pads owned by someone else
if ( Array . isArray ( data . owners ) && data . owners . length &&
if ( _ownedByOther ( Env , data . owners ) ) { return ; }
data . owners . indexOf ( edPublic ) === - 1 ) { return ; }
// Don't push duplicates
// Don't push duplicates
if ( result . indexOf ( data . channel ) === - 1 ) {
if ( result . indexOf ( data . channel ) === - 1 ) {
result . push ( data . channel ) ;
result . push ( data . channel ) ;
@ -631,8 +658,8 @@ define([
}
}
} ;
} ;
if ( type === 'owned' && ! edPublic) { return result ; }
if ( type === 'owned' && ! Env. edPublic) { return result ; }
if ( type === 'pin' && ! edPublic) { return result ; }
if ( type === 'pin' && ! Env. edPublic) { return result ; }
// Get the list of user objects
// Get the list of user objects
var userObjects = _getUserObjects ( Env ) ;
var userObjects = _getUserObjects ( Env ) ;
@ -646,8 +673,7 @@ define([
if ( type === "owned" ) {
if ( type === "owned" ) {
var sfOwned = Object . keys ( Env . user . proxy [ UserObject . SHARED _FOLDERS ] ) . filter ( function ( fId ) {
var sfOwned = Object . keys ( Env . user . proxy [ UserObject . SHARED _FOLDERS ] ) . filter ( function ( fId ) {
var owners = Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ fId ] . owners ;
var owners = Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ fId ] . owners ;
if ( Array . isArray ( owners ) && owners . length &&
if ( _ownedByMe ( Env , owners ) ) { return true ; }
owners . indexOf ( edPublic ) !== - 1 ) { return true ; }
} ) . map ( function ( fId ) {
} ) . map ( function ( fId ) {
return Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ fId ] . channel ;
return Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ fId ] . channel ;
} ) ;
} ) ;
@ -672,10 +698,22 @@ define([
p = resolved . path ;
p = resolved . path ;
}
}
var todo = function ( ) {
var todo = function ( ) {
uo . pushData ( pad , function ( e , id ) {
var error ;
if ( e ) { return void cb ( e ) ; }
nThen ( function ( waitFor ) {
uo . pushData ( pad , waitFor ( function ( e , id ) {
if ( e ) { error = e ; return ; }
uo . add ( id , p ) ;
uo . add ( id , p ) ;
cb ( ) ;
} ) ) ;
if ( uo . id && _ownedByMe ( Env , pad . owners ) ) {
// Creating an owned pad in a shared folder:
// We must add a copy in the user's personnal drive
Env . user . userObject . pushData ( pad , waitFor ( function ( e , id ) {
if ( e ) { error = e ; return ; }
Env . user . userObject . add ( id , [ 'root' ] ) ;
} ) ) ;
}
} ) . nThen ( function ( ) {
cb ( error ) ;
} ) ;
} ) ;
} ;
} ;
if ( ! Env . pinPads ) { return void todo ( ) ; }
if ( ! Env . pinPads ) { return void todo ( ) ; }
@ -777,14 +815,11 @@ define([
}
}
} , cb ) ;
} , cb ) ;
} ;
} ;
var deleteInner = function ( Env , paths , cb , nocheck , isOwnPadRemoved , noUnpin ) {
var deleteInner = function ( Env , paths , cb ) {
return void Env . sframeChan . query ( "Q_DRIVE_USEROBJECT" , {
return void Env . sframeChan . query ( "Q_DRIVE_USEROBJECT" , {
cmd : "delete" ,
cmd : "delete" ,
data : {
data : {
paths : paths ,
paths : paths ,
nocheck : nocheck ,
noUnpin : noUnpin ,
isOwnPadRemoved : isOwnPadRemoved
}
}
} , cb ) ;
} , cb ) ;
} ;
} ;
@ -857,7 +892,7 @@ define([
} ;
} ;
var getSharedFolderData = function ( Env , id ) {
var getSharedFolderData = function ( Env , id ) {
if ( ! Env . folders [ id ] ) { return ; }
if ( ! Env . folders [ id ] ) { return { } ; }
var obj = Env . folders [ id ] . proxy . metadata || { } ;
var obj = Env . folders [ id ] . proxy . metadata || { } ;
for ( var k in Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ id ] || { } ) {
for ( var k in Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ id ] || { } ) {
obj [ k ] = Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ id ] [ k ] ;
obj [ k ] = Env . user . proxy [ UserObject . SHARED _FOLDERS ] [ id ] [ k ] ;