diff --git a/lib/services/timelines/index.ts b/lib/services/timelines/index.ts index 6e5440a5..9467b457 100644 --- a/lib/services/timelines/index.ts +++ b/lib/services/timelines/index.ts @@ -1,7 +1,7 @@ import { Actor } from '@/lib/models/actor' import { Status } from '@/lib/models/status' import { Storage } from '@/lib/storage/types' -import { getSpan } from '@/lib/utils/trace' +import { getSpan, getTracer } from '@/lib/utils/trace' import { mainTimelineRule } from './main' import { mentionTimelineRule } from './mention' @@ -11,57 +11,63 @@ export const addStatusToTimelines = async ( storage: Storage, status: Status ): Promise => { - const span = getSpan('timelines', 'addStatusToTimelines', { - statusId: status.id - }) - const { to, cc, actorId } = status - const recipients = [...to, ...cc, actorId] - const localActors = ( - await Promise.all(recipients.map((id) => storage.getActorFromId({ id }))) - ).filter( - (actor): actor is Actor => actor !== undefined && actor.privateKey !== '' - ) - const getLocalActorsFromFollowerUrl = ( - await Promise.all( - recipients.map((id) => - storage.getLocalActorsFromFollowerUrl({ followerUrl: id }) + return getTracer().startActiveSpan( + 'timelines.addStatusToTimelines', + { attributes: { statusId: status.id } }, + async (span) => { + const { to, cc, actorId } = status + const recipients = [...to, ...cc, actorId] + const localActors = ( + await Promise.all( + recipients.map((id) => storage.getActorFromId({ id })) + ) + ).filter( + (actor): actor is Actor => + actor !== undefined && actor.privateKey !== '' ) - ) - ).flat() + const getLocalActorsFromFollowerUrl = ( + await Promise.all( + recipients.map((id) => + storage.getLocalActorsFromFollowerUrl({ followerUrl: id }) + ) + ) + ).flat() - const actors = Object.values( - [...localActors, ...getLocalActorsFromFollowerUrl].reduce( - (output, actor) => { - return { - ...output, - [actor.id]: actor - } - }, - {} as { [key in string]: Actor } - ) - ) - await Promise.all( - actors - .map((actor) => { - return [ - mainTimelineRule, - noannounceTimelineRule, - mentionTimelineRule - ].map(async (timelineFunction) => { - const timeline = await timelineFunction({ - currentActor: actor, - status: status.data, - storage - }) - if (!timeline) return - return storage.createTimelineStatus({ - actorId: actor.id, - status, - timeline + const actors = Object.values( + [...localActors, ...getLocalActorsFromFollowerUrl].reduce( + (output, actor) => { + return { + ...output, + [actor.id]: actor + } + }, + {} as { [key in string]: Actor } + ) + ) + await Promise.all( + actors + .map((actor) => { + return [ + mainTimelineRule, + noannounceTimelineRule, + mentionTimelineRule + ].map(async (timelineFunction) => { + const timeline = await timelineFunction({ + currentActor: actor, + status: status.data, + storage + }) + if (!timeline) return + return storage.createTimelineStatus({ + actorId: actor.id, + status, + timeline + }) + }) }) - }) - }) - .flat() + .flat() + ) + span.end() + } ) - span.end() } diff --git a/lib/services/timelines/main.ts b/lib/services/timelines/main.ts index aefeee92..19a48200 100644 --- a/lib/services/timelines/main.ts +++ b/lib/services/timelines/main.ts @@ -1,5 +1,5 @@ import { StatusType } from '@/lib/models/status' -import { getSpan } from '@/lib/utils/trace' +import { getTracer } from '@/lib/utils/trace' import { MainTimelineRule, Timeline } from './types' @@ -26,69 +26,75 @@ export const mainTimelineRule: MainTimelineRule = async ({ storage, currentActor, status -}) => { - const span = getSpan('timelines', 'mainTimelineRule', { - actorId: currentActor.id, - statusId: status.id - }) - if (status.type === StatusType.enum.Announce) { - const isFollowing = await storage.isCurrentActorFollowing({ - currentActorId: currentActor.id, - followingActorId: status.actorId - }) - if (!isFollowing) { - span.end() - return null - } +}) => + getTracer().startActiveSpan( + 'timelines.mainTimelineRule', + { + attributes: { + actorId: currentActor.id, + statusId: status.id + } + }, + async (span) => { + if (status.type === StatusType.enum.Announce) { + const isFollowing = await storage.isCurrentActorFollowing({ + currentActorId: currentActor.id, + followingActorId: status.actorId + }) + if (!isFollowing) { + span.end() + return null + } - const originalStatus = status.originalStatus - const timeline = await mainTimelineRule({ - storage, - currentActor, - status: originalStatus - }) - span.end() - if (timeline === Timeline.MAIN) return null - return Timeline.MAIN - } + const originalStatus = status.originalStatus + const timeline = await mainTimelineRule({ + storage, + currentActor, + status: originalStatus + }) + span.end() + if (timeline === Timeline.MAIN) return null + return Timeline.MAIN + } - if (status.actorId === currentActor.id) { - span.end() - return Timeline.MAIN - } - const isFollowing = await storage.isCurrentActorFollowing({ - currentActorId: currentActor.id, - followingActorId: status.actorId - }) + if (status.actorId === currentActor.id) { + span.end() + return Timeline.MAIN + } + const isFollowing = await storage.isCurrentActorFollowing({ + currentActorId: currentActor.id, + followingActorId: status.actorId + }) - if (!status.reply) { - span.end() - if (isFollowing) return Timeline.MAIN - return null - } + if (!status.reply) { + span.end() + if (isFollowing) return Timeline.MAIN + return null + } - const repliedStatus = await storage.getStatus({ - statusId: status.reply, - withReplies: false - }) - // Deleted parent status, don't show child status - if (!repliedStatus) { - span.end() - return null - } - if (repliedStatus.actorId === currentActor.id) { - span.end() - return Timeline.MAIN - } - if (!isFollowing) { - span.end() - return null - } - const value = await mainTimelineRule({ - storage, - currentActor, - status: repliedStatus.data - }) - span.end() - return value -} + const repliedStatus = await storage.getStatus({ + statusId: status.reply, + withReplies: false + }) + // Deleted parent status, don't show child status + if (!repliedStatus) { + span.end() + return null + } + if (repliedStatus.actorId === currentActor.id) { + span.end() + return Timeline.MAIN + } + if (!isFollowing) { + span.end() + return null + } + const value = await mainTimelineRule({ + storage, + currentActor, + status: repliedStatus.data + }) + span.end() + return value + } + ) diff --git a/lib/services/timelines/mention.ts b/lib/services/timelines/mention.ts index 0715174d..7e9a2726 100644 --- a/lib/services/timelines/mention.ts +++ b/lib/services/timelines/mention.ts @@ -6,46 +6,52 @@ import { getSubject, getTextContent } from '@/lib/services/email/templates/mention' -import { getSpan } from '@/lib/utils/trace' +import { getTracer } from '@/lib/utils/trace' import { MentionTimelineRule, Timeline } from './types' export const mentionTimelineRule: MentionTimelineRule = async ({ currentActor, status -}) => { - const span = getSpan('timelines', 'mentionTimelineRule', { - actorId: currentActor.id, - statusId: status.id - }) - const config = getConfig() - if (status.type === StatusType.enum.Announce) { - span.end() - return null - } +}) => + getTracer().startActiveSpan( + 'timelines.mentionTimelineRule', + { + attributes: { + actorId: currentActor.id, + statusId: status.id + } + }, + async (span) => { + const config = getConfig() + if (status.type === StatusType.enum.Announce) { + span.end() + return null + } - if (status.actorId === currentActor.id) { - span.end() - return Timeline.MENTION - } + if (status.actorId === currentActor.id) { + span.end() + return Timeline.MENTION + } - if (status.text.includes(currentActor.getActorPage())) { - const account = currentActor.account - if (config.email && account && status.actor) { - await sendMail({ - from: config.email.serviceFromAddress, - to: [account.email], - subject: getSubject(status.actor), - content: { - text: getTextContent(status), - html: getHTMLContent(status) + if (status.text.includes(currentActor.getActorPage())) { + const account = currentActor.account + if (config.email && account && status.actor) { + await sendMail({ + from: config.email.serviceFromAddress, + to: [account.email], + subject: getSubject(status.actor), + content: { + text: getTextContent(status), + html: getHTMLContent(status) + } + }) } - }) - } - span.end() - return Timeline.MENTION - } + span.end() + return Timeline.MENTION + } - span.end() - return null -} + span.end() + return null + } + ) diff --git a/lib/services/timelines/noaanounce.ts b/lib/services/timelines/noaanounce.ts index 1f4c6d82..0d6d1ae7 100644 --- a/lib/services/timelines/noaanounce.ts +++ b/lib/services/timelines/noaanounce.ts @@ -1,5 +1,5 @@ import { StatusType } from '@/lib/models/status' -import { getSpan } from '@/lib/utils/trace' +import { getSpan, getTracer } from '@/lib/utils/trace' import { NoAnnounceTimelineRule, Timeline } from './types' @@ -27,52 +27,58 @@ export const noannounceTimelineRule: NoAnnounceTimelineRule = async ({ storage, currentActor, status -}) => { - const span = getSpan('timelines', 'noAnnounceTimelineRule', { - actorId: currentActor.id, - statusId: status.id - }) - if (status.type === StatusType.enum.Announce) { - span.end() - return null - } - if (status.actorId === currentActor.id) { - span.end() - return Timeline.NOANNOUNCE - } - const isFollowing = await storage.isCurrentActorFollowing({ - currentActorId: currentActor.id, - followingActorId: status.actorId - }) +}) => + getTracer().startActiveSpan( + 'timelines.noAnnounceTimelineRule', + { + attributes: { + actorId: currentActor.id, + statusId: status.id + } + }, + async (span) => { + if (status.type === StatusType.enum.Announce) { + span.end() + return null + } + if (status.actorId === currentActor.id) { + span.end() + return Timeline.NOANNOUNCE + } + const isFollowing = await storage.isCurrentActorFollowing({ + currentActorId: currentActor.id, + followingActorId: status.actorId + }) - if (!status.reply) { - span.end() - if (isFollowing) return Timeline.NOANNOUNCE - return null - } + if (!status.reply) { + span.end() + if (isFollowing) return Timeline.NOANNOUNCE + return null + } - const repliedStatus = await storage.getStatus({ - statusId: status.reply, - withReplies: false - }) - // Deleted parent status, don't show child status - if (!repliedStatus) { - span.end() - return null - } - if (repliedStatus.actorId === currentActor.id) { - span.end() - return Timeline.NOANNOUNCE - } - if (!isFollowing) { - span.end() - return null - } - const value = await noannounceTimelineRule({ - storage, - currentActor, - status: repliedStatus.data - }) - span.end() - return value -} + const repliedStatus = await storage.getStatus({ + statusId: status.reply, + withReplies: false + }) + // Deleted parent status, don't show child status + if (!repliedStatus) { + span.end() + return null + } + if (repliedStatus.actorId === currentActor.id) { + span.end() + return Timeline.NOANNOUNCE + } + if (!isFollowing) { + span.end() + return null + } + const value = await noannounceTimelineRule({ + storage, + currentActor, + status: repliedStatus.data + }) + span.end() + return value + } + )