Skip to content

Commit

Permalink
Put timeline methods in active span
Browse files Browse the repository at this point in the history
  • Loading branch information
llun committed Sep 6, 2024
1 parent 5c78155 commit ae92791
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 194 deletions.
106 changes: 56 additions & 50 deletions lib/services/timelines/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,57 +11,63 @@ export const addStatusToTimelines = async (
storage: Storage,
status: Status
): Promise<void> => {
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()
}
132 changes: 69 additions & 63 deletions lib/services/timelines/main.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
}
)
72 changes: 39 additions & 33 deletions lib/services/timelines/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
Loading

0 comments on commit ae92791

Please sign in to comment.