Skip to content

fix: isASocialNetworkUrl() regex for summarize-cool-pages #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/__tests__/regex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest';

import { isASocialNetworkUrl } from '../helpers/regex.helper';

describe('Helpers: Regex', () => {
describe('Rule: isASocialNetworkUrl should regex correctly an url', () => {
it('isASocialNetworkUrl() should return true when the url is a social network url', () => {
const url = 'www.facebook.com/username';
const result = isASocialNetworkUrl(url);
expect(result).toBe(true);
});
it('isASocialNetworkUrl() should return false when the url is not a social network url', () => {
const url = 'https://blog.richardekwonye.com/bezier-curves';
const result = isASocialNetworkUrl(url);
expect(result).toBe(false);
});
});
});
32 changes: 28 additions & 4 deletions src/__tests__/summarize-cool-pages.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { afterEach } from 'node:test';

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import {
getPageSummaryDiscordView,
isPageSummarizeSuccessData,
NoContentFoundSummaryError,
parseHtmlSummarized,
} from '../summarize-cool-pages';

const createSummarizeCoolPagesFixture = () => {
return {
// from https://react.dev/learn/you-might-not-need-an-effect
Expand Down Expand Up @@ -71,4 +69,30 @@ describe('Feature: summarize cool pages', () => {
it('getPageSummaryDiscordView() should return a string with the page summary', () => {
expect(getPageSummaryDiscordView(fixture.pageSummary)).toEqual(fixture.pageSummaryDiscordView);
});

describe('Rule: isPageSummarizeSuccessData() should check if the data is a PageSummarizerDataSuccess', () => {
it('isPageSummarizeSuccessData() should return true when the data is a PageSummarizerDataSuccess', () => {
// I'm using as because eslint can't autofix and sort the import itself
const data = {
success: true,
html: fixture.htmlWithSummaryContent,
} as {
success: true;
html: string;
};
const result = isPageSummarizeSuccessData(data);
expect(result).toBe(true);
});
it('isPageSummarizeSuccessData() should return false when the data is not a PageSummarizerDataSuccess', () => {
const data = {
success: false,
html: undefined,
} as {
success: false;
html: undefined;
};
const result = isPageSummarizeSuccessData(data);
expect(result).toBe(false);
});
});
});
6 changes: 2 additions & 4 deletions src/cool-links-management.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ThreadAutoArchiveDuration, type Message } from 'discord.js';
import ogs from 'open-graph-scraper';

import { isASocialNetworkUrl } from './helpers/regex.helper';
import { getPageSummary } from './summarize-cool-pages';
import { getVideoSummary } from './summarize-cool-videos';

Expand Down Expand Up @@ -28,9 +29,6 @@ const getThreadNameFromOpenGraph = async (url: string): Promise<string | null> =
};

const youtubeUrlRegex = new RegExp('^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))');
const socialNetworksUrlRegex = new RegExp(
'^(?!.*(fb.me|facebook.com|t.co|instagr.am|instagram.com|lnkd.in|youtu.be|tiktok.com)).*$',
);

export const coolLinksManagement = async (message: Message) => {
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
Expand Down Expand Up @@ -58,7 +56,7 @@ export const coolLinksManagement = async (message: Message) => {

await thread.send(summary);
}
if (!youtubeUrlRegex.test(url) && !socialNetworksUrlRegex.test(url)) {
if (!youtubeUrlRegex.test(url) && !isASocialNetworkUrl(url)) {
try {
const pageSummaryDiscordView = await getPageSummary(url);
await thread.send(pageSummaryDiscordView);
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/regex.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const socialNetworksUrlRegex = new RegExp(
'^(https?://)?(www.)?(facebook.com|fb.me|twitter.com|vxtwitter.com|instagram.com|linkedin.com|youtube.com|youtu.be|pinterest.com|snapchat.com|tiktok.com)/[a-zA-Z0-9.-/?=&#_]+$',
);
export const isASocialNetworkUrl = (url: string): boolean => {
return socialNetworksUrlRegex.test(url);
};
8 changes: 4 additions & 4 deletions src/summarize-cool-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ type PageSummary = {
readingTime: string;
};

type PageSummarizerDataSuccess = {
export type PageSummarizerDataSuccess = {
success: true;
html: string;
};

type PageSummarizerDataFailure = {
export type PageSummarizerDataFailure = {
success: false;
html?: undefined;
};

type PageSummarizerData = PageSummarizerDataSuccess | PageSummarizerDataFailure;
export type PageSummarizerData = PageSummarizerDataSuccess | PageSummarizerDataFailure;

const isPageSummarizeSuccessData = (
export const isPageSummarizeSuccessData = (
data: PageSummarizerData,
): data is PageSummarizerDataSuccess => {
return data?.success && typeof data?.html === 'string';
Expand Down