Skip to content

Commit

Permalink
fix: dedupe found ref links in dc message
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler committed Feb 28, 2025
1 parent 12767d9 commit 2bee672
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,49 +233,66 @@ export class DiscordService {

content = content.replaceAll(/```.*```/gs, '');

const shortMatches = content.matchAll(/(((?<org>[\w\-.,_]*)\/)?(?<repo>[\w\-.,_]+))?#(?<num>\d+)/g);
for (const match of shortMatches) {
const longMatches = content.matchAll(
/https:\/\/github\.com\/(?<org>[\w\-.,]+)\/(?<repo>[\w\-.,]+)\/(?<category>(pull|issue|discussion))\/(?<num>\d+)/g,
);
for (const match of longMatches) {
if (!match || !match.groups) {
continue;
}

const { org, repo, num } = match.groups;
const { org, repo, category, num } = match.groups;
const id = Number(num);
if (Number.isNaN(id)) {
continue;
}

if (!org && !repo && id < 1000) {
continue;
}

links.push({ org: org || GithubOrg.ImmichApp, repo: repo || GithubRepo.Immich, id });
links.push({
id,
org: org || GithubOrg.ImmichApp,
repo: repo || GithubRepo.Immich,
type: category as LinkType,
});
}

const longMatches = content.matchAll(
/https:\/\/github\.com\/(?<org>[\w\-.,]+)\/(?<repo>[\w\-.,]+)\/(?<category>(pull|issue|discussion))\/(?<num>\d+)/g,
);
for (const match of longMatches) {
const shortMatches = content.matchAll(/(((?<org>[\w\-.,_]*)\/)?(?<repo>[\w\-.,_]+))?#(?<num>\d+)/g);
for (const match of shortMatches) {
if (!match || !match.groups) {
continue;
}

const { org, repo, category, num } = match.groups;
const { org, repo, num } = match.groups;
const id = Number(num);
if (Number.isNaN(id)) {
continue;
}

if (!org && !repo && id < 1000) {
continue;
}

links.push({
id,
org: org || GithubOrg.ImmichApp,
repo: repo || GithubRepo.Immich,
type: category as LinkType,
id,
});
}

const keys: Set<string> = new Set();
const requests: GithubLink[] = [];

for (const { id, org, repo, type } of links) {
const key = id + org + repo;
if (keys.has(key)) {
continue;
}

requests.push({ id, org, repo, type });
keys.add(key);
}

const results = await Promise.all(
links.map(async ({ org, repo, id, type }) => {
requests.map(async ({ org, repo, id, type }) => {
switch (type) {
case 'issue':
case 'pull':
Expand Down

0 comments on commit 2bee672

Please sign in to comment.