import Client , { ClientError , IQuota , UserGroup , UserGroupDoesNotExistError } from "./client" ; import FileSizeFormatter from "./fileSizeFormatter" ; export enum UserProperty { email = "email" , quota = "quota" , displayName = "displayname" , phone = "phone" , address = "address" , website = "website" , twitter = "twitter" , password = "password" , language = "language" , locale = "locale" } export interface IUserOptionsQuota { "free" ? : number ; "used" : number ; "total" ? : number ; "relative" : number ; "quota" : number ; } export interface IUserQuotaUserFriendly { "free" ? : string ; "used" : string ; "total" ? : string ; "quota" : string ; "relative" : string ; } export interface IUserOptions { "enabled" : boolean ; "lastLogin" ? : Date ; "subadminGroups" : string [ ] ; "memberGroups" : string [ ] ; "quota" : IUserOptionsQuota ; "email" : string ; "displayName" : string ; "phone" : string ; "address" : string ; "website" : string ; "twitter" : string ; "language" : string ; "locale" : string ; } /**
 * The user class represents a user in nextcloud.
 * async getGroups
 * async isDisabled
 * async getLastLogin
 * async getEmail
 * getId
 * async getDisplayName
 */ export default class User { private memento ? : IUserOptions ; private client : Client ; readonly id : string ; constructor param { Client } client param { string } id param { IUserOptions } options constructor ( client , id , options ? ) { this . id = id ; this . client = client ; this . memento = options ; } async returns { Promise < boolean > } throws async isEnabled ( ) { return ( await this . getUserData ( ) ) . enabled ; } async returns { Promise < void > } throws async disable ( ) { delete this . memento ; return await this . client . disableUser ( this . id ) ; } async returns { Promise < void > } throws async enable ( ) { delete this . memento ; return await this . client . enableUser ( this . id ) ; } async returns { Promise < Date | null > } throws async getLastLogin ( ) { const data = await this . getUserData ( ) ; if ( data . lastLogin ) { return data . lastLogin ; } return null ; } async returns { Promise < string > } throws async getDisplayName ( ) { return ( await this . getUserData ( ) ) . displayName ; } async param { string } value returns { Promise < void > } throws throws async setDisplayName ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . displayName , value ) ; delete this . memento ; } async returns { Promise < IUserOptionsQuota > } throws async getQuota ( ) { return ( await this . getUserData ( ) ) . quota ; } async returns { Promise < IUserQuotaUserFriendly > } throws async getQuotaUserFriendly ( ) { const q = ( await this . getUserData ( ) ) . quota ; const qUF = { used : new FileSizeFormatter ( q . used ) . getUserFriendlyFileSize ( ) , quota : new FileSizeFormatter ( q . quota ) . getUserFriendlyFileSize ( ) , relative : Math . round ( q . relative ) + " %" } ; if ( q . total ) { qUF . total = new FileSizeFormatter ( q . total ) . getUserFriendlyFileSize ( ) ; } if ( q . free ) { qUF . free = new FileSizeFormatter ( q . free ) . getUserFriendlyFileSize ( ) ; } return qUF ; } async param { string } value returns { Promise < void > } throws throws async setQuota ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . quota , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getEmail ( ) { return ( await this . getUserData ( ) ) . email ; } async param { string } value returns { Promise < void > } throws throws async setEmail ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . email , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getPhone ( ) { return ( await this . getUserData ( ) ) . phone ; } async param { string } value returns { Promise < void > } throws throws async setPhone ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . phone , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getAddress ( ) { return ( await this . getUserData ( ) ) . address ; } async param { string } value returns { Promise < void > } throws throws async setAddress ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . address , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getWebsite ( ) { return ( await this . getUserData ( ) ) . website ; } async param { string } value returns { Promise < void > } throws throws async setWebsite ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . website , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getTwitter ( ) { return ( await this . getUserData ( ) ) . twitter ; } async param { string } value returns { Promise < void > } throws throws async setTwitter ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . twitter , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getLanguage ( ) { return ( await this . getUserData ( ) ) . language ; } async param { string } value returns { Promise < void > } throws throws async setLanguage ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . language , value ) ; delete this . memento ; } async returns { Promise < string > } throws async getLocale ( ) { return ( await this . getUserData ( ) ) . locale ; } async param { string } value returns { Promise < void > } throws throws async setLocale ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . locale , value ) ; delete this . memento ; } async param { string } value returns { Promise < void > } throws throws async setPassword ( value ) { await this . client . updateUserProperty ( this . id , UserProperty . password , value ) ; } async returns { Promise < void > } throws async resendWelcomeEmail ( ) { await this . client . resendWelcomeEmail ( this . id ) ; } async returns { Promise < UserGroup [ ] } throws async getMemberUserGroups ( ) { const groupIds = ( await this . getUserData ( ) ) . memberGroups ; const result = [ ] ; for ( const groupId of groupIds ) { result . push ( new UserGroup ( this . client , groupId ) ) ; } return result ; } async returns { Promise < string [ ] } throws async getMemberUserGroupIds ( ) { return ( await this . getUserData ( ) ) . memberGroups ; } async param { UserGroup } userGroup returns { Promise < void > } throws throws throws throws async addToMemberUserGroup ( userGroup ) { await this . client . addUserToMemberUserGroup ( this . id , userGroup . id ) ; delete this . memento ; return ; } async param { UserGroup } userGroup returns { Promise < void > } throws throws throws throws async removeFromMemberUserGroup ( userGroup ) { await this . client . removeUserFromMemberUserGroup ( this . id , userGroup . id ) ; delete this . memento ; return ; } async returns { Promise < boolean > } throws async isSuperAdmin ( ) { return ( await this . getUserData ( ) ) . memberGroups . indexOf ( "admin" ) === - 1 ? false : true ; } async returns { Promise < void > } throws throws throws throws async promoteToSuperAdmin ( ) { await this . addToMemberUserGroup ( new UserGroup ( this . client , "admin" ) ) ; delete this . memento ; return ; } async returns { Promise < void > } throws throws throws throws async demoteFromSuperAdmin ( ) { if ( await this . isSuperAdmin ( ) ) { await this . removeFromMemberUserGroup ( new UserGroup ( this . client , "admin" ) ) ; delete this . memento ; } return ; } async returns { Promise < UserGroup [ ] } throws async getSubadminUserGroups ( ) { const groupIds = ( await this . getUserData ( ) ) . subadminGroups ; const result = [ ] ; for ( const groupId of groupIds ) { result . push ( new UserGroup ( this . client , groupId ) ) ; } return result ; } async returns { Promise < string [ ] } throws async getSubadminUserGroupIds ( ) { return ( await this . getUserData ( ) ) . subadminGroups ; } async param { UserGroup } userGroup returns { Promise < void > } throws throws throws throws async promoteToUserGroupSubadmin ( userGroup ) { await this . client . promoteUserToUserGroupSubadmin ( this . id , userGroup . id ) ; delete this . memento ; return ; } async param { UserGroup } userGroup returns { Promise < void > } throws throws throws throws async demoteFromSubadminUserGroup ( userGroup ) { await this . client . demoteUserFromSubadminUserGroup ( this . id , userGroup . id ) ; delete this . memento ; return ; } async returns { Promise < void > } throws async delete ( ) { return await this . client . deleteUser ( this . id ) ; } async returns { Promise < IUserOptions > } throws private async getUserData ( ) { if ( ! this . memento ) { this . memento = await this . client . getUserData ( this . id ) ; } return this . memento ; } }