Switch to recommended chat server endpoint.

This commit is contained in:
flash 2025-04-22 22:30:24 +00:00
commit c83aecfc67
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
4 changed files with 87 additions and 33 deletions
src/ami.js

View file

@ -94,6 +94,14 @@ const Flashii = function(baseUrl) {
fii.v1 = {};
const verifyChatServerId = id => {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable chat server id.');
return id;
};
fii.v1.chat = {};
fii.v1.chat.login = function({ redirect=null, assign=true }) {
return new Commitment(success => {
@ -111,6 +119,73 @@ const Flashii = function(baseUrl) {
success(url);
});
};
fii.v1.chat.servers = function({ proto=null, secure=null, fields=null, fresh=false }) {
return new Commitment((success, fail) => {
const params = {};
if(proto !== null && typeof proto !== 'string')
throw new Error('proto must be a string or null');
params['proto'] = proto;
if(secure !== null)
params['secure'] = secure ? '1' : '0';
send({ method: 'GET', path: '/v1/chat/servers', fields, fresh, params })
.success(({ status, body }) => {
if(status === 400)
throw new Error('An argument contains an unsupported value.');
if(status > 299)
throw new Error(`Failed to fetch chat server list with error code ${status}.`);
success(body);
})
.fail(fail)
.run();
});
};
fii.v1.chat.servers.server = function({ id, fields=null, fresh=false }) {
return new Commitment((success, fail) => {
id = verifyChatServerId(id);
send({ method: 'GET', path: `/v1/chat/servers/${id}`, fields, fresh })
.success(({ status, body }) => {
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
if(status === 404)
throw new Error('No chat server with that id could be found.');
if(status > 299)
throw new Error(`Failed to fetch chat server with error code ${status}.`);
success(body);
})
.fail(fail)
.run();
});
};
fii.v1.chat.servers.recommended = function({ proto, secure=null, fields=null, fresh=false }) {
return new Commitment((success, fail) => {
if(typeof proto !== 'string')
throw new Error('proto must be a string');
const params = { proto };
if(secure !== null)
params['secure'] = secure ? '1' : '0';
send({ method: 'GET', path: '/v1/chat/servers/recommended', fields, fresh, params })
.success(({ status, body }) => {
if(status === 400)
throw new Error('An argument contains an unsupported value.');
if(status === 404)
throw new Error('Was unable to determine a server based on the requested parameters.');
if(status > 299)
throw new Error(`Failed to fetch recommended chat server with error code ${status}.`);
success(body);
})
.fail(fail)
.run();
});
};
fii.v1.chat.token = function() {
return new Commitment((success, fail) => {
send({ method: 'GET', path: '/v1/chat/token', authed: true, fresh: true })