Răsfoiți Sursa

User list fetch in localStorage

Sotaro KARASAWA 9 ani în urmă
părinte
comite
c9cc2369b3
4 a modificat fișierele cu 92 adăugiri și 2 ștergeri
  1. 23 0
      lib/models/user.js
  2. 6 2
      lib/routes/user.js
  3. 1 0
      resource/js/app.js
  4. 62 0
      resource/js/util/Crowi.js

+ 23 - 0
lib/models/user.js

@@ -271,6 +271,29 @@ module.exports = function(crowi) {
 
   };
 
+  userSchema.statics.findAllUsers = function(option) {
+    var User = this;
+    var option = option || {}
+      , sort = option.sort || {createdAt: -1}
+      , status = option.status || STATUS_ACTIVE
+      , fields = option.fields || USER_PUBLIC_FIELDS
+      ;
+
+    return new Promise(function(resolve, reject) {
+      User
+        .find({status: status })
+        .select(fields)
+        .sort(sort)
+        .exec(function (err, userData) {
+          if (err) {
+            return reject(err);
+          }
+
+          return resolve(userData);
+        });
+    });
+  };
+
   userSchema.statics.findUsersByIds = function(ids, option) {
     var User = this;
     var option = option || {}

+ 6 - 2
lib/routes/user.js

@@ -45,11 +45,15 @@ module.exports = function(crowi, app) {
    */
   api.list = function(req, res) {
     var userIds = req.query.user_ids || null; // TODO: handling
+
+    var userFetcher;
     if (!userIds || userIds.split(',').length <= 0) {
-      return res.json(ApiResponse.error('user_ids param is required'));
+      userFetcher = User.findAllUsers()
+    } else {
+      userFetcher = User.findUsersByIds(userIds.split(','))
     }
 
-    User.findUsersByIds(userIds.split(','))
+    userFetcher
     .then(function(userList) {
       var result = {
         users: userList,

+ 1 - 0
resource/js/app.js

@@ -15,6 +15,7 @@ if (!window) {
 // FIXME
 const crowi = new Crowi({/* context */}, window);
 window.crowi = crowi;
+crowi.fetchUsers();
 
 const crowiRenderer = new CrowiRenderer();
 window.crowiRenderer = crowiRenderer;

+ 62 - 0
resource/js/util/Crowi.js

@@ -10,10 +10,72 @@ export default class Crowi {
 
     this.location = window.location || {};
     this.document = window.document || {};
+    this.localStorage = window.localStorage || {};
 
+    this.fetchUsers = this.fetchUsers.bind(this);
     this.apiGet = this.apiGet.bind(this);
     this.apiPost = this.apiPost.bind(this);
     this.apiRequest = this.apiRequest.bind(this);
+
+    this.users = [];
+    this.userByName = {};
+
+    this.recoverData();
+  }
+
+  recoverData() {
+    const keys = [
+      'userByName',
+      'users',
+    ];
+
+    keys.forEach(key => {
+      if (this.localStorage[key]) {
+        try {
+          this[key] = JSON.parse(this.localStorage[key]);
+        } catch (e) {
+          this.localStorage.removeItem(key);
+        }
+      }
+    });
+  }
+
+  fetchUsers () {
+    const interval = 1000*60*10; // 5min
+    const currentTime = new Date();
+    if (!this.localStorage.lastFetched) {
+      this.localStorage.lastFetched = new Date();
+    }
+    if (interval > currentTime - new Date(this.localStorage.lastFetched)) {
+      return ;
+    }
+
+    this.apiGet('/users.list', {})
+    .then(data => {
+      this.users = data.users;
+      this.localStorage.users = JSON.stringify(data.users);
+
+      let userByName = {};
+      for (let i = 0; i < data.users.length; i++) {
+        const user = data.users[i];
+        userByName[user.username] = user;
+      }
+      this.userByName = userByName;
+      this.localStorage.userByName = JSON.stringify(userByName);
+
+      this.localStorage.lastFetched = new Date();
+      //console.log('userByName', this.localStorage.userByName);
+    }).catch(err => {
+      // ignore errors
+    });
+  }
+
+  findUser(username) {
+    if (this.userByName && this.userByName[username]) {
+      return this.userByName[username];
+    }
+
+    return null;
   }
 
   apiGet(path, params) {