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: weekly newsletter with upcoming events #331

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
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 components/TRecipients.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default {
unsubscribedAt: 'bg-orange-500',
},
folderNames: {
scheduled: 'Scheduled',
deliveredAt: 'Unread',
openedAt: 'Opened',
clickedAt: 'Clicked',
Expand All @@ -78,6 +79,7 @@ export default {
const recipients = this.itemsList

return {
scheduled: recipients,
failedAt: recipients.filter(
(item) => item.failedAt && !item.deliveredAt
),
Expand Down
2 changes: 2 additions & 0 deletions services/firebase/.gitignore
iiio2 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ typings/

node_modules/
var/

serviceAccountKey.json
14 changes: 13 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/",
iiio2 marked this conversation as resolved.
Show resolved Hide 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,31 @@
"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",
"moment-timezone": "^0.5.43",
"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
9 changes: 9 additions & 0 deletions services/firebase/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { hideBin } = require('yargs/helpers')
import { firestore } from './firebase'
import { announceEventIG } from './lib/instagram'
import { getInstagramWebProfileInfo } from './lib/browser'
import { scheduleWeeklyNewsletter } from './lib/digest'

yargs(hideBin(process.argv))
.command(
Expand Down Expand Up @@ -173,6 +174,14 @@ yargs(hideBin(process.argv))
await getCities()
}
)
.command(
'newsletter',
'Generate Weekly Newsletter',
() => undefined,
async (argv: any) => {
await scheduleWeeklyNewsletter()
}
)
.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
13 changes: 10 additions & 3 deletions services/firebase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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 +142,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 +275,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 +656,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
220 changes: 220 additions & 0 deletions services/firebase/src/lib/digest.ts
iiio2 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
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'
import sendEmail from './sendEmail'
import { orderBy } from 'lodash'

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) {
let cityProfile: any = {}

const profileDocs = (
await firestore
.collection('profiles')
.where('username', '==', city)
iiio2 marked this conversation as resolved.
Show resolved Hide resolved
.get()
).docs

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

const today = moment(new Date())
const sevenDaysFromNow = moment(today).add(7, 'days')

let eventDocs = (
await firestore
.collection('posts')
.where('startDate', '>', today.unix())
.where('place', '==', cityProfile.place)
.get()
).docs.map((doc: any) => ({ id: doc.id, ...doc.data() }))

eventDocs = orderBy(eventDocs, ['startDate'], ['asc'])
eventDocs = eventDocs.filter((event: any) =>
['Party', 'Festival', 'Show', 'Concert'].includes(event.eventType)
)
eventDocs = eventDocs.filter(
(event: any) =>
moment(event.startDate).unix() >= today.unix() &&
moment(event.startDate).unix() <= sevenDaysFromNow.unix()
)

const days: any = {}

for (const event of eventDocs) {
const group = moment(event.startDate).format('YYYY-MM-DD')

if (!days[group]) {
days[group] = {
day: moment(event.startDate).format('dddd'),
date: moment(event.startDate).format('D MMM'),
events: [],
}
}

days[group].events.push({
title: event.name,
organizer: event.org.name,
venue: event.venue?.name,
format: event.eventType,
time: moment(event.startDate).format('HH:mm'),
link: `https://wedance.vip/events/${event.id}`,
cover: event.cover,
styles: Object.keys(event.styles),
})
}

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,
}

return events
}

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

const subscribers = cityProfile.watch?.usernames || []

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

if (profilesOfSubscriber.length !== 1) {
// console.log(`Subscriber ${subscriber} not found`)
continue
}

const profileId = profilesOfSubscriber[0].id

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

if (!accountDoc.exists) {
// console.log(`Account for ${subscriber} not found`)
continue
}

const account = accountDoc.data()

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

return recipients
}

export async function scheduleWeeklyNewsletter() {
const cityDocs = (
await firestore
.collection('profiles')
.where('username', '==', 'Munich')
.get()
).docs
iiio2 marked this conversation as resolved.
Show resolved Hide resolved

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

if (!cityProfile.watch?.usernames) {
continue
}

const weeklyEmailDetails = await getWeeklyData(cityProfile.username)

const html = await renderEmail('weekly', weeklyEmailDetails)
const recipients = await getSubscribers(cityProfile)

const emailDoc: any = {
status: 'scheduled',
scheduledAt: moment()
.hour(18)
.minute(0)
.second(0),
createdAt: Date.now(),
from: `WeDance <[email protected]>`,
subject: 'Weekly Newsletter',
recipients,
html,
}

const emailRef = await firestore.collection('emails').add(emailDoc)

emailDoc.id = emailRef.id

await sendEmail(emailDoc)

await firestore
.collection('emails')
.doc(emailDoc.id)
.update({
status: 'sent',
processedAt: Date.now(),
error: '',
})
}

return null
}
Loading