Skip to content

Commit

Permalink
Merge pull request #887 from jboolean/better-story-email
Browse files Browse the repository at this point in the history
Add map and image to story email
  • Loading branch information
jboolean authored Sep 16, 2023
2 parents d3039a9 + b67573c commit 766dc8d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export interface StoryEmailTemplateData {
photoDescription: string;
storyEditUrl: string;
viewPhotoUrl: string;
photoThumbnailUrl: string;
mapImageUrl: string | null;
mapImageUrlRetina: string | null;
}

export interface StoryEmailMetadata {
Expand Down
19 changes: 19 additions & 0 deletions backend/src/business/stories/StoryUserEmailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
StoryEmailTemplateData,
} from '../email/templates/StoryUserEmailTemplateData';
import StoryUserRemovedTemplate from '../email/templates/StoryUserRemovedTemplate';
import forgeStaticMapUrl from '../utils/forgeStaticMapUrl';
import required from '../utils/required';
import { createStoryToken } from './StoryTokenService';

Expand Down Expand Up @@ -62,12 +63,30 @@ function forgePhotoUrl(photo: Story['photo'], lngLat: Story['lngLat']): string {
return storyEditUrl.toString();
}

function forgeImageThumbnailUrl(photo: Story['photo']): string {
return `https://photos.1940s.nyc/420-jpg/${photo.identifier}.jpg`;
}

function forgeMapImageUrl(
photo: Story['photo'],
lngLat: Story['lngLat'],
retina = false
): string | null {
if (!lngLat) {
return null;
}
return forgeStaticMapUrl(photo.identifier, lngLat, 315, 315, 16, retina);
}

function forgeStoryTemplateContext(story: Story): StoryEmailTemplateData {
return {
storytellerName: required(story.storytellerName, 'storytellerName'),
photoDescription: describePhoto(story.photo),
storyEditUrl: forgeStoryEditUrl(story),
viewPhotoUrl: forgePhotoUrl(story.photo, story.lngLat),
photoThumbnailUrl: forgeImageThumbnailUrl(story.photo),
mapImageUrl: forgeMapImageUrl(story.photo, story.lngLat, false),
mapImageUrlRetina: forgeMapImageUrl(story.photo, story.lngLat, true),
};
}

Expand Down
39 changes: 39 additions & 0 deletions backend/src/business/utils/forgeStaticMapUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import LngLat from '../../enum/LngLat';

const MAPBOX_USER = 'julianboilen';
const MAPBOX_PK =
'pk.eyJ1IjoianVsaWFuYm9pbGVuIiwiYSI6ImNqb3A0ODg1djFkNGIza214aDQ0NjA2ZHYifQ.nw1o5FE0rdcN5DQLzRFQfQ';
const MAP_STYLE_ID = 'ck5jrzrs11r1p1imia7qzjkm1';
const LAYER_ID = 'photos-1940s';

export default function forgeStaticMapUrl(
photoIdentifier: string,
lngLat: LngLat,
width: number,
height: number,
zoomLevel: number,
retina = false
): string | null {
if (!lngLat) {
return null;
}
const { lng, lat } = lngLat;
const mapImageUrl: URL = new URL(
`/styles/v1/${MAPBOX_USER}/${MAP_STYLE_ID}/static/${lng},${lat},${zoomLevel}/${width}x${height}${
retina ? '@2x' : ''
}`,
'https://api.mapbox.com'
);

const mapImageUrlParams: URLSearchParams = new URLSearchParams();
mapImageUrlParams.append('access_token', MAPBOX_PK);
mapImageUrlParams.append(
'setfilter',
`["==", ["get", "photoIdentifier"], "${photoIdentifier}"]`
);
mapImageUrlParams.append('layer_id', LAYER_ID);

mapImageUrl.search = mapImageUrlParams.toString();

return mapImageUrl.toString();
}

0 comments on commit 766dc8d

Please sign in to comment.