2020-04-18 11:50:01 +03:00
|
|
|
import { isNil } from 'ramda';
|
2023-07-24 21:14:59 +03:00
|
|
|
import type { OptionalString } from '../../../src/utils/utils';
|
2023-07-23 19:30:59 +03:00
|
|
|
import type { ShortUrlCreationSettings } from '../../utils/settings';
|
2022-04-24 19:36:25 +03:00
|
|
|
import { DEFAULT_DOMAIN } from '../../visits/reducers/domainVisits';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrl, ShortUrlData } from '../data';
|
2020-04-18 11:50:01 +03:00
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
export const shortUrlMatches = (shortUrl: ShortUrl, shortCode: string, domain: OptionalString): boolean => {
|
2020-04-18 11:50:01 +03:00
|
|
|
if (isNil(domain)) {
|
|
|
|
return shortUrl.shortCode === shortCode && !shortUrl.domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shortUrl.shortCode === shortCode && shortUrl.domain === domain;
|
|
|
|
};
|
2022-04-24 19:36:25 +03:00
|
|
|
|
|
|
|
export const domainMatches = (shortUrl: ShortUrl, domain: string): boolean => {
|
|
|
|
if (!shortUrl.domain && domain === DEFAULT_DOMAIN) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shortUrl.domain === domain;
|
|
|
|
};
|
2022-06-12 21:41:40 +03:00
|
|
|
|
|
|
|
export const shortUrlDataFromShortUrl = (shortUrl?: ShortUrl, settings?: ShortUrlCreationSettings): ShortUrlData => {
|
|
|
|
const validateUrl = settings?.validateUrls ?? false;
|
|
|
|
|
|
|
|
if (!shortUrl) {
|
|
|
|
return { longUrl: '', validateUrl };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
longUrl: shortUrl.longUrl,
|
|
|
|
tags: shortUrl.tags,
|
|
|
|
title: shortUrl.title ?? undefined,
|
|
|
|
domain: shortUrl.domain ?? undefined,
|
|
|
|
validSince: shortUrl.meta.validSince ?? undefined,
|
|
|
|
validUntil: shortUrl.meta.validUntil ?? undefined,
|
|
|
|
maxVisits: shortUrl.meta.maxVisits ?? undefined,
|
|
|
|
crawlable: shortUrl.crawlable,
|
|
|
|
forwardQuery: shortUrl.forwardQuery,
|
2023-03-13 11:05:54 +03:00
|
|
|
deviceLongUrls: shortUrl.deviceLongUrls,
|
2022-06-12 21:41:40 +03:00
|
|
|
validateUrl,
|
|
|
|
};
|
|
|
|
};
|
2022-08-07 19:19:53 +03:00
|
|
|
|
|
|
|
const MULTI_SEGMENT_SEPARATOR = '__';
|
|
|
|
|
|
|
|
export const urlEncodeShortCode = (shortCode: string): string => shortCode.replaceAll('/', MULTI_SEGMENT_SEPARATOR);
|
|
|
|
|
|
|
|
export const urlDecodeShortCode = (shortCode: string): string => shortCode.replaceAll(MULTI_SEGMENT_SEPARATOR, '/');
|