Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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

3 changes: 3 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,6 @@ typings/

node_modules/
var/

serviceAccountKey.json
emails/
Comment thread
iiio2 marked this conversation as resolved.
Outdated
573 changes: 573 additions & 0 deletions services/firebase/emails/weekly.html
Comment thread
iiio2 marked this conversation as resolved.
Outdated

Large diffs are not rendered by default.

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 src/**/*.mjml dist/",
Comment thread
iiio2 marked this conversation as resolved.
"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
12 changes: 12 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 { getWeeklyData, renderEmail } from "./lib/digest";
import * as fs from "fs";

yargs(hideBin(process.argv))
.command(
Expand Down Expand Up @@ -173,6 +175,16 @@ yargs(hideBin(process.argv))
await getCities()
}
)
.command(
'newsletter',
'Generate Weekly Newsletter',
() => undefined,
async (argv: any) => {
const data = await getWeeklyData('Munich');
const html = await renderEmail('weekly', data)
fs.writeFileSync("./emails/weekly.html", html);
}
)
.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
93 changes: 90 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 { renderEmail, getWeeklyData } 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,92 @@ export const matchNotification = functions.firestore
await sendEmail(data)
})


export const scheduleEmail = functions.pubsub
.schedule("every monday 18:00")
.timeZone("Europe/Berlin")
.onRun(async (context) => {
const cityDocs = (
await firestore.collection("profiles").where("type", "==", "City").get()
).docs;

const cities: any = [];

for (let doc of cityDocs) {
Comment thread
iiio2 marked this conversation as resolved.
Outdated
cities.push({ id: doc.id, ...doc.data() });
}

let data;
let recipients: any = {};

Comment thread
iiio2 marked this conversation as resolved.
Outdated
for (let city of cities) {
if (!city.watch?.list) {
continue;
}
data = await getWeeklyData(city.username);
const html = await renderEmail("weekly", data);

const subscribers = Object.keys(city.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,
};

let userIds = [];
userIds.push(profileId);

const weeklyNewsLetter = await firestore
.collection("weekly-newsletters")
.add({
city: city?.username,
createdAt: Date.now(),
sentAt: Date.now(),
scheduledAt: Date.now(),
userIds: [...userIds],
});

const email: any = {
from: `WeDance <noreply@wedance.vip>`,
recipients,
subject: "Weekly Newsletter",
content: html,
id: weeklyNewsLetter.id,
type: "City",
};
return await sendEmail(email);
}
}

return null;
});


// export const taskRunner = functions
// .runWith({ memory: '2GB' })
// .pubsub.schedule('* * * * *')
Expand Down
118 changes: 118 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,118 @@
const { createSSRApp } = require("vue");
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(`./templates/${type}.mjml`, "utf8");
Comment thread
iiio2 marked this conversation as resolved.
Outdated

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 today = new Date().toISOString().slice(0, 10);
const then = new Date();
Comment thread
iiio2 marked this conversation as resolved.
Outdated
then.setDate(then.getDate() + 7);
const sevenDaysFromNow = then.toISOString().slice(0, 10);
const data = [];
let profile: any = {};
Comment thread
iiio2 marked this conversation as resolved.
Outdated

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

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

let usernames = Object.keys(profile.watch?.list);
Comment thread
iiio2 marked this conversation as resolved.
Outdated

for (let username of usernames) {
const eventDocs = (
await firestore
.collection("posts")
.where("startDate", ">", today)
.where("startDate", "<", sevenDaysFromNow)
.where("username", "==", username)
Comment thread
iiio2 marked this conversation as resolved.
Outdated
.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: profile.telegram,
instagram: profile.instagram,
facebook: profile.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;
}
Comment thread
iiio2 marked this conversation as resolved.
Outdated
Loading