2018-01-22 13:01:38 -08:00
'use strict' ;
2020-06-03 23:17:06 +02:00
/* global $ */
/* global angular */
/* global EventSource */
/* global async */
2018-01-22 13:01:38 -08:00
2019-09-27 18:11:48 +02:00
// keep in sync with box/src/apps.js
var ISTATES = {
PENDING _INSTALL : 'pending_install' ,
PENDING _CLONE : 'pending_clone' ,
PENDING _CONFIGURE : 'pending_configure' ,
PENDING _UNINSTALL : 'pending_uninstall' ,
PENDING _RESTORE : 'pending_restore' ,
2021-05-26 09:32:17 -07:00
PENDING _IMPORT : 'pending_import' ,
2019-09-27 18:11:48 +02:00
PENDING _UPDATE : 'pending_update' ,
PENDING _BACKUP : 'pending_backup' ,
PENDING _RECREATE _CONTAINER : 'pending_recreate_container' , // env change or addon change
PENDING _LOCATION _CHANGE : 'pending_location_change' ,
PENDING _DATA _DIR _MIGRATION : 'pending_data_dir_migration' ,
PENDING _RESIZE : 'pending_resize' ,
PENDING _DEBUG : 'pending_debug' ,
PENDING _START : 'pending_start' ,
PENDING _STOP : 'pending_stop' ,
2019-12-20 11:18:48 -08:00
PENDING _RESTART : 'pending_restart' ,
2019-09-27 18:11:48 +02:00
ERROR : 'error' ,
INSTALLED : 'installed'
} ;
var HSTATES = {
HEALTHY : 'healthy' ,
UNHEALTHY : 'unhealthy' ,
ERROR : 'error' ,
DEAD : 'dead'
} ;
var RSTATES = {
RUNNING : 'running' ,
STOPPED : 'stopped'
} ;
var ERROR = {
ACCESS _DENIED : 'Access Denied' ,
ALREADY _EXISTS : 'Already Exists' ,
BAD _FIELD : 'Bad Field' ,
COLLECTD _ERROR : 'Collectd Error' ,
CONFLICT : 'Conflict' ,
DATABASE _ERROR : 'Database Error' ,
DNS _ERROR : 'DNS Error' ,
DOCKER _ERROR : 'Docker Error' ,
EXTERNAL _ERROR : 'External Error' ,
FS _ERROR : 'FileSystem Error' ,
INTERNAL _ERROR : 'Internal Error' ,
LOGROTATE _ERROR : 'Logrotate Error' ,
NETWORK _ERROR : 'Network Error' ,
NOT _FOUND : 'Not found' ,
REVERSEPROXY _ERROR : 'ReverseProxy Error' ,
TASK _ERROR : 'Task Error' ,
UNKNOWN _ERROR : 'Unknown Error' // only used for portin,
} ;
2020-02-21 16:52:37 -08:00
var ROLES = {
OWNER : 'owner' ,
ADMIN : 'admin' ,
USER _MANAGER : 'usermanager' ,
USER : 'user'
} ;
2020-09-01 16:31:09 +02:00
// sync up with tasks.js
var TASK _TYPES = {
TASK _APP : 'app' ,
TASK _BACKUP : 'backup' ,
TASK _UPDATE : 'update' ,
TASK _RENEW _CERTS : 'renewcerts' ,
TASK _SETUP _DNS _AND _CERT : 'setupDnsAndCert' ,
TASK _CLEAN _BACKUPS : 'cleanBackups' ,
TASK _SYNC _EXTERNAL _LDAP : 'syncExternalLdap' ,
TASK _CHANGE _MAIL _LOCATION : 'changeMailLocation' ,
2021-02-24 22:18:39 -08:00
TASK _SYNC _DNS _RECORDS : 'syncDnsRecords' ,
2020-09-01 16:31:09 +02:00
} ;
2020-05-14 23:04:19 +02:00
var SECRET _PLACEHOLDER = String . fromCharCode ( 0x25CF ) . repeat ( 8 ) ;
2021-04-07 10:47:29 +02:00
// ----------------------------------------------
// Helper to ensure loading a fallback app icon on first load failure
// ----------------------------------------------
function imageErrorHandler ( elem ) {
elem . src = elem . getAttribute ( 'fallback-icon' ) ;
elem . onerror = null ; // avoid retry after default icon cannot be loaded
}
2020-06-02 14:34:38 +02:00
// ----------------------------------------------
// Shared Angular Filters
// ----------------------------------------------
// binary units (non SI) 1024 based
function prettyByteSize ( size , fallback ) {
if ( ! size ) return fallback || 0 ;
var i = Math . floor ( Math . log ( size ) / Math . log ( 1024 ) ) ;
return ( size / Math . pow ( 1024 , i ) ) . toFixed ( 2 ) * 1 + ' ' + [ 'B' , 'kB' , 'MB' , 'GB' , 'TB' ] [ i ] ;
2021-01-29 11:15:33 +01:00
}
2020-06-02 14:34:38 +02:00
angular . module ( 'Application' ) . filter ( 'prettyByteSize' , function ( ) {
2021-01-29 11:15:33 +01:00
return function ( size , fallback ) { return prettyByteSize ( size , fallback ) || '0 kb' ; } ;
2020-06-02 14:34:38 +02:00
} ) ;
angular . module ( 'Application' ) . filter ( 'prettyDiskSize' , function ( ) {
2021-01-29 11:15:33 +01:00
return function ( size , fallback ) { return prettyByteSize ( size , fallback ) || 'Not available yet' ; } ;
2020-06-02 14:34:38 +02:00
} ) ;
2021-04-01 16:05:13 +02:00
angular . module ( 'Application' ) . filter ( 'trKeyFromPeriod' , function ( ) {
return function ( period ) {
if ( period === 6 ) return 'app.graphs.period.6h' ;
if ( period === 12 ) return 'app.graphs.period.12h' ;
if ( period === 24 ) return 'app.graphs.period.24h' ;
if ( period === 24 * 7 ) return 'app.graphs.period.7d' ;
if ( period === 24 * 30 ) return 'app.graphs.period.30d' ;
return '' ;
} ;
} ) ;
2021-04-01 17:04:46 +02:00
angular . module ( 'Application' ) . filter ( 'prettyDate' , function ( $translate ) {
2021-01-29 11:52:47 +01:00
// http://ejohn.org/files/pretty.js
return function prettyDate ( utc ) {
var date = new Date ( utc ) , // this converts utc into browser timezone and not cloudron timezone!
diff = ( ( ( new Date ( ) ) . getTime ( ) - date . getTime ( ) ) / 1000 ) + 30 , // add 30seconds for clock skew
day _diff = Math . floor ( diff / 86400 ) ;
2021-04-01 17:04:46 +02:00
if ( isNaN ( day _diff ) || day _diff < 0 ) return $translate . instant ( 'main.prettyDate.justNow' , { } ) ;
2021-01-29 11:52:47 +01:00
return day _diff === 0 && (
2021-04-01 17:04:46 +02:00
diff < 60 && $translate . instant ( 'main.prettyDate.justNow' , { } ) ||
diff < 120 && $translate . instant ( 'main.prettyDate.minutesAgo' , { m : 1 } ) ||
diff < 3600 && $translate . instant ( 'main.prettyDate.minutesAgo' , { m : Math . floor ( diff / 60 ) } ) ||
diff < 7200 && $translate . instant ( 'main.prettyDate.hoursAgo' , { h : 1 } ) ||
diff < 86400 && $translate . instant ( 'main.prettyDate.hoursAgo' , { h : Math . floor ( diff / 3600 ) } )
) ||
day _diff === 1 && $translate . instant ( 'main.prettyDate.yeserday' , { } ) ||
day _diff < 7 && $translate . instant ( 'main.prettyDate.daysAgo' , { d : day _diff } ) ||
day _diff < 31 && $translate . instant ( 'main.prettyDate.weeksAgo' , { w : Math . ceil ( day _diff / 7 ) } ) ||
day _diff < 365 && $translate . instant ( 'main.prettyDate.monthsAgo' , { m : Math . round ( day _diff / 30 ) } ) ||
$translate . instant ( 'main.prettyDate.yearsAgo' , { m : Math . round ( day _diff / 365 ) } ) ;
2021-01-29 11:52:47 +01:00
} ;
} ) ;
angular . module ( 'Application' ) . filter ( 'prettyLongDate' , function ( ) {
return function prettyLongDate ( utc ) {
return moment ( utc ) . format ( 'MMMM Do YYYY, h:mm:ss a' ) ; // this converts utc into browser timezone and not cloudron timezone!
} ;
} ) ;
angular . module ( 'Application' ) . filter ( 'prettyShortDate' , function ( ) {
return function prettyShortDate ( utc ) {
return moment ( utc ) . format ( 'MMMM Do YYYY' ) ; // this converts utc into browser timezone and not cloudron timezone!
} ;
} ) ;
2020-07-10 15:20:53 +02:00
angular . module ( 'Application' ) . filter ( 'markdown2html' , function ( ) {
var converter = new showdown . Converter ( {
simplifiedAutoLink : true ,
strikethrough : true ,
2020-08-08 21:58:12 -07:00
tables : true ,
openLinksInNewWindow : true
2020-07-10 15:20:53 +02:00
} ) ;
// without this cache, the code runs into some infinite loop (https://github.com/angular/angular.js/issues/3980)
var cache = { } ;
return function ( text ) {
if ( cache [ text ] ) return cache [ text ] ;
cache [ text ] = converter . makeHtml ( text ) ;
return cache [ text ] ;
} ;
} ) ;
2020-11-08 10:41:42 +01:00
angular . module ( 'Application' ) . config ( [ '$translateProvider' , function ( $translateProvider ) {
$translateProvider . useStaticFilesLoader ( {
prefix : 'translation/' ,
2021-01-30 11:55:21 +01:00
suffix : '.json?' + '<%= revision %>'
2020-11-08 10:41:42 +01:00
} ) ;
$translateProvider . useLocalStorage ( ) ;
$translateProvider . preferredLanguage ( 'en' ) ;
$translateProvider . fallbackLanguage ( 'en' ) ;
} ] ) ;
// Add shorthand "tr" filter to avoid having ot use "translate"
// This is a copy of the code at https://github.com/angular-translate/angular-translate/blob/master/src/filter/translate.js
// If we find out how to get that function handle somehow dynamically we can use that, otherwise the copy is required
function translateFilterFactory ( $parse , $translate ) {
var translateFilter = function ( translationId , interpolateParams , interpolation , forceLanguage ) {
if ( ! angular . isObject ( interpolateParams ) ) {
var ctx = this || {
'__SCOPE_IS_NOT_AVAILABLE' : 'More info at https://github.com/angular/angular.js/commit/8863b9d04c722b278fa93c5d66ad1e578ad6eb1f'
} ;
interpolateParams = $parse ( interpolateParams ) ( ctx ) ;
}
return $translate . instant ( translationId , interpolateParams , interpolation , forceLanguage ) ;
} ;
if ( $translate . statefulFilter ( ) ) {
translateFilter . $stateful = true ;
}
return translateFilter ;
}
translateFilterFactory . displayName = 'translateFilterFactory' ;
angular . module ( 'Application' ) . filter ( 'tr' , translateFilterFactory ) ;
2020-06-02 14:34:38 +02:00
// ----------------------------------------------
// Cloudron REST API wrapper
// ----------------------------------------------
2019-09-05 22:22:42 +02:00
angular . module ( 'Application' ) . service ( 'Client' , [ '$http' , '$interval' , '$timeout' , 'md5' , 'Notification' , function ( $http , $interval , $timeout , md5 , Notification ) {
2018-01-22 13:01:38 -08:00
var client = null ;
// variable available only here to avoid this._property pattern
var token = null ;
function ClientError ( statusCode , messageOrObject ) {
Error . call ( this ) ;
this . name = this . constructor . name ;
this . statusCode = statusCode ;
if ( messageOrObject === null || typeof messageOrObject === 'undefined' ) {
this . message = 'Empty message or object' ;
} else if ( typeof messageOrObject === 'string' ) {
this . message = messageOrObject ;
2019-09-01 21:38:30 -07:00
} else if ( messageOrObject ) {
angular . extend ( this , messageOrObject ) ; // status, message, reason and other properties
2018-01-22 13:01:38 -08:00
}
}
function defaultErrorHandler ( callback ) {
2020-03-06 02:38:17 -08:00
function handleServerOffline ( ) {
if ( client . offline ) return ;
client . offline = true ;
( function onlineCheck ( ) {
$http . get ( client . apiOrigin + '/api/v1/cloudron/status' , { } ) . success ( function ( data , status ) {
client . offline = false ;
client . _reconnectListener . forEach ( function ( handler ) { handler ( ) ; } ) ;
} ) . error ( function ( data , status ) {
$timeout ( onlineCheck , 5000 ) ;
} ) ;
} ) ( ) ;
}
2018-01-22 13:01:38 -08:00
return function ( data , status ) {
2018-12-11 18:17:53 +01:00
// handle request killed by browser (eg. cors issue)
if ( data === null && status === - 1 ) {
2020-03-06 02:38:17 -08:00
handleServerOffline ( ) ;
2019-01-10 14:37:43 +01:00
return callback ( new ClientError ( 'Request cancelled by browser' ) ) ;
2018-12-11 18:17:53 +01:00
}
2019-05-10 09:45:37 -07:00
// re-login will make the code get a new token
2020-03-06 12:23:50 -08:00
if ( status === 401 ) return client . login ( ) ;
2019-09-07 09:18:35 +02:00
if ( status === 500 || status === 501 ) {
2021-03-29 10:37:11 +02:00
// actual internal server error, most likely a bug or timeout log to console only to not alert the user
if ( ! client . offline ) {
console . error ( status , data ) ;
console . log ( '------\nCloudron Internal Error\n\nIf you see this, please send a mail with above log to support@cloudron.io\n------\n' ) ;
}
2019-09-07 09:18:35 +02:00
} else if ( status === 502 || status === 503 || status === 504 ) {
// This means the box service is not reachable. We just show offline banner for now
}
2018-12-11 18:17:53 +01:00
2021-06-22 16:25:11 +02:00
if ( status >= 502 ) {
2020-03-06 02:38:17 -08:00
handleServerOffline ( ) ;
2018-01-22 13:01:38 -08:00
return callback ( new ClientError ( status , data ) ) ;
}
var obj = data ;
try {
obj = JSON . parse ( data ) ;
} catch ( e ) { }
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( new ClientError ( status , obj ) ) ;
} ;
}
2018-12-11 18:17:53 +01:00
function defaultSuccessHandler ( callback ) {
return function ( data , status ) {
return callback ( null , data , status ) ;
2019-05-05 09:05:06 -07:00
} ;
2018-12-11 18:17:53 +01:00
}
2018-01-22 13:01:38 -08:00
// XHR wrapper to set the auth header
2018-12-11 18:17:53 +01:00
function get ( url , config , callback ) {
2018-12-11 18:55:25 +01:00
if ( arguments . length !== 3 ) {
console . error ( 'GET' , arguments ) ;
throw ( 'Wrong number of arguments' ) ;
}
2018-01-22 13:01:38 -08:00
config = config || { } ;
config . headers = config . headers || { } ;
config . headers . Authorization = 'Bearer ' + token ;
2021-03-02 16:24:15 +01:00
return $http . get ( client . apiOrigin + url , config )
2018-12-11 18:17:53 +01:00
. success ( defaultSuccessHandler ( callback ) )
. error ( defaultErrorHandler ( callback ) ) ;
2018-01-22 13:01:38 -08:00
}
2018-12-11 18:17:53 +01:00
function head ( url , config , callback ) {
2018-12-11 18:55:25 +01:00
if ( arguments . length !== 3 ) {
console . error ( 'HEAD' , arguments ) ;
throw ( 'Wrong number of arguments' ) ;
}
2018-01-22 13:01:38 -08:00
config = config || { } ;
config . headers = config . headers || { } ;
config . headers . Authorization = 'Bearer ' + token ;
2021-03-02 16:24:15 +01:00
return $http . head ( client . apiOrigin + url , config )
2018-12-11 18:17:53 +01:00
. success ( defaultSuccessHandler ( callback ) )
. error ( defaultErrorHandler ( callback ) ) ;
2018-01-22 13:01:38 -08:00
}
2018-12-11 18:17:53 +01:00
function post ( url , data , config , callback ) {
2018-12-11 18:55:25 +01:00
if ( arguments . length !== 4 ) {
console . error ( 'POST' , arguments ) ;
throw ( 'Wrong number of arguments' ) ;
}
2018-01-22 13:01:38 -08:00
data = data || { } ;
config = config || { } ;
config . headers = config . headers || { } ;
config . headers . Authorization = 'Bearer ' + token ;
2021-03-02 16:24:15 +01:00
return $http . post ( client . apiOrigin + url , data , config )
2018-12-11 18:17:53 +01:00
. success ( defaultSuccessHandler ( callback ) )
. error ( defaultErrorHandler ( callback ) ) ;
2018-01-22 13:01:38 -08:00
}
2018-12-11 18:17:53 +01:00
function put ( url , data , config , callback ) {
2018-12-11 18:55:25 +01:00
if ( arguments . length !== 4 ) {
console . error ( 'PUT' , arguments ) ;
throw ( 'Wrong number of arguments' ) ;
}
2018-01-22 13:01:38 -08:00
data = data || { } ;
config = config || { } ;
config . headers = config . headers || { } ;
config . headers . Authorization = 'Bearer ' + token ;
2021-03-02 16:24:15 +01:00
return $http . put ( client . apiOrigin + url , data , config )
2018-12-11 18:17:53 +01:00
. success ( defaultSuccessHandler ( callback ) )
. error ( defaultErrorHandler ( callback ) ) ;
2018-01-22 13:01:38 -08:00
}
2018-12-11 18:17:53 +01:00
function del ( url , config , callback ) {
2018-12-11 18:55:25 +01:00
if ( arguments . length !== 3 ) {
console . error ( 'DEL' , arguments ) ;
throw ( 'Wrong number of arguments' ) ;
}
2018-01-22 13:01:38 -08:00
config = config || { } ;
config . headers = config . headers || { } ;
config . headers . Authorization = 'Bearer ' + token ;
2021-03-02 16:24:15 +01:00
return $http . delete ( client . apiOrigin + url , config )
2018-12-11 18:17:53 +01:00
. success ( defaultSuccessHandler ( callback ) )
. error ( defaultErrorHandler ( callback ) ) ;
2018-01-22 13:01:38 -08:00
}
function Client ( ) {
2018-12-11 18:17:53 +01:00
this . offline = false ;
2018-01-22 13:01:38 -08:00
this . _ready = false ;
this . _configListener = [ ] ;
this . _readyListener = [ ] ;
2020-03-06 02:38:17 -08:00
this . _reconnectListener = [ ] ;
2018-01-22 13:01:38 -08:00
this . _userInfo = {
id : null ,
username : null ,
email : null ,
2019-08-30 13:36:52 +02:00
twoFactorAuthenticationEnabled : false ,
2019-11-25 16:12:43 +01:00
source : null ,
avatarUrl : null
2018-01-22 13:01:38 -08:00
} ;
this . _config = {
apiServerOrigin : null ,
webServerOrigin : null ,
fqdn : null ,
ip : null ,
revision : null ,
update : { box : null , apps : null } ,
progress : { } ,
region : null ,
2019-12-20 10:02:01 -08:00
size : null
2018-01-22 13:01:38 -08:00
} ;
this . _installedApps = [ ] ;
2018-06-26 19:07:34 -07:00
this . _installedAppsById = { } ;
2019-04-12 11:06:56 +02:00
this . _appTags = [ ] ;
2018-01-22 13:01:38 -08:00
// window.location fallback for websocket connections which do not have relative uris
2020-02-07 13:41:10 +01:00
this . apiOrigin = '<%= apiOrigin %>' || window . location . origin ;
2018-01-22 13:01:38 -08:00
this . avatar = '' ;
2020-11-18 00:28:10 +01:00
this . _availableLanguages = [ 'en' ] ;
2019-05-04 18:15:33 -07:00
this . _appstoreAppCache = [ ] ;
2018-01-22 13:01:38 -08:00
this . resetAvatar ( ) ;
this . setToken ( localStorage . token ) ;
}
2019-09-05 23:59:38 +02:00
Client . prototype . error = function ( error , action ) {
2018-01-22 13:01:38 -08:00
var message = '' ;
2019-09-05 23:59:38 +02:00
console . error ( error ) ;
2018-01-22 13:01:38 -08:00
if ( typeof error === 'object' ) {
message = error . message || error ;
} else {
message = error ;
}
2019-09-06 00:00:22 +02:00
// give more info in case the error was a request which failed with empty response body,
// this happens mostly if the box crashes
if ( message === 'Empty message or object' ) {
2021-01-29 11:15:33 +01:00
message = 'Got empty response. Click to check the server logs.' ;
2019-09-06 00:00:22 +02:00
action = action || '/logs.html?id=box' ;
}
2019-09-05 23:59:38 +02:00
this . notify ( 'Cloudron Error' , message , true , 'error' , action ) ;
2018-01-22 13:01:38 -08:00
} ;
2019-09-05 22:22:42 +02:00
// handles application startup errors, mostly only when dashboard is loaded and api endpoint is down
Client . prototype . initError = function ( error , initFunction ) {
console . error ( 'Application startup error' , error ) ;
$timeout ( initFunction , 5000 ) ; // we will try to re-init the app
} ;
2018-06-07 11:19:43 -07:00
Client . prototype . clearNotifications = function ( ) {
Notification . clearAll ( ) ;
} ;
2018-01-22 13:01:38 -08:00
/ *
2019-01-07 17:30:01 +01:00
If ` action ` is a non - empty string , it will be treated as a url , if it is a function , that function will be exectued on click
2018-01-22 13:01:38 -08:00
* /
2019-01-07 17:23:26 +01:00
Client . prototype . notify = function ( title , message , persistent , type , action ) {
2018-01-22 13:01:38 -08:00
var options = { title : title , message : message } ;
2018-06-07 11:19:43 -07:00
if ( persistent ) options . delay = 'never' ; // any non Number means never timeout
2018-01-22 13:01:38 -08:00
2019-01-07 17:23:26 +01:00
if ( action ) {
options . onClick = function ( /* params */ ) {
// if action is a string, we assume it is a link
if ( typeof action === 'string' && action !== '' ) window . location = action ;
else if ( typeof action === 'function' ) action ( ) ;
else console . warn ( 'Notification action is not supported.' , action ) ;
} ;
2018-01-22 13:01:38 -08:00
}
if ( type === 'error' ) Notification . error ( options ) ;
else if ( type === 'success' ) Notification . success ( options ) ;
else if ( type === 'info' ) Notification . info ( options ) ;
else if ( type === 'warning' ) Notification . warning ( options ) ;
else throw ( 'Invalid notification type "' + type + '"' ) ;
} ;
Client . prototype . setReady = function ( ) {
if ( this . _ready ) return ;
this . _ready = true ;
this . _readyListener . forEach ( function ( callback ) {
callback ( ) ;
} ) ;
// clear the listeners, we only callback once!
this . _readyListener = [ ] ;
} ;
Client . prototype . onReady = function ( callback ) {
if ( this . _ready ) callback ( ) ;
else this . _readyListener . push ( callback ) ;
} ;
Client . prototype . onConfig = function ( callback ) {
this . _configListener . push ( callback ) ;
if ( this . _config && this . _config . apiServerOrigin ) callback ( this . _config ) ;
} ;
2020-03-06 02:38:17 -08:00
Client . prototype . onReconnect = function ( callback ) {
if ( this . _ready ) callback ( ) ;
else this . _reconnectListener . push ( callback ) ;
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . resetAvatar = function ( ) {
this . avatar = this . apiOrigin + '/api/v1/cloudron/avatar?' + String ( Math . random ( ) ) . slice ( 2 ) ;
var favicon = $ ( '#favicon' ) ;
if ( favicon ) favicon . attr ( 'href' , this . avatar ) ;
} ;
Client . prototype . setUserInfo = function ( userInfo ) {
// In order to keep the angular bindings alive, set each property individually
this . _userInfo . id = userInfo . id ;
this . _userInfo . username = userInfo . username ;
this . _userInfo . email = userInfo . email ;
this . _userInfo . fallbackEmail = userInfo . fallbackEmail ;
this . _userInfo . displayName = userInfo . displayName ;
2018-04-26 15:12:29 +02:00
this . _userInfo . twoFactorAuthenticationEnabled = userInfo . twoFactorAuthenticationEnabled ;
2020-02-24 12:56:13 +01:00
this . _userInfo . role = userInfo . role ;
2019-08-30 13:36:52 +02:00
this . _userInfo . source = userInfo . source ;
2019-12-13 13:50:48 -08:00
this . _userInfo . avatarUrl = userInfo . avatarUrl + '?s=128&default=mp&ts=' + Date . now ( ) ; // we add the timestamp to avoid caching
2020-02-24 12:56:13 +01:00
this . _userInfo . isAtLeastOwner = [ ROLES . OWNER ] . indexOf ( userInfo . role ) !== - 1 ;
this . _userInfo . isAtLeastAdmin = [ ROLES . OWNER , ROLES . ADMIN ] . indexOf ( userInfo . role ) !== - 1 ;
this . _userInfo . isAtLeastUserManager = [ ROLES . OWNER , ROLES . ADMIN , ROLES . USER _MANAGER ] . indexOf ( userInfo . role ) !== - 1 ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . setConfig = function ( config ) {
var that = this ;
angular . copy ( config , this . _config ) ;
2019-08-05 20:32:56 +02:00
< % if ( appstore . webOrigin ) { - % >
this . _config . webServerOrigin = '<%= appstore.webOrigin %>' ;
< % } - % >
< % if ( appstore . ApiOrigin ) { - % >
2019-08-06 10:22:00 +02:00
this . _config . apiServerOrigin = '<%= appstore.apiOrigin %>' ;
2019-08-05 20:32:56 +02:00
< % } - % >
2020-02-14 20:44:27 +01:00
// => This is just for easier testing
2020-02-26 16:48:51 +01:00
// this._config.features.userMaxCount = 5;
2021-01-14 16:48:41 +01:00
// this._config.features.userRoles = false;
// this._config.features.userGroups = false;
2020-06-18 13:56:37 +02:00
// this._config.features.domainMaxCount = 1;
2020-02-14 20:44:27 +01:00
// this._config.features.externalLdap = false;
// this._config.features.privateDockerRegistry = false;
2020-03-09 14:19:12 -07:00
// this._config.features.branding = true;
2020-03-06 01:08:55 -08:00
// this._config.features.support = true;
2020-07-09 21:51:51 -07:00
// this._config.features.directoryConfig = true;
2020-07-16 18:51:29 +02:00
// this._config.features.mailboxMaxCount = 5;
// this._config.features.emailPremium = false;
2020-02-14 20:44:27 +01:00
2018-01-22 13:01:38 -08:00
this . _configListener . forEach ( function ( callback ) {
callback ( that . _config ) ;
} ) ;
} ;
Client . prototype . getInstalledApps = function ( ) {
return this . _installedApps ;
} ;
2019-04-12 11:06:56 +02:00
Client . prototype . getAppTags = function ( ) {
return this . _appTags ;
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . getUserInfo = function ( ) {
return this . _userInfo ;
} ;
Client . prototype . getConfig = function ( ) {
return this . _config ;
} ;
2020-11-18 00:28:10 +01:00
Client . prototype . getAvailableLanguages = function ( ) {
return this . _availableLanguages ;
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . setToken = function ( accessToken ) {
if ( ! accessToken ) localStorage . removeItem ( 'token' ) ;
else localStorage . token = accessToken ;
// set the token closure
token = accessToken ;
} ;
Client . prototype . getToken = function ( ) {
return token ;
} ;
2018-12-11 18:17:53 +01:00
Client . prototype . makeURL = function ( url ) {
if ( url . indexOf ( '?' ) === - 1 ) {
return this . apiOrigin + url + '?access_token=' + token ;
} else {
return this . apiOrigin + url + '&access_token=' + token ;
}
} ;
2018-01-22 13:01:38 -08:00
/ *
* Rest API wrappers
* /
Client . prototype . config = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/config' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 || typeof data !== 'object' ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . userInfo = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/profile' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 || typeof data !== 'object' ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . changeCloudronAvatar = function ( avatarFile , callback ) {
var fd = new FormData ( ) ;
fd . append ( 'avatar' , avatarFile ) ;
2018-12-11 18:17:53 +01:00
var config = {
2018-01-22 13:01:38 -08:00
headers : { 'Content-Type' : undefined } ,
transformRequest : angular . identity
2018-12-11 18:17:53 +01:00
} ;
2020-03-18 21:31:51 -07:00
post ( '/api/v1/branding/cloudron_avatar' , fd , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . changeCloudronName = function ( name , callback ) {
var data = {
name : name
} ;
2020-03-18 21:31:51 -07:00
post ( '/api/v1/branding/cloudron_name' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . installApp = function ( id , manifest , title , config , callback ) {
var that = this ;
var data = {
appStoreId : id + '@' + manifest . version ,
location : config . location ,
domain : config . domain ,
portBindings : config . portBindings ,
accessRestriction : config . accessRestriction ,
cert : config . cert ,
key : config . key ,
2019-09-24 00:21:01 +02:00
sso : config . sso ,
overwriteDns : config . overwriteDns
2018-01-22 13:01:38 -08:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/apps/install' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . id ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-05-28 00:47:53 -07:00
Client . prototype . cloneApp = function ( appId , config , callback ) {
var data = {
location : config . location ,
domain : config . domain ,
portBindings : config . portBindings ,
backupId : config . backupId
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/apps/' + appId + '/clone' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-05-28 00:47:53 -07:00
2018-06-26 08:33:04 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-05-28 00:47:53 -07:00
} ;
2019-05-13 23:31:45 +02:00
Client . prototype . restoreApp = function ( appId , backupId , callback ) {
var data = { backupId : backupId } ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/apps/' + appId + '/restore' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-05-23 20:36:54 -07:00
Client . prototype . backupApp = function ( appId , callback ) {
2018-12-11 18:17:53 +01:00
var data = { } ;
post ( '/api/v1/apps/' + appId + '/backup' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-05-23 20:36:54 -07:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-05-23 20:36:54 -07:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-05-23 20:36:54 -07:00
} ;
2019-05-13 23:31:45 +02:00
Client . prototype . uninstallApp = function ( appId , callback ) {
var data = { } ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/apps/' + appId + '/uninstall' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2019-09-10 19:21:30 +02:00
Client . prototype . configureApp = function ( id , setting , data , callback ) {
post ( '/api/v1/apps/' + id + '/configure/' + setting , data , null , function ( error , data , status ) {
2019-09-21 22:45:26 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 && status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . repairApp = function ( id , data , callback ) {
2019-12-06 11:44:33 -08:00
post ( '/api/v1/apps/' + id + '/repair' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2019-09-10 19:21:30 +02:00
if ( status !== 200 && status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2019-09-10 19:21:30 +02:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2019-09-26 20:10:25 -07:00
Client . prototype . updateApp = function ( id , manifest , options , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
2019-09-26 20:10:25 -07:00
appStoreId : manifest . id + '@' + manifest . version ,
skipBackup : ! ! options . skipBackup
2018-01-22 13:01:38 -08:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/apps/' + id + '/update' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2019-09-23 15:50:41 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . startApp = function ( id , callback ) {
2018-12-14 16:01:10 -08:00
post ( '/api/v1/apps/' + id + '/start' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2019-09-23 15:50:41 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . stopApp = function ( id , callback ) {
2018-12-14 16:01:10 -08:00
post ( '/api/v1/apps/' + id + '/stop' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2019-09-23 15:50:41 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2019-09-12 17:42:33 +02:00
Client . prototype . restartApp = function ( id , callback ) {
2020-01-06 16:38:48 +01:00
post ( '/api/v1/apps/' + id + '/restart' , { } , null , function ( error , data , status ) {
2019-09-12 17:42:33 +02:00
if ( error ) return callback ( error ) ;
2019-12-20 11:18:48 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2019-09-12 17:42:33 +02:00
2019-12-20 11:18:48 -08:00
callback ( null , data ) ;
2019-09-12 17:42:33 +02:00
} ) ;
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . debugApp = function ( id , state , callback ) {
var data = {
debugMode : state ? {
2021-03-26 13:20:38 -07:00
readonlyRootfs : false ,
2018-01-22 13:01:38 -08:00
cmd : [ '/bin/bash' , '-c' , 'echo "Repair mode. Use the webterminal or cloudron exec to repair. Sleeping" && sleep infinity' ]
} : null
} ;
2019-09-19 17:30:24 -07:00
post ( '/api/v1/apps/' + id + '/configure/debug_mode' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . version = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/cloudron/status' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2019-09-05 21:52:26 +02:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getStatus = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/cloudron/status' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 || typeof data !== 'object' ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
2019-09-05 21:52:26 +02:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . setBackupConfig = function ( backupConfig , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/settings/backup_config' , backupConfig , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getBackupConfig = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/settings/backup_config' , null , function ( error , data , status ) {
2020-07-28 18:08:57 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2020-02-05 14:15:46 -08:00
Client . prototype . getSupportConfig = function ( callback ) {
get ( '/api/v1/settings/support_config' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2019-08-29 09:59:57 +02:00
Client . prototype . setExternalLdapConfig = function ( config , callback ) {
post ( '/api/v1/settings/external_ldap_config' , config , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getExternalLdapConfig = function ( callback ) {
get ( '/api/v1/settings/external_ldap_config' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-07-09 21:51:51 -07:00
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . setDirectoryConfig = function ( config , callback ) {
post ( '/api/v1/settings/directory_config' , config , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getDirectoryConfig = function ( callback ) {
get ( '/api/v1/settings/directory_config' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2019-08-29 09:59:57 +02:00
callback ( null , data ) ;
} ) ;
} ;
2020-08-31 21:45:56 -07:00
// network
2019-10-31 19:33:05 -07:00
Client . prototype . setSysinfoConfig = function ( config , callback ) {
post ( '/api/v1/settings/sysinfo_config' , config , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2019-11-07 10:41:24 -08:00
Client . prototype . getServerIp = function ( callback ) {
get ( '/api/v1/cloudron/server_ip' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . ip ) ;
} ) ;
} ;
2019-10-31 19:33:05 -07:00
Client . prototype . getSysinfoConfig = function ( callback ) {
get ( '/api/v1/settings/sysinfo_config' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2020-08-31 21:45:56 -07:00
Client . prototype . getBlocklist = function ( callback ) {
var config = { } ;
get ( '/api/v1/network/blocklist' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . blocklist ) ;
} ) ;
} ;
Client . prototype . setBlocklist = function ( blocklist , callback ) {
post ( '/api/v1/network/blocklist' , { blocklist : blocklist } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2018-10-31 16:03:09 +01:00
Client . prototype . setDynamicDnsConfig = function ( enabled , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/settings/dynamic_dns' , { enabled : enabled } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-10-31 16:03:09 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-10-31 16:03:09 +01:00
} ;
Client . prototype . getDynamicDnsConfig = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/settings/dynamic_dns' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-10-31 16:03:09 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-10-31 16:03:09 +01:00
callback ( null , data . enabled ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-10-31 16:03:09 +01:00
} ;
2020-08-31 21:45:56 -07:00
// branding
2020-02-14 15:34:44 +01:00
Client . prototype . setFooter = function ( footer , callback ) {
2020-03-18 17:53:50 -07:00
post ( '/api/v1/branding/footer' , { footer : footer } , null , function ( error , data , status ) {
2020-02-14 15:34:44 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getFooter = function ( callback ) {
2020-03-18 17:53:50 -07:00
get ( '/api/v1/branding/footer' , null , function ( error , data , status ) {
2020-02-14 15:34:44 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . footer ) ;
} ) ;
} ;
2019-04-27 22:38:09 +02:00
Client . prototype . setUnstableAppsConfig = function ( enabled , callback ) {
post ( '/api/v1/settings/unstable_apps' , { enabled : enabled } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getUnstableAppsConfig = function ( callback ) {
get ( '/api/v1/settings/unstable_apps' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . enabled ) ;
} ) ;
} ;
2019-10-22 22:31:38 -07:00
Client . prototype . setRegistryConfig = function ( rc , callback ) {
post ( '/api/v1/settings/registry_config' , rc , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getRegistryConfig = function ( callback ) {
get ( '/api/v1/settings/registry_config' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2018-06-28 17:44:11 -07:00
Client . prototype . getUpdateInfo = function ( callback ) {
2020-02-24 17:15:50 +01:00
if ( ! this . _userInfo . isAtLeastAdmin ) return callback ( new Error ( 'Not allowed' ) ) ;
2019-05-14 16:41:08 +02:00
2018-12-11 18:17:53 +01:00
get ( '/api/v1/cloudron/update' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-06-28 17:44:11 -07:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-06-28 17:44:11 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-06-28 17:44:11 -07:00
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . checkForUpdates = function ( callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/cloudron/check_for_updates' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
client . refreshConfig ( callback ) ;
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-08-19 22:30:48 -07:00
Client . prototype . setAutoupdatePattern = function ( pattern , callback ) {
post ( '/api/v1/settings/autoupdate_pattern' , { pattern : pattern } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-02-06 19:23:52 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-02-06 19:23:52 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-02-06 19:23:52 +01:00
} ;
2020-08-19 22:30:48 -07:00
Client . prototype . getAutoupdatePattern = function ( callback ) {
get ( '/api/v1/settings/autoupdate_pattern' , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-01-07 21:41:45 +01:00
Client . prototype . setTimeZone = function ( timeZone , callback ) {
post ( '/api/v1/settings/time_zone' , { timeZone : timeZone } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getTimeZone = function ( callback ) {
get ( '/api/v1/settings/time_zone' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . timeZone ) ;
} ) ;
} ;
2020-11-18 00:10:29 +01:00
Client . prototype . setLanguage = function ( language , callback ) {
post ( '/api/v1/settings/language' , { language : language } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getLanguage = function ( callback ) {
get ( '/api/v1/settings/language' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . language ) ;
} ) ;
} ;
2018-12-19 13:22:27 -08:00
Client . prototype . getRemoteSupport = function ( callback ) {
get ( '/api/v1/support/remote_support' , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-12-19 13:22:27 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-12-19 13:22:27 -08:00
callback ( null , data . enabled ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-12-19 13:22:27 -08:00
Client . prototype . enableRemoteSupport = function ( enable , callback ) {
post ( '/api/v1/support/remote_support' , { enable : enable } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getBackups = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/backups' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . backups ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-12-08 20:21:11 -08:00
Client . prototype . getLatestTaskByType = function ( type , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/tasks?page=1&per_page=1&type=' + type , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2019-01-06 14:52:36 -08:00
callback ( null , data . tasks . length ? data . tasks [ 0 ] : null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-12-08 20:21:11 -08:00
} ;
Client . prototype . getTask = function ( taskId , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/tasks/' + taskId , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-11-19 17:34:14 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-19 17:34:14 -08:00
} ;
2018-12-08 21:45:49 -08:00
Client . prototype . getTaskLogs = function ( taskId , follow , lines , callback ) {
if ( follow ) {
var eventSource = new EventSource ( client . apiOrigin + '/api/v1/tasks/' + taskId + '/logstream?lines=' + lines + '&access_token=' + token ) ;
callback ( null , eventSource ) ;
} else {
2019-01-08 13:12:37 -08:00
get ( '/api/v1/services/' + taskId + '/logs?lines=' + lines , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-08 21:45:49 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-12-08 21:45:49 -08:00
}
} ;
2018-11-17 19:55:37 -08:00
Client . prototype . startBackup = function ( callback ) {
2019-04-13 18:09:51 -07:00
post ( '/api/v1/backups/create' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . taskId ) ;
} ) ;
} ;
Client . prototype . cleanupBackups = function ( callback ) {
post ( '/api/v1/backups/cleanup' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-08 20:21:11 -08:00
callback ( null , data . taskId ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-17 19:55:37 -08:00
} ;
2018-11-19 17:34:14 -08:00
Client . prototype . stopTask = function ( taskId , callback ) {
2018-12-14 16:01:10 -08:00
post ( '/api/v1/tasks/' + taskId + '/stop' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2019-01-06 14:52:36 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-02-24 15:16:29 -08:00
Client . prototype . restore = function ( backupConfig , backupId , version , sysinfoConfig , skipDnsSetup , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
backupConfig : backupConfig ,
backupId : backupId ,
2019-11-11 11:07:52 -08:00
version : version ,
2021-02-24 15:16:29 -08:00
sysinfoConfig : sysinfoConfig ,
skipDnsSetup : skipDnsSetup
2018-01-22 13:01:38 -08:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/cloudron/restore' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-02-06 16:08:22 -08:00
Client . prototype . importBackup = function ( appId , backupId , backupFormat , backupConfig , callback ) {
var data = {
backupId : backupId ,
backupFormat : backupFormat ,
backupConfig : backupConfig ,
} ;
post ( '/api/v1/apps/' + appId + '/import' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status ) ) ;
callback ( null ) ;
} ) ;
} ;
2021-05-29 12:11:17 -07:00
Client . prototype . getNotifications = function ( options , page , perPage , callback ) {
2019-01-07 17:23:26 +01:00
var config = {
params : {
page : page ,
per _page : perPage
}
} ;
2021-05-29 12:11:17 -07:00
if ( typeof options . acknowledged === 'boolean' ) config . params . acknowledged = options . acknowledged ;
2019-01-07 17:23:26 +01:00
get ( '/api/v1/notifications' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . notifications ) ;
} ) ;
} ;
2021-05-29 12:11:17 -07:00
Client . prototype . ackNotification = function ( notificationId , acknowledged , callback ) {
post ( '/api/v1/notifications/' + notificationId , { acknowledged : acknowledged } , null , function ( error , data , status ) {
2019-01-07 18:04:52 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status ) ) ;
callback ( null ) ;
} ) ;
} ;
2019-01-23 16:44:57 +01:00
Client . prototype . getEvent = function ( eventId , callback ) {
get ( '/api/v1/cloudron/eventlog/' + eventId , { } , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . event ) ;
} ) ;
} ;
2018-03-05 11:15:25 +01:00
Client . prototype . getEventLogs = function ( actions , search , page , perPage , callback ) {
2018-01-22 13:01:38 -08:00
var config = {
params : {
2018-03-05 11:15:25 +01:00
actions : actions ,
2018-01-22 13:01:38 -08:00
search : search ,
page : page ,
per _page : perPage
}
} ;
2018-12-11 19:03:35 +01:00
get ( '/api/v1/cloudron/eventlog' , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . eventlogs ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-06-11 20:10:57 +02:00
Client . prototype . getPlatformLogs = function ( unit , follow , lines , callback ) {
2018-01-22 13:01:38 -08:00
if ( follow ) {
2018-06-11 20:10:57 +02:00
var eventSource = new EventSource ( client . apiOrigin + '/api/v1/cloudron/logstream/' + unit + '?lines=' + lines + '&access_token=' + token ) ;
2018-01-22 13:01:38 -08:00
callback ( null , eventSource ) ;
} else {
2019-01-08 13:12:37 -08:00
get ( '/api/v1/cloudron/logs/' + unit + '?lines=' + lines , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
}
} ;
2018-12-02 18:07:11 -08:00
Client . prototype . getServiceLogs = function ( serviceName , follow , lines , callback ) {
2018-11-26 14:49:50 +01:00
if ( follow ) {
2018-12-02 18:07:11 -08:00
var eventSource = new EventSource ( client . apiOrigin + '/api/v1/services/' + serviceName + '/logstream?lines=' + lines + '&access_token=' + token ) ;
2018-11-26 14:49:50 +01:00
callback ( null , eventSource ) ;
} else {
2019-01-08 13:12:37 -08:00
get ( '/api/v1/services/' + serviceName + '/logs?lines=' + lines , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-11-26 14:49:50 +01:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-26 14:49:50 +01:00
}
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . getApps = function ( callback ) {
2020-06-08 17:20:18 -07:00
var that = this ;
2018-12-11 18:17:53 +01:00
get ( '/api/v1/apps' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-06-08 17:20:18 -07:00
var apps = data . apps ;
for ( var i = 0 ; i < apps . length ; i ++ ) {
that . _appPostProcess ( apps [ i ] ) ; // this will also set the correct iconUrl
}
callback ( null , apps ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getAppLogs = function ( appId , follow , lines , callback ) {
if ( follow ) {
var eventSource = new EventSource ( client . apiOrigin + '/api/v1/apps/' + appId + '/logstream?lines=' + lines + '&access_token=' + token ) ;
callback ( null , eventSource ) ;
} else {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/apps/' + appId + '/logs' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
}
} ;
Client . prototype . getAppBackups = function ( appId , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/apps/' + appId + '/backups' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . backups ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-12-02 18:07:11 -08:00
Client . prototype . getServices = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/services' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-02 18:07:11 -08:00
callback ( null , data . services ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-15 19:59:24 +01:00
} ;
2018-12-02 18:07:11 -08:00
Client . prototype . getService = function ( serviceName , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/services/' + serviceName , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-02 18:07:11 -08:00
callback ( null , data . service ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-15 19:59:24 +01:00
} ;
2020-01-28 13:34:08 -08:00
Client . prototype . configureService = function ( serviceName , data , callback ) {
post ( '/api/v1/services/' + serviceName , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-11-21 15:57:26 +01:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-11-21 15:57:26 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-20 16:53:42 +01:00
} ;
2018-12-02 18:07:11 -08:00
Client . prototype . restartService = function ( serviceName , callback ) {
2018-12-14 16:01:10 -08:00
post ( '/api/v1/services/' + serviceName + '/restart' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-11-16 17:21:57 +01:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-11-16 17:21:57 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-16 17:21:57 +01:00
} ;
2021-01-21 17:41:16 -08:00
Client . prototype . rebuildService = function ( serviceName , callback ) {
post ( '/api/v1/services/' + serviceName + '/rebuild' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2019-01-16 14:02:08 +01:00
Client . prototype . getUsers = function ( search , page , perPage , callback ) {
if ( typeof search === 'function' ) {
callback = search ;
search = '' ;
2019-01-15 13:30:37 +01:00
page = 1 ;
2019-11-05 22:08:48 +01:00
perPage = 5000 ;
2019-01-15 13:30:37 +01:00
}
2019-01-14 16:57:41 +01:00
var config = {
params : {
2019-01-15 13:30:37 +01:00
page : page ,
per _page : perPage
2019-01-14 16:57:41 +01:00
}
} ;
2019-01-16 14:02:08 +01:00
if ( search ) config . params . search = search ;
2019-01-14 16:57:41 +01:00
get ( '/api/v1/users' , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . users ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-06-25 16:14:14 -07:00
Client . prototype . getUser = function ( userId , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/users/' + userId , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-06-25 16:14:14 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-06-25 16:14:14 -07:00
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . getGroups = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/groups' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . groups ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . setGroups = function ( userId , groupIds , callback ) {
2018-12-11 18:17:53 +01:00
put ( '/api/v1/users/' + userId + '/groups' , { groupIds : groupIds } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getGroup = function ( groupId , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/groups/' + groupId , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-07-26 11:38:20 -07:00
Client . prototype . createGroup = function ( name , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
2018-07-26 11:38:20 -07:00
name : name
2018-01-22 13:01:38 -08:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/groups' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-07-26 11:38:20 -07:00
Client . prototype . updateGroup = function ( id , name , callback ) {
2018-06-18 17:45:09 -07:00
var data = {
2018-07-26 11:38:20 -07:00
name : name
2018-06-18 17:45:09 -07:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/groups/' + id , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-07-24 22:20:00 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-07-24 22:20:00 -07:00
} ;
Client . prototype . setGroupMembers = function ( id , userIds , callback ) {
var data = {
userIds : userIds
} ;
2018-12-11 18:17:53 +01:00
put ( '/api/v1/groups/' + id + '/members' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-06-18 17:45:09 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-06-18 17:45:09 -07:00
} ;
2019-05-13 23:55:54 +02:00
Client . prototype . removeGroup = function ( groupId , callback ) {
2018-01-22 13:01:38 -08:00
var config = {
2019-05-13 23:55:54 +02:00
data : { } ,
2018-01-22 13:01:38 -08:00
headers : {
'Content-Type' : 'application/json'
}
} ;
2018-12-11 18:17:53 +01:00
del ( '/api/v1/groups/' + groupId , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getApp = function ( appId , callback ) {
2019-09-10 19:21:30 +02:00
var that = this ;
2018-12-11 18:17:53 +01:00
get ( '/api/v1/apps/' + appId , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2019-09-10 19:21:30 +02:00
that . _appPostProcess ( data ) ;
2019-03-20 09:22:54 -07:00
2018-06-25 18:55:07 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-06-25 18:55:07 -07:00
} ;
2020-06-14 13:35:28 +02:00
Client . prototype . getAppWithTask = function ( appId , callback ) {
var that = this ;
this . getApp ( appId , function ( error , app ) {
if ( error ) return callback ( error ) ;
if ( ! app . taskId ) return callback ( null , app ) ;
that . getTask ( app . taskId , function ( error , task ) {
if ( error ) return callback ( error ) ;
if ( task ) {
app . progress = task . percent ;
app . message = task . message ;
app . taskMinutesActive = moment . duration ( moment . utc ( ) . diff ( moment . utc ( task . creationTime ) ) ) . asMinutes ( ) ;
} else {
app . progress = 0 ;
app . message = '' ;
app . taskMinutesActive = 0 ;
}
callback ( null , app ) ;
} ) ;
} ) ;
} ;
2019-02-05 16:40:46 +01:00
Client . prototype . getCachedAppSync = function ( appId ) {
var appFound = null ;
this . _installedApps . some ( function ( app ) {
if ( app . id === appId ) {
appFound = app ;
return true ;
} else {
return false ;
}
} ) ;
return appFound ;
} ;
2018-08-17 16:01:40 -07:00
Client . prototype . sendInvite = function ( userId , callback ) {
2018-12-14 16:01:10 -08:00
post ( '/api/v1/users/' + userId + '/send_invite' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-08-17 16:01:40 -07:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2021-09-16 15:46:26 +02:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-08-17 16:01:40 -07:00
} ;
2021-04-14 20:58:43 -07:00
Client . prototype . disableTwoFactorAuthenticationByUserId = function ( userId , callback ) {
post ( '/api/v1/users/' + userId + '/twofactorauthentication_disable' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2019-12-11 13:58:29 -08:00
Client . prototype . setup = function ( data , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/cloudron/setup' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-12-21 22:36:43 -08:00
Client . prototype . createAdmin = function ( data , callback ) {
2018-01-22 13:01:38 -08:00
var that = this ;
2020-12-21 22:36:43 -08:00
post ( '/api/v1/cloudron/activate' , data , null , function ( error , result , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2020-12-21 22:36:43 -08:00
if ( status !== 201 ) return callback ( new ClientError ( status , result ) ) ;
2018-01-22 13:01:38 -08:00
2020-12-21 22:36:43 -08:00
that . setToken ( result . token ) ;
that . setUserInfo ( { username : data . username , email : data . email , admin : true , twoFactorAuthenticationEnabled : false , source : '' , avatarUrl : null } ) ;
2018-01-22 13:01:38 -08:00
2020-12-21 22:36:43 -08:00
callback ( null , result . activated ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-02-07 16:42:35 +01:00
Client . prototype . getTokens = function ( callback ) {
get ( '/api/v1/tokens/' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . tokens ) ;
} ) ;
} ;
Client . prototype . createToken = function ( name , callback ) {
var data = {
name : name
} ;
post ( '/api/v1/tokens' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2021-06-04 13:11:06 -07:00
callback ( null , data ) ;
2020-02-07 16:42:35 +01:00
} ) ;
} ;
// FIXME clashes with existing getToken()
// Client.prototype.getToken = function (id, callback) {
// get('/api/v1/tokens/' + id, null, function (error, data, status) {
// if (error) return callback(error);
// if (status !== 200) return callback(new ClientError(status, data));
// callback(null, data.token);
// });
// };
Client . prototype . delToken = function ( tokenId , callback ) {
del ( '/api/v1/tokens/' + tokenId , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2020-02-01 10:04:09 -08:00
Client . prototype . addAppPassword = function ( identifier , name , callback ) {
var data = {
identifier : identifier ,
name : name
} ;
post ( '/api/v1/app_passwords' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . getAppPasswords = function ( callback ) {
get ( '/api/v1/app_passwords' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . delAppPassword = function ( id , callback ) {
del ( '/api/v1/app_passwords/' + id , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2019-05-12 13:08:40 -07:00
Client . prototype . update = function ( options , callback ) {
var data = {
skipBackup : ! ! options . skipBackup
} ;
2018-01-22 13:01:38 -08:00
2018-12-11 18:17:53 +01:00
post ( '/api/v1/cloudron/update' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-08 20:21:11 -08:00
callback ( null , data . taskId ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-11-26 08:48:58 +01:00
Client . prototype . isRebootRequired = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/cloudron/reboot' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-11-26 08:48:58 +01:00
callback ( null , data . rebootRequired ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-11-26 08:48:58 +01:00
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . reboot = function ( callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/cloudron/reboot' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . setCertificate = function ( certificateFile , keyFile , callback ) {
var data = {
cert : certificateFile ,
key : keyFile
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/settings/certificate' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . disks = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/cloudron/disks' , null , function ( error , data , status ) {
2019-11-21 13:22:24 -08:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . memory = function ( callback ) {
get ( '/api/v1/cloudron/memory' , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2019-08-20 19:38:32 -07:00
Client . prototype . graphs = function ( targets , from , options , callback ) {
2020-04-18 21:25:10 -07:00
// if we have a lot of apps, targets can be very large. node will just disconnect since it exceeds header size
var size = 10 , chunks = [ ] ;
for ( var i = 0 ; i < targets . length ; i += size ) {
chunks . push ( targets . slice ( i , i + size ) ) ;
}
2018-01-22 13:01:38 -08:00
2020-04-18 21:25:10 -07:00
var result = [ ] ;
2020-06-03 23:17:06 +02:00
async . eachSeries ( chunks , function ( chunk , iteratorCallback ) {
2020-04-18 21:25:10 -07:00
var config = {
params : {
target : chunk ,
format : 'json' ,
2020-05-22 17:16:37 +02:00
from : from ,
until : 'now'
2020-04-18 21:25:10 -07:00
}
} ;
2019-08-20 19:38:32 -07:00
2020-04-18 21:25:10 -07:00
if ( options . noNullPoints ) config . params . noNullPoints = true ;
2018-12-11 18:17:53 +01:00
2020-04-18 21:25:10 -07:00
get ( '/api/v1/cloudron/graphs' , config , function ( error , data , status ) {
if ( error ) return iteratorCallback ( error ) ;
if ( status !== 200 ) return iteratorCallback ( new ClientError ( status , data ) ) ;
// the datapoint returned here is an [value, timestamp]
result = result . concat ( data ) ;
iteratorCallback ( null ) ;
} ) ;
} , function iteratorDone ( error ) {
callback ( error , result ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-10-05 15:20:07 +02:00
Client . prototype . createTicket = function ( ticket , callback ) {
// just to be eplicit here
2018-01-22 13:01:38 -08:00
var data = {
2020-10-05 15:20:07 +02:00
enableSshSupport : ! ! ticket . enableSshSupport ,
type : ticket . type ,
subject : ticket . subject ,
description : ticket . description ,
appId : ticket . appId || undefined ,
altEmail : ticket . altEmail || undefined
2018-01-22 13:01:38 -08:00
} ;
2019-05-07 11:36:12 -07:00
post ( '/api/v1/support/ticket' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2019-05-28 10:04:18 -07:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-08-10 13:53:28 -07:00
Client . prototype . addUser = function ( user , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
2018-07-26 15:52:06 -07:00
email : user . email ,
displayName : user . displayName ,
2020-02-21 21:12:25 +01:00
role : user . role
2018-01-22 13:01:38 -08:00
} ;
2018-07-26 15:52:06 -07:00
if ( user . username !== null ) data . username = user . username ;
2021-09-16 08:40:25 +02:00
if ( user . password !== null ) data . password = user . password ;
2018-01-22 13:01:38 -08:00
2018-12-11 18:17:53 +01:00
post ( '/api/v1/users' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2021-08-10 13:53:28 -07:00
callback ( null , data . id ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . updateUser = function ( user , callback ) {
var data = {
email : user . email ,
2018-01-24 16:21:14 +01:00
displayName : user . displayName ,
2018-07-26 15:45:52 -07:00
fallbackEmail : user . fallbackEmail ,
2020-02-17 14:05:26 +01:00
active : user . active ,
2020-02-21 21:12:25 +01:00
role : user . role
2018-01-22 13:01:38 -08:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/users/' + user . id , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-01-15 14:28:52 +01:00
Client . prototype . changeOwnership = function ( userId , callback ) {
post ( '/api/v1/users/' + userId + '/make_owner' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2019-05-13 23:55:54 +02:00
Client . prototype . removeUser = function ( userId , callback ) {
2018-01-22 13:01:38 -08:00
var config = {
2019-05-13 23:55:54 +02:00
data : { } ,
2018-01-22 13:01:38 -08:00
headers : {
'Content-Type' : 'application/json'
}
} ;
2018-12-11 18:17:53 +01:00
del ( '/api/v1/users/' + userId , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . updateProfile = function ( data , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/profile' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-07-07 14:39:17 +02:00
Client . prototype . changeAvatar = function ( avatarFileOrType , callback ) {
// Blob type if object
if ( typeof avatarFileOrType === 'object' ) {
var fd = new FormData ( ) ;
fd . append ( 'avatar' , avatarFileOrType ) ;
2019-11-25 16:12:43 +01:00
2021-07-07 14:39:17 +02:00
var config = {
headers : { 'Content-Type' : undefined } ,
transformRequest : angular . identity
} ;
2019-11-25 16:12:43 +01:00
2021-07-07 14:39:17 +02:00
post ( '/api/v1/profile/avatar' , fd , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} else {
post ( '/api/v1/profile/avatar' , { avatar : avatarFileOrType === 'gravatar' ? 'gravatar' : '' } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
}
2019-11-25 16:12:43 +01:00
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . changePassword = function ( currentPassword , newPassword , callback ) {
var data = {
password : currentPassword ,
newPassword : newPassword
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/profile/password' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-09-16 14:35:17 +02:00
Client . prototype . sendPasswordReset = function ( identifier , callback ) {
post ( '/api/v1/cloudron/password_reset_request' , { identifier } , null , function ( error , data , status ) {
2021-09-09 22:24:35 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2021-09-16 14:35:17 +02:00
callback ( null , data ) ;
2021-09-09 22:24:35 +02:00
} ) ;
} ;
2018-04-26 15:12:29 +02:00
Client . prototype . setTwoFactorAuthenticationSecret = function ( callback ) {
var data = { } ;
2021-04-14 20:00:31 -07:00
post ( '/api/v1/profile/twofactorauthentication_secret' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-04-26 15:12:29 +02:00
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-04-26 15:12:29 +02:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-04-26 15:12:29 +02:00
} ;
Client . prototype . enableTwoFactorAuthentication = function ( totpToken , callback ) {
var data = {
totpToken : totpToken
} ;
2021-04-14 20:00:31 -07:00
post ( '/api/v1/profile/twofactorauthentication_enable' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-04-26 15:12:29 +02:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-04-26 15:12:29 +02:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-04-26 15:12:29 +02:00
} ;
Client . prototype . disableTwoFactorAuthentication = function ( password , callback ) {
var data = {
password : password
} ;
2021-04-14 20:00:31 -07:00
post ( '/api/v1/profile/twofactorauthentication_disable' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-04-26 15:12:29 +02:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-04-26 15:12:29 +02:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-04-26 15:12:29 +02:00
} ;
2021-09-17 15:53:11 +02:00
Client . prototype . setGhost = function ( userId , password , expiresAt , callback ) {
var data = { password } ;
if ( expiresAt ) data . expiresAt = expiresAt ;
post ( '/api/v1/users/' + userId + '/ghost' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2019-08-29 17:22:39 +02:00
Client . prototype . startExternalLdapSync = function ( callback ) {
post ( '/api/v1/cloudron/sync_external_ldap' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2019-08-29 23:19:34 +02:00
callback ( null , data . taskId ) ;
2019-08-29 17:22:39 +02:00
} ) ;
} ;
2019-08-08 07:39:43 -07:00
Client . prototype . setUserActive = function ( userId , active , callback ) {
var data = {
active : active
} ;
post ( '/api/v1/users/' + userId + '/active' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . refreshUserInfo = function ( callback ) {
var that = this ;
callback = typeof callback === 'function' ? callback : function ( ) { } ;
this . userInfo ( function ( error , result ) {
if ( error ) return callback ( error ) ;
that . setUserInfo ( result ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . refreshConfig = function ( callback ) {
var that = this ;
callback = typeof callback === 'function' ? callback : function ( ) { } ;
this . config ( function ( error , result ) {
if ( error ) return callback ( error ) ;
2018-06-28 17:44:11 -07:00
that . getUpdateInfo ( function ( error , info ) { // note: non-admin users may get access denied for this
if ( ! error ) result . update = info . update ; // attach update information to config object
2018-01-22 13:01:38 -08:00
2018-06-28 17:44:11 -07:00
that . setConfig ( result ) ;
callback ( null ) ;
} ) ;
2018-01-22 13:01:38 -08:00
} ) ;
} ;
2020-11-18 00:28:10 +01:00
Client . prototype . refreshAvailableLanguages = function ( callback ) {
var that = this ;
get ( '/api/v1/cloudron/languages' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
angular . copy ( data . languages , that . _availableLanguages ) ;
callback ( null , data . languages ) ;
} ) ;
} ;
2019-09-12 12:18:10 +02:00
Client . prototype . _appPostProcess = function ( app ) {
// calculate the icon paths
2021-04-30 16:05:04 -07:00
app . iconUrl = app . iconUrl ? ( this . apiOrigin + app . iconUrl + '?access_token=' + token + '&ts=' + app . ts ) : null ;
2018-01-22 13:01:38 -08:00
2018-06-14 15:46:55 +02:00
// amend the post install confirm state
app . pendingPostInstallConfirmation = ! ! localStorage [ 'confirmPostInstall_' + app . id ] ;
2019-11-12 17:09:18 -08:00
if ( app . manifest . description ) { // can be empty for dev apps
var tmp = app . manifest . description . match ( /\<upstream\>(.*)\<\/upstream\>/i ) ;
app . upstreamVersion = ( tmp && tmp [ 1 ] ) ? tmp [ 1 ] : '' ;
} else {
app . upstreamVersion = '' ;
}
if ( ! app . manifest . title ) app . manifest . title = 'Untitled' ;
2019-09-10 19:21:30 +02:00
2020-08-08 18:07:06 -07:00
if ( app . manifest . postInstallMessage ) {
var text = app . manifest . postInstallMessage ;
// we chose - because underscore has special meaning in markdown
2021-05-25 11:32:53 -07:00
text = text . replace ( /\$CLOUDRON-APP-LOCATION/g , app . location ) ;
text = text . replace ( /\$CLOUDRON-APP-DOMAIN/g , app . domain ) ;
text = text . replace ( /\$CLOUDRON-APP-FQDN/g , app . fqdn ) ;
2020-08-08 22:02:31 -07:00
text = text . replace ( /\$CLOUDRON-APP-ORIGIN/g , 'https://' + app . fqdn ) ;
text = text . replace ( /\$CLOUDRON-API-DOMAIN/g , this . _config . adminFqdn ) ;
text = text . replace ( /\$CLOUDRON-API-ORIGIN/g , 'https://' + this . _config . adminFqdn ) ;
text = text . replace ( /\$CLOUDRON-USERNAME/g , this . _userInfo . username ) ;
text = text . replace ( /\$CLOUDRON-APP-ID/g , app . id ) ;
2020-08-08 18:07:06 -07:00
// [^] matches even newlines. '?' makes it non-greedy
if ( app . sso ) text = text . replace ( /<nosso>[^]*?<\/nosso>/g , '' ) ;
else text = text . replace ( /<sso>[^]*?<\/sso>/g , '' ) ;
app . manifest . postInstallMessage = text ;
}
2018-01-22 13:01:38 -08:00
return app ;
} ;
2018-06-26 17:12:55 -07:00
function binarySearch ( array , pred ) {
var lo = - 1 , hi = array . length ;
while ( 1 + lo !== hi ) {
var mi = lo + ( ( hi - lo ) >> 1 ) ;
if ( pred ( array [ mi ] ) ) {
hi = mi ;
} else {
lo = mi ;
}
}
return hi ;
}
2018-06-26 08:41:25 -07:00
Client . prototype . _updateAppCache = function ( app ) {
var tmp = { } ;
angular . copy ( app , tmp ) ;
2020-06-14 16:31:06 +02:00
var foundIndex = this . _installedApps . findIndex ( function ( a ) { return a . id === app . id ; } ) ;
// we replace new data into the existing reference to keep angular bindings
if ( foundIndex !== - 1 ) {
angular . copy ( tmp , this . _installedApps [ foundIndex ] ) ;
2018-06-26 08:41:25 -07:00
} else {
2020-06-14 16:31:06 +02:00
this . _installedApps . push ( tmp ) ;
2018-06-26 08:41:25 -07:00
}
2018-06-26 19:07:34 -07:00
2020-06-14 16:31:06 +02:00
// add reference to object map with appId keys
this . _installedAppsById [ app . id ] = this . _installedApps [ foundIndex ] ;
2019-04-12 11:06:56 +02:00
// TODO this not very elegant
// update app tags
tmp = this . _installedApps
. map ( function ( app ) { return app . tags || [ ] ; } ) // return array of arrays
. reduce ( function ( a , i ) { return a . concat ( i ) ; } , [ ] ) // merge all arrays into one
2019-05-16 09:42:34 -07:00
. filter ( function ( v , i , self ) { return self . indexOf ( v ) === i ; } ) // filter duplicates
. sort ( function ( a , b ) { return a . localeCompare ( b ) ; } ) ; // sort
2019-04-12 11:06:56 +02:00
// keep tag array references
angular . copy ( tmp , this . _appTags ) ;
2018-06-26 08:41:25 -07:00
} ;
2018-01-22 13:01:38 -08:00
Client . prototype . refreshInstalledApps = function ( callback ) {
2021-02-18 17:01:35 +01:00
callback = callback || function ( error ) { if ( error ) console . error ( error ) ; } ;
2018-01-22 13:01:38 -08:00
var that = this ;
2018-06-26 17:56:23 -07:00
this . getApps ( function ( error , apps ) {
2018-01-22 13:01:38 -08:00
if ( error ) return callback ( error ) ;
2020-06-03 23:38:00 +02:00
async . eachLimit ( apps , 20 , function ( app , iteratorCallback ) {
2021-04-20 20:43:37 +02:00
app . ssoAuth = ( app . manifest . addons [ 'ldap' ] || app . manifest . addons [ 'proxyAuth' ] ) && app . sso ;
2021-02-24 17:12:18 +01:00
// only fetch if we have permissions
if ( ! that . _userInfo . isAtLeastAdmin ) {
app . progress = 0 ;
app . message = '' ;
app . taskMinutesActive = 0 ;
that . _updateAppCache ( app ) ;
return iteratorCallback ( ) ;
}
2020-06-14 13:35:28 +02:00
var getTaskFunc = app . taskId ? that . getTask . bind ( null , app . taskId ) : function ( next ) { return next ( ) ; } ;
getTaskFunc ( function ( error , task ) {
if ( error ) return iteratorCallback ( error ) ;
if ( task ) {
app . progress = task . percent ;
app . message = task . message ;
app . taskMinutesActive = moment . duration ( moment . utc ( ) . diff ( moment . utc ( task . creationTime ) ) ) . asMinutes ( ) ;
} else {
app . progress = 0 ;
app . message = '' ;
app . taskMinutesActive = 0 ;
}
2018-01-22 13:01:38 -08:00
2018-06-26 17:56:23 -07:00
that . _updateAppCache ( app ) ;
2018-01-22 13:01:38 -08:00
2021-02-24 17:12:18 +01:00
iteratorCallback ( ) ;
2020-06-14 13:35:28 +02:00
} ) ;
2018-06-26 19:07:34 -07:00
} , function iteratorDone ( error ) {
if ( error ) return callback ( error ) ;
2018-06-26 17:56:23 -07:00
// filter out old apps, going backwards to allow splicing
2018-06-26 19:07:34 -07:00
for ( var i = that . _installedApps . length - 1 ; i >= 0 ; -- i ) {
2018-06-26 17:56:23 -07:00
if ( ! apps . some ( function ( elem ) { return ( elem . id === that . _installedApps [ i ] . id ) ; } ) ) {
2018-06-26 19:07:34 -07:00
var removed = that . _installedApps . splice ( i , 1 ) ;
delete that . _installedAppsById [ removed [ 0 ] . id ] ;
2018-06-26 17:56:23 -07:00
}
}
callback ( null ) ;
} ) ;
2018-01-22 13:01:38 -08:00
} ) ;
} ;
Client . prototype . login = function ( ) {
this . setToken ( null ) ;
2020-02-04 14:35:59 +01:00
window . location . href = '/login.html?returnTo=/' + encodeURIComponent ( window . location . hash ) ;
2018-01-22 13:01:38 -08:00
} ;
2020-02-04 14:35:59 +01:00
Client . prototype . logout = function ( ) {
var token = this . getToken ( ) ;
2018-01-22 13:01:38 -08:00
this . setToken ( null ) ;
2020-02-04 14:35:59 +01:00
// invalidates the token
window . location . href = client . apiOrigin + '/api/v1/cloudron/logout?access_token=' + token ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . uploadFile = function ( appId , file , progressCallback , callback ) {
var fd = new FormData ( ) ;
fd . append ( 'file' , file ) ;
2018-12-11 18:17:53 +01:00
var config = {
2018-01-22 13:01:38 -08:00
headers : { 'Content-Type' : undefined } ,
transformRequest : angular . identity ,
uploadEventHandlers : {
progress : progressCallback
}
2018-12-11 18:17:53 +01:00
} ;
2019-03-04 11:54:41 -08:00
post ( '/api/v1/apps/' + appId + '/upload?file=' + encodeURIComponent ( '/tmp/' + file . name ) , fd , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . checkDownloadableFile = function ( appId , filePath , callback ) {
2018-12-11 18:17:53 +01:00
var config = {
2018-01-22 13:01:38 -08:00
headers : { 'Content-Type' : undefined }
2018-12-11 18:17:53 +01:00
} ;
2019-03-04 12:59:41 -08:00
head ( '/api/v1/apps/' + appId + '/download?file=' + encodeURIComponent ( filePath ) , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-01-23 16:10:09 -08:00
Client . prototype . sendTestMail = function ( domain , to , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
2018-01-23 16:10:09 -08:00
to : to
2018-01-22 13:01:38 -08:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/send_test_mail' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-22 13:01:38 -08:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
// Domains
Client . prototype . getDomains = function ( callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/domains' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data . domains ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
Client . prototype . getDomain = function ( domain , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/domains/' + domain , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-22 13:01:38 -08:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2019-09-24 00:04:31 -07:00
Client . prototype . checkDNSRecords = function ( domain , subdomain , callback ) {
2019-09-23 23:47:33 +02:00
get ( '/api/v1/domains/' + domain + '/dns_check?subdomain=' + subdomain , null , function ( error , data , status ) {
2019-09-20 01:55:45 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2019-09-23 23:46:55 +02:00
callback ( null , data ) ;
2019-09-20 01:55:45 +02:00
} ) ;
} ;
2021-01-07 19:19:53 -08:00
Client . prototype . addDomain = function ( domain , zoneName , provider , config , fallbackCertificate , tlsConfig , wellKnown , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
domain : domain ,
provider : provider ,
2018-01-31 20:51:25 -08:00
config : config ,
2021-01-07 19:19:53 -08:00
tlsConfig : tlsConfig ,
wellKnown : wellKnown
2018-01-22 13:01:38 -08:00
} ;
2018-05-15 12:37:16 -07:00
if ( zoneName ) data . zoneName = zoneName ;
2018-01-25 10:46:41 -08:00
var that = this ;
2018-01-22 13:01:38 -08:00
if ( fallbackCertificate ) data . fallbackCertificate = fallbackCertificate ;
2018-01-25 10:46:41 -08:00
// hack until we fix the domains.js
2018-12-11 18:17:53 +01:00
post ( '/api/v1/domains' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-25 10:46:41 -08:00
2020-03-31 10:56:02 -07:00
callback ( ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-01-07 19:19:53 -08:00
Client . prototype . updateDomain = function ( domain , zoneName , provider , config , fallbackCertificate , tlsConfig , wellKnown , callback ) {
2018-01-22 13:01:38 -08:00
var data = {
provider : provider ,
2018-01-31 20:51:25 -08:00
config : config ,
2021-01-07 19:19:53 -08:00
tlsConfig : tlsConfig ,
wellKnown : wellKnown
2018-01-22 13:01:38 -08:00
} ;
2018-05-15 12:37:16 -07:00
if ( zoneName ) data . zoneName = zoneName ;
2018-03-08 17:23:39 -08:00
var that = this ;
2018-01-22 13:01:38 -08:00
if ( fallbackCertificate ) data . fallbackCertificate = fallbackCertificate ;
2018-12-11 18:17:53 +01:00
put ( '/api/v1/domains/' + domain , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-22 13:01:38 -08:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-03-08 17:23:39 -08:00
2021-02-24 22:00:05 -08:00
that . setDnsRecords ( { domain : domain , type : 'mail' } , callback ) ; // this is done so that an out-of-sync dkim key can be synced
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2021-03-13 21:52:37 -08:00
Client . prototype . renewCerts = function ( callback ) {
post ( '/api/v1/cloudron/renew_certs' , { } , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-10-24 15:24:23 -07:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-12-11 10:17:27 -08:00
callback ( null , data . taskId ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-10-24 15:24:23 -07:00
} ;
2019-05-20 18:41:08 -07:00
Client . prototype . removeDomain = function ( domain , callback ) {
2018-01-25 10:46:41 -08:00
var config = {
data : {
} ,
headers : {
'Content-Type' : 'application/json'
}
} ;
2020-03-31 10:56:02 -07:00
del ( '/api/v1/domains/' + domain , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2020-03-31 10:56:02 -07:00
callback ( null ) ;
2018-01-25 10:46:41 -08:00
} ) ;
2018-01-22 13:01:38 -08:00
} ;
2018-12-18 15:05:11 -08:00
Client . prototype . prepareDashboardDomain = function ( domain , callback ) {
var data = {
domain : domain
} ;
post ( '/api/v1/cloudron/prepare_dashboard_domain' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . taskId ) ;
} ) ;
} ;
Client . prototype . setDashboardDomain = function ( domain , callback ) {
var data = {
domain : domain
} ;
post ( '/api/v1/cloudron/set_dashboard_domain' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2018-01-23 12:29:57 +01:00
// Email
2020-03-25 20:13:45 -07:00
Client . prototype . getMailEventLogs = function ( search , types , page , perPage , callback ) {
2020-02-11 22:07:58 -08:00
var config = {
params : {
page : page ,
2020-03-25 20:13:45 -07:00
types : types ,
2020-02-14 09:09:32 -08:00
per _page : perPage ,
search : search
2020-02-11 22:07:58 -08:00
}
} ;
get ( '/api/v1/mailserver/eventlog' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . eventlogs ) ;
} ) ;
} ;
2020-02-20 12:08:56 -08:00
Client . prototype . getMailUsage = function ( domain , callback ) {
var config = {
params : {
domain : domain
}
} ;
get ( '/api/v1/mailserver/usage' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-08-20 22:07:20 -07:00
callback ( null , data . usage ) ;
} ) ;
} ;
2020-08-20 23:06:22 -07:00
Client . prototype . getMailLocation = function ( callback ) {
var config = { } ;
get ( '/api/v1/mailserver/location' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ; // { subdomain, domain }
} ) ;
} ;
Client . prototype . setMailLocation = function ( subdomain , domain , callback ) {
post ( '/api/v1/mailserver/location' , { subdomain : subdomain , domain : domain } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , { taskId : data . taskId } ) ;
} ) ;
} ;
2020-08-20 22:07:20 -07:00
Client . prototype . getMaxEmailSize = function ( callback ) {
var config = { } ;
get ( '/api/v1/mailserver/max_email_size' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . size ) ;
} ) ;
} ;
Client . prototype . setMaxEmailSize = function ( size , callback ) {
post ( '/api/v1/mailserver/max_email_size' , { size : size } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
2020-02-20 12:08:56 -08:00
} ) ;
} ;
2020-11-19 17:45:16 -08:00
Client . prototype . getSolrConfig = function ( callback ) {
var config = { } ;
get ( '/api/v1/mailserver/solr_config' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . setSolrConfig = function ( enabled , callback ) {
post ( '/api/v1/mailserver/solr_config' , { enabled : enabled } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2020-08-22 13:01:25 -07:00
Client . prototype . getSpamAcl = function ( callback ) {
var config = { } ;
get ( '/api/v1/mailserver/spam_acl' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . setSpamAcl = function ( acl , callback ) {
post ( '/api/v1/mailserver/spam_acl' , { whitelist : acl . whitelist , blacklist : acl . blacklist } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
Client . prototype . getSpamCustomConfig = function ( callback ) {
var config = { } ;
get ( '/api/v1/mailserver/spam_custom_config' , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . config ) ;
} ) ;
} ;
Client . prototype . setSpamCustomConfig = function ( config , callback ) {
post ( '/api/v1/mailserver/spam_custom_config' , { config : config } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2018-01-23 12:29:57 +01:00
Client . prototype . getMailConfigForDomain = function ( domain , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/mail/' + domain , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-23 12:29:57 +01:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-23 12:29:57 +01:00
} ;
Client . prototype . enableMailForDomain = function ( domain , enabled , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/enable' , { enabled : enabled } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-23 15:35:02 +01:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-23 15:35:02 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-23 12:29:57 +01:00
} ;
2021-02-24 22:00:05 -08:00
Client . prototype . setDnsRecords = function ( options , callback ) {
post ( '/api/v1/cloudron/sync_dns' , options , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-07-25 10:51:58 -07:00
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2021-02-24 22:00:05 -08:00
callback ( null , data . taskId ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-07-25 10:51:58 -07:00
} ;
2018-01-23 12:29:57 +01:00
Client . prototype . getMailStatusForDomain = function ( domain , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/mail/' + domain + '/status' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-01-23 12:29:57 +01:00
callback ( null , data ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-23 12:29:57 +01:00
} ;
2018-12-11 18:17:53 +01:00
Client . prototype . setMailRelay = function ( domain , data , callback ) {
post ( '/api/v1/mail/' + domain + '/relay' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-23 12:29:57 +01:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2020-08-24 10:16:11 -07:00
callback ( null ) ;
} ) ;
} ;
Client . prototype . setMailBanner = function ( domain , data , callback ) {
post ( '/api/v1/mail/' + domain + '/banner' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-23 12:29:57 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-23 12:29:57 +01:00
} ;
Client . prototype . setCatchallAddresses = function ( domain , addresses , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/catch_all' , { addresses : addresses } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-05-23 23:34:09 -07:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-05-23 23:34:09 -07:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-05-23 23:34:09 -07:00
} ;
Client . prototype . setMailFromValidation = function ( domain , enabled , callback ) {
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/mail_from_validation' , { enabled : enabled } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-23 12:29:57 +01:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-23 12:29:57 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-23 12:29:57 +01:00
} ;
2018-01-24 16:20:21 +01:00
// Mailboxes
2020-07-15 15:35:32 -07:00
Client . prototype . getMailboxCount = function ( domain , callback ) {
get ( '/api/v1/mail/' + domain + '/mailbox_count' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . count ) ;
} ) ;
} ;
2021-01-07 21:40:51 -08:00
Client . prototype . listMailboxes = function ( domain , search , page , perPage , callback ) {
2020-03-28 17:35:51 -07:00
var config = {
params : {
2020-07-05 12:18:34 -07:00
search : search ,
page : page ,
per _page : perPage
2020-03-28 17:35:51 -07:00
}
} ;
get ( '/api/v1/mail/' + domain + '/mailboxes' , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-24 16:20:21 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-04-09 15:45:54 +02:00
2018-03-30 18:06:40 +02:00
callback ( null , data . mailboxes ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-24 16:20:21 +01:00
} ;
2018-04-05 14:02:56 +02:00
Client . prototype . getMailbox = function ( domain , name , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/mail/' + domain + '/mailboxes/' + name , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-24 16:20:21 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-24 16:20:21 +01:00
callback ( null , data . mailbox ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-24 16:20:21 +01:00
} ;
2020-11-12 23:53:02 -08:00
Client . prototype . addMailbox = function ( domain , name , ownerId , ownerType , callback ) {
2018-04-05 14:02:56 +02:00
var data = {
name : name ,
2020-11-12 23:53:02 -08:00
ownerId : ownerId ,
2021-04-14 22:37:59 -07:00
ownerType : ownerType ,
active : true
2018-04-05 14:02:56 +02:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/mailboxes' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-24 16:20:21 +01:00
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-24 16:20:21 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-24 16:20:21 +01:00
} ;
2021-04-14 22:37:59 -07:00
Client . prototype . updateMailbox = function ( domain , name , ownerId , ownerType , active , callback ) {
2018-04-05 14:02:56 +02:00
var data = {
2020-11-12 23:53:02 -08:00
ownerId : ownerId ,
2021-04-14 22:37:59 -07:00
ownerType : ownerType ,
active : active
2018-04-05 14:02:56 +02:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/mailboxes/' + name , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-04-05 14:02:56 +02:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-04-05 14:02:56 +02:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-04-05 14:02:56 +02:00
} ;
2020-07-27 22:36:38 -07:00
Client . prototype . removeMailbox = function ( domain , name , deleteMails , callback ) {
var config = {
data : {
deleteMails : deleteMails
} ,
headers : {
'Content-Type' : 'application/json'
}
} ;
del ( '/api/v1/mail/' + domain + '/mailboxes/' + name , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-24 16:20:21 +01:00
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-24 16:20:21 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-24 16:20:21 +01:00
} ;
2020-04-20 19:09:25 -07:00
Client . prototype . getAliases = function ( name , domain , callback ) {
2020-03-28 17:35:51 -07:00
var config = {
params : {
page : 1 ,
per _page : 1000
}
} ;
2020-04-20 19:09:25 -07:00
get ( '/api/v1/mail/' + domain + '/mailboxes/' + name + '/aliases' , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-25 18:16:47 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-25 18:16:47 +01:00
callback ( null , data . aliases ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-25 18:16:47 +01:00
} ;
2020-04-20 19:09:25 -07:00
Client . prototype . setAliases = function ( name , domain , aliases , callback ) {
2018-01-25 18:16:47 +01:00
var data = {
aliases : aliases
} ;
2020-04-20 19:09:25 -07:00
put ( '/api/v1/mail/' + domain + '/mailboxes/' + name + '/aliases' , data , null , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-01-26 15:39:02 +01:00
if ( status !== 202 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-25 18:16:47 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-25 18:16:47 +01:00
} ;
2020-07-05 11:55:17 -07:00
Client . prototype . listMailingLists = function ( domain , search , page , perPage , callback ) {
2020-07-05 10:49:30 -07:00
var config = {
params : {
2020-07-05 11:55:17 -07:00
search : search ,
page : page ,
per _page : perPage
2020-07-05 10:49:30 -07:00
}
} ;
get ( '/api/v1/mail/' + domain + '/lists' , config , function ( error , data , status ) {
2018-12-11 18:17:53 +01:00
if ( error ) return callback ( error ) ;
2018-04-01 21:58:12 +02:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-04-01 21:58:12 +02:00
callback ( null , data . lists ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-04-01 21:58:12 +02:00
} ;
2018-04-05 14:02:56 +02:00
Client . prototype . getMailingList = function ( domain , name , callback ) {
2018-12-11 18:17:53 +01:00
get ( '/api/v1/mail/' + domain + '/lists/' + name , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-26 11:31:19 +01:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-26 11:31:19 +01:00
callback ( null , data . list ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-26 11:31:19 +01:00
} ;
2020-04-17 17:37:19 -07:00
Client . prototype . addMailingList = function ( domain , name , members , membersOnly , callback ) {
2018-01-26 11:31:19 +01:00
var data = {
2018-04-05 14:02:56 +02:00
name : name ,
2020-04-17 17:37:19 -07:00
members : members ,
2021-04-14 22:37:59 -07:00
membersOnly : membersOnly ,
active : true
2018-01-26 11:31:19 +01:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/lists' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-26 11:31:19 +01:00
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-26 11:31:19 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-26 11:31:19 +01:00
} ;
2021-04-14 22:37:59 -07:00
Client . prototype . updateMailingList = function ( domain , name , members , membersOnly , active , callback ) {
2018-04-05 14:02:56 +02:00
var data = {
2020-04-17 17:37:19 -07:00
members : members ,
2021-04-14 22:37:59 -07:00
membersOnly : membersOnly ,
active : active
2018-04-05 14:02:56 +02:00
} ;
2018-12-11 18:17:53 +01:00
post ( '/api/v1/mail/' + domain + '/lists/' + name , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-04-05 14:02:56 +02:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-04-05 14:02:56 +02:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-04-05 14:02:56 +02:00
} ;
Client . prototype . removeMailingList = function ( domain , name , callback ) {
2018-12-11 18:17:53 +01:00
del ( '/api/v1/mail/' + domain + '/lists/' + name , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
2018-01-30 13:38:06 +01:00
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
2018-12-11 18:17:53 +01:00
2018-01-26 11:31:19 +01:00
callback ( null ) ;
2018-12-11 18:17:53 +01:00
} ) ;
2018-01-26 11:31:19 +01:00
} ;
2020-10-28 16:14:32 -07:00
// Volumes
Client . prototype . getVolumes = function ( callback ) {
get ( '/api/v1/volumes' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . volumes ) ;
} ) ;
} ;
Client . prototype . getVolume = function ( volume , callback ) {
get ( '/api/v1/volumes/' + volume , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2021-05-13 15:33:07 -07:00
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . getVolumeStatus = function ( volume , callback ) {
get ( '/api/v1/volumes/' + volume + '/status' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-10-28 16:14:32 -07:00
callback ( null , data ) ;
} ) ;
} ;
2021-06-24 16:59:13 -07:00
Client . prototype . addVolume = function ( name , mountType , hostPath , mountOptions , callback ) {
2020-10-28 16:14:32 -07:00
var data = {
name : name ,
2021-05-12 21:40:30 -07:00
mountType : mountType ,
mountOptions : mountOptions
2020-10-28 16:14:32 -07:00
} ;
2021-06-24 16:59:13 -07:00
if ( hostPath ) data . hostPath = hostPath ;
2020-10-28 16:14:32 -07:00
var that = this ;
post ( '/api/v1/volumes' , data , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
2021-05-12 21:40:30 -07:00
callback ( null , data . id ) ;
} ) ;
} ;
2021-05-13 09:14:34 -07:00
Client . prototype . updateVolume = function ( volumeId , mountType , mountOptions , callback ) {
var data = {
mountType : mountType ,
mountOptions : mountOptions
} ;
2021-05-12 21:40:30 -07:00
var that = this ;
2021-05-13 09:14:34 -07:00
post ( '/api/v1/volumes/' + volumeId , data , null , function ( error , data , status ) {
2021-05-12 21:40:30 -07:00
if ( error ) return callback ( error ) ;
2021-05-13 09:14:34 -07:00
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-10-28 16:14:32 -07:00
callback ( ) ;
} ) ;
} ;
Client . prototype . removeVolume = function ( volume , callback ) {
var config = {
data : {
} ,
headers : {
'Content-Type' : 'application/json'
}
} ;
del ( '/api/v1/volumes/' + volume , config , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 204 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2020-02-21 12:34:40 +01:00
Client . prototype . getAppstoreUserToken = function ( callback ) {
post ( '/api/v1/appstore/user_token' , { } , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data . accessToken ) ;
} ) ;
} ;
// This will change the location
Client . prototype . openSubscriptionSetup = function ( subscription ) {
2020-04-14 18:38:59 +02:00
// we only allow the owner to do so
if ( ! this . _userInfo . isAtLeastOwner ) return ;
2020-06-18 12:16:16 +02:00
// basically the user has not setup appstore account yet
if ( ! subscription . plan ) return window . location . href = '/#/appstore' ;
2020-02-21 12:34:40 +01:00
var that = this ;
2020-02-24 15:10:21 +01:00
var email = subscription . emailEncoded ;
var cloudronId = subscription . cloudronId ;
2020-02-21 12:34:40 +01:00
2020-02-24 15:10:21 +01:00
if ( ! this . _userInfo . isAtLeastOwner ) return window . location . href = that . getConfig ( ) . webServerOrigin + '/console.html#/userprofile?view=subscriptions&email=' + email + '&cloudronId=' + cloudronId ;
this . getAppstoreUserToken ( function ( error , token ) {
if ( error ) console . error ( 'Unable to get appstore user token.' , error ) ;
2020-02-21 12:34:40 +01:00
2020-04-14 18:38:59 +02:00
var url = that . getConfig ( ) . webServerOrigin + '/console.html#/userprofile?view=subscriptions&email=' + email + '&token=' + token ;
// Only open the subscription setup dialog when no subscription exists
2020-06-18 12:16:16 +02:00
if ( ! subscription . plan || subscription . plan . id === 'free' ) url += '&cloudronId=' + cloudronId
2020-04-14 18:38:59 +02:00
window . location . href = url ;
2020-02-21 12:34:40 +01:00
} ) ;
} ;
2019-05-04 18:15:33 -07:00
Client . prototype . getAppstoreAppByIdAndVersion = function ( appId , version , callback ) {
var url = '/api/v1/appstore/apps/' + appId ;
if ( version && version !== 'latest' ) url += '/versions/' + version ;
get ( url , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . getAppstoreApps = function ( callback ) {
var that = this ;
get ( '/api/v1/appstore/apps' , null , function ( error , data , status ) {
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
angular . copy ( data . apps , that . _appstoreAppCache ) ;
return callback ( null , that . _appstoreAppCache ) ;
} ) ;
} ;
2019-05-05 07:46:06 -07:00
Client . prototype . getAppstoreAppsFast = function ( callback ) {
2019-05-04 18:15:33 -07:00
if ( this . _appstoreAppCache . length !== 0 ) return callback ( null , this . _appstoreAppCache ) ;
this . getAppstoreApps ( callback ) ;
} ;
2019-05-04 19:22:24 -07:00
Client . prototype . getSubscription = function ( callback ) {
2020-02-24 17:15:50 +01:00
if ( ! this . _userInfo . isAtLeastAdmin ) return callback ( new Error ( 'Not allowed' ) ) ;
2019-05-14 16:41:08 +02:00
2019-05-05 13:02:23 -07:00
get ( '/api/v1/appstore/subscription' , null , function ( error , data , status ) {
2019-05-04 19:22:24 -07:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
// just some helper property, since angular bindings cannot dot his easily
data . emailEncoded = encodeURIComponent ( data . email ) ;
callback ( null , data ) ; // { email, plan: { id, name }, cancel_at, status }
} ) ;
} ;
2021-08-10 13:29:06 -07:00
Client . prototype . registerCloudron = function ( email , password , totpToken , signup , callback ) {
2019-05-04 19:22:24 -07:00
var data = {
email : email ,
password : password ,
2020-01-30 15:36:05 +01:00
signup : signup ,
2019-05-04 19:22:24 -07:00
} ;
if ( totpToken ) data . totpToken = totpToken ;
2019-05-05 13:02:23 -07:00
post ( '/api/v1/appstore/register_cloudron' , data , null , function ( error , data , status ) {
2019-05-04 19:22:24 -07:00
if ( error ) return callback ( error ) ;
if ( status !== 201 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null ) ;
} ) ;
} ;
2020-07-08 17:30:37 +02:00
// FileManager API
2020-07-21 16:27:51 +02:00
// mode can be 'download', 'open', 'link' or 'data'
2020-10-30 10:33:05 -07:00
Client . prototype . filesGet = function ( id , type , path , mode , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
2020-07-21 16:27:51 +02:00
if ( mode === 'download' ) {
2020-10-30 10:33:05 -07:00
window . open ( client . apiOrigin + '/api/v1/' + objpath + '/files/' + path + '?download=true&access_token=' + token ) ;
2020-07-21 16:27:51 +02:00
callback ( null ) ;
} else if ( mode === 'open' ) {
2020-10-30 10:33:05 -07:00
window . open ( client . apiOrigin + '/api/v1/' + objpath + '/files/' + path + '?download=false&access_token=' + token ) ;
2020-07-21 16:27:51 +02:00
callback ( null ) ;
} else if ( mode === 'link' ) {
2020-10-30 10:33:05 -07:00
callback ( null , client . apiOrigin + '/api/v1/' + objpath + '/files/' + path + '?download=false&access_token=' + token ) ;
2020-07-13 17:48:53 +02:00
} else {
2020-07-14 17:17:43 +02:00
function responseHandler ( data , headers , status ) {
if ( headers ( ) [ 'content-type' ] && headers ( ) [ 'content-type' ] . indexOf ( 'application/json' ) !== - 1 ) return JSON . parse ( data ) ;
return data ;
}
2020-10-30 10:33:05 -07:00
get ( '/api/v1/' + objpath + '/files/' + path , { transformResponse : responseHandler } , function ( error , data , status ) {
2020-07-13 17:48:53 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-07-08 17:30:37 +02:00
2020-07-13 17:48:53 +02:00
callback ( null , data ) ;
} ) ;
}
2020-07-08 17:30:37 +02:00
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesRemove = function ( id , type , path , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
del ( '/api/v1/' + objpath + '/files/' + path , null , function ( error , data , status ) {
2020-07-09 11:00:11 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesExtract = function ( id , type , path , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
put ( '/api/v1/' + objpath + '/files/' + path , { action : 'extract' } , { } , function ( error , data , status ) {
2020-10-19 19:18:47 -07:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesChown = function ( id , type , path , uid , recursive , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
put ( '/api/v1/' + objpath + '/files/' + path , { action : 'chown' , uid : uid , recursive : recursive } , { } , function ( error , data , status ) {
2020-07-13 18:30:29 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
} ) ;
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesRename = function ( id , type , path , newPath , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
2021-03-02 11:12:43 +01:00
put ( '/api/v1/' + objpath + '/files/' + path , { action : 'rename' , newFilePath : decodeURIComponent ( newPath ) } , { } , function ( error , data , status ) {
2020-07-09 12:05:14 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2021-02-01 16:45:55 +01:00
callback ( null , data ) ;
} ) ;
} ;
Client . prototype . filesCopy = function ( id , type , path , newPath , callback ) {
2021-03-16 21:24:26 +01:00
var that = this ;
2021-02-01 16:45:55 +01:00
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
2021-03-02 11:12:43 +01:00
put ( '/api/v1/' + objpath + '/files/' + path , { action : 'copy' , newFilePath : decodeURIComponent ( newPath ) } , { } , function ( error , data , status ) {
2021-03-16 21:24:26 +01:00
if ( error && error . statusCode === 409 ) return that . filesCopy ( id , type , path , newPath + '-copy' , callback ) ;
2021-02-01 16:45:55 +01:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-07-09 12:05:14 +02:00
callback ( null , data ) ;
} ) ;
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesCreateDirectory = function ( id , type , path , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
2021-03-02 11:12:43 +01:00
post ( '/api/v1/' + objpath + '/files/' + path , { directory : decodeURIComponent ( path ) } , { } , function ( error , data , status ) {
2020-07-09 12:05:14 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-10-22 10:25:50 +02:00
callback ( null , data ) ;
} ) ;
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesCreateFile = function ( id , type , path , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
post ( '/api/v1/' + objpath + '/files/' + path , { } , { } , function ( error , data , status ) {
2020-10-22 10:25:50 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
2020-07-09 12:05:14 +02:00
callback ( null , data ) ;
} ) ;
} ;
2020-10-30 10:33:05 -07:00
Client . prototype . filesUpload = function ( id , type , path , file , overwrite , progressHandler , callback ) {
var objpath = ( type === 'app' ? 'apps/' : 'volumes/' ) + id ;
2020-07-09 12:05:14 +02:00
var fd = new FormData ( ) ;
fd . append ( 'file' , file ) ;
2020-10-22 12:45:05 +02:00
if ( overwrite ) fd . append ( 'overwrite' , 'true' ) ;
2020-07-10 19:10:29 +02:00
function done ( error , data , status ) {
2020-07-09 12:05:14 +02:00
if ( error ) return callback ( error ) ;
if ( status !== 200 ) return callback ( new ClientError ( status , data ) ) ;
callback ( null , data ) ;
2020-07-10 19:10:29 +02:00
}
$http ( {
2020-10-30 10:33:05 -07:00
url : client . apiOrigin + '/api/v1/' + objpath + '/files/' + path ,
2020-07-10 19:10:29 +02:00
method : 'POST' ,
data : fd ,
transformRequest : angular . identity ,
headers : {
'Content-Type' : undefined ,
Authorization : 'Bearer ' + token
} ,
uploadEventHandlers : {
progress : function ( e ) {
progressHandler ( e . loaded ) ;
}
}
} ) . success ( defaultSuccessHandler ( done ) ) . error ( defaultErrorHandler ( done ) ) ;
2020-07-09 12:05:14 +02:00
} ;
2021-09-14 12:17:17 +02:00
// ----------------------------------------------
// Eventlog helpers
// ----------------------------------------------
Client . prototype . eventLogDetails = function ( eventLog ) {
var ACTION _ACTIVATE = 'cloudron.activate' ;
var ACTION _PROVISION = 'cloudron.provision' ;
var ACTION _RESTORE = 'cloudron.restore' ;
var ACTION _APP _CLONE = 'app.clone' ;
var ACTION _APP _REPAIR = 'app.repair' ;
var ACTION _APP _CONFIGURE = 'app.configure' ;
var ACTION _APP _INSTALL = 'app.install' ;
var ACTION _APP _RESTORE = 'app.restore' ;
var ACTION _APP _IMPORT = 'app.import' ;
var ACTION _APP _UNINSTALL = 'app.uninstall' ;
var ACTION _APP _UPDATE = 'app.update' ;
var ACTION _APP _UPDATE _FINISH = 'app.update.finish' ;
var ACTION _APP _LOGIN = 'app.login' ;
var ACTION _APP _OOM = 'app.oom' ;
var ACTION _APP _UP = 'app.up' ;
var ACTION _APP _DOWN = 'app.down' ;
var ACTION _APP _START = 'app.start' ;
var ACTION _APP _STOP = 'app.stop' ;
var ACTION _APP _RESTART = 'app.restart' ;
var ACTION _BACKUP _FINISH = 'backup.finish' ;
var ACTION _BACKUP _START = 'backup.start' ;
var ACTION _BACKUP _CLEANUP _START = 'backup.cleanup.start' ;
var ACTION _BACKUP _CLEANUP _FINISH = 'backup.cleanup.finish' ;
var ACTION _CERTIFICATE _NEW = 'certificate.new' ;
var ACTION _CERTIFICATE _RENEWAL = 'certificate.renew' ;
var ACTION _DASHBOARD _DOMAIN _UPDATE = 'dashboard.domain.update' ;
var ACTION _DOMAIN _ADD = 'domain.add' ;
var ACTION _DOMAIN _UPDATE = 'domain.update' ;
var ACTION _DOMAIN _REMOVE = 'domain.remove' ;
var ACTION _START = 'cloudron.start' ;
var ACTION _UPDATE = 'cloudron.update' ;
var ACTION _UPDATE _FINISH = 'cloudron.update.finish' ;
var ACTION _USER _ADD = 'user.add' ;
var ACTION _USER _LOGIN = 'user.login' ;
var ACTION _USER _LOGOUT = 'user.logout' ;
var ACTION _USER _REMOVE = 'user.remove' ;
var ACTION _USER _UPDATE = 'user.update' ;
var ACTION _USER _TRANSFER = 'user.transfer' ;
var ACTION _MAIL _LOCATION = 'mail.location' ;
var ACTION _MAIL _ENABLED = 'mail.enabled' ;
var ACTION _MAIL _DISABLED = 'mail.disabled' ;
var ACTION _MAIL _MAILBOX _ADD = 'mail.box.add' ;
var ACTION _MAIL _MAILBOX _UPDATE = 'mail.box.update' ;
var ACTION _MAIL _MAILBOX _REMOVE = 'mail.box.remove' ;
var ACTION _MAIL _LIST _ADD = 'mail.list.add' ;
var ACTION _MAIL _LIST _UPDATE = 'mail.list.update' ;
var ACTION _MAIL _LIST _REMOVE = 'mail.list.remove' ;
var ACTION _SUPPORT _TICKET = 'support.ticket' ;
var ACTION _SUPPORT _SSH = 'support.ssh' ;
var ACTION _VOLUME _ADD = 'volume.add' ;
var ACTION _VOLUME _UPDATE = 'volume.update' ;
var ACTION _VOLUME _REMOVE = 'volume.remove' ;
var ACTION _DYNDNS _UPDATE = 'dyndns.update' ;
var ACTION _SYSTEM _CRASH = 'system.crash' ;
var data = eventLog . data ;
var errorMessage = data . errorMessage ;
var details , app ;
function appName ( app ) {
return ( app . label || app . fqdn || app . location ) + ' (' + app . manifest . title + ')' ;
}
switch ( eventLog . action ) {
case ACTION _ACTIVATE :
return 'Cloudron was activated' ;
case ACTION _PROVISION :
return 'Cloudron was setup' ;
case ACTION _RESTORE :
return 'Cloudron was restored using backup ' + data . backupId ;
case ACTION _APP _CONFIGURE : {
if ( ! data . app ) return '' ;
app = data . app ;
var q = function ( x ) {
return '"' + x + '"' ;
} ;
if ( 'accessRestriction' in data ) { // since it can be null
return 'Access restriction of ' + appName ( app ) + ' was changed' ;
} else if ( data . label ) {
return 'Label of ' + appName ( app ) + ' was set to ' + q ( data . label ) ;
} else if ( data . tags ) {
return 'Tags of ' + appName ( app ) + ' was set to ' + q ( data . tags . join ( ',' ) ) ;
} else if ( data . icon ) {
return 'Icon of ' + appName ( app ) + ' was changed' ;
} else if ( data . memoryLimit ) {
return 'Memory limit of ' + appName ( app ) + ' was set to ' + data . memoryLimit ;
} else if ( data . cpuShares ) {
return 'CPU shares of ' + appName ( app ) + ' was set to ' + Math . round ( ( data . cpuShares * 100 ) / 1024 ) + '%' ;
} else if ( data . env ) {
return 'Env vars of ' + appName ( app ) + ' was changed' ;
} else if ( 'debugMode' in data ) { // since it can be null
if ( data . debugMode ) {
return appName ( app ) + ' was placed in repair mode' ;
} else {
return appName ( app ) + ' was taken out of repair mode' ;
}
} else if ( 'enableBackup' in data ) {
return 'Automatic backups of ' + appName ( app ) + ' was ' + ( data . enableBackup ? 'enabled' : 'disabled' ) ;
} else if ( 'enableAutomaticUpdate' in data ) {
return 'Automatic updates of ' + appName ( app ) + ' was ' + ( data . enableAutomaticUpdate ? 'enabled' : 'disabled' ) ;
} else if ( 'reverseProxyConfig' in data ) {
return 'Reverse proxy configuration of ' + appName ( app ) + ' was updated' ;
} else if ( 'cert' in data ) {
if ( data . cert ) {
return 'Custom certificate was set for ' + appName ( app ) ;
} else {
return 'Certificate of ' + appName ( app ) + ' was reset' ;
}
} else if ( data . location ) {
if ( data . fqdn !== data . app . fqdn ) {
return 'Location of ' + appName ( app ) + ' was changed to ' + data . fqdn ;
} else if ( ! angular . equals ( data . alternateDomains , data . app . alternateDomains ) ) {
var altFqdns = data . alternateDomains . map ( function ( a ) { return a . fqdn ; } ) ;
return 'Alternate domains of ' + appName ( app ) + ' was ' + ( altFqdns . length ? 'set to ' + altFqdns . join ( ', ' ) : 'reset' ) ;
} else if ( ! angular . equals ( data . aliasDomains , data . app . aliasDomains ) ) {
var aliasDomains = data . aliasDomains . map ( function ( a ) { return a . fqdn ; } ) ;
return 'Alias domains of ' + appName ( app ) + ' was ' + ( aliasDomains . length ? 'set to ' + aliasDomains . join ( ', ' ) : 'reset' ) ;
} else if ( ! angular . equals ( data . portBindings , data . app . portBindings ) ) {
return 'Port bindings of ' + appName ( app ) + ' was changed' ;
}
} else if ( 'dataDir' in data ) {
if ( data . dataDir ) {
return 'Data directory of ' + appName ( app ) + ' was set ' + data . dataDir ;
} else {
return 'Data directory of ' + appName ( app ) + ' was reset' ;
}
} else if ( 'icon' in data ) {
if ( data . icon ) {
return 'Icon of ' + appName ( app ) + ' was set' ;
} else {
return 'Icon of ' + appName ( app ) + ' was reset' ;
}
} else if ( ( 'mailboxName' in data ) && data . mailboxName !== data . app . mailboxName ) {
if ( data . mailboxName ) {
return 'Mailbox of ' + appName ( app ) + ' was set to ' + q ( data . mailboxName ) ;
} else {
return 'Mailbox of ' + appName ( app ) + ' was reset' ;
}
}
return appName ( app ) + ' was re-configured' ;
}
case ACTION _APP _INSTALL :
if ( ! data . app ) return '' ;
return data . app . manifest . title + ' (package v' + data . app . manifest . version + ') was installed at ' + ( data . app . fqdn || data . app . location ) ;
case ACTION _APP _RESTORE :
if ( ! data . app ) return '' ;
details = data . app . manifest . title + ' was restored at ' + ( data . app . fqdn || data . app . location ) ;
// older versions (<3.5) did not have these fields
if ( data . fromManifest ) details += ' from version ' + data . fromManifest . version ;
if ( data . toManifest ) details += ' to version ' + data . toManifest . version ;
if ( data . backupId ) details += ' using backup ' + data . backupId ;
return details ;
case ACTION _APP _IMPORT :
if ( ! data . app ) return '' ;
details = data . app . manifest . title + ' was imported at ' + ( data . app . fqdn || data . app . location ) ;
if ( data . toManifest ) details += ' to version ' + data . toManifest . version ;
if ( data . backupId ) details += ' using backup ' + data . backupId ;
return details ;
case ACTION _APP _UNINSTALL :
if ( ! data . app ) return '' ;
return data . app . manifest . title + ' (package v' + data . app . manifest . version + ') was uninstalled at ' + ( data . app . fqdn || data . app . location ) ;
case ACTION _APP _UPDATE :
if ( ! data . app ) return '' ;
return 'Update of ' + data . app . manifest . title + ' at ' + ( data . app . fqdn || data . app . location ) + ' started from v' + data . fromManifest . version + ' to v' + data . toManifest . version ;
case ACTION _APP _UPDATE _FINISH :
if ( ! data . app ) return '' ;
return data . app . manifest . title + ' at ' + ( data . app . fqdn || data . app . location ) + ' was updated to v' + data . app . manifest . version ;
case ACTION _APP _CLONE :
return data . newApp . manifest . title + ' at ' + ( data . newApp . fqdn || data . newApp . location ) + ' was cloned from ' + ( data . oldApp . fqdn || data . oldApp . location ) + ' using backup ' + data . backupId + ' with v' + data . oldApp . manifest . version ;
case ACTION _APP _REPAIR :
return 'App ' + appName ( data . app ) + ' was re-configured' ; // re-configure of email apps is more common?
case ACTION _APP _LOGIN : {
app = this . getCachedAppSync ( data . appId ) ;
if ( ! app ) return '' ;
return 'App ' + app . fqdn + ' logged in' ;
}
case ACTION _APP _OOM :
if ( ! data . app ) return '' ;
return appName ( data . app ) + ' ran out of memory' ;
case ACTION _APP _DOWN :
if ( ! data . app ) return '' ;
return appName ( data . app ) + ' is down' ;
case ACTION _APP _UP :
if ( ! data . app ) return '' ;
return appName ( data . app ) + ' is back online' ;
case ACTION _APP _START :
if ( ! data . app ) return '' ;
return appName ( data . app ) + ' was started' ;
case ACTION _APP _STOP :
if ( ! data . app ) return '' ;
return appName ( data . app ) + ' was stopped' ;
case ACTION _APP _RESTART :
if ( ! data . app ) return '' ;
return appName ( data . app ) + ' was restarted' ;
case ACTION _BACKUP _START :
return 'Backup started' ;
case ACTION _BACKUP _FINISH :
if ( ! errorMessage ) {
return 'Cloudron backup created with Id ' + data . backupId ;
} else {
return 'Cloudron backup errored with error: ' + errorMessage ;
}
case ACTION _BACKUP _CLEANUP _START :
return 'Backup cleaner started' ;
case ACTION _BACKUP _CLEANUP _FINISH :
return data . errorMessage ? 'Backup cleaner errored: ' + data . errorMessage : 'Backup cleaner removed ' + ( data . removedBoxBackups ? data . removedBoxBackups . length : '0' ) + ' backups' ;
case ACTION _CERTIFICATE _NEW :
return 'Certificate install for ' + data . domain + ( errorMessage ? ' failed' : ' succeeded' ) ;
case ACTION _CERTIFICATE _RENEWAL :
return 'Certificate renewal for ' + data . domain + ( errorMessage ? ' failed' : ' succeeded' ) ;
case ACTION _DASHBOARD _DOMAIN _UPDATE :
return 'Dashboard domain set to ' + data . fqdn ;
case ACTION _DOMAIN _ADD :
return 'Domain ' + data . domain + ' with ' + data . provider + ' provider was added' ;
case ACTION _DOMAIN _UPDATE :
return 'Domain ' + data . domain + ' with ' + data . provider + ' provider was updated' ;
case ACTION _DOMAIN _REMOVE :
return 'Domain ' + data . domain + ' was removed' ;
case ACTION _MAIL _LOCATION :
return 'Mail server location was changed to ' + data . subdomain + ( data . subdomain ? '.' : '' ) + data . domain ;
case ACTION _MAIL _ENABLED :
return 'Mail was enabled for domain ' + data . domain ;
case ACTION _MAIL _DISABLED :
return 'Mail was disabled for domain ' + data . domain ;
case ACTION _MAIL _MAILBOX _ADD :
return 'Mailbox with name ' + data . name + ' was added in domain ' + data . domain ;
case ACTION _MAIL _MAILBOX _UPDATE :
return 'Mailbox with name ' + data . name + ' was updated in domain ' + data . domain ;
case ACTION _MAIL _MAILBOX _REMOVE :
return 'Mailbox with name ' + data . name + ' was removed in domain ' + data . domain ;
case ACTION _MAIL _LIST _ADD :
return 'Mail list with name ' + data . name + ' was added in domain ' + data . domain ;
case ACTION _MAIL _LIST _UPDATE :
return 'Mail list with name ' + data . name + ' was updated in domain ' + data . domain ;
case ACTION _MAIL _LIST _REMOVE :
return 'Mail list with name ' + data . name + ' was removed in domain ' + data . domain ;
case ACTION _START :
return 'Cloudron started with version ' + data . version ;
case ACTION _UPDATE :
return 'Cloudron update to version ' + data . boxUpdateInfo . version + ' was started' ;
case ACTION _UPDATE _FINISH :
if ( data . errorMessage ) {
return 'Cloudron update errored. Error: ' + data . errorMessage ;
} else {
return 'Cloudron updated to version ' + data . newVersion ;
}
case ACTION _USER _ADD :
return data . email + ( data . user . username ? ' (' + data . user . username + ')' : '' ) + ' was added' ;
case ACTION _USER _UPDATE :
return ( data . user ? ( data . user . email + ( data . user . username ? ' (' + data . user . username + ')' : '' ) ) : data . userId ) + ' was updated' ;
case ACTION _USER _REMOVE :
return ( data . user ? ( data . user . email + ( data . user . username ? ' (' + data . user . username + ')' : '' ) ) : data . userId ) + ' was removed' ;
case ACTION _USER _TRANSFER :
return 'Apps of ' + data . oldOwnerId + ' was transferred to ' + data . newOwnerId ;
case ACTION _USER _LOGIN :
return ( data . user ? data . user . username : data . userId ) + ' logged in' ;
case ACTION _USER _LOGOUT :
return ( data . user ? data . user . username : data . userId ) + ' logged out' ;
case ACTION _DYNDNS _UPDATE :
return 'DNS was updated from ' + data . fromIp + ' to ' + data . toIp ;
case ACTION _SUPPORT _SSH :
return 'Remote Support was ' + ( data . enable ? 'enabled' : 'disabled' ) ;
case ACTION _SUPPORT _TICKET :
return 'Support ticket was created' ;
case ACTION _SYSTEM _CRASH :
return 'A system process crashed' ;
case ACTION _VOLUME _ADD :
return 'Volume "' + data . volume . name + '" was added' ;
case ACTION _VOLUME _UPDATE :
return 'Volme "' + data . volume . name + '" was updated' ;
case ACTION _VOLUME _REMOVE :
return 'Volume "' + data . volume . name + '" was removed' ;
default : return eventLog . action ;
}
}
Client . prototype . eventLogSource = function ( eventLog ) {
var source = eventLog . source ;
var line = '' ;
line = source . username || source . userId || source . mailboxId || source . authType || 'system' ;
if ( source . appId ) {
var app = this . getCachedAppSync ( source . appId ) ;
line += ' - ' + ( app ? app . fqdn : source . appId ) ;
} else if ( source . ip ) {
line += ' - ' + source . ip ;
}
return line ;
}
2018-01-22 13:01:38 -08:00
client = new Client ( ) ;
return client ;
} ] ) ;