summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-01-11 13:55:55 +0100
committerJules Laplace <julescarbon@gmail.com>2018-01-11 13:55:55 +0100
commitb3ba48a22ce966e62cfad1671e4ee6f4357bb69f (patch)
tree4c670b51c4f43ebf6f857b5999c2bc0f3f0e807c
parent2e9e542bfb81c3aff407801e3ae3f08eee0fb3d9 (diff)
move file api, debug flag on url to show ids
-rw-r--r--bucky/app/api.js20
-rw-r--r--bucky/app/bucky.js30
-rw-r--r--bucky/db/index.js21
-rw-r--r--public/assets/js/index.js1
-rw-r--r--public/assets/js/lib/views/details/comments.js3
-rw-r--r--public/assets/js/lib/views/details/files.js8
-rw-r--r--public/assets/js/lib/views/details/gallery.js10
-rw-r--r--public/assets/js/lib/views/index/threadbox.js15
-rw-r--r--views/partials/comments.ejs2
-rw-r--r--views/partials/files.ejs2
-rw-r--r--views/partials/gallery.ejs2
-rw-r--r--views/partials/threads.ejs2
12 files changed, 104 insertions, 12 deletions
diff --git a/bucky/app/api.js b/bucky/app/api.js
index a8149d0..44c4982 100644
--- a/bucky/app/api.js
+++ b/bucky/app/api.js
@@ -169,6 +169,26 @@ function route (app){
function(req, res){
res.json({ comment: res.comment })
})
+ // move a file to another thread
+ app.get("/api/file/:id/move/:thread_id",
+ middleware.ensureAuthenticated,
+ bucky.checkIsAdmin,
+ bucky.ensureFile,
+ bucky.ensureThreadById,
+ bucky.moveFile,
+ function(req, res){
+ res.json({ file: res.file })
+ })
+ // move a comment to another thread
+ app.get("/api/comment/:id/move/:thread_id",
+ middleware.ensureAuthenticated,
+ bucky.checkIsAdmin,
+ bucky.ensureComment,
+ bucky.ensureThreadById,
+ bucky.moveComment,
+ function(req, res){
+ res.json({ comment: res.comment })
+ })
// delete a comment
app.delete("/api/comment/:id",
middleware.ensureAuthenticated,
diff --git a/bucky/app/bucky.js b/bucky/app/bucky.js
index 84bbe70..77e1667 100644
--- a/bucky/app/bucky.js
+++ b/bucky/app/bucky.js
@@ -113,6 +113,21 @@ var bucky = module.exports = {
}
})
},
+ ensureThreadById: function (req, res, next){
+ var id = req.params.thread_id.replace(/\D/g, "")
+ if (! id) {
+ return res.sendStatus(404)
+ }
+ db.getThread(id).then(function(thread){
+ if (thread) {
+ res.thread = thread
+ next()
+ }
+ else {
+ res.sendStatus(404)
+ }
+ })
+ },
prepareThread: function (req, res, next){
var thread = res.thread
if (thread) {
@@ -380,6 +395,14 @@ var bucky = module.exports = {
res.sendStatus(500)
})
},
+ moveComment: function(req, res, next){
+ res.comment.set('thread', res.thread.get('id'))
+ res.comment.save().then(() => {
+ next()
+ }).catch(err => {
+ res.sendStatus(500)
+ })
+ },
destroyComment: function(req, res, next){
res.comment.destroy().then(() => {
next()
@@ -451,6 +474,13 @@ var bucky = module.exports = {
console.log(err)
})
},
+ moveFile: function(req, res, next){
+ db.moveFile(res.file, res.thread.get('id')).then(() => {
+ next()
+ }).catch(err => {
+ res.sendStatus(500)
+ })
+ },
destroyFile: function(req,res,next){
var filePromises = db.destroyFiles([res.file])
Promise.all(filePromises).then( () => next() )
diff --git a/bucky/db/index.js b/bucky/db/index.js
index b10e3d8..350f2a4 100644
--- a/bucky/db/index.js
+++ b/bucky/db/index.js
@@ -152,6 +152,27 @@ db.createFile = function(data){
}
db.destroyFile = function(id){
}
+db.moveFile = function(file, thread_id){
+ var s3client = upload.client()
+ var srcPath = '/bucky/data/' + file.get('thread') + '/' + file.get('filename')
+ var destPath = '/bucky/data/' + thread_id + '/' + file.get('filename')
+ var copyPromise = new Promise((resolve, reject) => {
+ s3client.copy(srcPath, destPath).on('response', function(res){
+ s3client.deleteFile(srcPath, function(err, res){
+ if (err) {
+ return reject(err)
+ }
+ file.set('thread', thread_id)
+ file.save().then(() => {
+ resolve()
+ }).catch(err => {
+ reject(err)
+ })
+ })
+ }).end()
+ })
+ return copyPromise
+}
db.destroyFiles = function(files){
var s3client = upload.client()
var rmPromises = files.map((file) => {
diff --git a/public/assets/js/index.js b/public/assets/js/index.js
index bbb7fd9..06f2158 100644
--- a/public/assets/js/index.js
+++ b/public/assets/js/index.js
@@ -2,6 +2,7 @@ var app = (function(){
var app = {}
app.init = function(){
+ app.debug = !! window.location.search.match('debug')
app.router = new SiteRouter ()
app.view = null
diff --git a/public/assets/js/lib/views/details/comments.js b/public/assets/js/lib/views/details/comments.js
index 8894676..967204e 100644
--- a/public/assets/js/lib/views/details/comments.js
+++ b/public/assets/js/lib/views/details/comments.js
@@ -39,6 +39,9 @@ var CommentsView = FormView.extend({
if (auth.user.username !== comment.username) {
$t.find(".edit-links").remove()
}
+ if (app.debug) {
+ $t.find('.date').prepend('#' + comment.id + ' ')
+ }
return $t
},
diff --git a/public/assets/js/lib/views/details/files.js b/public/assets/js/lib/views/details/files.js
index b68d42c..a074e2a 100644
--- a/public/assets/js/lib/views/details/files.js
+++ b/public/assets/js/lib/views/details/files.js
@@ -82,7 +82,8 @@ var FilesView = FormView.extend({
var date_class = carbon_date(file.date)
var link = make_link(file)
- var t = this.template.replace(/{{username}}/g, file.username)
+ var t = this.template.replace(/{{id}}/g, file.id)
+ .replace(/{{username}}/g, file.username)
.replace(/{{link}}/g, link)
.replace(/{{filename}}/g, file.filename)
.replace(/{{date_class}}/g, date_class)
@@ -91,7 +92,10 @@ var FilesView = FormView.extend({
.replace(/{{size_class}}/g, size[0])
.replace(/{{size}}/g, size[1])
var $t = $(t)
- return t
+ if (app.debug) {
+ $t.find('.user').append(' #' + file.id)
+ }
+ return $t
},
prependFile: function(file){
diff --git a/public/assets/js/lib/views/details/gallery.js b/public/assets/js/lib/views/details/gallery.js
index da6e97f..de9a6a4 100644
--- a/public/assets/js/lib/views/details/gallery.js
+++ b/public/assets/js/lib/views/details/gallery.js
@@ -31,16 +31,20 @@ var GalleryView = View.extend({
.replace(/{{thumb}}/g, thumb)
.replace(/{{link}}/g, link)
.replace(/{{age}}/g, age)
- return t
+ var $t = $(t)
+ if (app.debug) {
+ $t.find('.date').append(', #' + file.id)
+ }
+ return $t
},
prependFile: function(file){
- var $el = $( this.parse(file) )
+ var $el = this.parse(file)
this.$el.prepend($el)
},
appendFile: function(file){
- var $el = $( this.parse(file) )
+ var $el = this.parse(file)
this.$el.append($el)
},
diff --git a/public/assets/js/lib/views/index/threadbox.js b/public/assets/js/lib/views/index/threadbox.js
index 70dc961..01d1de4 100644
--- a/public/assets/js/lib/views/index/threadbox.js
+++ b/public/assets/js/lib/views/index/threadbox.js
@@ -75,7 +75,16 @@ var ThreadBox = View.extend({
.replace(/{{show_files}}/g, thread.file_count == 0 ? "hidden" : "")
.replace(/{{size_class}}/g, size[0] )
.replace(/{{color}}/g, thread.color || "blue" )
- return t
+ var $t = $(t)
+ if (app.debug) {
+ var $title = $t.find('.title a')
+ var href = $title.attr('href')
+ $title.attr('href', href + '?debug')
+ }
+ if (app.debug) {
+ $t.find('.date').append(' #' + thread.id)
+ }
+ return $t
},
parseKeyword: function(keyword){
@@ -85,7 +94,7 @@ var ThreadBox = View.extend({
},
prependThread: function(thread){
- var $row = $( this.parse(thread) )
+ var $row = this.parse(thread)
this.$el.prepend($row)
},
@@ -99,7 +108,7 @@ var ThreadBox = View.extend({
appendThread: function(thread){
thread.appended = true
- var $row = $( this.parse(thread) )
+ var $row = this.parse(thread)
if (thread.first) $row.addClass('first')
if (thread.last) $row.addClass('last')
this.$el.append($row)
diff --git a/views/partials/comments.ejs b/views/partials/comments.ejs
index 17eb6a5..87c9dec 100644
--- a/views/partials/comments.ejs
+++ b/views/partials/comments.ejs
@@ -8,7 +8,7 @@
<td colspan="2" class="comment">
<div class="body">
<span class="contents">{{comment}}</span>
- <div class="date">
+ <div class="date" title="{{id}}">
{{date}} {{time}}<br>
</div>
<div class="edit-links">
diff --git a/views/partials/files.ejs b/views/partials/files.ejs
index 72c39af..ec61fe2 100644
--- a/views/partials/files.ejs
+++ b/views/partials/files.ejs
@@ -2,7 +2,7 @@
<script class="template" type="text/html">
<tr class="file">
<td>
- <a href="{{link}}" class="file">{{filename}}</a>
+ <a href="{{link}}" title="{{id}}" class="file">{{filename}}</a>
</td>
<td class="{{date_class}}">
{{date}}
diff --git a/views/partials/gallery.ejs b/views/partials/gallery.ejs
index 7115d4d..053d59c 100644
--- a/views/partials/gallery.ejs
+++ b/views/partials/gallery.ejs
@@ -2,7 +2,7 @@
<script class="template" type="text/html">
<div>
<a href="{{link}}"><img class="thumb" src="{{thumb}}"></a>
- <br>(<a href="/profile/{{username}}">{{username}}</a>, {{age}})
+ <br>(<a href="/profile/{{username}}">{{username}}</a>, <span class='date'>{{age}}</span>)
</div>
</script>
</div> \ No newline at end of file
diff --git a/views/partials/threads.ejs b/views/partials/threads.ejs
index e429219..4da415b 100644
--- a/views/partials/threads.ejs
+++ b/views/partials/threads.ejs
@@ -34,7 +34,7 @@
<td class="{{color}} title">
<a href="/details/{{id}}">{{title}}</a>
</td>
- <td class="{{date_class}}">
+ <td class="date {{date_class}}">
{{date}} <small>{{time}}</small>
</td>
<td class="{{views_class}}">