diff --git a/functions/lib/getFeed.js b/functions/lib/getFeed.js new file mode 100644 index 0000000..b5aa0bf --- /dev/null +++ b/functions/lib/getFeed.js @@ -0,0 +1,59 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const admin = require("firebase-admin"); +exports.getFeedModule = function (req, res) { + const uid = String(req.query.uid); + function compileFeedPost() { + return __awaiter(this, void 0, void 0, function* () { + const following = yield getFollowing(uid, res); + let listOfPosts = yield getAllPosts(following, res); + listOfPosts = [].concat.apply([], listOfPosts); // flattens list + res.send(listOfPosts); + }); + } + compileFeedPost().then().catch(); +}; +function getAllPosts(following, res) { + return __awaiter(this, void 0, void 0, function* () { + let listOfPosts = []; + for (let user in following) { + listOfPosts.push(yield getUserPosts(following[user], res)); + } + return listOfPosts; + }); +} +function getUserPosts(userId, res) { + const posts = admin.firestore().collection("insta_posts").where("ownerId", "==", userId).orderBy("timestamp"); + return posts.get() + .then(function (querySnapshot) { + let listOfPosts = []; + querySnapshot.forEach(function (doc) { + listOfPosts.push(doc.data()); + }); + return listOfPosts; + }); +} +function getFollowing(uid, res) { + const doc = admin.firestore().doc(`insta_users/${uid}`); + return doc.get().then(snapshot => { + const followings = snapshot.data().following; + let following_list = []; + for (const following in followings) { + if (followings[following] === true) { + following_list.push(following); + } + } + return following_list; + }).catch(error => { + res.status(500).send(error); + }); +} +//# sourceMappingURL=getFeed.js.map \ No newline at end of file diff --git a/functions/lib/getFeed.js.map b/functions/lib/getFeed.js.map new file mode 100644 index 0000000..f124851 --- /dev/null +++ b/functions/lib/getFeed.js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AS,GAAG;YAC9B,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACvB,CAAC,CAAC,CAAA;AACN,CAAC;AAGD,sBAAsB,GAAG,EAAE,GAAG;IAC1B,MAAM,GAAG,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,eAAe,GAAG,EAAE,CAAC,CAAA;IACvD,OAAO,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;QAC/B,MAAM,UAAU,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,SAAS,CAAC;QAE7C,IAAI,cAAc,GAAG,EAAE,CAAC;QAExB,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE;YAClC,IAAI,UAAU,CAAC,SAAS,CAAC,KAAK,IAAI,EAAC;gBACjC,cAAc,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;aAChC;SACF;QACD,OAAO,cAAc,CAAC;IAC1B,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;QACb,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC,CAAC,CAAA;AACN,CAAC"} \ No newline at end of file diff --git a/functions/lib/index.js b/functions/lib/index.js index 76c6519..fe03c62 100644 --- a/functions/lib/index.js +++ b/functions/lib/index.js @@ -10,14 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", { value: true }); const functions = require("firebase-functions"); const admin = require("firebase-admin"); -const notificationHandlerModule = require('./notificationHandler'); -const getFeedModule = require('./getFeed'); +const notificationHandler_1 = require("./notificationHandler"); +const getFeed_1 = require("./getFeed"); admin.initializeApp(); exports.notificationHandler = functions.firestore.document("/insta_a_feed/{userId}/items/{activityFeedItem}") .onCreate((snapshot, context) => __awaiter(this, void 0, void 0, function* () { - notificationHandlerModule.notificationHandler(snapshot, context); + yield notificationHandler_1.notificationHandlerModule(snapshot, context); })); exports.getFeed = functions.https.onRequest((req, res) => { - exports.getFeed(req, res); + getFeed_1.getFeedModule(req, res); }); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/functions/lib/index.js.map b/functions/lib/index.js.map index 559d133..33f6342 100644 --- a/functions/lib/index.js.map +++ b/functions/lib/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,gDAAgD;AAChD,wCAAwC;AACxC,MAAM,yBAAyB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;AACnE,MAAM,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAE3C,KAAK,CAAC,aAAa,EAAE,CAAC;AAET,QAAA,mBAAmB,GAAG,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,iDAAiD,CAAC;KAC7G,QAAQ,CAAC,CAAO,QAAQ,EAAE,OAAO,EAAE,EAAE;IACnC,yBAAyB,CAAC,mBAAmB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACpE,CAAC,CAAA,CAAC,CAAC;AAGM,QAAA,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;IAC5D,eAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACpB,CAAC,CAAC,CAAA"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,gDAAgD;AAChD,wCAAwC;AACxC,+DAAiE;AACjE,uCAAyC;AACzC,KAAK,CAAC,aAAa,EAAE,CAAC;AAET,QAAA,mBAAmB,GAAG,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,iDAAiD,CAAC;KAC7G,QAAQ,CAAC,CAAO,QAAQ,EAAE,OAAO,EAAE,EAAE;IACnC,MAAM,+CAAyB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACtD,CAAC,CAAA,CAAC,CAAC;AAGM,QAAA,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;IAC5D,uBAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAC1B,CAAC,CAAC,CAAA"} \ No newline at end of file diff --git a/functions/lib/notificationHandler.js b/functions/lib/notificationHandler.js new file mode 100644 index 0000000..6ee6db7 --- /dev/null +++ b/functions/lib/notificationHandler.js @@ -0,0 +1,56 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const admin = require("firebase-admin"); +exports.notificationHandlerModule = function (snapshot, context) { + return __awaiter(this, void 0, void 0, function* () { + console.log(snapshot.data()); + const ownerDoc = admin.firestore().doc("insta_users/" + context.params.userId); + const ownerData = yield ownerDoc.get(); + const androidNotificationToken = ownerData.data()["androidNotificationToken"]; + if (androidNotificationToken) { + sendNotification(androidNotificationToken, snapshot.data()); + } + else { + console.log("No token for User, not sending a notification"); + } + return 0; + }); +}; +function sendNotification(androidNotificationToken, activityItem) { + let title; + let body = ""; + if (activityItem['type'] === "comment") { + title = "New Comment"; + body = activityItem['username'] + " commented on your post: " + activityItem["commentData"]; + } + else if (activityItem["type"] === "like") { + title = activityItem['username'] + " liked your post"; + } + else if (activityItem["type"] === "follow") { + title = activityItem['username'] + " started following you"; + } + const message = { + notification: { + title: title, + body: body + }, + token: androidNotificationToken + }; + admin.messaging().send(message) + .then((response) => { + // Response is a message ID string. + console.log('Successfully sent message:', response); + }) + .catch((error) => { + console.log('Error sending message:', error); + }); +} +//# sourceMappingURL=notificationHandler.js.map \ No newline at end of file diff --git a/functions/lib/notificationHandler.js.map b/functions/lib/notificationHandler.js.map new file mode 100644 index 0000000..86ad161 --- /dev/null +++ b/functions/lib/notificationHandler.js.map @@ -0,0 +1 @@ +{"version":3,"file":"notificationHandler.js","sourceRoot":"","sources":["../src/notificationHandler.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,wCAAwC;AAG3B,QAAA,yBAAyB,GAAG,UAAgB,QAAQ,EAAE,OAAO;;QACpE,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAA;QAE5B,MAAM,QAAQ,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;QAC9E,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,GAAG,EAAE,CAAA;QAItC,MAAM,wBAAwB,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC,0BAA0B,CAAC,CAAC;QAC9E,IAAI,wBAAwB,EAAE;YAC3B,gBAAgB,CAAC,wBAAwB,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;SAG9D;aAAM;YACL,OAAO,CAAC,GAAG,CAAC,+CAA+C,CAAC,CAAC;SAC9D;QAED,OAAO,CAAC,CAAC;IACX,CAAC;CAAA,CAAC;AAEN,0BAA0B,wBAAgC,EAAE,YAA4C;IAEpG,IAAI,KAAa,CAAC;IAClB,IAAI,IAAI,GAAW,EAAE,CAAC;IAEtB,IAAI,YAAY,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE;QACtC,KAAK,GAAG,aAAa,CAAA;QACrB,IAAI,GAAG,YAAY,CAAC,UAAU,CAAC,GAAG,2BAA2B,GAAG,YAAY,CAAC,aAAa,CAAC,CAAA;KAC5F;SAAM,IAAI,YAAY,CAAC,MAAM,CAAC,KAAK,MAAM,EAAE;QAC1C,KAAK,GAAG,YAAY,CAAC,UAAU,CAAC,GAAG,kBAAkB,CAAA;KACtD;SAAM,IAAI,YAAY,CAAC,MAAM,CAAC,KAAK,QAAQ,EAAC;QAC3C,KAAK,GAAG,YAAY,CAAC,UAAU,CAAC,GAAG,wBAAwB,CAAA;KAC5D;IAED,MAAM,OAAO,GAAG;QACd,YAAY,EAAE;YACZ,KAAK,EAAE,KAAK;YACZ,IAAI,EAAE,IAAI;SACX;QACD,KAAK,EAAE,wBAAwB;KAChC,CAAA;IAED,KAAK,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC;SAC9B,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QACjB,mCAAmC;QACnC,OAAO,CAAC,GAAG,CAAC,4BAA4B,EAAE,QAAQ,CAAC,CAAC;IACtD,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QACf,OAAO,CAAC,GAAG,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;AACL,CAAC"} \ No newline at end of file diff --git a/functions/src/getFeed.ts b/functions/src/getFeed.ts index 3e27b36..c1b4fd4 100644 --- a/functions/src/getFeed.ts +++ b/functions/src/getFeed.ts @@ -2,7 +2,7 @@ import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; -export const getFeed = function(req, res) { +export const getFeedModule = function(req, res) { const uid = String(req.query.uid); async function compileFeedPost() { diff --git a/functions/src/index.ts b/functions/src/index.ts index 411d22e..83cb7b6 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -1,16 +1,15 @@ import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; -const notificationHandlerModule = require('./notificationHandler'); -const getFeedModule = require('./getFeed'); - +import { notificationHandlerModule } from "./notificationHandler" +import { getFeedModule } from "./getFeed" admin.initializeApp(); export const notificationHandler = functions.firestore.document("/insta_a_feed/{userId}/items/{activityFeedItem}") .onCreate(async (snapshot, context) => { - notificationHandlerModule.notificationHandler(snapshot, context); + await notificationHandlerModule(snapshot, context); }); export const getFeed = functions.https.onRequest((req, res) => { - getFeed(req, res); + getFeedModule(req, res); }) \ No newline at end of file diff --git a/functions/src/notificationHandler.ts b/functions/src/notificationHandler.ts index cab6513..2d1782b 100644 --- a/functions/src/notificationHandler.ts +++ b/functions/src/notificationHandler.ts @@ -1,7 +1,7 @@ import * as admin from 'firebase-admin'; import { DocumentSnapshot } from 'firebase-functions/lib/providers/firestore'; -export const notificationHandler = async function (snapshot, context) { +export const notificationHandlerModule = async function (snapshot, context) { console.log(snapshot.data()) const ownerDoc = admin.firestore().doc("insta_users/" + context.params.userId)