diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-12-08 01:35:26 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-12-08 01:35:26 +0100 |
| commit | e4e0cf21a31b74d5ee1e6d45b343ea60ed44f372 (patch) | |
| tree | d6e3d14c4b5ddfed5d8bd036a5eba29af505553b /lib | |
| parent | 3a4f027ec05aa5fdf4098ceb0dab09f69c5e0b8b (diff) | |
hootbox stuff
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/db/index.js | 7 | ||||
| -rw-r--r-- | lib/index.js | 6 | ||||
| -rw-r--r-- | lib/router.js | 19 | ||||
| -rw-r--r-- | lib/search/index.js | 102 |
4 files changed, 130 insertions, 4 deletions
diff --git a/lib/db/index.js b/lib/db/index.js index 6906b27..f376308 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -62,7 +62,7 @@ db.getLastlog = function(limit){ db.getLatestThreads = function () { return Thread.query(function(qb){ - qb.orderBy("id", "desc").limit(50) + qb.orderBy("lastmodified", "desc").limit(50) }).fetchAll() } db.getThreadsForKeyword = function (keyword) { @@ -74,6 +74,7 @@ db.getThread = function (id) { return Thread.query("where", "id", "=", id).fetch() } db.createThread = function(data){ + return new db.Thread(data).save() } db.updateThread = function(data){ } @@ -92,6 +93,7 @@ db.getFileSizes = function(ids){ return knex.column('thread').sum('size as size').select().from('files').where('thread', 'in', ids).groupBy('thread') } db.createFile = function(data){ + return new db.File(data).save() } db.removeFile = function(id){ } @@ -119,6 +121,7 @@ db.getCommentCounts = function(ids){ return knex.column('thread').count('* as count').select().from('comments').where('thread', 'in', ids).groupBy('thread') } db.createComment = function(data){ + return new db.Comment(data).save() } db.updateComment = function(data){ } @@ -135,6 +138,7 @@ db.getKeyword = function (keyword) { return Keyword.query("where", "keyword", "=", keyword).fetch() } db.createKeyword = function(data){ + return new db.Keyword(data).save() } db.updateKeyword = function(data){ } @@ -175,6 +179,7 @@ db.getMessage = function (id){ }) } db.createMessage = function(data){ + return new db.Message(data).save() } db.updateMessage = function(data){ } diff --git a/lib/index.js b/lib/index.js index ad97526..5aef342 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,9 +37,9 @@ site.init = function(){ type: 'mongodb', host: 'localhost', port: 27017, - dbName: 'sessionDb', + dbName: 'buckySessionDb', collectionName: 'sessions', - timeout: 10000 + timeout: 10000, }), resave: true, saveUninitialized: false, @@ -50,7 +50,7 @@ site.init = function(){ app.use(passport.initialize()) app.use(passport.session()) - server = http.createServer(app).listen(5000, function () { + server = http.createServer(app).listen(process.env.PORT || 5000, function () { console.log('Bucky listening at http://5.k:%s', server.address().port) }) diff --git a/lib/router.js b/lib/router.js index 4451a13..c08037e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2,6 +2,7 @@ var auth = require('./auth') var middleware = require('./middleware') var fortune = require('./fortune') var bucky = require('./bucky') +var db = require('./db') var util = require('./util') module.exports = function(app){ @@ -66,6 +67,24 @@ module.exports = function(app){ }) app.post("/api/thread/:id/comment", middleware.ensureAuthenticated, + bucky.ensureThread, + function(req, res){ + if (!req.params.id) return res.sendStatus(500) + var comment = { + thread: req.params.id, + parent_id: req.body.parent_id || -1, + username: req.user.get('username'), + date: Math.round(+(new Date) / 1000), + comment: req.body.comment, + hidden: false, + } + db.createComment(comment).then(function(c){ + res.json(comment) + }) + }) + app.post("/api/thread/:id/file", + middleware.ensureAuthenticated, + bucky.ensureThread, function(req, res){ // add comments and files }) diff --git a/lib/search/index.js b/lib/search/index.js new file mode 100644 index 0000000..8d209e6 --- /dev/null +++ b/lib/search/index.js @@ -0,0 +1,102 @@ +var db = require('../db') +var bdb_lib = require('berkeleydb') +var bdb = new bdb_lib.Db() +bdb.open('search.db') + +var wordRegexp = new RegExp("(\W+)"); +var wordBoundaryRegexp = new RegExp("\W"); +function parse_terms (s) { + return s.toLowerCase().split(wordRegexp).filter((term) => { + if (! term.match(wordBoundaryRegexp)) { + return true + } + return false + }) +} +function cmp (a,b){ return (a<b)?a:(a===b)?0:1 } + +var STOPWORDS = new Set( + "a about above across adj after again against all almost alone along also " + + "although always am among an and another any anybody anyone anything anywhere " + + "apart are around as aside at away be because been before behind being below " + + "besides between beyond both but by can cannot could did do does doing done " + + "down downwards during each either else enough etc even ever every everybody " + + "everyone except far few for forth from get gets got had hardly has have having " + + "her here herself him himself his how however i if in indeed instead into inward " + + "is it its itself just kept many maybe might mine more most mostly much must " + + "myself near neither next no nobody none nor not nothing nowhere of off often on " + + "only onto or other others ought our ours out outside over own p per please plus " + + "pp quite rather really said seem self selves several shall she should since so " + + "some somebody somewhat still such than that the their theirs them themselves " + + "then there therefore these they this thorough thoroughly those through thus to " + + "together too toward towards under until up upon v very was well were what " + + "whatever when whenever where whether which while who whom whose will with" + + "within without would yet young your yourself s".split(" ") +); + +function find_term(term) { + bdb.get(term) + +} + +function search (query, start, limit) { + if (!query) return + start = start || 0; + limit = limit || 10; + var scores = {}; + var terms = parse_terms($query); + var i = 0 + var total + var to_display = limit + var threads = {} + var comment_ids = [] + var file_ids = [] + var results = [] + + terms.forEach((term) => { + if (STOPWORDS.has(term)) return; + var results = find_term(term); + if (!results) return; + results.forEach((result) => { + var score = scores[result.thread] = scores[result.thread] || { count: 0, strength: 0 } + score.thread = score.thread || result.thread + score.comment = score.comment || result.comment + score.file = score.file || result.file + score.strength += result.strength + score.count += 1 + }) + }) + total = Object.keys(scores).length + Object.values(scores).sort((a,b) => { + if (b.count !== a.count) { + return cmp(b.count, a.count) + } + return cmp(b.strength * b.count, a.strength * a.count) + }).some((match) => { + if (i++ < start) return false + if (to_display-- === 0) return true + results.push(match) + thread_ids.push(match.thread) + if (match.comment) comment_ids.push(match.comment) + if (match.file) file_ids.push(match.file) + return false + }) + + db.storeQuery(query, total) + + my $files = $self->files_by_id($files_to_get); + my $comments = $self->comments_by_id($comments_to_get); + $self->log_query($query, $total); + return { + start => $start + $limit, + limit => $limit, + total => $total, + results => $results, + threads => $threads, + comments => $comments, + files => $files, + terms => $terms, + }; +} + +module.exports = { search: search } |
