Explorar o código

Merge pull request #2249 from weseek/imprv/update-cache-bulk-with-comments

Imprv/update cache bulk with comments
Yuki Takei %!s(int64=5) %!d(string=hai) anos
pai
achega
6e7612a25a

+ 29 - 8
src/client/js/services/CommentContainer.js

@@ -34,6 +34,7 @@ export default class CommentContainer extends Container {
     };
     };
 
 
     this.retrieveComments = this.retrieveComments.bind(this);
     this.retrieveComments = this.retrieveComments.bind(this);
+    this.checkAndUpdateImageOfCommentAuthers = this.checkAndUpdateImageOfCommentAuthers.bind(this);
   }
   }
 
 
   /**
   /**
@@ -62,17 +63,37 @@ export default class CommentContainer extends Container {
   /**
   /**
    * Load data of comments and store them in state
    * Load data of comments and store them in state
    */
    */
-  retrieveComments() {
-    // [TODO][GW - 1942] add method for updating imageUrlCached
+  async retrieveComments() {
     const { pageId } = this.getPageContainer().state;
     const { pageId } = this.getPageContainer().state;
 
 
     // get data (desc order array)
     // get data (desc order array)
-    return this.appContainer.apiGet('/comments.get', { page_id: pageId })
-      .then((res) => {
-        if (res.ok) {
-          this.setState({ comments: res.comments });
-        }
-      });
+    const res = await this.appContainer.apiGet('/comments.get', { page_id: pageId });
+    if (res.ok) {
+      const comments = res.comments;
+      this.setState({ comments });
+
+      this.checkAndUpdateImageOfCommentAuthers(comments);
+    }
+  }
+
+  async checkAndUpdateImageOfCommentAuthers(comments) {
+    const noImageCacheUserIds = comments.filter((comment) => {
+      return comment.creator.imageUrlCached == null;
+    }).map((comment) => {
+      return comment.creator._id;
+    });
+
+    if (noImageCacheUserIds.length === 0) {
+      return;
+    }
+
+    try {
+      await this.appContainer.apiv3Put('/users/update.imageUrlCache', { userIds: noImageCacheUserIds });
+    }
+    catch (err) {
+      // Error alert doesn't apear, because user don't need to notice this error.
+      logger.error(err);
+    }
   }
   }
 
 
   /**
   /**

+ 4 - 3
src/client/js/services/PageContainer.js

@@ -72,6 +72,7 @@ export default class PageContainer extends Container {
 
 
     this.setTocHtml = this.setTocHtml.bind(this);
     this.setTocHtml = this.setTocHtml.bind(this);
     this.save = this.save.bind(this);
     this.save = this.save.bind(this);
+    this.checkAndUpdateImageUrlCached = this.checkAndUpdateImageUrlCached.bind(this);
     this.addWebSocketEventHandlers = this.addWebSocketEventHandlers.bind(this);
     this.addWebSocketEventHandlers = this.addWebSocketEventHandlers.bind(this);
     this.addWebSocketEventHandlers();
     this.addWebSocketEventHandlers();
 
 
@@ -138,7 +139,7 @@ export default class PageContainer extends Container {
       const { users } = await this.appContainer.apiGet('/users.list', { user_ids: userIdsStr });
       const { users } = await this.appContainer.apiGet('/users.list', { user_ids: userIdsStr });
       this.setState({ seenUsers: users });
       this.setState({ seenUsers: users });
 
 
-      await this.updateImageUrlCached(users);
+      this.checkAndUpdateImageUrlCached(users);
     }
     }
 
 
 
 
@@ -152,11 +153,11 @@ export default class PageContainer extends Container {
       const { users } = await this.appContainer.apiGet('/users.list', { user_ids: userIdsStr });
       const { users } = await this.appContainer.apiGet('/users.list', { user_ids: userIdsStr });
       this.setState({ likerUsers: users });
       this.setState({ likerUsers: users });
 
 
-      await this.updateImageUrlCached(users);
+      this.checkAndUpdateImageUrlCached(users);
     }
     }
   }
   }
 
 
-  async updateImageUrlCached(users) {
+  async checkAndUpdateImageUrlCached(users) {
     const noImageCacheUsers = users.filter((user) => { return user.imageUrlCached == null });
     const noImageCacheUsers = users.filter((user) => { return user.imageUrlCached == null });
     if (noImageCacheUsers.length === 0) {
     if (noImageCacheUsers.length === 0) {
       return;
       return;