Skip to content
Snippets Groups Projects
Verified Commit 734f8f8c authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Basic display of suggestions

parent 80f8d93d
No related branches found
No related tags found
No related merge requests found
......@@ -19,26 +19,38 @@
<a @click="fetch(account.id)" class="secondary-content"><i class="material-icons">refresh</i></a>
</li>
</ul>
<h2>Suggestions</h2>
<h2>
Suggestions
<button
@click="fetch()"
:class="['waves-effect', 'waves-light', {disabled: isLoading}, 'btn-small']" :disabled="isLoading">
<i class="material-icons left">refresh</i>Fetch data
</button>
</h2>
<div v-if="isLoading" class="progress">
<div class="indeterminate"></div>
</div>
<ul class="collection">
<li class="collection-item avatar" v-for="suggestion in filteredSuggestions" :key="suggestion.fullId">
<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>
</li>
</ul>
</div>
</template>
<script>
import sources from '@/sources'
import orderBy from 'lodash/orderBy'
export default {
data () {
return {
isLoading: false,
results: {}
results: {},
aggregatedSuggestions: {}
}
},
computed: {
......@@ -47,9 +59,31 @@ export default {
a._source = sources.sources[a.source]
return a
})
}
},
filteredSuggestions () {
return orderBy(Object.values(this.aggregatedSuggestions), ['weight', 'id'], ['desc', 'asc'])
},
},
methods: {
aggregateSuggestions (results) {
const aggregated = {}
Object.keys(results).forEach((account) => {
let r = results[account]
if (!r.results || !r.results.accounts) {
} else {
Object.keys(r.results.accounts).forEach((key) => {
if (aggregated[key]) {
aggregated[key].weight += r.results.accounts[key].weight
aggregated[key].accounts.push(account)
} else {
aggregated[key] = {...r.results.accounts[key], accounts: [account], fullId: key}
}
})
}
})
return aggregated
},
async fetch (id) {
let accounts
if (id) {
......@@ -71,6 +105,14 @@ export default {
}
this.isLoading = false
}
},
watch: {
results: {
handler (v) {
this.aggregatedSuggestions = this.aggregateSuggestions(v)
},
deep: true,
}
}
}
</script>
......@@ -83,28 +83,28 @@ export default {
router.push({path: '/'})
},
async fetch ({account, cache, store, results, vue}) {
vue.$set(results, 'status', `Fetching favorites...`)
results.status = `Fetching favorites...`
const token = store.state.sources.mastodon.appTokens[`${account.username}@${account.domain}`].access_token
const client = axios.create({
baseURL: `https://${account.domain}`,
headers: {'Authorization': `Bearer ${token}`},
})
const maxFavorites = 100
const maxFavorites = 500
let url = '/api/v1/favourites'
let handledFavorites = 0
results.progress = 0
vue.$set(results, 'accounts', {})
results.accounts = {}
while (handledFavorites < maxFavorites) {
let response = await client.get(url, {params: {limit: 40}})
response.data.forEach((f) => {
handledFavorites += 1
let account = f.account.acct
let account = `webfinger:${f.account.acct}`
if (results.accounts[account]) {
results.accounts[account] += 1
results.accounts[account].weight += 1
} else {
results.accounts[account] = 1
results.accounts[account] = {weight: 1, source: 'webfinger', id: f.account.acct, avatar: f.account.avatar, name: f.account.display_name, url: f.account.url}
}
results.progress = Math.min(100, results.progress + 1)
results.progress = Math.min(100, handledFavorites / maxFavorites * 100)
})
let link = response.headers.link || ''
let parsed = parseLink(link)
......@@ -113,7 +113,7 @@ export default {
} else {
break
}
vue.$set(results, 'status', `Fetched favorites ${handledFavorites}/${maxFavorites}`)
results.status = `Fetched favorites ${handledFavorites}/${maxFavorites}`
}
results.isLoading = false
return results
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment