( post: Post, explain: FeedItemExplain | null | undefined, limit: number, /** The reader set a level on the author since the card was ranked. */ hasAuthorControl = false, )
| 262 | * brought the card in puts its source or author first. |
| 263 | */ |
| 264 | export const getExplainSignals = ( |
| 265 | post: Post, |
| 266 | explain: FeedItemExplain | null | undefined, |
| 267 | limit: number, |
| 268 | /** The reader set a level on the author since the card was ranked. */ |
| 269 | hasAuthorControl = false, |
| 270 | ): ExplainSignal[] => { |
| 271 | const matched = isRankedExplain(explain) ? explain.match?.tag : undefined; |
| 272 | const topics = [ |
| 273 | ...new Set([...(matched ? [matched] : []), ...getPostTags(post)]), |
| 274 | ].map((tag): ExplainSignal => ({ |
| 275 | kind: ExplainSignalKind.Topic, |
| 276 | id: tag, |
| 277 | label: `#${tag}`, |
| 278 | })); |
| 279 | const { source, author } = post; |
| 280 | const ranked = isRankedExplain(explain) ? explain : undefined; |
| 281 | let leadSide: FollowSide | undefined; |
| 282 | if (ranked) { |
| 283 | leadSide = ranked.follow.kind |
| 284 | ? getFollowSide(ranked) |
| 285 | : getGatheringSide(ranked); |
| 286 | } |
| 287 | const sourceSignal: ExplainSignal | undefined = |
| 288 | source?.id && !isSourceUserSource(source) |
| 289 | ? { |
| 290 | kind: ExplainSignalKind.Source, |
| 291 | id: source.id, |
| 292 | label: source.name ?? source.id, |
| 293 | } |
| 294 | : undefined; |
| 295 | const isAuthorCounted = |
| 296 | hasAuthorControl || |
| 297 | leadSide === FollowSide.Author || |
| 298 | !!ranked?.follow.author_control; |
| 299 | const authorSignal: ExplainSignal | undefined = |
| 300 | author?.id && isAuthorCounted |
| 301 | ? { |
| 302 | kind: ExplainSignalKind.Author, |
| 303 | id: author.id, |
| 304 | label: getAuthorLabel(author), |
| 305 | } |
| 306 | : undefined; |
| 307 | const sideSignal: Record<FollowSide, ExplainSignal | undefined> = { |
| 308 | [FollowSide.Source]: sourceSignal, |
| 309 | [FollowSide.Author]: authorSignal, |
| 310 | }; |
| 311 | const lead = leadSide && sideSignal[leadSide]; |
| 312 | const trailing = [sourceSignal, authorSignal].filter( |
| 313 | (signal): signal is ExplainSignal => !!signal && signal !== lead, |
| 314 | ); |
| 315 | const otherTopics = topics.slice( |
| 316 | 0, |
| 317 | Math.max(0, limit - trailing.length - (lead ? 1 : 0)), |
| 318 | ); |
| 319 | return [...(lead ? [lead] : []), ...otherTopics, ...trailing].slice(0, limit); |
| 320 | }; |
| 321 |
no test coverage detected