From 0d72c67df042c593c1411e3553fecd873bdabeeb Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Tue, 4 Jun 2019 09:03:02 +0200
Subject: [PATCH] Improved caching

---
 src/components/Suggestions.vue | 65 +++++++++++++++++++++++++++++++++-
 src/sources/mastodon.js        |  2 +-
 src/store.js                   |  3 +-
 3 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/src/components/Suggestions.vue b/src/components/Suggestions.vue
index 3c45bc8..e682cfd 100644
--- a/src/components/Suggestions.vue
+++ b/src/components/Suggestions.vue
@@ -35,6 +35,23 @@
         <img v-if="suggestion.avatar" :src="suggestion.avatar" alt="" class="circle">
         <a target="_blank" rel="noopener noreferrer" :href="suggestion.url" class="title">{{ suggestion.name }}</a>
         <p>Score: {{ suggestion.weight }}</p>
+        <a v-if="retributeProfiles[suggestion.fullId] === undefined" @click="lookup(suggestion.fullId)" class="secondary-content"><i class="material-icons">search</i></a>
+        <div v-else-if="retributeProfiles[suggestion.fullId]">
+          <h6>Donation platforms</h6>
+          <!-- {{ retributeProfiles[suggestion.fullId] }} -->
+          <template v-for="mean in retributeProfiles[suggestion.fullId].means">
+            <a
+              :href="mean.url"
+              :key="mean.id"
+              target="_blank"
+              rel="noopener noreferrer"
+              :class="['waves-effect', 'waves-light', 'btn-small']">
+              <span :title="mean.summary">{{ mean.provider }}</span>
+            </a>
+
+          </template>
+        </div>
+        <div v-else>No retribute information found for this account</div>
       </li>
     </ul>
   </div>
@@ -44,13 +61,16 @@
 <script>
 import sources from '@/sources'
 import orderBy from 'lodash/orderBy'
+import chunk from 'lodash/chunk'
+import axios from 'axios'
 
 export default {
   data () {
     return {
       isLoading: false,
       results: {},
-      aggregatedSuggestions: {}
+      aggregatedSuggestions: this.$store.state.cache.aggregatedSuggestions || {},
+      retributeProfiles: this.$store.state.cache.retributeProfiles || {},
     }
   },
   computed: {
@@ -103,15 +123,58 @@ export default {
       for(let i = 0; i < keys.length; i++){
         await this.results[keys[i]].promise
       }
+      await this.lookupAll()
       this.isLoading = false
+    },
+    async lookupAll () {
+      const toLookup = this.filteredSuggestions.filter((s) => {
+        return this.retributeProfiles[s.fullId] === undefined
+      })
+      const chunkSize = 5
+      const chunks = chunk(toLookup, chunkSize)
+      for (let i = 0; i < chunks.length; i++){
+        let chunk = chunks[i]
+        let ids = chunk.map((s) => {
+          return s.fullId
+        })
+        await this.lookups(ids)
+      }
+
+    },
+    async lookups(ids) {
+      const tasks = ids.map((i) => {
+        return this.lookup(i)
+      })
+      for (let i = 0; i < tasks.length; i++){
+        await tasks[i]
+      }
+
+    },
+    async lookup (id) {
+      const client = axios.create({timeout: 5000})
+      let url = `http://localhost:8000/api/v1/search/${id}`
+      try {
+        const response = await client.get(url)
+        this.$set(this.retributeProfiles, id, response.data)
+      } catch {
+        this.$set(this.retributeProfiles, id, null)
+        return
+      }
     }
   },
   watch: {
     results: {
       handler (v) {
         this.aggregatedSuggestions = this.aggregateSuggestions(v)
+        this.$store.commit('setRecursiveState', {key: 'cache.aggregatedSuggestions', value: this.aggregatedSuggestions})
       },
       deep: true,
+    },
+    retributeProfiles: {
+      handler (v) {
+        this.$store.commit('setRecursiveState', {key: 'cache.retributeProfiles', value: this.retributeProfiles})
+      },
+      deep: true
     }
   }
 }
diff --git a/src/sources/mastodon.js b/src/sources/mastodon.js
index bb91f18..ae128dc 100644
--- a/src/sources/mastodon.js
+++ b/src/sources/mastodon.js
@@ -89,7 +89,7 @@ export default {
       baseURL: `https://${account.domain}`,
       headers: {'Authorization': `Bearer ${token}`},
     })
-    const maxFavorites = 500
+    const maxFavorites = 100
     let url = '/api/v1/favourites'
     let handledFavorites = 0
     results.progress = 0
diff --git a/src/store.js b/src/store.js
index 40d00a7..5f77ddd 100644
--- a/src/store.js
+++ b/src/store.js
@@ -13,7 +13,8 @@ export const storeConfig = {
   state: {
 
     accounts: {},
-    sources: {}
+    sources: {},
+    cache: {}
   },
   mutations: {
     addAccount (state, payload) {
-- 
GitLab