mirror of
https://activitypub.software/TransFem-org/Sharkey
synced 2024-11-22 14:05:12 +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 urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;
|
||||
|
||||
export function fromHtml(html: string, hashtagNames?: string[]): string | null {
|
||||
if (html == null) return null;
|
||||
|
||||
export function fromHtml(html: string, hashtagNames?: string[]): string {
|
||||
const dom = parse5.parseFragment(html);
|
||||
|
||||
let text = '';
|
||||
|
|
|
@ -24,14 +24,14 @@ const SHUTDOWN_TIMEOUT = 15000;
|
|||
* down the process.
|
||||
* @type {BeforeShutdownListener[]}
|
||||
*/
|
||||
const shutdownListeners = [];
|
||||
const shutdownListeners: ((signalOrEvent: string) => void)[] = [];
|
||||
|
||||
/**
|
||||
* Listen for signals and execute given `fn` function once.
|
||||
* @param {string[]} signals System signals to listen to.
|
||||
* @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) {
|
||||
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.
|
||||
* @param {number} timeout Time to wait before forcing shutdown (milliseconds)
|
||||
*/
|
||||
const forceExitAfter = timeout => () => {
|
||||
const forceExitAfter = (timeout: number) => () => {
|
||||
setTimeout(() => {
|
||||
// Force shutdown after timeout
|
||||
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.
|
||||
* @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);
|
||||
|
||||
console.warn(`Shutting down: received [${signalOrEvent}] signal`);
|
||||
|
@ -64,7 +64,9 @@ async function shutdownHandler(signalOrEvent) {
|
|||
try {
|
||||
await listener(signalOrEvent);
|
||||
} 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.
|
||||
* @returns {BeforeShutdownListener} Echoes back the supplied `listener`.
|
||||
*/
|
||||
export function beforeShutdown(listener) {
|
||||
export function beforeShutdown(listener: () => void) {
|
||||
shutdownListeners.push(listener);
|
||||
return listener;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,9 @@ export async function importCustomEmojis(job: Bull.Job<DbUserImportJobData>, don
|
|||
fs.writeFileSync(destPath, '', 'binary');
|
||||
await downloadUrl(file.url, destPath);
|
||||
} catch (e) { // TODO: 何度か再試行
|
||||
logger.error(e);
|
||||
if (e instanceof Error || typeof e === 'string') {
|
||||
logger.error(e);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,10 +54,12 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
|
|||
authUser = await dbResolver.getAuthUserFromApId(getApId(activity.actor));
|
||||
} catch (e) {
|
||||
// 対象が4xxならスキップ
|
||||
if (e instanceof StatusError && e.isClientError) {
|
||||
return `skip: Ignored deleted actors on both ends ${activity.actor} - ${e.statusCode}`;
|
||||
if (e instanceof StatusError) {
|
||||
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);
|
||||
} catch (e) {
|
||||
// 対象が4xxならスキップ
|
||||
if (e instanceof StatusError && e.isClientError) {
|
||||
logger.warn(`Ignored announce target ${targetUri} - ${e.statusCode}`);
|
||||
return;
|
||||
if (e instanceof StatusError) {
|
||||
if (e.isClientError) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ export default async (actor: IRemoteUser, activity: IFlag): Promise<string> => {
|
|||
// 対象ユーザーは一番最初のユーザー として あとはコメントとして格納する
|
||||
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({
|
||||
id: In(userIds),
|
||||
});
|
||||
|
|
|
@ -25,8 +25,10 @@ export async function performActivity(actor: IRemoteUser, activity: IObject) {
|
|||
const act = await resolver.resolve(item);
|
||||
try {
|
||||
await performOneActivity(actor, act);
|
||||
} catch (e) {
|
||||
apLogger.error(e);
|
||||
} catch (err) {
|
||||
if (err instanceof Error || typeof err === 'string') {
|
||||
apLogger.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue