Skip to content

Commit

Permalink
Fix passing comparisons being blocked by duplicate checks
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Dec 10, 2024
1 parent 2784bc5 commit 92b27aa
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 84 deletions.
49 changes: 7 additions & 42 deletions services/user-feeds/src/delivery/delivery.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from "@nestjs/common";
import { randomUUID } from "crypto";
import { ArticleFiltersService } from "../article-filters/article-filters.service";
import { ArticleRateLimitService } from "../article-rate-limit/article-rate-limit.service";
import { CacheStorageService } from "../cache-storage/cache-storage.service";
Expand All @@ -11,6 +10,7 @@ import {
MediumPayload,
} from "../shared";
import { RegexEvalException } from "../shared/exceptions";
import { generateDeliveryId } from "../shared/utils/generate-delivery-id";
import logger from "../shared/utils/logger";
import { DeliveryMedium } from "./mediums/delivery-medium.interface";
import { DiscordMediumService } from "./mediums/discord-medium.service";
Expand Down Expand Up @@ -100,8 +100,7 @@ export class DeliveryService {
event,
article,
medium,
limitState,
randomUUID()
limitState
);

results.push(...articleStates);
Expand All @@ -114,14 +113,13 @@ export class DeliveryService {
event: FeedV2Event,
article: Article,
medium: MediumPayload,
limitState: LimitState,
deliveryId: string
limitState: LimitState
): Promise<ArticleDeliveryState[]> {
try {
if (limitState.remaining <= 0 || limitState.remainingInMedium <= 0) {
return [
{
id: deliveryId,
id: generateDeliveryId(),
mediumId: medium.id,
status:
limitState.remaining <= 0
Expand Down Expand Up @@ -153,7 +151,7 @@ export class DeliveryService {
if (!result) {
return [
{
id: deliveryId,
id: generateDeliveryId(),
mediumId: medium.id,
status: ArticleDeliveryStatus.FilteredOut,
articleIdHash: article.flattened.idHash,
Expand All @@ -167,42 +165,9 @@ export class DeliveryService {
}
}

const cacheKey = `delivery:${event.data.feed.id}:${medium.id}:${article.flattened.idHash}`;

const priorDeliveryDate = await this.cacheStorageService.set({
key: cacheKey,
getOldValue: true,
expSeconds: 60 * 30,
body: new Date().toISOString(),
});

if (priorDeliveryDate) {
const secondsElapsed =
priorDeliveryDate !== "1"
? Math.round(
(new Date().getTime() - new Date(priorDeliveryDate).getTime()) /
1000
)
: undefined;

logger.warn(
`Article already delivered to feed ${event.data.feed.id}, medium ${medium.id},` +
` article ${formattedArticle.flattened.id}`,
{
articleIdHash: formattedArticle.flattened.idHash,
secondsElapsed,
feedId: event.data.feed.id,
mediumId: medium.id,
}
);

return [];
}

const articleStates = await mediumService.deliverArticle(
formattedArticle,
{
deliveryId,
mediumId: medium.id,
deliverySettings: medium.details,
feedDetails: event.data.feed,
Expand All @@ -218,7 +183,7 @@ export class DeliveryService {
if (err instanceof RegexEvalException) {
return [
{
id: deliveryId,
id: generateDeliveryId(),
mediumId: medium.id,
status: ArticleDeliveryStatus.Rejected,
articleIdHash: article.flattened.idHash,
Expand All @@ -243,7 +208,7 @@ export class DeliveryService {

return [
{
id: deliveryId,
id: generateDeliveryId(),
mediumId: medium.id,
status: ArticleDeliveryStatus.Failed,
errorCode: ArticleDeliveryErrorCode.Internal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Article, FeedV2Event, MediumPayload } from "../../shared";
import { ArticleDeliveryState } from "../types";

export interface DeliverArticleDetails {
deliveryId: string;
mediumId: string;
feedDetails: FeedV2Event["data"]["feed"];
deliverySettings: MediumPayload["details"];
Expand Down
48 changes: 29 additions & 19 deletions services/user-feeds/src/delivery/mediums/discord-medium.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import logger from "../../shared/utils/logger";
import { ConfigService } from "@nestjs/config";
import { ArticleFormatterService } from "../../article-formatter/article-formatter.service";
import { FormatOptions } from "../../article-formatter/types";
import { randomUUID } from "node:crypto";
import { ArticleFiltersService } from "../../article-filters/article-filters.service";
import {
FilterExpressionReference,
LogicalExpression,
} from "../../article-filters/types";
import dayjs from "dayjs";
import { generateDeliveryId } from "../../shared/utils/generate-delivery-id";

@Injectable()
export class DiscordMediumService implements DeliveryMedium {
Expand Down Expand Up @@ -361,7 +361,7 @@ export class DiscordMediumService implements DeliveryMedium {
if (!channel && !webhook) {
return [
{
id: details.deliveryId,
id: generateDeliveryId(),
mediumId: details.mediumId,
status: ArticleDeliveryStatus.Failed,
errorCode: ArticleDeliveryErrorCode.NoChannelOrWebhook,
Expand Down Expand Up @@ -393,9 +393,13 @@ export class DiscordMediumService implements DeliveryMedium {

const channelId = channel.id;

return [
await this.deliverArticleToChannel(article, channelId, details),
];
const res = await this.deliverArticleToChannel(
article,
channelId,
details
);

return [res];
} else {
throw new Error("No channel or webhook specified for Discord medium");
}
Expand All @@ -414,7 +418,7 @@ export class DiscordMediumService implements DeliveryMedium {

return [
{
id: details.deliveryId,
id: generateDeliveryId(),
mediumId: details.mediumId,
status: ArticleDeliveryStatus.Failed,
errorCode: ArticleDeliveryErrorCode.Internal,
Expand Down Expand Up @@ -518,9 +522,11 @@ export class DiscordMediumService implements DeliveryMedium {
threadId,
});

const parentDeliveryId = generateDeliveryId();

const additionalDeliveryStates: ArticleDeliveryState[] = await Promise.all(
bodies.slice(1, bodies.length).map(async (body) => {
const additionalDeliveryId = randomUUID();
const additionalDeliveryId = generateDeliveryId();

await this.producer.enqueue(
channelApiUrl,
Expand All @@ -544,15 +550,15 @@ export class DiscordMediumService implements DeliveryMedium {
status: ArticleDeliveryStatus.PendingDelivery,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordArticleMessage,
parent: details.deliveryId,
parent: parentDeliveryId,
articleIdHash: article.flattened.idHash,
};
})
);

return [
{
id: details.deliveryId,
id: parentDeliveryId,
status: ArticleDeliveryStatus.Sent,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordThreadCreation,
Expand Down Expand Up @@ -617,7 +623,7 @@ export class DiscordMediumService implements DeliveryMedium {
if (res.status === 404) {
return [
{
id: details.deliveryId,
id: generateDeliveryId(),
status: ArticleDeliveryStatus.Rejected,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordThreadCreation,
Expand All @@ -631,7 +637,7 @@ export class DiscordMediumService implements DeliveryMedium {
} else if (res.status === 403) {
return [
{
id: details.deliveryId,
id: generateDeliveryId(),
status: ArticleDeliveryStatus.Rejected,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordThreadCreation,
Expand All @@ -644,7 +650,7 @@ export class DiscordMediumService implements DeliveryMedium {
} else if (res.status === 400) {
return [
{
id: details.deliveryId,
id: generateDeliveryId(),
status: ArticleDeliveryStatus.Rejected,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordThreadCreation,
Expand All @@ -667,9 +673,10 @@ export class DiscordMediumService implements DeliveryMedium {

const channelApiUrl = this.getChannelApiUrl(threadId);

const parentDeliveryId = generateDeliveryId();
const additionalDeliveryStates: ArticleDeliveryState[] = await Promise.all(
bodies.slice(1, bodies.length).map(async (body) => {
const additionalDeliveryId = randomUUID();
const additionalDeliveryId = generateDeliveryId();

await this.producer.enqueue(
channelApiUrl,
Expand All @@ -693,15 +700,15 @@ export class DiscordMediumService implements DeliveryMedium {
status: ArticleDeliveryStatus.PendingDelivery,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordArticleMessage,
parent: details.deliveryId,
parent: parentDeliveryId,
articleIdHash: article.flattened.idHash,
};
})
);

return [
{
id: details.deliveryId,
id: parentDeliveryId,
status: ArticleDeliveryStatus.Sent,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordThreadCreation,
Expand Down Expand Up @@ -739,6 +746,8 @@ export class DiscordMediumService implements DeliveryMedium {
components,
});

const deliveryId = generateDeliveryId();

await Promise.all(
bodies.map((body) =>
this.producer.enqueue(
Expand All @@ -748,7 +757,7 @@ export class DiscordMediumService implements DeliveryMedium {
body: JSON.stringify(body),
},
{
id: details.deliveryId,
id: deliveryId,
articleID: article.flattened.id,
feedURL: url,
channel: channelId,
Expand All @@ -761,7 +770,7 @@ export class DiscordMediumService implements DeliveryMedium {
);

return {
id: details.deliveryId,
id: deliveryId,
status: ArticleDeliveryStatus.PendingDelivery,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordArticleMessage,
Expand Down Expand Up @@ -834,6 +843,7 @@ export class DiscordMediumService implements DeliveryMedium {
}),
}));

const deliveryId = generateDeliveryId();
await Promise.all(
bodies.map((body) =>
this.producer.enqueue(
Expand All @@ -843,7 +853,7 @@ export class DiscordMediumService implements DeliveryMedium {
body: JSON.stringify(body),
},
{
id: details.deliveryId,
id: deliveryId,
articleID: article.flattened.id,
feedURL: url,
webhookId,
Expand All @@ -856,7 +866,7 @@ export class DiscordMediumService implements DeliveryMedium {
);

return {
id: details.deliveryId,
id: generateDeliveryId(),
status: ArticleDeliveryStatus.PendingDelivery,
mediumId: details.mediumId,
contentType: ArticleDeliveryContentType.DiscordArticleMessage,
Expand Down
3 changes: 3 additions & 0 deletions services/user-feeds/src/shared/utils/generate-delivery-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { randomUUID } from "crypto";

export const generateDeliveryId = () => randomUUID();
Loading

0 comments on commit 92b27aa

Please sign in to comment.