Procházet zdrojové kódy

Add user bookmark page

Sotaro KARASAWA před 10 roky
rodič
revize
cc5bf18e2d

+ 1 - 0
lib/crowi/index.js

@@ -120,6 +120,7 @@ Crowi.prototype.setupDatabase = function() {
   return new Promise(function(resolve, reject) {
     mongoose.connect(mongoUri, function(e) {
       if (e) {
+        debug('DB Connect Error: ', e);
         debug('DB Connect Error: ', mongoUri);
         return reject(new Error('Cann\'t connect to Database Server.'));
       }

+ 34 - 4
lib/models/bookmark.js

@@ -12,6 +12,34 @@ module.exports = function(crowi) {
   });
   bookmarkSchema.index({page: 1, user: 1}, {unique: true});
 
+  bookmarkSchema.statics.populatePage = function(bookmarks) {
+    var Bookmark = this;
+    var User = crowi.model('User');
+    var Page = crowi.model('Page');
+
+    return new Promise(function(resolve, reject) {
+      Bookmark.populate(bookmarks, {path: 'page'}, function(err, bookmarks) {
+        if (err) {
+          return reject(err);
+        }
+
+        Bookmark.populate(bookmarks, {path: 'page.revision', model: 'Revision'}, function(err, bookmarks) {
+          if (err) {
+            return reject(err);
+          }
+
+          Bookmark.populate(bookmarks, {path: 'page.revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS}, function(err, bookmarks) {
+            if (err) {
+              return reject(err);
+            }
+
+            return resolve(bookmarks);
+          });
+        });
+      });
+    });
+  };
+
   // bookmark チェック用
   bookmarkSchema.statics.findByPageIdAndUserId = function(pageId, userId) {
     var Bookmark = this;
@@ -32,23 +60,25 @@ module.exports = function(crowi) {
     var Bookmark = this;
 
     var limit = option.limit || 50;
-    var offset = option.skip || 0;
+    var offset = option.offset || 0;
+    var populatePage = option.populatePage || false;
 
     return new Promise(function(resolve, reject) {
       Bookmark
         .find({ user: user._id })
         .sort({createdAt: -1})
         .skip(offset)
-        .populate([{path: 'page'}, {path: 'user', select: User.USER_PUBLIC_FIELDS}])
         .limit(limit)
         .exec(function(err, bookmarks) {
           if (err) {
             return reject(err);
           }
 
-          //Page.populatePageList();
+          if (!populatePage) {
+            return resolve(bookmarks);
+          }
 
-          return resolve(bookmarks);
+          return Bookmark.populatePage(bookmarks).then(resolve).catch(reject);
         });
     });
   };

+ 24 - 14
lib/routes/page.js

@@ -131,10 +131,9 @@ module.exports = function(crowi, app) {
           userData = data;
           renderVars.pageUser = userData;
 
-          return Bookmark.findByUser(userData, {limit: 10}).then(function(bookmarkList) {
-            return Page.findListByPageIds(Object.keys(bookmarkList).map(function(e) { return bookmarkList[e].page; }), {});
-          });
+          return Bookmark.findByUser(userData, {limit: 10, populatePage: true});
         }).then(function(bookmarkList) {
+          debug(bookmarkList);
           renderVars.bookmarkList = bookmarkList;
 
           return Page.findListByCreator(userData, {limit: 10});
@@ -254,27 +253,38 @@ module.exports = function(crowi, app) {
   // app.get( '/users/:username([^/]+)/bookmarks'      , loginRequired(crowi, app) , page.userBookmarkList);
   actions.userBookmarkList = function(req, res) {
     var username = req.params.username;
-    var offset = req.query.offset || 0;
-    var userData;
+    var limit = parseInt(req.query.limit)  || 50;
+    var offset = parseInt(req.query.offset)  || 0;
+
+    var user;
     var renderVars = {};
 
+    var pagerOptions = { offset: offset, limit : limit };
+    var queryOptions = { offset: offset, limit : limit + 1, populatePage: true};
+
     User.findUserByUsername(username)
-    .then(function(data) {
-      if (data === null) {
+    .then(function(user) {
+      if (user === null) {
         throw new Error('The user not found.');
       }
-      renderVars.pageUser = userData = data;
+      renderVars.user = user;
 
-      return Bookmark.findByUser(userData, {offset: offset, limit: 50}).then(function(bookmarkList) {
-        return Page.findListByPageIds(Object.keys(bookmarkList).map(function(e) { return bookmarkList[e].page; }), {});
-      });
+      return Bookmark.findByUser(user, queryOptions);
+    }).then(function(bookmarks) {
+
+      if (bookmarks.length > limit) {
+        bookmarks.pop();
+      }
+      pagerOptions.length = bookmarks.length;
+
+      renderVars.pager = generatePager(pagerOptions);
+      renderVars.bookmarks = bookmarks;
+
+      return res.render('user/bookmarks', renderVars);
     }).catch(function(err) {
       debug('Error on rendereing bookmark', err);
       res.redirect('/');
     });
-    return res.render('user/bookmarks', {
-      username: username
-    });
   };
 
   // app.get( '/users/:username([^/]+)/recent-create' , loginRequired(crowi, app) , page.userRecentCreatedList);

+ 0 - 8
lib/views/layout/2column.html

@@ -152,10 +152,6 @@
   {% endif %}
   <article>
   {% block content_head %}
-    <header>
-    <h2>-</h2>
-    <p>-</p>
-    </header>
   {% endblock %}
 
   {% block content_main %}
@@ -163,10 +159,6 @@
   {% endblock content_main %}
 
   {% block content_footer %}
-    <footer>
-    <h3>-</h3>
-    <p>-</p>
-    </footer>
   {% endblock %}
   </article>
 </div>

+ 32 - 1
lib/views/user/bookmarks.html

@@ -1 +1,32 @@
-{{ username }}
+{% extends '../layout/2column.html' %}
+
+{% block main_css_class %}bookmark-page{% endblock %}
+
+{% block html_title %}{{ path|path2name }} · {{ path }}{% endblock %}
+
+{% block content_head %}
+<div class="header-wrap">
+  <header id="page-header">
+    <p class="stopper"><a href="#" data-affix-disable="#page-header"><i class="fa fa-chevron-up"></i></a></p>
+
+    <h1 class="title">{{ user.name }}'s Bookmarks</h1>
+  </header>
+</div>
+{% endblock %}
+
+{% block content_main %}
+<div id="content-main" class="content-main page-list" >
+  <div class="page-list-container" id="bookamrk-list">
+    {% include '../widget/page_list.html' with { pages: bookmarks, pagePropertyName: 'page' } %}
+  </div>
+</div>
+{% endblock %}
+
+{% block side_header %}
+{% endblock %} {# side_header #}
+
+{% block side_content %}
+{% endblock %}
+
+{% block footer %}
+{% endblock %}

+ 1 - 1
lib/views/user_page.html

@@ -47,7 +47,7 @@
           {% if bookmarkList.length == 0 %}
           No bookmarks yet.
           {% else %}
-            {% include 'widget/page_list.html' with { pages: bookmarkList } %}
+            {% include 'widget/page_list.html' with { pages: bookmarkList, pagePropertyName: 'page' } %}
             <div class="user-page-list-additional-link">
               <a href="/user/{{ pageUser.username }}/bookmarks"><i class="fa fa-angle-double-right"></i> See bookmarks</a>
             </div>

+ 8 - 1
lib/views/widget/page_list.html

@@ -1,5 +1,12 @@
 <ul class="page-list-ul">
-{% for page in pages %}
+{% for data in pages %}
+
+{% if pagePropertyName %}
+  {% set page = data[pagePropertyName] %}
+{% else %}
+  {% set page = data %}
+{% endif %}
+
 <li class="page-list-li">
   <img src="{{ page.revision.author|picture }}" class="picture picture-rounded">