Compare commits

...

2 commits

Author SHA1 Message Date
c4872f1fc0
Parenthesis fixes. 2025-05-19 11:33:10 +00:00
911bf2f934
Attempt at better handling of auth redirect. 2025-05-19 11:13:31 +00:00
4 changed files with 52 additions and 46 deletions

View file

@ -22,9 +22,10 @@ const MamiFlashiiAuth = function(flashii) {
async refresh() {
try {
({ token_type: type=null, access_token: token=null } = await flashii.v1.chat.token({}));
({ token_type: type=null, access_token: token=null } = await flashii.v1.chat.token());
} catch(ex) {
//await flashii.v1.chat.login({}); what if the internet didn't suck balls
if(ex instanceof Error && ex.cause === 'flashii.v1.unauthorized')
await flashii.v1.chat.login();
}
},
};

View file

@ -70,7 +70,7 @@ const Flashii = function(baseUrl) {
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.');
throw new Error('id argument is not an acceptable chat server id.', { cause: 'flashii.v1.chat.servers.id' });
return id;
};
@ -79,7 +79,7 @@ const Flashii = function(baseUrl) {
return new Promise(resolve => {
redirect ??= `${location.protocol}//${location.host}`;
if(typeof redirect !== 'string')
throw new Error('redirect must a string.');
throw new Error('redirect must a string.', { cause: 'flashii.v1.chat.login.redirect' });
const url = createUrl('/v1/chat/login');
url.searchParams.set('redirect', redirect);
@ -95,7 +95,7 @@ const Flashii = function(baseUrl) {
const params = {};
if(proto !== null && typeof proto !== 'string')
throw new Error('proto must be a string or null');
throw new Error('proto must be a string or null', { cause: 'flashii.v1.chat.servers.proto' });
params['proto'] = proto;
if(secure !== null)
@ -104,9 +104,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/servers', fields, fresh, params });
if(status === 400)
throw new Error('An argument contains an unsupported value.');
throw new Error('An argument contains an unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch chat server list with error code ${status}.`);
throw new Error(`Failed to fetch chat server list with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -116,11 +116,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/chat/servers/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('No chat server with that id could be found.');
throw new Error('No chat server with that id could be found.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch chat server with error code ${status}.`);
throw new Error(`Failed to fetch chat server with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -135,11 +135,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/servers/recommended', fields, fresh, params });
if(status === 400)
throw new Error('An argument contains an unsupported value.');
throw new Error('An argument contains an unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Was unable to determine a server based on the requested parameters.');
throw new Error('Was unable to determine a server based on the requested parameters.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch recommended chat server with error code ${status}.`);
throw new Error(`Failed to fetch recommended chat server with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -147,9 +147,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/token', authed: true, fresh: true });
if(status === 403)
throw new Error('You must be logged in to use chat.');
throw new Error('You must be logged in to use chat.', { cause: 'flashii.v1.unauthorized' });
if(status > 299)
throw new Error(`Failed to fetch authorization token with error code ${status}.`);
throw new Error(`Failed to fetch authorization token with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -165,9 +165,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/colours/presets', fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch colour presets with error code ${status}.`);
throw new Error(`Failed to fetch colour presets with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -177,11 +177,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/colours/presets/${name}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested colour preset does not exist.');
throw new Error('Requested colour preset does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch colour preset "${name}" with error code ${status}.`);
throw new Error(`Failed to fetch colour preset "${name}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -190,7 +190,7 @@ const Flashii = function(baseUrl) {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable emoticon id.');
throw new Error('id argument is not an acceptable emoticon id.', { cause: 'flashii.v1.emotes.id' });
return id;
};
@ -198,9 +198,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/emotes', fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch emoticons with error code ${status}.`);
throw new Error(`Failed to fetch emoticons with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -210,11 +210,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/emotes/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested emoticon does not exist.');
throw new Error('Requested emoticon does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`);
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -224,11 +224,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/emotes/${id}/strings`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested emoticon does not exist.');
throw new Error('Requested emoticon does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`);
throw new Error(`Failed to fetch emoticon "${id}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -237,7 +237,7 @@ const Flashii = function(baseUrl) {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable kaomoji id.');
throw new Error('id argument is not an acceptable kaomoji id.', { cause: 'flashii.v1.kaomoji.id' });
return id;
};
@ -245,9 +245,9 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: '/v1/kaomoji', params: { as }, fields, fresh });
if(status === 400)
throw new Error('As or fields argument contains unsupported value.');
throw new Error('As or fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status > 299)
throw new Error(`Failed to fetch kaomoji with error code ${status}.`);
throw new Error(`Failed to fetch kaomoji with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};
@ -257,11 +257,11 @@ const Flashii = function(baseUrl) {
const { status, body } = await send({ method: 'GET', path: `/v1/kaomoji/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
throw new Error('Fields argument contains unsupported value.', { cause: 'flashii.v1.value' });
if(status === 404)
throw new Error('Requested kaomoji does not exist.');
throw new Error('Requested kaomoji does not exist.', { cause: 'flashii.v1.notfound' });
if(status > 299)
throw new Error(`Failed to fetch kaomoji "${id}" with error code ${status}.`);
throw new Error(`Failed to fetch kaomoji "${id}" with error code ${status}.`, { cause: `flashii.http${status}` });
return body;
};

View file

@ -117,7 +117,7 @@ const MamiSockChatHandlers = function(
}
if(ev.detail.session.needsAuth) {
ctx.flashii.v1.chat.login({});
ctx.flashii.v1.chat.login();
return;
}

View file

@ -254,8 +254,13 @@ Umi.UI.Messages = (function() {
continue;
let offset = 0;
const parts = childNode.data.split(/([\p{White_Space}(){}\[\]<>'"]+)/u);
for(const part of parts) {
const parts = childNode.data.split(/([\p{White_Space}{}\[\]<>'"]+)/u);
for(let part of parts) {
while(part.startsWith('(') && part.endsWith(')')) {
offset += 1;
part = part.substring(1, part.length - 1);
}
const noProto = part.startsWith('//');
if(!noProto && !part.includes('://')) {
offset += part.length;
@ -275,20 +280,20 @@ Umi.UI.Messages = (function() {
offset = 0;
}
let text = part;
while(!text.endsWith('/') && /\p{P}$/u.test(text))
text = text.substring(0, text.length - 1);
const length = part.length;
while(!part.endsWith('/') && /\p{Po}$/u.test(part))
part = part.substring(0, part.length - 1);
if(text.length < part.length) {
url = new URL(noProto ? (location.protocol + text) : text);
offset += part.length - text.length;
if(part.length < length) {
url = new URL(noProto ? (location.protocol + part) : part);
offset += length - part.length;
}
const replaceNode = childNode;
childNode = replaceNode.splitText(text.length);
childNode = replaceNode.splitText(part.length);
replaceNode.replaceWith(<a class="markup__link" href={url.toString()} target="_blank" rel="nofollow noreferrer noopener">
{decodeURI(text)}
{decodeURIComponent(part)}
</a>);
}
} finally {