From 252cdf60b34826728489a79ec835016b584c19d4 Mon Sep 17 00:00:00 2001 From: vishtree Date: Thu, 25 Feb 2021 15:19:13 +0100 Subject: [PATCH 1/2] Fixing Linking on urls - Ignore email address from generating og preview - sanitize the urls before using Linking module from react-native --- src/components/Activity.js | 5 +++-- src/components/Card.js | 3 ++- src/components/StatusUpdateForm.js | 9 +++++++-- src/utils.js | 9 +++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/components/Activity.js b/src/components/Activity.js index 80cb9843..e87b4efd 100644 --- a/src/components/Activity.js +++ b/src/components/Activity.js @@ -18,7 +18,7 @@ import _ from 'lodash'; import UserBar from './UserBar'; import Card from './Card'; -import { smartRender } from '../utils'; +import { sanitizeUrlForLinking, smartRender } from '../utils'; /** * Renders feed activities @@ -188,7 +188,8 @@ export default class Activity extends React.Component { Object.keys(activity.attachments.og).length > 0 && tokens[i] === activity.attachments.og.url ) { - const url = activity.attachments.og.url; + const url = sanitizeUrlForLinking(activity.attachments.og.url); + rendered.push( Linking.openURL(url)} style={styles.url}> {tokens[i].slice(0, 20)} diff --git a/src/components/Card.js b/src/components/Card.js index 35a5f194..b8929012 100644 --- a/src/components/Card.js +++ b/src/components/Card.js @@ -6,6 +6,7 @@ import PropTypes from 'prop-types'; import { buildStylesheet } from '../styles'; import _ from 'lodash'; +import { sanitizeUrlForLinking } from '../utils'; /** * Card element @@ -18,7 +19,7 @@ const Card = (props) => { return ( { - Linking.openURL(url); + Linking.openURL(sanitizeUrlForLinking(url)); }} style={styles.container} > diff --git a/src/components/StatusUpdateForm.js b/src/components/StatusUpdateForm.js index 917aae5f..7b949f4f 100644 --- a/src/components/StatusUpdateForm.js +++ b/src/components/StatusUpdateForm.js @@ -29,8 +29,8 @@ const ImageState = Object.freeze({ UPLOAD_FAILED: Symbol('upload_failed'), }); -const urlRegex = /(https?:\/\/[^\s]+)/gi; - +const urlRegex = /(?:[\w/:@-]+\.[\w/:@.-]*)+(?=\s|$)/g; +const emailRegex = /.*@.*/ ; class StatusUpdateForm extends React.Component { static defaultProps = { feedGroup: 'user', @@ -291,6 +291,11 @@ class StatusUpdateFormInner extends React.Component { } urls.forEach((url) => { + // Generally you only want to generate previews for proper urls, and not email addresses. + if (url.match(emailRegex)) { + return; + } + if ( url !== this.state.ogLink && !(this.state.dismissedUrls.indexOf(url) > -1) && diff --git a/src/utils.js b/src/utils.js index 4363cbfc..583bcc73 100644 --- a/src/utils.js +++ b/src/utils.js @@ -26,6 +26,15 @@ export function humanizeTimestamp(timestamp, tDateTimeParser) { return time.from(now); } +// https://reactnative.dev/docs/linking +export const sanitizeUrlForLinking = (url) => { + if (url.indexOf('http://') === -1 && url.indexOf('https://') === -1) { + url = `https://${url}`; + } + + return url.replace(/(www\.)/, ''); +}; + export const smartRender = (ElementOrComponentOrLiteral, props, fallback) => { if (ElementOrComponentOrLiteral === undefined) { ElementOrComponentOrLiteral = fallback; From c1178d8c6bc145841d4e970fb0000c20598ea89a Mon Sep 17 00:00:00 2001 From: vishtree Date: Fri, 5 Mar 2021 10:53:38 +0100 Subject: [PATCH 2/2] Updating the regex for url detection --- src/components/StatusUpdateForm.js | 8 +------- src/utils.js | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/components/StatusUpdateForm.js b/src/components/StatusUpdateForm.js index 7b949f4f..2d6a64a5 100644 --- a/src/components/StatusUpdateForm.js +++ b/src/components/StatusUpdateForm.js @@ -29,8 +29,7 @@ const ImageState = Object.freeze({ UPLOAD_FAILED: Symbol('upload_failed'), }); -const urlRegex = /(?:[\w/:@-]+\.[\w/:@.-]*)+(?=\s|$)/g; -const emailRegex = /.*@.*/ ; +const urlRegex = /(?:\s|^)((?:https?:\/\/)?(?:[a-z0-9-]+(?:\.[a-z0-9-]+)+)(?::[0-9]+)?(?:\/(?:[^\s]+)?)?)/g; class StatusUpdateForm extends React.Component { static defaultProps = { feedGroup: 'user', @@ -291,11 +290,6 @@ class StatusUpdateFormInner extends React.Component { } urls.forEach((url) => { - // Generally you only want to generate previews for proper urls, and not email addresses. - if (url.match(emailRegex)) { - return; - } - if ( url !== this.state.ogLink && !(this.state.dismissedUrls.indexOf(url) > -1) && diff --git a/src/utils.js b/src/utils.js index 583bcc73..c5237dfc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -28,7 +28,7 @@ export function humanizeTimestamp(timestamp, tDateTimeParser) { // https://reactnative.dev/docs/linking export const sanitizeUrlForLinking = (url) => { - if (url.indexOf('http://') === -1 && url.indexOf('https://') === -1) { + if (!/^https?:\/\//.test(url)) { url = `https://${url}`; }