UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 8.35 kB
import{toTxObject,toTxString,toTxAddress,toTxAddressVec,isValidName,isValidDescription,PackageAddress,MAX_NAME_LENGTH,isValidArray,GetObjectExisted,GetObject,GetObjectDescription,contactFn,MAX_DESCRIPTION_LENGTH,isValidLongName,MAX_LONG_NAME_LENGTH}from'../common.js';import{W_ERROR,WErrors}from'../exception.js';import{ObjectType}from'../query/object.js';import{CallBase}from'./base.js';import{BuiltinPermissionIndex,newPermission}from'./permission.js';import{create as a148a}from'./permission.js';import{owner_receive,validObjects}from'../util.js';import{Account}from'../local/index.js';import{query_received}from'../query/received.js';import{isValidWowAddress}from'../../utils/wow-types.js';export const MAX_IM_COUNT=0xc8;export class CallContact extends CallBase{['data'];['object_address']=undefined;['permission_address']=undefined;['new_permission_description']=undefined;constructor(a){super(),this['data']=a;}async['prepare'](a){await super['prepare'](a);!this['object_address']&&(this['object_address']=await GetObjectExisted(this['data']?.['object']));if(this['object_address']){this['content']=undefined,await this['update_content'](a,ObjectType['Contact'],this['object_address']);if(!this['content'])W_ERROR(WErrors['InvalidParam'],'CallContact_Data.data.object:'+this['object_address']);this['permission_address']=this['content']['permission'];}else{const b=GetObject(this['data']['object']);this['permission_address']=await GetObjectExisted(b?.['permission']),this['new_permission_description']=GetObjectDescription(b?.['permission']);}}async['call'](a){await this['prepare'](a);const b=[],c=[],d=f=>{this['permission_address']&&!b['includes'](f)&&b['push'](f);},e=this['content'];this['data']?.['my_status']!=null&&((!e||!e['ims']['some'](f=>f['at']===a['account']))&&W_ERROR(WErrors['InvalidParam'],'CallContact_Data.data.my_status:\x20your\x20account\x20not\x20in\x20im\x20list'));this['permission_address']&&!this['object_address']&&d(BuiltinPermissionIndex['MESSNGER_NEW']);this['permission_address']&&this['data']?.['description']!=null&&this['object_address']&&d(BuiltinPermissionIndex['MESSNGER_DESCRIPTION']);this['permission_address']&&this['data']?.['location']!==undefined&&d(BuiltinPermissionIndex['MESSNGER_LOCATION']);this['permission_address']&&this['data']['ims']!=null&&d(BuiltinPermissionIndex['MESSNGER_IM']);this['permission_address']&&this['data']?.['owner_receive']&&d(BuiltinPermissionIndex['MESSNGER_OWNER_RECEIVE']);if(this['permission_address']||c['length']>0x0)return await this['check_permission_and_call'](a,this['permission_address'],b,c);return await this['exec'](a);}async['operate'](a,b,c,d){let e=this['permission_address'],f=this['object_address'];const g=this['content'];!this['object_address']&&(!e&&(e=newPermission(b,this['new_permission_description']??'')),e&&(f=newContact(b,e,this['data']?.['description']??'',c)));(!f||!e)&&W_ERROR(WErrors['InvalidParam'],'CallContact_Data.data.object:'+this['object_address']);this['data']?.['my_status']!=null&&change_status(b,f,this['data']['my_status']);this['data']?.['description']!=null&&this['object_address']&&description_set(b,f,e,this['data']['description'],c);this['data']?.['location']!=null&&location_set(b,f,e,this['data']['location'],c);if(this['data']?.['ims']!=null)switch(this['data']['ims']['op']){case'add':case'set':{this['data']['ims']['im']['length']>MAX_IM_COUNT-(g?.['ims']?.['length']??0x0)&&W_ERROR(WErrors['InvalidParam'],'CallContact_Data.data.ims.im\x20exceeds\x20max\x20count:'+MAX_IM_COUNT);this['data']['ims']['op']==='set'&&im_clear(b,f,e,c);const h=await Account['Instance']()['get_many_address'](this['data']['ims']['im']['map'](k=>k['at'])),j=this['data']['ims']['im']['filter']((k,l)=>h[l]===undefined)['map'](k=>k['at']);j['length']>0x0&&W_ERROR(WErrors['AddressNotFound'],'Address\x20not\x20found:\x20'+j['join'](',\x20'));for(let k=0x0;k<h['length'];k++){const l=h[k];if(l){const m=this['data']['ims']['im'][k];im_add(b,f,e,l,m?.['description'],c);}}break;}case'remove':{const n=await Account['Instance']()['get_many_address'](this['data']['ims']['im']),o=this['data']['ims']['im']['filter']((q,s)=>n[s]===undefined);o['length']>0x0&&W_ERROR(WErrors['AddressNotFound'],'Address\x20not\x20found:\x20'+o['join'](',\x20'));im_remove(b,f,e,n['filter'](q=>q!=null),c);break;}case'clear':im_clear(b,f,e,c);break;}this['data']['owner_receive']!=null&&this['object_address']&&(this['data']['owner_receive']==='recently'&&(this['data']['owner_receive']=await query_received({'name_or_address':{'name_or_address':this['object_address'],'local_mark_first':!![]},'all_type':!![],'no_cache':a['no_cache'],'network':a['network']})),await owner_receive(b,f,e,this['data']['owner_receive'],undefined,q=>PackageAddress+'::contact::'+q,c));if(!this['permission_address']){const q=GetObject(this['data']?.['object']);await this['new_with_mark'](a,b,ObjectType['Permission'],a148a(b,e),GetObject(q['permission']));}!this['object_address']&&await this['new_with_mark'](a,b,ObjectType['Contact'],create(b,f),GetObject(this['data']?.['object']));}}export function create(a,b){return a['moveCall']({'target':contactFn('create'),'arguments':[toTxObject(a,b)]});}export function change_status(a,b,c){return!isValidName(c)&&W_ERROR(WErrors['InvalidParam'],'Status\x20name('+c+')\x20exceeds\x20the\x20length\x20limit('+MAX_NAME_LENGTH+')'),a['moveCall']({'target':contactFn('change_status'),'arguments':[toTxObject(a,b),toTxString(a,c)]});}export function newContact(a,b,c,d){return validObjects([b]),!isValidDescription(c)&&W_ERROR(WErrors['IsValidDescription'],'Description\x20exceeds\x20maximum\x20length('+MAX_DESCRIPTION_LENGTH+')'),d?a['moveCall']({'target':contactFn('new_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,d),toTxString(a,c),a['object']['mutRegistrar'](),a['object']['clock']()]}):a['moveCall']({'target':contactFn('new'),'arguments':[toTxObject(a,b),toTxString(a,c),a['object']['mutRegistrar'](),a['object']['clock']()]});}export function description_set(a,b,c,d,e){validObjects([b,c]),!isValidDescription(d)&&W_ERROR(WErrors['IsValidDescription'],'Description\x20exceeds\x20maximum\x20length('+MAX_DESCRIPTION_LENGTH+')'),e?a['moveCall']({'target':contactFn('description_set_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,e),toTxString(a,d)]}):a['moveCall']({'target':contactFn('description_set'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxString(a,d)]});}export function location_set(a,b,c,d,e){validObjects([b,c]),!isValidLongName(d)&&W_ERROR(WErrors['IsValidLocation'],'Location\x20exceeds\x20maximum\x20length('+MAX_LONG_NAME_LENGTH+')'),e?a['moveCall']({'target':contactFn('location_set_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,e),toTxString(a,d)]}):a['moveCall']({'target':contactFn('location_set'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxString(a,d)]});}export function im_add(a,b,c,d,e,f){validObjects([b,c]),!isValidWowAddress(d)&&W_ERROR(WErrors['InvalidParam'],'IM\x20address('+d+')\x20is\x20not\x20a\x20valid\x20address'),e&&!isValidDescription(e)&&W_ERROR(WErrors['IsValidDescription'],'Description\x20exceeds\x20maximum\x20length('+MAX_DESCRIPTION_LENGTH+')'),f?a['moveCall']({'target':contactFn('im_add_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,f),toTxAddress(a,d),a['pure']['option']('string',e),a['object']['clock']()]}):a['moveCall']({'target':contactFn('im_add'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxAddress(a,d),a['pure']['option']('string',e),a['object']['clock']()]});}export function im_remove(a,b,c,d,e){if(d['length']===0x0)return;validObjects([b,c]),isValidArray(d,f=>{if(!isValidWowAddress(f))return W_ERROR(WErrors['InvalidParam'],'IM\x20address('+f+')\x20is\x20not\x20a\x20valid\x20address'),![];return!![];}),e?a['moveCall']({'target':contactFn('im_remove_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,e),toTxAddressVec(a,d,0x3e8)]}):a['moveCall']({'target':contactFn('im_remove'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxAddressVec(a,d,0x3e8)]});}export function im_clear(a,b,c,d){validObjects([b,c]),d?a['moveCall']({'target':contactFn('im_clear_with_passport'),'arguments':[toTxObject(a,b),toTxObject(a,c),toTxObject(a,d)]}):a['moveCall']({'target':contactFn('im_clear'),'arguments':[toTxObject(a,b),toTxObject(a,c)]});}