diff options
Diffstat (limited to 'bucky')
| -rw-r--r-- | bucky/app/bucky.js | 20 | ||||
| -rw-r--r-- | bucky/app/router.js | 15 | ||||
| -rw-r--r-- | bucky/db/index.js | 35 |
3 files changed, 68 insertions, 2 deletions
diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js index cf74ec2..1b14f43 100644 --- a/bucky/app/bucky.js +++ b/bucky/app/bucky.js @@ -201,7 +201,25 @@ var bucky = module.exports = { res.thread.set('settings', settings) res.thread.save().then( () => next() ) }, - + ensureInterestedUsers: function(req, res, next){ + // given a thread, find people who might be interested in it + // - other people who have been in threads with you + // - other people who have posted on the keyword + // for now though, just show the last 20 people who have logged in.. + db.getLastlog(21).then( (users) => { + res.interestedUsers = users + next() + }).catch( () => { + res.interestedUsers = [] + next() + }) + }, + ensureThreadUsers: function(req, res, next) { + db.getThreadUsers(res.thread.get('id')).then(thread_users => { + res.thread_users = thread_users + next() + }) + }, destroyThread: function (req, res, next) { console.log(">>> destroying thread", res.thread.get('id')) var commentPromises = res.comments.map((comment) => { diff --git a/bucky/app/router.js b/bucky/app/router.js index 7e13cd6..cb90350 100644 --- a/bucky/app/router.js +++ b/bucky/app/router.js @@ -97,6 +97,7 @@ module.exports = function(app){ bucky.ensureLastlog, middleware.ensureAuthenticated, bucky.ensureLatestThreads, + bucky.ensureThreadsPrivacy, bucky.ensureCommentCountsForThreads, bucky.ensureFileCountsForThreads, bucky.ensureKeywordsForThreads, @@ -113,6 +114,7 @@ module.exports = function(app){ bucky.ensureLastlog, middleware.ensureAuthenticated, bucky.ensureThreadsForKeyword, + bucky.ensureThreadsPrivacy, bucky.ensureCommentCountsForThreads, bucky.ensureFileCountsForThreads, bucky.ensureKeywordsForThreads, @@ -125,6 +127,17 @@ module.exports = function(app){ lastlog: res.lastlog, }) }) + // app.get("/api/thread/:id/interested", + // middleware.ensureAuthenticated, + // bucky.ensureThread, + // bucky.ensureThreadPrivacy, + // bucky.ensureInterestedUsers, + // // bucky.ensureThreadUsers, + // function(req, res){ + // res.json({ + // interestedUsers: res.interestedUsers, + // }) + // }) app.get("/api/thread/:id", middleware.ensureAuthenticated, bucky.ensureThread, @@ -132,11 +145,13 @@ module.exports = function(app){ bucky.ensureKeywordForThread, bucky.ensureCommentsForThread, bucky.ensureFilesForThread, + // bucky.ensureThreadUsers, bucky.prepareThread, bucky.bumpLastSeen, function(req, res){ res.json({ thread: res.thread, + thread_users: res.thread, comments: res.comments, files: res.files, keyword: res.keyword, diff --git a/bucky/db/index.js b/bucky/db/index.js index 8715125..36e5c93 100644 --- a/bucky/db/index.js +++ b/bucky/db/index.js @@ -15,6 +15,10 @@ var Thread = db.Thread = bookshelf.Model.extend({ tableName: 'threads', hasTimestamps: false, }) +var ThreadUser = db.ThreadUser = bookshelf.Model.extend({ + tableName: 'thread_users', + hasTimestamps: false, +}) var Comment = db.Comment = bookshelf.Model.extend({ tableName: 'comments', hasTimestamps: false, @@ -50,11 +54,17 @@ db.getUser = function(id) { var model = new User({'id': id}) return model.fetch() } +db.getUsersById = function(ids){ + return User.where("id", "in", ids).fetchAll() +} +db.getUsernamesById = function(ids){ + return User.column("id").column("username").where("id", "in", ids).fetchAll() +} db.getUserByUsername = function(username) { return new User({'username': username}).fetch() } db.getLastlog = function(limit){ - return knex.column('username').column('lastseen').select().from('users').orderBy('lastseen', 'desc').limit(limit || 10) + return knex.column('id').column('username').column('lastseen').select().from('users').orderBy('lastseen', 'desc').limit(limit || 10) } /* THREADS */ @@ -83,6 +93,29 @@ db.updateThread = function(data){ db.destroyThread = function(id){ } +/* THREAD USERS (user privacy) */ +db.addUserToThread = function(user_id, thread_id){ + return new ThreadUser({ user_id, thread_id }).save() +} +db.removeUserFromThread = function(user_id, thread_id){ + return new ThreadUser({ user_id, thread_id }).destroy() +} +db.getThreadUserIds = function(thread_id){ + return ThreadUser.column("user_id").query("where", "thread_id", "=", thread_id).fetch() +} +db.getThreadUsers = function(thread_id){ + return db.getThreadUserIds(thread_id).then(thread_users => { + if (! thread_users.length) return [] + var user_ids = thread_users.pluck("user_id") + return db.getUsernamesById(user_ids).then(usernames => { + return usernames + }) + }) +} +db.getUserThreadIds = function(user_id){ + return ThreadUser.query("where", "user_id", "=", user_id).fetch() +} + /* FILES */ db.getFilesForThread = function (id){ |
