Commits

Carbo Kuo committed a5fda6f

Add guid for post

Comments (0)

Files changed (5)

+crypto = require('crypto')
+
+exports.md5 = (data) ->
+  hash = crypto.createHash('md5')
+  hash.update data
+  hash.digest 'hex'

models/post.coffee

 'use continuation'
 mongoose = require '../lib/mongoose'
 translate = require '../lib/translate'
+utils = require '../lib/utils'
 marked = require 'marked'
 
 contentSchema = new mongoose.Schema
   language: String
 
 postSchema = new mongoose.Schema
+  guid:
+    type: String
+    index: true
+    unique: true
   id:
     type: String
     index: true
 postSchema.pre 'save', (next) ->
   if not @postTime?
     @postTime = new Date
+  if not @guid?
+    @guid = utils.md5(@postTime.toString()).substr(0, 10)
   if not @clicks?
     @clicks = 0
   if not @private?

routes/admin.coffee

 exports.editPostPage = (req, res, next) ->
   if not req.session.user?
     return res.redirect '/admin/login'
-  postId = req.params[0]
-  Post.findOne {id: postId}, cont(err, post)
+  postGuid = req.params[0]
+  Post.findOne {guid: postGuid}, cont(err, post)
   return next err if err
   return next new Error('Invalid post id') if not post?
 
 exports.editPost = (req, res, next) ->
   if not req.session.user?
     return res.redirect '/admin/login'
-  postId = req.params[0]
+  postGuid = req.params[0]
   try
-    Post.findOne {id: postId}, obtain(post)
+    Post.findOne {guid: postGuid}, obtain(post)
     throw new Error('Invalid post id') if not post?
     post.modify req.body.post, obtain(savedPost)
     req.session.success = 'Post saved'
-    res.redirect '/admin/edit/' + savedPost.id
+    res.redirect '/admin/edit/' + savedPost.guid
   catch err
     req.session.error = err.toString()
     res.render 'admin/editpost',

views/admin/postslist.jade

     each post in posts
       tr
         td
-          a(href='/admin/edit/' + post.id)= post.id
+          a(href='/admin/edit/' + post.guid)= post.id
         td
           a(href='/' + post.id)= post.contents[0].title
         td= dateFormat(post.postTime, 'yyyy-mm-dd HH:MM:ss')
 block scripts
   script(type='text/javascript')
     var disqus_shortname = 'byvoid';
-    var disqus_identifier = '#{post.id}';
+    var disqus_identifier = '#{post.guid}';
     var disqus_url = location.protocol + '//' + location.host + '/' + disqus_identifier;
     (function() {
     var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;