summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2017-12-15 02:43:51 +0100
committerJules Laplace <julescarbon@gmail.com>2017-12-15 02:43:51 +0100
commit1466563673ffe6c41ec9a18bf98565b8562ee4f3 (patch)
tree1eb576380877d3231910ccbb634f33906210ab3f
parent0541e7d7457d646dceca375b7fa6e1f382232772 (diff)
thread_users hackery
-rw-r--r--bucky/app/bucky.js20
-rw-r--r--bucky/app/router.js15
-rw-r--r--bucky/db/index.js35
-rw-r--r--public/assets/js/lib/views/details/settings.js33
-rw-r--r--public/assets/js/util/format.js2
-rw-r--r--views/partials/settings.ejs11
6 files changed, 111 insertions, 5 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){
diff --git a/public/assets/js/lib/views/details/settings.js b/public/assets/js/lib/views/details/settings.js
index b702908..1124826 100644
--- a/public/assets/js/lib/views/details/settings.js
+++ b/public/assets/js/lib/views/details/settings.js
@@ -16,6 +16,7 @@ var ThreadSettingsForm = FormView.extend({
initialize: function(){
this.__super__.initialize.call(this)
this.template = this.$(".template").html()
+ this.allowedTemplate = this.$(".allowedTemplate").html()
},
populate: function(){
@@ -47,6 +48,13 @@ var ThreadSettingsForm = FormView.extend({
})
$color.val(thread.color || keyword ? keyword.color : "")
+ this.fetchKeywords()
+// this.fetchAllowedUsers(thread)
+
+ $("body").removeClass("loading")
+ },
+
+ fetchKeywords: function(){
$.get('/api/keywords', function(data){
var $keyword = this.$('[name=keyword]')
data.keywords
@@ -60,10 +68,31 @@ var ThreadSettingsForm = FormView.extend({
})
$keyword.val(thread.keyword)
}.bind(this))
-
- $("body").removeClass("loading")
},
+// fetchAllowedUsers: function(thread){
+// var usernameRegexp = new RegExp('{{username}}', g)
+// $.get('/api/thread/' + thread.id + '/interested', function(data){
+// var $allowed = this.$(".allowed")
+// var tmpl = this.allowedTemplate
+// // make a lookup of existing users
+// var allowed = {}
+// thread.allowed.split(" ").forEach((username) => {
+//
+// })
+// // build the ui
+// data.interestedUsers
+// .map( (a) => a.username)
+// .sort( (a,b) => a < b ? -1 : a === b ? 0 : 1 )
+// .forEach((username) => {
+// var t = tmpl.replace(usernameRegexp, "")
+// .replace('{{checked}}',
+// $keyword.append(option)
+// })
+// $keyword.val(thread.keyword)
+// }.bind(this))
+// },
+
validate: function(){
var errors = []
var title = $("[name=title]").val()
diff --git a/public/assets/js/util/format.js b/public/assets/js/util/format.js
index c86bec3..339e4f6 100644
--- a/public/assets/js/util/format.js
+++ b/public/assets/js/util/format.js
@@ -170,7 +170,7 @@ function get_age (t) {
if (age < 60) { return r(age) + "m" }
m = r(age % 60)
age /= 60
- if (m > 0 && age < 2) { return m + "m" + r(age) + "h" }
+ if (m > 0 && age < 2) { return r(age) + "h" + m + "m" }
if (age < 24) { return r(age) + "h" }
age /= 24
if (age < 7) { return r(age) + "d" }
diff --git a/views/partials/settings.ejs b/views/partials/settings.ejs
index b174546..3964429 100644
--- a/views/partials/settings.ejs
+++ b/views/partials/settings.ejs
@@ -44,6 +44,17 @@
<input id="thread_shorturls" name="shorturls" value="1" type="checkbox">
</div>
<div>
+ <label for="thread_privacy">private?</label>
+ <input id="thread_privacy" name="privacy" value="0" type="hidden">
+ <input id="thread_privacy" name="privacy" value="1" type="checkbox">
+ </div>
+ <div class="allowed">
+ <script type="text/html" class="allowedTemplate">
+ <label for="user_{{username}}">{{username}}</label>
+ <input id="user_{{username}}" name="allowed" value="{{username}}" type="checkbox">
+ </script>
+ </div>
+ <div>
<label></label>
<input id="thread_submit" value="Save" type="submit">
</div>