Skip to content

Commit

Permalink
Added serverside full text search
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHllm committed Jul 1, 2024
1 parent 19aff90 commit b01690c
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 22,516 deletions.
13 changes: 2 additions & 11 deletions lib/allNotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,15 @@ var logger = require('../logger')
// var response = require('../response')
var models = require('../models')
var Sequelize = require('sequelize')

function checkViewPermission (req, note) {
if (note.permission === 'private') {
if (!req.isAuthenticated() || note.ownerId !== req.user.id) { return false } else { return true }
} else if (note.permission === 'freely' || note.permission === 'editable' || note.permission === 'limited' || note.permission === 'locked' || note.permission === 'protected') {
if (!req.isAuthenticated()) { return false } else { return true }
} else {
return false
}
}
var utils = require('../utils')

function allNotesGet (req, res, next) {
const invalidNotes = ['favicon.ico', 'status', 'build', 'robots.txt', 'register', 'login']

models.Note.findAll(
{ attributes: ['id', 'alias', 'permission', 'title', [Sequelize.fn('SPLIT_PART', Sequelize.col('content'), '\n', 1), 'content'], 'authorship', 'createdAt', 'updatedAt', 'ownerId', 'lastchangeuserId'] }
).then((notes) => {
const parsedNotes = notes.filter(note => checkViewPermission(req, note))
const parsedNotes = notes.filter(note => utils.getViewPermission(req, note))
.map(note => {
return {
id: note.alias || models.Note.encodeNoteId(note.id),
Expand Down
3 changes: 3 additions & 0 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const historyController = require('./history')
const userController = require('./user')
const noteController = require('./note')
const allNotesController = require('./allNotes')
const searchController = require('./search')
const response = require('./response')
const bodyParser = require('body-parser')
const appRouter = Router()
Expand Down Expand Up @@ -93,4 +94,6 @@ appRouter.get('/:noteId/:action', noteController.noteActions)
// note actions with action id
appRouter.get('/:noteId/:action/:actionId', noteController.noteActions)

appRouter.post('/search', urlencodedParser, searchController.search)

exports.router = appRouter
32 changes: 32 additions & 0 deletions lib/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'
// search

var models = require('../models')
var Sequelize = require('sequelize')
var response = require('../response')
var models = require('../models')
var utils = require('../utils')

function search (req, res, next) {
if (!req.isAuthenticated()) return response.errorForbidden(req, res)
if (typeof req.body === 'undefined') return response.errorBadRequest(req, res)
if (typeof req.body.query === 'undefined') return response.errorBadRequest(req, res)

const query = req.body.query

models.Note.findAll(
{
attributes: ['id', 'alias', 'permission', 'title', 'content', 'authorship',
'createdAt', 'updatedAt', 'ownerId', 'lastchangeuserId', [Sequelize.fn('to_tsvector', Sequelize.col('content')), 'vec']],
where: Sequelize.and(
Sequelize.literal(`to_tsvector("content") @@ to_tsquery($query)`),
),
bind: { query }
}
).then((notes) => {
const parsedNotes = notes.filter(note => utils.getViewPermission(req, note)).map((note) => { return note.alias || models.Note.encodeNoteId(note.id) })
res.json({ allNotes: parsedNotes })
})
}

exports.search = search
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ exports.markdownParser = bodyParser.text({
type: ['text/plain', 'text/markdown'],
limit: 1024 * 1024 * 10 // 10 mb
})

exports.getViewPermission = function (req, note) {
if (note.permission === 'private') {
if (!req.isAuthenticated() || note.ownerId !== req.user.id) { return false } else { return true }
} else if (note.permission === 'freely' || note.permission === 'editable' || note.permission === 'limited' || note.permission === 'locked' || note.permission === 'protected') {
if (!req.isAuthenticated()) { return false } else { return true }
} else {
return false
}
}
23 changes: 22 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,26 @@
"Refresh notes": "Refresh notes",
"All Notes": "All Notes",
"No Notes": "No Notes",
"Hide Untitled": "Hide Untitled"
"Hide Untitled": "Hide Untitled",
"CONNECTED": "CONNECTED",
"ONLINE": "ONLINE",
"OFFLINE": "OFFLINE",
"Anyone can edit": "Anyone can edit",
"Freely": "Freely",
"Signed-in people can edit": "Signed-in people can edit",
"Editable": "Editable",
"Signed-in people can edit (forbid guests)": "Signed-in people can edit (forbid guests)",
"Limited": "Limited",
"Only owner can edit": "Only owner can edit",
"Locked": "Locked",
"Only owner can edit (forbid guests)": "Only owner can edit (forbid guests)",
"Protected": "Protected",
"Only owner can view & edit": "Only owner can view & edit",
"Private": "Private",
"Delete this note": "Delete this note",
"owned this note": "owned this note",
"Expand all": "Expand all",
"Collapse all": "Collapse all",
"Back to top": "Back to top",
"Go to bottom": "Go to bottom"
}
Loading

0 comments on commit b01690c

Please sign in to comment.