summaryrefslogtreecommitdiff
path: root/bucky
diff options
context:
space:
mode:
Diffstat (limited to 'bucky')
-rw-r--r--bucky/app/bucky.js69
-rw-r--r--bucky/db/index.js25
2 files changed, 79 insertions, 15 deletions
diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js
index 86982f8..a2f677a 100644
--- a/bucky/app/bucky.js
+++ b/bucky/app/bucky.js
@@ -75,23 +75,62 @@ var bucky = (module.exports = {
offset: req.query?.offset || 0,
};
query.has_query = query.thread || query.keyword || query.username;
- Promise.all([
- db.getHootstreamFiles(query),
- db.getHootstreamComments(query),
- ]).then(([files, comments]) => {
- db.getHootstreamThreads({ files, comments }).then((threads) => {
- res.query = query;
- res.files = files;
- res.comments = comments.map((comment) => {
- comment.comment = comment.comment.toString();
- return comment;
+
+ if (query.keyword) {
+ db.getThreadsForKeyword(query.keyword).then((threads) =>
+ Promise.all([
+ db.getHootstreamSomeFilesForThreads(threads, 3),
+ db.getHootstreamSomeCommentsForThreads(threads, 1),
+ ]).then(([files, comments]) => {
+ res.query = query;
+ res.files = files;
+ res.comments = comments.map((comment) => {
+ comment.set("comment", comment.get("comment").toString());
+ return comment;
+ });
+ res.threads = Array.from(threads);
+ res.threads_ids = threads.pluck("id").sort();
+ res.keywords = _.uniq(threads.pluck("keyword"));
+ next();
+ })
+ );
+ } else if (query.thread) {
+ db.getThread(query.thread).then((thread) =>
+ Promise.all([
+ db.getHootstreamAllFilesForThread(thread),
+ db.getHootstreamAllCommentsForThread(thread),
+ ]).then(([files, comments]) => {
+ res.query = query;
+ res.files = files;
+ res.comments = comments.map((comment) => {
+ comment.set("comment", comment.get("comment").toString());
+ return comment;
+ });
+ res.threads = [thread];
+ res.threads_ids = [query.thread];
+ res.keywords = [thread.get("keyword")];
+ next();
+ })
+ );
+ } else {
+ Promise.all([
+ db.getHootstreamFiles(query),
+ db.getHootstreamComments(query),
+ ]).then(([files, comments]) => {
+ db.getHootstreamThreads({ files, comments }).then((threads) => {
+ res.query = query;
+ res.files = files;
+ res.comments = comments.map((comment) => {
+ comment.comment = comment.comment.toString();
+ return comment;
+ });
+ res.threads = threads;
+ res.threads_ids = res.threads.pluck("id").sort();
+ res.keywords = _.uniq(res.threads.pluck("keyword"));
+ next();
});
- res.threads = threads;
- res.threads_ids = res.threads.pluck("id").sort();
- res.keywords = _.uniq(res.threads.pluck("keyword"));
- next();
});
- });
+ }
},
ensureLastlog: function (req, res, next) {
db.getLastlog(5).then(function (lastlog) {
diff --git a/bucky/db/index.js b/bucky/db/index.js
index 2e6deab..57b9c1c 100644
--- a/bucky/db/index.js
+++ b/bucky/db/index.js
@@ -167,6 +167,31 @@ db.getHootstreamThreads = ({ files, comments }) =>
)
)
).fetchAll();
+db.getHootstreamAllCommentsForThread = (thread) =>
+ Comment.where((builder) => builder.where("thread", thread.id)).fetchAll();
+db.getHootstreamAllFilesForThread = (thread) =>
+ File.where((builder) => builder.where("thread", thread.id)).fetchAll();
+
+db.getHootstreamSomeFilesForThreads = (threads, count = 1) =>
+ Promise.all(
+ threads.map((thread) =>
+ File.query((builder) =>
+ builder.where("thread", thread.id).orderBy("date", "desc").limit(count)
+ ).fetchAll()
+ )
+ ).then((results) =>
+ results.reduce((list, result) => list.concat(Array.from(result)), [])
+ );
+db.getHootstreamSomeCommentsForThreads = (threads, count = 1) =>
+ Promise.all(
+ threads.map((thread) =>
+ Comment.query((builder) =>
+ builder.where("thread", thread.id).orderBy("date", "desc").limit(count)
+ ).fetchAll()
+ )
+ ).then((results) =>
+ results.reduce((list, result) => list.concat(Array.from(result)), [])
+ );
/* THREADS */