mirror of
https://activitypub.software/TransFem-org/Sharkey
synced 2024-11-26 07:55:14 +00:00
refactor
This commit is contained in:
parent
73de9be6d5
commit
bd7662e5e4
7 changed files with 29 additions and 20 deletions
|
@ -5,9 +5,7 @@ import { URL } from 'url';
|
||||||
const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/;
|
const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/;
|
||||||
const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;
|
const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;
|
||||||
|
|
||||||
export function fromHtml(html: string, hashtagNames?: string[]): string | null {
|
export function fromHtml(html: string, hashtagNames?: string[]): string {
|
||||||
if (html == null) return null;
|
|
||||||
|
|
||||||
const dom = parse5.parseFragment(html);
|
const dom = parse5.parseFragment(html);
|
||||||
|
|
||||||
let text = '';
|
let text = '';
|
||||||
|
|
|
@ -24,14 +24,14 @@ const SHUTDOWN_TIMEOUT = 15000;
|
||||||
* down the process.
|
* down the process.
|
||||||
* @type {BeforeShutdownListener[]}
|
* @type {BeforeShutdownListener[]}
|
||||||
*/
|
*/
|
||||||
const shutdownListeners = [];
|
const shutdownListeners: ((signalOrEvent: string) => void)[] = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listen for signals and execute given `fn` function once.
|
* Listen for signals and execute given `fn` function once.
|
||||||
* @param {string[]} signals System signals to listen to.
|
* @param {string[]} signals System signals to listen to.
|
||||||
* @param {function(string)} fn Function to execute on shutdown.
|
* @param {function(string)} fn Function to execute on shutdown.
|
||||||
*/
|
*/
|
||||||
const processOnce = (signals, fn) => {
|
const processOnce = (signals: string[], fn: (signalOrEvent: string) => void) => {
|
||||||
for (const sig of signals) {
|
for (const sig of signals) {
|
||||||
process.once(sig, fn);
|
process.once(sig, fn);
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ const processOnce = (signals, fn) => {
|
||||||
* Sets a forced shutdown mechanism that will exit the process after `timeout` milliseconds.
|
* Sets a forced shutdown mechanism that will exit the process after `timeout` milliseconds.
|
||||||
* @param {number} timeout Time to wait before forcing shutdown (milliseconds)
|
* @param {number} timeout Time to wait before forcing shutdown (milliseconds)
|
||||||
*/
|
*/
|
||||||
const forceExitAfter = timeout => () => {
|
const forceExitAfter = (timeout: number) => () => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// Force shutdown after timeout
|
// Force shutdown after timeout
|
||||||
console.warn(`Could not close resources gracefully after ${timeout}ms: forcing shutdown`);
|
console.warn(`Could not close resources gracefully after ${timeout}ms: forcing shutdown`);
|
||||||
|
@ -55,7 +55,7 @@ const forceExitAfter = timeout => () => {
|
||||||
* be logged out as a warning, but won't prevent other callbacks from executing.
|
* be logged out as a warning, but won't prevent other callbacks from executing.
|
||||||
* @param {string} signalOrEvent The exit signal or event name received on the process.
|
* @param {string} signalOrEvent The exit signal or event name received on the process.
|
||||||
*/
|
*/
|
||||||
async function shutdownHandler(signalOrEvent) {
|
async function shutdownHandler(signalOrEvent: string) {
|
||||||
if (process.env.NODE_ENV === 'test') return process.exit(0);
|
if (process.env.NODE_ENV === 'test') return process.exit(0);
|
||||||
|
|
||||||
console.warn(`Shutting down: received [${signalOrEvent}] signal`);
|
console.warn(`Shutting down: received [${signalOrEvent}] signal`);
|
||||||
|
@ -64,7 +64,9 @@ async function shutdownHandler(signalOrEvent) {
|
||||||
try {
|
try {
|
||||||
await listener(signalOrEvent);
|
await listener(signalOrEvent);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(`A shutdown handler failed before completing with: ${err.message || err}`);
|
if (err instanceof Error) {
|
||||||
|
console.warn(`A shutdown handler failed before completing with: ${err.message || err}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +80,7 @@ async function shutdownHandler(signalOrEvent) {
|
||||||
* @param {BeforeShutdownListener} listener The shutdown listener to register.
|
* @param {BeforeShutdownListener} listener The shutdown listener to register.
|
||||||
* @returns {BeforeShutdownListener} Echoes back the supplied `listener`.
|
* @returns {BeforeShutdownListener} Echoes back the supplied `listener`.
|
||||||
*/
|
*/
|
||||||
export function beforeShutdown(listener) {
|
export function beforeShutdown(listener: () => void) {
|
||||||
shutdownListeners.push(listener);
|
shutdownListeners.push(listener);
|
||||||
return listener;
|
return listener;
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,9 @@ export async function importCustomEmojis(job: Bull.Job<DbUserImportJobData>, don
|
||||||
fs.writeFileSync(destPath, '', 'binary');
|
fs.writeFileSync(destPath, '', 'binary');
|
||||||
await downloadUrl(file.url, destPath);
|
await downloadUrl(file.url, destPath);
|
||||||
} catch (e) { // TODO: 何度か再試行
|
} catch (e) { // TODO: 何度か再試行
|
||||||
logger.error(e);
|
if (e instanceof Error || typeof e === 'string') {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,10 +54,12 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
|
||||||
authUser = await dbResolver.getAuthUserFromApId(getApId(activity.actor));
|
authUser = await dbResolver.getAuthUserFromApId(getApId(activity.actor));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// 対象が4xxならスキップ
|
// 対象が4xxならスキップ
|
||||||
if (e instanceof StatusError && e.isClientError) {
|
if (e instanceof StatusError) {
|
||||||
return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`;
|
if (e.isClientError) {
|
||||||
|
return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`;
|
||||||
|
}
|
||||||
|
throw `Error in actor ${activity.actor} - ${e.statusCode || e}`;
|
||||||
}
|
}
|
||||||
throw `Error in actor ${activity.actor} - ${e.statusCode || e}`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,11 +42,14 @@ export default async function(resolver: Resolver, actor: IRemoteUser, activity:
|
||||||
renote = await resolveNote(targetUri);
|
renote = await resolveNote(targetUri);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// 対象が4xxならスキップ
|
// 対象が4xxならスキップ
|
||||||
if (e instanceof StatusError && e.isClientError) {
|
if (e instanceof StatusError) {
|
||||||
logger.warn(`Ignored announce target ${targetUri} - ${e.statusCode}`);
|
if (e.isClientError) {
|
||||||
return;
|
logger.warn(`Ignored announce target ${targetUri} - ${e.statusCode}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.warn(`Error in announce target ${targetUri} - ${e.statusCode || e}`);
|
||||||
}
|
}
|
||||||
logger.warn(`Error in announce target ${targetUri} - ${e.statusCode || e}`);
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ export default async (actor: IRemoteUser, activity: IFlag): Promise<string> => {
|
||||||
// 対象ユーザーは一番最初のユーザー として あとはコメントとして格納する
|
// 対象ユーザーは一番最初のユーザー として あとはコメントとして格納する
|
||||||
const uris = getApIds(activity.object);
|
const uris = getApIds(activity.object);
|
||||||
|
|
||||||
const userIds = uris.filter(uri => uri.startsWith(config.url + '/users/')).map(uri => uri.split('/').pop());
|
const userIds = uris.filter(uri => uri.startsWith(config.url + '/users/')).map(uri => uri.split('/').pop()!);
|
||||||
const users = await Users.find({
|
const users = await Users.find({
|
||||||
id: In(userIds),
|
id: In(userIds),
|
||||||
});
|
});
|
||||||
|
|
|
@ -25,8 +25,10 @@ export async function performActivity(actor: IRemoteUser, activity: IObject) {
|
||||||
const act = await resolver.resolve(item);
|
const act = await resolver.resolve(item);
|
||||||
try {
|
try {
|
||||||
await performOneActivity(actor, act);
|
await performOneActivity(actor, act);
|
||||||
} catch (e) {
|
} catch (err) {
|
||||||
apLogger.error(e);
|
if (err instanceof Error || typeof err === 'string') {
|
||||||
|
apLogger.error(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue