Skip to content
Open
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ FIREBASE_CONFIG=

# Sentry configuration - https://docs.sentry.io/platforms/node/configuration/options/#dsn
SENTRY_DSN=

3 changes: 2 additions & 1 deletion .firebaserc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"projects": {
"live": "wedance-4abe3"
"live": "wedance-4abe3",
"dev": "wedancedev-aa00e"
},
"targets": {},
"etags": {}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ sw.*

# Firebase
.vscode

2 changes: 2 additions & 0 deletions services/firebase/.gitignore
Comment thread
iiio2 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ typings/

node_modules/
var/

serviceAccountKey.json
13 changes: 12 additions & 1 deletion services/firebase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"build": "yarn clean && tsc && yarn copy-files",
"copy-files": "copyfiles -u 1 src/**/*.js templates/**/*.mjml dist/",
Comment thread
iiio2 marked this conversation as resolved.
Outdated
"clean": "rimraf dist/",
"test": "npm run build && node dist/test.js",
"cli": "npm run build && node dist/cli.js",
"serve": "npm run build && firebase emulators:start --only functions",
Expand Down Expand Up @@ -30,21 +32,30 @@
"express": "^4.17.1",
"firebase-admin": "^8.13.0",
"firebase-functions": "^3.22.0",
"fs": "^0.0.1-security",
"handlebars": "^4.7.6",
"https-proxy-agent": "^5.0.0",
"instagram-private-api": "^1.45.3",
"mailgun-js": "^0.22.0",
"markdown-it": "^10.0.0",
"mjml": "^4.13.0",
"moment": "^2.29.4",
"node-fetch": "^3.2.10",
"png-to-jpeg": "^1.0.1",
"puppeteer": "^7.0.0",
"telegraf": "^4.8.6",
"vue": "^3.2.45",
"yargs": "^17.3.1"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/mjml": "^4.7.0",
"@types/puppeteer": "^5.4.2",
"@types/uuid": "^9.0.1",
"@types/vue": "^2.0.0",
"copyfiles": "^2.4.1",
"firebase-functions-test": "^0.1.6",
"rimraf": "^3.0.2",
"tslint": "^5.12.0",
"typescript": "^3.9.10"
},
Expand Down
10 changes: 10 additions & 0 deletions services/firebase/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const { hideBin } = require('yargs/helpers')
import { firestore } from './firebase'
import { announceEventIG } from './lib/instagram'
import { getInstagramWebProfileInfo } from './lib/browser'
import { scheduleWeeklyEmails } from './lib/digest'


yargs(hideBin(process.argv))
.command(
Expand Down Expand Up @@ -173,6 +175,14 @@ yargs(hideBin(process.argv))
await getCities()
}
)
.command(
'newsletter',
'Generate Weekly Newsletter',
() => undefined,
async (argv: any) => {
await scheduleWeeklyEmails()
}
)
.command(
'cleanup:cities',
'Cleanup cities',
Expand Down
4 changes: 3 additions & 1 deletion services/firebase/src/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as admin from 'firebase-admin'

admin.initializeApp()
admin.initializeApp({
credential: admin.credential.applicationDefault()
});

const firestore = admin.firestore()

Expand Down
14 changes: 11 additions & 3 deletions services/firebase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { wrap } from './sentry'
import { announceEvent } from './lib/telegram'
import { announceEventIG } from './lib/instagram'
import { getInstagramWebProfileInfo } from './lib/browser'
import { scheduleWeeklyEmails } from './lib/digest'

require('dotenv').config()

Expand Down Expand Up @@ -113,7 +114,7 @@ app.post('/track/:action', async (req, res) => {
})
})

app.get('/share/*', async (req, res) => {
app.get('/share/*', async (req: any, res: any) => {
const path = req.params[0]
const timezone = req.query.timezone as string

Expand Down Expand Up @@ -142,7 +143,7 @@ app.get('/share/*', async (req, res) => {
} catch (e) {
return res.json({
success: false,
error: e.message,
error: (e as Error).message,
})
}
})
Expand Down Expand Up @@ -275,7 +276,7 @@ export const profileCreated = functions
} catch (e) {
await snapshot.ref.update({
import: 'failed',
importError: e.message,
importError: (e as Error).message,
})

return
Expand Down Expand Up @@ -656,6 +657,13 @@ export const matchNotification = functions.firestore
await sendEmail(data)
})

export const scheduleEmail = functions.pubsub
.schedule('every monday 18:00')
.timeZone('Europe/Berlin')
.onRun(async (context) => {
await scheduleWeeklyEmails()
})

// export const taskRunner = functions
// .runWith({ memory: '2GB' })
// .pubsub.schedule('* * * * *')
Expand Down
197 changes: 197 additions & 0 deletions services/firebase/src/lib/digest.ts
Comment thread
iiio2 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
const { createSSRApp } = require('vue');
Comment thread
iiio2 marked this conversation as resolved.
Outdated
const { renderToString } = require('vue/server-renderer');
import mjml2html = require('mjml');
import * as fs from 'fs';
import * as moment from 'moment';
import { firestore } from '../firebase';

export async function renderEmail(type: string, data: any, customUtms = {}) {
const template = fs.readFileSync(`./src/templates/${type}.mjml`, 'utf8');

const defaultUtms = {
campaign: type,
medium: 'email',
source: 'newsletter',
};

const utm = {
...defaultUtms,
...customUtms,
};

const app = createSSRApp({
data: () => {
return data;
},
template,
methods: {
link(url: string, utmContent = '') {
return (
url +
'?utm_campaign=' +
utm.campaign +
'&utm_medium=' +
utm.medium +
'&utm_source=' +
utm.source +
'&utm_content=' +
utmContent
);
},
},
});

app.config.compilerOptions.isCustomElement = (tag: any) =>
tag.startsWith('mj');

return mjml2html(await renderToString(app)).html;
}

export async function getWeeklyData(city: string) {
const time = new Date();
const today = time.toISOString().slice(0, 10);
time.setDate(time.getDate() + 7);
const sevenDaysFromNow = time.toISOString().slice(0, 10);

const data = [];
let cityProfile: any = {};

const profileDocs = (
await firestore.collection('profiles').where('username', '==', city).get()
).docs;

for (const doc of profileDocs) {
cityProfile = { id: doc.id, ...doc.data() };
}

const eventDocs = (
await firestore
.collection('posts')
.where('startDate', '>', today)
.where('startDate', '<', sevenDaysFromNow)
.where('place', '==', cityProfile.place)
.get()
).docs;

for (const doc of eventDocs) {
const event = {
id: doc.id,
...doc.data(),
} as any;

data.push(event);
}

const events: any = {
intro:
'Hope you had a great weekend and are ready with your dancing shoes on for a fantastic week ahead.',
title: `${city} Dance Calendar`,
links: {
telegram: cityProfile.telegram,
instagram: cityProfile.instagram,
facebook: cityProfile.facebook,
addEvent: 'https://wedance.vip/events/-/edit',
city: `https://wedance.vip/${city}`,
},
days: data.map((event) => ({
day: moment(event.startDate).format('dddd'),
date: moment(event.startDate).format('D MMM'),
events: [
{
title: event.name,
organizer: event.org.name,
venue: event.venue?.name,
format: event.eventType,
time: moment(event.startDate).format('hh:mm'),
link: event.link,
cover: event.cover,
styles: Object.keys(event.styles),
},
],
})),
};

return events;
}

export async function getSubscribers(cityProfile: any) {
const recipients = {} as any;

const subscribers = Object.keys(cityProfile.watch?.list);

for (let subscriber of subscribers) {
const profilesOfSubscriber = (
await firestore
.collection('profiles')
.where('username', '==', subscriber)
.get()
).docs;

if (profilesOfSubscriber.length !== 1) {
continue;
}

const profileId = profilesOfSubscriber[0].id;

const accountDoc = await firestore
.collection('accounts')
.doc(profileId)
.get();

if (!accountDoc.exists) {
continue;
}

const account = accountDoc.data();

recipients[profileId] = {
name: account?.name,
email: account?.email,
};
}

return recipients;
}

function getNextMonday() {
const nextMonday = moment()
.add(1, 'weeks')
.day('Monday')
.hour(18)
.minute(0)
.second(0);
const formattedDate = nextMonday.format('MMMM DD, YYYY hh:mm A');
return formattedDate;
}

export async function scheduleWeeklyEmails() {
const cityDocs = (
await firestore.collection('profiles').where('type', '==', 'City').get()
).docs;

const nextMonday = getNextMonday();

for (let cityDoc of cityDocs) {
const cityProfile = cityDoc.data();

if (!cityProfile.watch?.list) {
continue;
}

const weeklyEmailDetails = await getWeeklyData(cityProfile.username);
const html = await renderEmail('weekly', weeklyEmailDetails);
const recipients = await getSubscribers(cityProfile);

await firestore.collection('emails').add({
status: 'scheduled',
scheduledAt: nextMonday,
createdAt: Date.now(),
from: `WeDance <noreply@wedance.vip>`,
subject: 'Weekly Newsletter',
recipients,
content: html,
});
}

return null;
}
Loading