wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 10.2 kB
JavaScript
import{MessengerError,MessengerErrorCode,DEFAULT_MESSENGER_CONFIG}from'./types.js';export class MessengerServerClient{['config'];constructor(a){this['config']={...DEFAULT_MESSENGER_CONFIG,...a};}async['registerDevice'](a){try{const b=await fetch(this['config']['serverUrl']+'/v1/register',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](a)});if(!b['ok']){const d=await b['text']();if(b['status']===0x199){if(d['includes']('different\x20key'))throw new MessengerError(MessengerErrorCode['IDENTITY_CONFLICT'],'Identity\x20already\x20exists\x20with\x20different\x20key:\x20'+d);return{'success':!![],'uploadedCount':0x0,'failedPrekeys':[],'currentTotal':0x0,'maxAllowed':0x64};}throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Registration\x20failed:\x20'+b['status']+'\x20'+b['statusText']+'\x20-\x20'+d);}const c=await b['json']();if(c['failedPrekeys']&&c['failedPrekeys']['length']>0x0){}return{'success':c['success'],'uploadedCount':c['uploaded_count'],'failedPrekeys':c['failed_prekeys']||[],'currentTotal':c['current_total'],'maxAllowed':c['max_allowed']};}catch(e){if(e instanceof MessengerError)throw e;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20register\x20device',e);}}async['uploadPreKeys'](a){try{const b=await fetch(this['config']['serverUrl']+'/v1/prekeys/upload',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](a)});if(!b['ok']){const d=await b['text']();throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20upload\x20prekeys:\x20'+b['status']+'\x20'+b['statusText']+'\x20-\x20'+d);}const c=await b['json']();if(c['failedPrekeys']&&c['failedPrekeys']['length']>0x0)throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Some\x20prekeys\x20failed\x20to\x20upload:\x20'+JSON['stringify'](c['failedPrekeys']));return{'success':c['success'],'uploadedCount':c['uploaded_count'],'failedPrekeys':c['failed_prekeys']||[],'currentTotal':c['current_total'],'maxAllowed':c['max_allowed']};}catch(e){if(e instanceof MessengerError)throw e;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20upload\x20prekeys',e);}}async['fetchRemoteBundle'](a,b,c,d,e){try{const f=new URLSearchParams({'publicKey':c,'signatureScheme':d['signatureScheme'],'signature':d['signature'],'timestamp':d['timestamp']['toString'](),'nonce':d['nonce']}),g=await fetch(this['config']['serverUrl']+'/v1/bundle/'+a+'?'+f);if(!g['ok']){if(g['status']===0x194)throw new MessengerError(MessengerErrorCode['IDENTITY_NOT_FOUND'],'User\x20'+a+'\x20not\x20found\x20on\x20server');throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20fetch\x20remote\x20bundle:\x20'+g['statusText']);}const h=await g['json']();return h;}catch(i){if(i instanceof MessengerError)throw i;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20fetch\x20remote\x20bundle',i);}}async['sendMessage'](a){try{const b=await fetch(this['config']['serverUrl']+'/v1/message',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](a)}),c=await b['json']();if(!b['ok']){const d=c?.['error']?.['message']||c?.['message']||b['statusText'],e=new MessengerError(MessengerErrorCode['SERVER_ERROR'],d);e['guardList']=c?.['error']?.['guard_list']||c?.['guard_list'];throw e;}return c;}catch(f){if(f instanceof MessengerError)throw f;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20send\x20message',f);}}async['pullMessages'](a,b,c,d){try{const e=new URLSearchParams({'publicKey':b,'signatureScheme':c['signatureScheme'],'signature':c['signature'],'timestamp':c['timestamp']['toString'](),'nonce':c['nonce']});d!==undefined&&e['set']('limit',d['toString']());const f=await fetch(this['config']['serverUrl']+'/v1/messages/'+a+'?'+e);if(!f['ok'])throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20pull\x20messages:\x20'+f['statusText']);const g=await f['json']();return g;}catch(h){if(h instanceof MessengerError)throw h;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20pull\x20messages',h);}}async['getPrekeyStatus'](a){try{const b=await fetch(this['config']['serverUrl']+'/v1/prekeys/'+a+'/status');if(!b['ok'])throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20get\x20prekey\x20status:\x20'+b['statusText']);const c=await b['json']();return{'currentCount':c['currentCount'],'maxAllowed':c['maxAllowed'],'threshold':c['threshold'],'shouldRefill':c['shouldRefill']};}catch(d){if(d instanceof MessengerError)throw d;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20get\x20prekey\x20status',d);}}async['acknowledgeMessages'](a,b,c){try{const d=await fetch(this['config']['serverUrl']+'/v1/messages/ack',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify']({'publicKey':c['publicKey'],'messageIds':a,'senderPublicKey':b,'signatureScheme':c['signatureScheme'],'signature':c['signature'],'timestamp':c['timestamp'],'nonce':c['nonce']})});if(!d['ok'])throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20acknowledge\x20messages:\x20'+d['statusText']);}catch(e){if(e instanceof MessengerError)throw e;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20acknowledge\x20messages',e);}}async['addToBlacklist'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/blacklist',b,'POST');}async['removeFromBlacklist'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/blacklist',b,'DELETE');}async['clearBlacklist'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/blacklist/clear',b,'POST');}async['getBlacklist'](a,b){try{const c=await fetch(this['config']['serverUrl']+'/v1/spam-protection/'+a+'/blacklist/get',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](b)});if(!c['ok']){const d=await c['text']();console['error']('getBlacklist\x20error\x20response:',d);throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20get\x20blacklist:\x20'+c['statusText']+'\x20-\x20'+d);}return await c['json']();}catch(e){if(e instanceof MessengerError)throw e;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20get\x20blacklist',e);}}async['existInBlacklist'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/blacklist/exist',b,'POST');}async['addToFriendsList'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/friends-list',b,'POST');}async['removeFromFriendsList'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/friends-list',b,'DELETE');}async['clearFriendsList'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/friends-list/clear',b,'POST');}async['getFriendsList'](a,b){try{const c=await fetch(this['config']['serverUrl']+'/v1/spam-protection/'+a+'/friends-list/get',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](b)});if(!c['ok'])throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20get\x20friends\x20list:\x20'+c['statusText']);return await c['json']();}catch(d){if(d instanceof MessengerError)throw d;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20get\x20friends\x20list',d);}}async['existInFriendsList'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/friends-list/exist',b,'POST');}async['addToGuardList'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/guard-list',b,'POST');}async['removeFromGuardList'](a,b){return this['_listOperation'](this['config']['serverUrl']+'/v1/spam-protection/'+a+'/guard-list',b,'DELETE');}async['getGuardList'](a,b){try{const c=await fetch(this['config']['serverUrl']+'/v1/spam-protection/'+a+'/guard-list/get',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](b)});if(!c['ok'])throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20get\x20guard\x20list:\x20'+c['statusText']);return await c['json']();}catch(d){if(d instanceof MessengerError)throw d;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20get\x20guard\x20list',d);}}async['updateSettings'](a,b){try{const c=await fetch(this['config']['serverUrl']+'/v1/spam-protection/'+a+'/settings',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](b)});if(!c['ok']){const e=await c['text']();throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20update\x20settings:\x20'+c['statusText']+'\x20-\x20'+e);}const d=await c['json']();return d['success'];}catch(f){if(f instanceof MessengerError)throw f;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20update\x20settings',f);}}async['getSettings'](a,b){try{const c=await fetch(this['config']['serverUrl']+'/v1/spam-protection/'+a+'/settings/get',{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](b)});if(!c['ok']){const d=await c['text']();throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'Failed\x20to\x20get\x20settings:\x20'+c['statusText']+'\x20-\x20'+d);}return await c['json']();}catch(e){if(e instanceof MessengerError)throw e;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20get\x20settings',e);}}async['_listOperation'](a,b,c='POST'){try{const d=await fetch(a,{'method':c,'headers':{'Content-Type':'application/json'},'body':JSON['stringify'](b)});if(!d['ok']){const e=await d['text']();throw new MessengerError(MessengerErrorCode['SERVER_ERROR'],'List\x20operation\x20failed:\x20'+d['statusText']+'\x20-\x20'+e);}return await d['json']();}catch(f){if(f instanceof MessengerError)throw f;throw new MessengerError(MessengerErrorCode['NETWORK_ERROR'],'Failed\x20to\x20perform\x20list\x20operation',f);}}}export const defaultServerClient=new MessengerServerClient();