From dadf070ac5809875fc547fc84c19e84540a0d834 Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Thu, 12 Jan 2023 12:19:29 -0500 Subject: [PATCH] Add bible API --- dbTools/profile.js | 16 +++++++---- def/post.js | 3 ++ def/profile.js | 5 ++-- index.js | 3 ++ routes/bible.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 routes/bible.js diff --git a/dbTools/profile.js b/dbTools/profile.js index 54bfca4..ab0ad23 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -128,6 +128,7 @@ userDB = (DB) => { let regEx = new RegExp(queryStr, 'i'); let query = { isGroup: false, + isChat: false, $or: [ {"profile.firstName": { $regex: regEx @@ -140,7 +141,7 @@ userDB = (DB) => { }}, ] }; - let r = await DB.profileCols.find(queryStr ? query : {isGroup: false}) + let r = await DB.profileCols.find(queryStr ? query : {isGroup: false, isChat: false}) .sort({ lastUpdate: -1 }).limit(20) .toArray().catch((err) => { console.log(err); @@ -166,7 +167,7 @@ userDB = (DB) => { return false; }); let index = 0; - while(r[index].isGroup) index += 1; + while(r[index].isGroup || r[index].isChat) index += 1; if (r[index]) userProfileCache[r[index].id] = r[index]; return r[index]; } @@ -273,6 +274,7 @@ userDB = (DB) => { let query = { isGroup: true, isCourse: {$ne: true}, + isChat: {$ne: true}, }; if(excludePrivate) query.isPrivate = false; let r = await DB.profileCols.find(query).sort({ lastUpdate: -1 }).limit(10) @@ -289,13 +291,14 @@ userDB = (DB) => { for(id in profile.following){ let oId = DB.ObjectID(id); let checkProfile = await DB.getProfileCache(oId) - if(checkProfile && checkProfile.isGroup){ + if(checkProfile && checkProfile.isGroup && !checkProfile.isChat){ ids.push(oId) } } let query = { isGroup: true, isCourse: {$ne: true}, + isChat: {$ne: true}, _id: { $in: ids } @@ -312,6 +315,7 @@ userDB = (DB) => { let regEx = new RegExp(queryStr, 'i'); let query = queryStr ? { isGroup: true, + isChat: {$ne: true}, isCourse: coursesB, $or: [ {"profile.firstName": { @@ -327,7 +331,7 @@ userDB = (DB) => { $regex: regEx }} ] - } : {isGroup: true, isCourse: coursesB}; + } : {isGroup: true, isChat: {$ne: true}, isCourse: coursesB}; let r = await DB.profileCols.find(query) .sort({ lastUpdate: -1 }).limit(20) .toArray().catch((err) => { @@ -353,7 +357,7 @@ userDB = (DB) => { DB.getGroup = async (groupid) => { const _id = DB.ObjectID(groupid); //if(userProfileCache[groupid]) return userProfileCache[groupid]; - let r = await DB.profileCols.findOne({_id, isGroup: true}).catch((err) => { + let r = await DB.profileCols.findOne({_id, isGroup: true, isChat: {$ne: true},}).catch((err) => { console.log(err); return false; }); @@ -425,7 +429,7 @@ userDB = (DB) => { //Courses DB.getCourses = async () => { - let r = await DB.profileCols.find({isGroup: true, isCourse: true}) + let r = await DB.profileCols.find({isGroup: true, isCourse: true, isChat: {$ne: true}}) .sort({ lastUpdate: -1 }).limit(20) .toArray().catch((err) => { console.log(err); diff --git a/def/post.js b/def/post.js index be10393..d18d439 100644 --- a/def/post.js +++ b/def/post.js @@ -16,6 +16,9 @@ class Post { this.lastUpdated = info.lastUpdated || this.createdAt; // Add tags to posts this.tags = info.tags; + // For chats profileid will be the chat profile and the chatSenderId will be + // the user sending the message, this will prevent showing on feed. + this.chatSenderId = info.chatSenderId; } addComment(comment){ diff --git a/def/profile.js b/def/profile.js index d3d45f8..497e8bf 100644 --- a/def/profile.js +++ b/def/profile.js @@ -22,9 +22,9 @@ class User { this.isGroup = info.isGroup || false; this.isCourse = info.isCourse || false; this.isPrivate = info.isPrivate || false; + this.isChat = info.isChat || false; this.subscribed = info.subscribed || {}; //Subscribed user to groups - this.pending = info.subscribed || {}; //Private groups require authorization - + this.pending = info.pending || {}; //Private groups require authorization } toObj(){ @@ -42,6 +42,7 @@ class User { r.isGroup = this.isGroup; r.isCourse = this.isCourse; r.isPrivate = this.isPrivate; + r.isChat = this.isChat; r.subscribed = this.subscribed; r.pending = this.pending; return r; diff --git a/index.js b/index.js index a0987b7..77ed9ef 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,7 @@ const profileRoute = require('./routes/profile.js'); const postRoute = require('./routes/post.js'); const paymentsRoute = require('./routes/payments.js'); const subsplashRoute = require('./routes/subsplash.js'); +const bibleRoute = require('./routes/bible.js'); DB.getDB.then((DB) => { @@ -256,9 +257,11 @@ DB.getDB.then((DB) => { app.use('/user', sessionChecker, profileRoute); app.use('/post', sessionChecker, postRoute); app.use('/payments', sessionChecker, paymentsRoute); + app.use('/bible', sessionChecker, bibleRoute); //Public Routes app.use('/subsplash', subsplashRoute); + // route for handling 404 requests(unavailable routes) app.use(function (req, res, next) { diff --git a/routes/bible.js b/routes/bible.js new file mode 100644 index 0000000..e33c07a --- /dev/null +++ b/routes/bible.js @@ -0,0 +1,70 @@ +const axios = require('axios'); +var express = require('express') +var router = express.Router() +const DB = require("./../mongoDB.js"); + +const fetchAPI = async (path) => { + baseUrl = "https://api.scripture.api.bible/v1/" + let detailHtml = await axios.get(baseUrl + path, {headers:{'api-key':'8b43472a173a39e04cd868fd4848ed75'}}).catch(console.error); + return detailHtml?.data; +} + +const defaultBibleId = "592420522e16049f-01"; + +//getMedia('y42zyf3').then(console.log) +DB.getDB.then((DB) => { + router.get("", async (req, res) => { + const bibles = await fetchAPI('bibles'); + return res.json(bibles); + }); + + router.get("/books", async (req, res) => { + const bibleId = req.query.bibleId || defaultBibleId; + const bibles = await fetchAPI('bibles/' + bibleId +"/books"); + return res.json(bibles); + }); + + router.get("/books", async (req, res) => { + const bibleId = req.query.bibleId || defaultBibleId; + const bibles = await fetchAPI('bibles/' + bibleId +"/books"); + return res.json(bibles); + }); + + router.get("/books/:bookId", async (req, res) => { + const bookId = req.params.bookId; + const bibles = await fetchAPI('bibles/' + bibleId +"/books/" + bookId); + return res.json(bibles); + }); + + router.get("/books/:bookId/chapters", async (req, res) => { + const bookId = req.params.bookId; + const bibleId = req.query.bibleId || defaultBibleId; + const bibles = await fetchAPI('bibles/' + bibleId +"/books/" + bookId + "/chapters"); + return res.json(bibles); + }); + + router.get("/chapters/:chapterId", async (req, res) => { + const chapterId = req.params.chapterId; + const bibleId = req.query.bibleId || defaultBibleId; + const bibles = await fetchAPI('bibles/' + bibleId + "/chapters/" + chapterId); + return res.json(bibles); + }); + + router.get("/chapters/:chapterId/verses", async (req, res) => { + const chapterId = req.params.chapterId; + const bibleId = req.query.bibleId || defaultBibleId; + const bibles = await fetchAPI('bibles/' + bibleId + "/chapters/" + chapterId + "/verses"); + return res.json(bibles); + }); + + router.get("/search", async (req, res) => { + const query = req.query.query; + const limit = req.query.limit || 10; + const bibleId = req.query.bibleId || defaultBibleId; + const bibles = await fetchAPI('bibles/' + bibleId + "/search?query=" + query + "&limit=" + limit); + return res.json(bibles); + }); + +}); + +module.exports = router \ No newline at end of file