Advertisement
Alior

Untitled

Oct 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const functions = require("firebase-functions");
  4. const admin = require("firebase-admin");
  5.  
  6. admin.initializeApp();
  7. const adminsPath = "/Users/{uid}/Status/Admin";
  8.  
  9. exports.manageAdminsListUpdate = functions.database.ref(adminsPath)
  10. .onUpdate((snapshot, context) => {
  11. const ref = admin.database().ref("Admins/" + context.params.uid);
  12. if (snapshot.after.val() === null || snapshot.after.val() === false)
  13. return ref.remove();
  14. else
  15. return ref.set(true);
  16. });
  17.  
  18. exports.manageAdminsListAdd = functions.database.ref(adminsPath)
  19. .onCreate((snapshot, context) => {
  20. const ref = admin.database().ref("Admins/" + context.params.uid);
  21. if (snapshot.val() === false)
  22. return ref.remove();
  23. else
  24. return ref.set(true);
  25. });
  26.  
  27. exports.manageAdminListRemove = functions.database.ref(adminsPath)
  28. .onDelete((snapshot, context) => {
  29. return admin.database().ref("Admins/" + context.params.uid).remove();
  30. });
  31.  
  32. exports.reactToNewProposedWord = functions.database.ref("/Proposed/{lang}/{word}")
  33. .onCreate((snapshot, context) => {
  34. return admin.database().ref("/Admins/")
  35. .on('value', (valueSnapshot) => {
  36. if (valueSnapshot === null)
  37. return;
  38.  
  39. const payload = {
  40. notification: {
  41. title: 'New word has been proposed',
  42. body: 'The word \'' + context.word + '\' has been proposed'
  43. }
  44. };
  45.  
  46. valueSnapshot.forEach(child => {
  47. const key = child.key();
  48. const token = await admin.database().ref("Users/" + key + "/Messaging/Token").once('value);
  49.  
  50. await admin.messaging().sendToDevice(token, payload);
  51. });
  52. });
  53. });
  54.  
  55. //# sourceMappingURL=index.js.map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement