Bläddra i källkod

WIP: user page

Sotaro KARASAWA 10 år sedan
förälder
incheckning
4040eeb1eb
7 ändrade filer med 152 tillägg och 75 borttagningar
  1. 15 10
      lib/models/bookmark.js
  2. 31 10
      lib/models/page.js
  3. 16 8
      lib/models/revision.js
  4. 42 12
      lib/routes/page.js
  5. 1 35
      lib/views/page_list.html
  6. 10 0
      lib/views/user_page.html
  7. 37 0
      lib/views/widget/page_list.html

+ 15 - 10
lib/models/bookmark.js

@@ -27,21 +27,26 @@ module.exports = function(crowi) {
     });
   };
 
-  bookmarkSchema.statics.findByUser = function(user, option, callback) {
+  bookmarkSchema.statics.findByUser = function(user, option) {
     var Bookmark = this;
 
     var limit = option.limit || 50;
     var offset = option.skip || 0;
 
-    Bookmark
-      .find({ user: user._id })
-      //.sort('createdAt', -1)
-      .skip(offset)
-      .limit(limit)
-      .exec(function(err, bookmarks) {
-        //debug ('bookmarks', bookmarks);
-        callback(err, bookmarks);
-      });
+    return new Promise(function(resolve, reject) {
+      Bookmark
+        .find({ user: user._id })
+        .sort({createdAt: -1})
+        .skip(offset)
+        .limit(limit)
+        .exec(function(err, bookmarks) {
+          if (err) {
+            return reject(err);
+          }
+
+          return resolve(bookmarks);
+        });
+    });
   };
 
   bookmarkSchema.statics.add = function(page, user) {

+ 31 - 10
lib/models/page.js

@@ -280,7 +280,7 @@ module.exports = function(crowi) {
   pageSchema.statics.findUpdatedList = function(offset, limit, cb) {
     this
       .find({})
-      .sort('updatedAt', -1)
+      .sort({updatedAt: -1})
       .skip(offset)
       .limit(limit)
       .exec(function(err, data) {
@@ -348,25 +348,46 @@ module.exports = function(crowi) {
     });
   };
 
-  pageSchema.statics.findListByPageIds = function(ids, options) {
+  pageSchema.statics.findListByPageIds = function(ids, option) {
   };
 
-  pageSchema.statics.findListByStartWith = function(path, userData, options) {
+  pageSchema.statics.findListByCreator = function(user, option) {
     var Page = this;
+    var limit = option.limit || 50;
+    var offset = option.skip || 0;
 
-    if (!options) {
-      options = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
+    return new Promise(function(resolve, reject) {
+      Page
+        .find({ creator: user._id, grant: GRANT_PUBLIC })
+        .sort({createdAt: -1})
+        .skip(offset)
+        .limit(limit)
+        .exec(function(err, pages) {
+          if (err) {
+            return reject(err);
+          }
+
+          return resolve(pages);
+        });
+    });
+  };
+
+  pageSchema.statics.findListByStartWith = function(path, userData, option) {
+    var Page = this;
+
+    if (!option) {
+      option = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
     }
     var opt = {
-      sort: options.sort || 'updatedAt',
-      desc: options.desc || -1,
-      offset: options.offset || 0,
-      limit: options.limit || 50
+      sort: option.sort || 'updatedAt',
+      desc: option.desc || -1,
+      offset: option.offset || 0,
+      limit: option.limit || 50
     };
     var sortOpt = {};
     sortOpt[opt.sort] = opt.desc;
     var queryReg = new RegExp('^' + path);
-    var sliceOption = options.revisionSlice || {$slice: 1};
+    var sliceOption = option.revisionSlice || {$slice: 1};
 
     return new Promise(function(resolve, reject) {
       var q = Page.find({

+ 16 - 8
lib/models/revision.js

@@ -14,20 +14,28 @@ module.exports = function(crowi) {
 
   revisionSchema.statics.findLatestRevision = function(path, cb) {
     this.find({path: path})
-      .sort({'createdAt': -1})
+      .sort({createdAt: -1})
       .limit(1)
       .exec(function(err, data) {
         cb(err, data.shift());
       });
   };
 
-  revisionSchema.statics.findRevisionList = function(path, options, cb) {
-    this.find({path: path})
-      .sort({'createdAt': -1})
-      .populate('author')
-      .exec(function(err, data) {
-        cb(err, data);
-      });
+  revisionSchema.statics.findRevisionList = function(path, options) {
+    var Revision = this;
+
+    return new Promise(function(resolve, reject) {
+      Revision.find({path: path})
+        .sort({createdAt: -1})
+        .populate('author')
+        .exec(function(err, data) {
+          if (err) {
+            return reject(err);
+          }
+
+          return resolve(data);
+        });
+    });
   };
 
   revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options, cb) {

+ 42 - 12
lib/routes/page.js

@@ -15,7 +15,7 @@ module.exports = function(crowi, app) {
   }
 
   function isUserPage(path) {
-    if (path.match(/^\/user\/[^\/]+$/)) {
+    if (path.match(/^\/user\/[^\/]+\/?$/)) {
       return true;
     }
 
@@ -98,7 +98,6 @@ module.exports = function(crowi, app) {
     // create page
     if (!pageData) {
       return res.render('page', {
-        revision: {},
         author: {},
         page: false,
       });
@@ -108,20 +107,51 @@ module.exports = function(crowi, app) {
       return res.redirect(encodeURI(pageData.redirectTo + '?renamed=' + pageData.path));
     }
 
-    Revision.findRevisionList(pageData.path, {}, function(err, tree) {
-      var revision = pageData.revision || {};
+    var renderVars = {
+      path: pageData.path,
+      page: pageData,
+      revision: pageData.revision || {},
+      author: pageData.revision.author || false,
+    };
+    var userPage = isUserPage(pageData.path);
+
+    Revision.findRevisionList(pageData.path, {})
+    .then(function(tree) {
+      renderVars.tree = tree;
+
+      if (userPage) {
+        return Bookmark.findByUser(req.user, {limit: 10});
+      } else {
+        return Promise.resolve();
+      }
+    }).then(function(bookmarkList) {
+      if (bookmarkList) {
+        renderVars.bookmarkList = bookmarkList;
+      }
+
+      if (userPage) {
+        return Page.findListByCreator(req.user, {limit: 10});
+      } else {
+        return Promise.resolve();
+      }
+    }).then(function(createdList) {
+      if (createdList) {
+        renderVars.createdList = createdList;
+      }
+
+      return Promise.resolve();
+    }).then(function() {
       var defaultPageTeamplate = 'page';
-      if (isUserPage(pageData.path)) {
+      if (userPage) {
         defaultPageTeamplate = 'user_page';
       }
 
-      res.render(req.query.presentation ? 'page_presentation' : defaultPageTeamplate, {
-        path: pageData.path,
-        revision: revision,
-        author: revision.author || false,
-        page: pageData,
-        tree: tree || [],
-      });
+      res.render(req.query.presentation ? 'page_presentation' : defaultPageTeamplate, renderVars);
+    }).catch(function(err) {
+      debug('Error: renderPage()', err);
+      if (err) {
+        res.redirect('/');
+      }
     });
   }
 

+ 1 - 35
lib/views/page_list.html

@@ -110,41 +110,7 @@
 
     {# list view #}
     <div class="active tab-pane fade page-list-container in" id="view-list">
-      <ul class="page-list-ul">
-      {% for page in pages %}
-      <li class="page-list-li">
-        <img src="{{ page.revision.author|picture }}" class="picture picture-rounded">
-
-        <a class="page-list-link" href="{{ page.path }}"
-          data-path="{{ page.path }}"
-          data-short-path="{{ page.path|path2name }}">{{ page.path }}</a>
-
-        <span class="page-list-meta">
-          {% if page.isPortal() %}
-            <span class="label label-info">PORTAL</span>
-          {% endif  %}
-
-
-          {% if page.commentCount > 0 %}
-            <i class="fa fa-comment"></i>{{ page.commentCount }}
-          {% endif  %}
-
-          {% if !page.isPublic() %}
-            <i class="fa fa-lock"></i>
-          {% endif %}
-        </span>
-      </li>
-      {% endfor %}
-      </ul>
-
-        <ul class="pagination">
-          {% if pager.prev !== null %}
-            <li class="prev"><a href="{{ path }}?offset={{ pager.prev }}&limit={{ pager.limit }}"><i class="fa fa-arrow-left"></i> Prev</a></li>
-          {% endif %}
-          {% if pager.next %}
-            <li class="next"><a href="{{ path }}?offset={{ pager.next }}&limit={{ pager.limit }}">Next <i class="fa fa-arrow-right"></i></a></li>
-          {% endif %}
-        </ul>
+      {% include 'widget/page_list.html' with { pages: pages, pager: pager } %}
     </div>
 
     {# timeline view #}

+ 10 - 0
lib/views/user_page.html

@@ -4,4 +4,14 @@
 {% endblock %}
 
 {% block content_main_before %}
+<div class="user-page-content row">
+  <div class="user-bookmark-list col-md-6">
+    <h2><i class="fa fa-star"></i> Bookmarks</h2>
+    {% include 'widget/page_list.html' with { pages: bookmarkList } %}
+  </div>
+  <div class="user-created-list col-md-6">
+    <i class="fa fa-star"></i> Created
+    {% include 'widget/page_list.html' with { pages: createdList } %}
+  </div>
+</div>
 {% endblock %}

+ 37 - 0
lib/views/widget/page_list.html

@@ -0,0 +1,37 @@
+<ul class="page-list-ul">
+{% for page in pages %}
+<li class="page-list-li">
+  <img src="{{ page.revision.author|picture }}" class="picture picture-rounded">
+
+  <a class="page-list-link" href="{{ page.path }}"
+    data-path="{{ page.path }}"
+    data-short-path="{{ page.path|path2name }}">{{ page.path }}</a>
+
+  <span class="page-list-meta">
+    {% if page.isPortal() %}
+      <span class="label label-info">PORTAL</span>
+    {% endif  %}
+
+
+    {% if page.commentCount > 0 %}
+      <i class="fa fa-comment"></i>{{ page.commentCount }}
+    {% endif  %}
+
+    {% if !page.isPublic() %}
+      <i class="fa fa-lock"></i>
+    {% endif %}
+  </span>
+</li>
+{% endfor %}
+</ul>
+
+{% if pager %}
+<ul class="pagination">
+  {% if pager.prev !== null %}
+    <li class="prev"><a href="{{ path }}?offset={{ pager.prev }}&limit={{ pager.limit }}"><i class="fa fa-arrow-left"></i> Prev</a></li>
+  {% endif %}
+  {% if pager.next %}
+    <li class="next"><a href="{{ path }}?offset={{ pager.next }}&limit={{ pager.limit }}">Next <i class="fa fa-arrow-right"></i></a></li>
+  {% endif %}
+</ul>
+{% endif %}