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/notification integration #813

Merged
merged 2 commits into from
Dec 4, 2019
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
4 changes: 3 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
"google-auth-library": "^2.0.1",
"googleapis": "^35.0.0",
"log-update": "^3.2.0",
"sharp": "^0.22.1"
"request": "^2.88.0",
"sharp": "^0.23.3"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/cors": "^2.8.5",
"@types/dateformat": "^3.0.0",
"@types/fs-extra": "^5.0.5",
"@types/request": "^2.48.3",
"@types/sharp": "^0.22.1",
"concurrently": "^4.1.1",
"tslint": "^5.12.0",
Expand Down
89 changes: 89 additions & 0 deletions functions/src/Integrations/firebase-slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { CONFIG } from '../config/config'
import * as functions from 'firebase-functions'
import * as request from 'request'

const project = CONFIG.service.project_id
// add prefix to dev site
const prefix = project === 'precious-plastics-v4-dev' ? '[DEV] ' : ''
const SLACK_WEBHOOK_URL = CONFIG.integrations.slack_webhook

export const notifyNewPin = functions.firestore
.document('v2_mappins/{pinId}')
.onCreate((snapshot, context) => {
const info = snapshot.data()
const id = info._id
const type = info.type
const subType = info.subType
const loc = info.location
// console.log(info);
request.post(
SLACK_WEBHOOK_URL,
{
json: {
text: `${prefix}New ${subType} ${type} from ${id},
You can find it on google maps (yeh yeh i know its coming soon straight to the Precious Plastic Map)
https://google.com/maps/@${loc.lat},${loc.lng},14z`,
},
},
(err, res) => {
if (err) {
console.error(err)
return
} else {
return res
}
},
)
})
export const notifyNewHowTo = functions.firestore
.document('v2_howtos/{id}')
.onCreate((snapshot, context) => {
const info = snapshot.data()
const user = info._createdBy
const title = info.title
const slug = info.slug
// console.log(info);
request.post(
SLACK_WEBHOOK_URL,
{
json: {
text: `${prefix}Fuck Ya!! New How To ${title} by ${user}
check it out @ https://community.preciousplastic.com/how-to/${slug}`,
},
},
(err, res) => {
if (err) {
console.error(err)
return
} else {
return res
}
},
)
})
export const notifyNewEvent = functions.firestore
.document('v2_events/{id}')
.onCreate((snapshot, context) => {
const info = snapshot.data()
const user = info._createdBy
const url = info.url
const location = info.location.country
console.info(info)
request.post(
SLACK_WEBHOOK_URL,
{
json: {
text: `${prefix}Jeej new event in ${location} by ${user} posted here:
${url}`,
},
},
(err, res) => {
if (err) {
console.error(err)
} else {
console.log('post success')
return res
}
},
)
})
5 changes: 5 additions & 0 deletions functions/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const c = config() as configVars
// strip additional character escapes (\\n -> \n)
c.service.private_key = c.service.private_key.replace(/\\n/g, '\n')

export const CONFIG = c
export const SERVICE_ACCOUNT_CONFIG = c.service
export const ANALYTICS_CONFIG = c.analytics
/************** Interfaces ************** */
Expand All @@ -27,10 +28,14 @@ interface IAnalytics {
tracking_code: string
view_id: string
}
interface IIntergrations {
slack_webhook: string
}

interface configVars {
service: IServiceAccount
analytics: IAnalytics
integrations: IIntergrations
}

// if passing complex config variables, may want to
Expand Down
4 changes: 4 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { api } from './exports/api'
import { weeklyTasks, dailyTasks } from './exports/tasks'
import { DH_Exports } from './DaveHakkensNL'
import * as Integrations from './Integrations/firebase-slack'

// the following endpoints are exposed for use by various triggers
// see individual files for more informaiton
Expand All @@ -10,3 +11,6 @@ exports.dailyTasks = dailyTasks
exports.DHSite_getUser = DH_Exports.DHSite_getUser
exports.DHSite_migrateAvatar = DH_Exports.DHSite_migrateAvatar
exports.DHSite_login = DH_Exports.DHSite_login
exports.notifyNewPin = Integrations.notifyNewPin
exports.notifyNewHowTo = Integrations.notifyNewHowTo
exports.notifyNewEvent = Integrations.notifyNewEvent
Loading