Skip to content

Commit

Permalink
fix: body length in embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler committed Oct 3, 2024
1 parent 6de959d commit 5b4a7f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Constants, GithubOrg, GithubRepo, HELP_TEXTS } from 'src/constants';
import { IDatabaseRepository } from 'src/interfaces/database.interface';
import { DiscordChannel, IDiscordInterface } from 'src/interfaces/discord.interface';
import { IGithubInterface } from 'src/interfaces/github.interface';
import { logError } from 'src/util';
import { logError, shorten } from 'src/util';

const PREVIEW_BLACKLIST = [Constants.Urls.Immich, Constants.Urls.GitHub, Constants.Urls.MyImmich];
const LINK_NOT_FOUND = { message: 'Link not found', isPrivate: true };
Expand Down Expand Up @@ -103,7 +103,7 @@ export class DiscordService {
}

return links.map(({ name, link }) => ({
name: this.shortenName(`${name}${link}`),
name: shorten(`${name}${link}`),
value: name,
}));
}
Expand Down Expand Up @@ -191,7 +191,7 @@ export class DiscordService {
});

return result.items.map((item) => ({
name: this.shortenName(`${item.pull_request ? '[PR]' : '[Issue]'} (${item.number}) ${item.title}`),
name: shorten(`${item.pull_request ? '[PR]' : '[Issue]'} (${item.number}) ${item.title}`),
value: String(item.number),
}));
} catch {
Expand Down Expand Up @@ -278,8 +278,4 @@ export class DiscordService {
getPrOrIssue(id: number) {
return this.github.getIssueOrPr(GithubOrg.ImmichApp, GithubRepo.Immich, id);
}

private shortenName(name: string, maxLength: number = 100) {
return name.length > maxLength ? `${name.substring(0, maxLength - 3)}...` : name;
}
}
4 changes: 2 additions & 2 deletions src/services/webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GithubStatusComponent, GithubStatusIncident, PaymentIntent, StripeBase
import { IDatabaseRepository } from 'src/interfaces/database.interface';
import { DiscordChannel, IDiscordInterface } from 'src/interfaces/discord.interface';
import { IZulipInterface } from 'src/interfaces/zulip.interface';
import { makeLicenseFields, withErrorLogging } from 'src/util';
import { makeLicenseFields, shorten, withErrorLogging } from 'src/util';

const isIncidentUpdate = (dto: GithubStatusComponent | GithubStatusIncident): dto is GithubStatusIncident => {
return !!(dto as GithubStatusIncident).incident;
Expand Down Expand Up @@ -199,7 +199,7 @@ export class WebhookService {
iconURL: event.user.avatar_url,
},
url: event.html_url,
description: action === 'opened' || action === 'created' ? (event.body ?? undefined) : undefined,
description: action === 'opened' || action === 'created' ? shorten(event.body ?? undefined, 4096) : undefined,
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ export const makeLicenseFields = ({ server, client }: { server: number; client:
},
];
};

export const shorten = (text?: string, maxLength: number = 100) => {
if (text === undefined) {
return;
}

return text.length > maxLength ? `${text.substring(0, maxLength - 3)}...` : text;
};

0 comments on commit 5b4a7f0

Please sign in to comment.