From 249dd638ec37b8f86bae0fb918f6cb1c0214b8f3 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 17 Dec 2015 11:51:14 +0000 Subject: [PATCH] experimental: update references index from fronted. --- .../ReferencesSearchController.coffee | 35 +++++++++++++++++++ .../ReferencesSearchHandler.coffee | 26 ++++++++++++++ services/web/app/coffee/router.coffee | 19 +++++----- 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchController.coffee create mode 100644 services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee diff --git a/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchController.coffee b/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchController.coffee new file mode 100644 index 0000000000..12b2356fbb --- /dev/null +++ b/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchController.coffee @@ -0,0 +1,35 @@ +logger = require('logger-sharelatex') +ReferencesSearchHandler = require('./ReferencesSearchHandler') +ProjectLocator = require("../Project/ProjectLocator") +settings = require('settings-sharelatex') +U = require('underscore') + + +module.exports = ReferencesSearchController = + + indexFile: (req, res) -> + project_id = req.params.Project_id + user_id = req.session.user._id + doc_id = req.body.docId + logger.log {project_id, user_id, doc_id}, "indexing references" + + if !doc_id + logger.log project_id: project_id, user_id: user_id, "no fileUrl supplied" + return res.send 400 + ProjectLocator.findElement {project_id: project_id, element_id: doc_id, type: 'doc'}, (err, doc) -> + if err? + logger.err {err, project_id, user_id, doc_id}, "error finding element for downloading file" + return res.send 500 + doc_url = ReferencesSearchController._buildDocUrl project_id, doc_id + console.log ">>" + console.log doc, doc_url + console.log "<<" + ReferencesSearchHandler.indexFile user_id, doc_url, (err) -> + if err + logger.err {err, project_id, user_id, doc_id}, "error indexing references file" + return res.send 500 + + res.send 200 + + _buildDocUrl: (project_id, doc_id) -> + "#{settings.apis.web.url}/project/#{project_id}/doc/#{doc_id}" diff --git a/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee b/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee new file mode 100644 index 0000000000..82a46c2c56 --- /dev/null +++ b/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee @@ -0,0 +1,26 @@ +logger = require("logger-sharelatex") +request = require("request") +settings = require("settings-sharelatex") + +oneMinInMs = 60 * 1000 +fiveMinsInMs = oneMinInMs * 5 + + +module.exports = ReferencesSearchHandler = + + indexFile: (user_id, file_url, callback = (err)->) -> + logger.log {user_id, file_url}, "sending index request to references api" + url = "#{settings.apis.references.url}/user/#{user_id}" + request.post { + url: url + json: + referencesUrl: file_url + }, (err, res, result) -> + if err + return callback(err) + if 200 <= res.statusCode < 300 + return callback(null) + else + err = new Error("references api responded with non-success code: #{res.statusCode}") + logger.log {err, user_id, file_url}, "error updating references" + return callback(err) diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index a34864f62b..ae9f4db508 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -37,6 +37,7 @@ RateLimiterMiddlewear = require('./Features/Security/RateLimiterMiddlewear') RealTimeProxyRouter = require('./Features/RealTimeProxy/RealTimeProxyRouter') InactiveProjectController = require("./Features/InactiveData/InactiveProjectController") ContactRouter = require("./Features/Contacts/ContactRouter") +ReferencesSearchController = require('./Features/ReferencesSearch/ReferencesSearchController') logger = require("logger-sharelatex") _ = require("underscore") @@ -46,7 +47,7 @@ module.exports = class Router if !Settings.allowPublicAccess webRouter.all '*', AuthenticationController.requireGlobalLogin - + webRouter.get '/login', UserPagesController.loginPage AuthenticationController.addEndpointToLoginWhitelist '/login' @@ -67,18 +68,18 @@ module.exports = class Router StaticPagesRouter.apply(webRouter, apiRouter) RealTimeProxyRouter.apply(webRouter, apiRouter) ContactRouter.apply(webRouter, apiRouter) - + Modules.applyRouter(webRouter, apiRouter) if Settings.enableSubscriptions webRouter.get '/user/bonus', AuthenticationController.requireLogin(), ReferalMiddleware.getUserReferalId, ReferalController.bonus - + webRouter.get '/blog', BlogController.getIndexPage webRouter.get '/blog/*', BlogController.getPage - + webRouter.get '/user/activate', UserPagesController.activateAccountPage - + webRouter.get '/user/settings', AuthenticationController.requireLogin(), UserPagesController.settingsPage webRouter.post '/user/settings', AuthenticationController.requireLogin(), UserController.updateUserSettings webRouter.post '/user/password/update', AuthenticationController.requireLogin(), UserController.changePassword @@ -158,7 +159,7 @@ module.exports = class Router apiRouter.post '/user/:user_id/update/*', AuthenticationController.httpAuth, TpdsController.mergeUpdate apiRouter.delete '/user/:user_id/update/*', AuthenticationController.httpAuth, TpdsController.deleteUpdate - + apiRouter.post '/project/:project_id/contents/*', AuthenticationController.httpAuth, TpdsController.updateProjectContents apiRouter.delete '/project/:project_id/contents/*', AuthenticationController.httpAuth, TpdsController.deleteProjectContents @@ -167,9 +168,11 @@ module.exports = class Router webRouter.get "/project/:Project_id/messages", SecurityManager.requestCanAccessProject, ChatController.getMessages webRouter.post "/project/:Project_id/messages", SecurityManager.requestCanAccessProject, ChatController.sendMessage - + webRouter.get /learn(\/.*)?/, WikiController.getPage + webRouter.post "/project/:Project_id/references", SecurityManager.requestCanAccessProject, ReferencesSearchController.indexFile + #Admin Stuff webRouter.get '/admin', SecurityManager.requestIsAdmin, AdminController.index webRouter.get '/admin/user', SecurityManager.requestIsAdmin, (req, res)-> res.redirect("/admin/register") #this gets removed by admin-panel addon @@ -188,7 +191,7 @@ module.exports = class Router apiRouter.get '/status', (req,res)-> res.send("websharelatex is up") - + webRouter.get '/health_check', HealthCheckController.check webRouter.get '/health_check/redis', HealthCheckController.checkRedis