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/email automation #893

Merged
merged 7 commits into from
Mar 11, 2020
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
31 changes: 31 additions & 0 deletions firestore.rules.WiP
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// WiP - CC - 2020-02-24
// As part of future security update will make better use of firestore rules, as stubbed out below

service cloud.firestore {
// rules will apply to all docs in database
match /databases/{database}/documents {
// Function to check whether request user has specific permission set in user database
function hasUserRole(username,role){
// TODO - look up a profile to see if the user has specific role (e.g. admin)
// NOTE - this is not currently possible as the username is not sent with a request
// but could be made possible by setting the firebase auth name to the username
// and ensuring all users had a userRoles property
// NOTE 2 - possible code below but would need testing (https://firebase.google.com/docs/reference/rules/rules.List)
// return role in get(/databases/$(database)/documents/v3_users/$(username)).data.userRoles
return true
}
// Function to check if request to modify a document is by the auth owner
function isSameUser(username){
return get(/databases/$(database)/documents/v3_users/$(username)).data._authID==request.auth.uid
}
match /v3_users/{username} {
allow read, write: if hasUserRole('admin')
allow read,write
}
// users can read/write their own user docs
match /v3_users/{username} {
allow read, write: if isSameUser(username)
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably be separated in a draft PR for more clarity

54 changes: 0 additions & 54 deletions functions/src/Analytics/analytics.ts

This file was deleted.

16 changes: 0 additions & 16 deletions functions/src/Analytics/comments.ts

This file was deleted.

29 changes: 29 additions & 0 deletions functions/src/Integrations/firebase-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as functions from 'firebase-functions'
import { IDBEndpoint } from '../models'
import { db } from '..//Firebase/firestoreDB'

const USER_ENDPOINT: IDBEndpoint = 'v3_users'

/**
* Example function to show how an automated email can be triggered
* In this case it is being used temporarily to help debug
* https://github.com/ONEARMY/community-platform/issues/883
*/
export const notifyEmailDemo = functions.firestore
.document(`${USER_ENDPOINT}/precious-plastic`)
.onWrite(async (change, context) => {
return db.collection('mail').add({
to: 'chris.m.clarke@live.co.uk',
message: {
subject: 'PP Profile Edited',
html: `
<p>Just thought you should know that an edit has been made to your profile</p>
<h2>Before</h2>
<code>${JSON.stringify(change.before.data())}</code>
<h2>After</h2>
<code>${JSON.stringify(change.after.data())}</code>
<p>To see a clear breakdown of differences you could copy-paste each section into http://www.jsondiff.com</p>
`,
},
})
})
22 changes: 22 additions & 0 deletions functions/src/Integrations/firebase-userBackup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as functions from 'firebase-functions'
import { DBDoc, IDBEndpoint } from '../models'
const USERS_ENDPOINT: IDBEndpoint = 'v3_users'

/**
* Automatically create user revision on update
* Nests revision as subcollection of original document,
* labeled by previous _modified timestamp
*/
export const FirebaseUserBackup = functions.firestore
.document(`${USERS_ENDPOINT}/{username}`)
.onUpdate((change, context) => {
const { before, after } = change
const rev = before.data() as DBDoc
if (rev && rev._modified) {
return before.ref
.collection('revisions')
.doc(rev._modified)
.set(rev)
}
return null
})
4 changes: 4 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { weeklyTasks, dailyTasks } from './exports/tasks'
import { DH_Exports } from './DaveHakkensNL'
import * as IntegrationsSlack from './Integrations/firebase-slack'
import * as IntegrationsDiscord from './Integrations/firebase-discord'
import { FirebaseUserBackup } from './Integrations/firebase-userBackup'
import * as IntegrationsEmail from './Integrations/firebase-email'

// the following endpoints are exposed for use by various triggers
// see individual files for more informaiton
Expand All @@ -18,3 +20,5 @@ exports.notifyNewEvent = IntegrationsSlack.notifyNewEvent
exports.notifyPinAccepted = IntegrationsDiscord.notifyPinAccepted
exports.notifyHowToAccepted = IntegrationsDiscord.notifyHowToAccepted
exports.notifyEventAccepted = IntegrationsDiscord.notifyEventAccepted
exports.firebaseUserBackup = FirebaseUserBackup
exports.emailNotificationDemo = IntegrationsEmail.notifyEmailDemo
43 changes: 0 additions & 43 deletions src/stores/Analytics/analytics.store.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/stores/User/user.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class UserStore extends ModuleStore {
if (userMeta) {
this.updateUser(userMeta)
} else {
this.createUserProfile()
throw new Error(`could not find user profile [${user.uid}]`)
}
}
}
Expand Down