Skip to content
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

feat/hide phone numbers #332

Merged
merged 3 commits into from
Jul 28, 2023
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
24 changes: 16 additions & 8 deletions api/seeders/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const sendMessage = async (currentUserId, chatId, message, useLastMessageSeen =
return msg.id;
};

const createGarden = async ({ latitude, longitude }, user) => {
const createGarden = async ({ latitude, longitude }, user, extraProps) => {
await createGardenDoc(user.uid, {
description: 'Hello, this is a test camping spot. You are welcome to stay!',
location: {
Expand All @@ -147,7 +147,8 @@ const createGarden = async ({ latitude, longitude }, user) => {
tent: true
},
photo: null,
listed: true
listed: true,
...extraProps
});
return user;
};
Expand All @@ -159,11 +160,18 @@ const seed = async () => {
createNewUser(
{ email: 'user1@slowby.travel' },
{ firstName: 'Bob', lastName: 'Dylan', countryCode: 'US' }
).then(
createGarden.bind(null, {
latitude: 50.952798579681854,
longitude: 4.763172541851901
})
).then((user) =>
createGarden(
{
latitude: 50.952798579681854,
longitude: 4.763172541851901
},
user,
{
description:
'Hello, this is a test garden. If you want to stay here, please send an SMS to 0679669739 or 0681483065.'
}
)
),
// Superfan, no garden
createNewUser(
Expand All @@ -174,7 +182,7 @@ const seed = async () => {
createNewUser(
{ email: 'user3@slowby.travel' },
{ firstName: 'Jospehine', lastName: 'Delafroid', countryCode: 'FR', superfan: true }
).then(createGarden.bind(null, { latitude: 50.9427, longitude: 4.5124 })),
).then((user) => createGarden({ latitude: 50.9427, longitude: 4.5124 }, user)),
// No superfan, no garden, has past chats
createNewUser(
{
Expand Down
65 changes: 63 additions & 2 deletions src/lib/components/Garden/GardenDrawer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import trackEvent from '$lib/util/track-plausible';
import { PlausibleEvent } from '$lib/types/Plausible';
import { anchorText } from '$lib/util/translation-helpers';
import HiddenPhoneNumber from './HiddenPhoneNumber.svelte';

const dispatch = createEventDispatcher();
const phoneRegex =
/\+?\d{1,4}?[-/\\.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g;
let descriptionEl: unknown | undefined;

$: gardenIsSelected = !!garden;
$: facilities = [
Expand Down Expand Up @@ -151,6 +155,42 @@
};

$: chatWithGardenLink = `${routes.CHAT}?with=${garden?.id}`;

const createPhoneNumberPlaceHolder = (length: number) => {
const container = document.createElement('span');
new HiddenPhoneNumber({
target: container,
props: {
length
}
});
return container;
};

$: isPhoneNumberInDescription = garden?.description ? phoneRegex.test(garden.description) : false;

$: if (descriptionEl) {
// Replace hidden phone number markers with a component.
const el = descriptionEl.firstChild as HTMLParagraphElement;
const hiddenNumberRegex = /\[(\*+)\]/;
if (hiddenNumberRegex.test(el?.textContent)) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#splitting_with_a_regexp_to_include_parts_of_the_separator_in_the_result
// We use groups in the regex to still have access to the length of the phone number.
// Returns an array like ['sms me at ', 8, ' or ', 9]
const newNodes = el.textContent
.split(hiddenNumberRegex)
.map((el) =>
el.match(/\*+/) ? createPhoneNumberPlaceHolder(el.length) : document.createTextNode(el)
);

// Remove the original text node of <p>
el.firstChild.remove();
// Re-insert new nodes
for (const node of newNodes) {
el.appendChild(node);
}
}
}
</script>

<Progress active={isGettingMagnifiedPhoto} />
Expand Down Expand Up @@ -212,8 +252,23 @@
{/if}
</header>
<div class="drawer-content-area">
<div class="description">
<Text class="mb-l">{garden.description}</Text>
<div class="description" bind:this={descriptionEl}>
<Text class="mb-l"
>{$user?.superfan
? garden?.description
: garden?.description.replaceAll(
phoneRegex,
// With client-side code, we replace this with more aesthetic HTML & CSS (see above).
// We can not use the @html directive here, because of a high XSS injection risk.
(match) => `[${'*'.repeat(match.length)}]`
)}</Text
>
{#if isPhoneNumberInDescription && !$user?.superfan}
<p class="phone-notice">
<span style="font-style: normal;">🔐 </span>{$_('garden.drawer.phone-notice')}
</p>
{/if}
<p />
</div>
<div class="chips-container">
{#each facilities as facility (facility.name)}
Expand Down Expand Up @@ -431,6 +486,12 @@
word-wrap: break-word;
}

.phone-notice {
font-size: 1.2rem;
font-style: italic;
margin: 1rem 0;
}

.magnified-photo-wrapper {
width: 100vw;
height: calc(var(--vh, 1vh) * 100);
Expand Down
15 changes: 15 additions & 0 deletions src/lib/components/Garden/HiddenPhoneNumber.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script lang="ts">
export let length = 6;
</script>

<span title="Hidden phone number">🔐 {'⋆'.repeat(length)}</span>

<style>
span {
background-color: #eee;
border-radius: 3px;
display: inline-flex;
align-items: center;
padding: 0 0.5rem;
}
</style>
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@
"become-member-text": "become a member"
},
"save": "Save",
"saved": "Saved"
"saved": "Saved",
"phone-notice": "Phone numbers are only visible to members."
},
"add": {
"title": "Add your garden"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@
"capacity": "Place pour {styleCapacity} {capacity, plural, one {tente} other {tentes}}"
},
"save": "Sauvegarder",
"saved": "Sauvegardé"
"saved": "Sauvegardé",
"phone-notice": "Les numéros de téléphone sont visibles uniquement par les membres."
},
"add": {
"title": "Ajoutez votre jardin"
Expand Down
3 changes: 2 additions & 1 deletion src/locales/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
"become-member-text": "lid worden"
},
"save": "Bewaar",
"saved": "Bewaard"
"saved": "Bewaard",
"phone-notice": "Telefoonnummers zijn alleen zichtbaar voor leden."
},
"add": {
"title": "Voeg jouw tuin toe"
Expand Down