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

Added input to select max days

parent ce5f21fe
Branches
Tags
No related merge requests found
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"lodash": "^4.17.11", "lodash": "^4.17.11",
"material-icons": "^0.3.1", "material-icons": "^0.3.1",
"materialize-css": "^1.0.0-rc.2", "materialize-css": "^1.0.0-rc.2",
"moment": "^2.24.0",
"parse-link-header": "^1.0.1", "parse-link-header": "^1.0.1",
"vue": "^2.6.10", "vue": "^2.6.10",
"vue-router": "^3.0.3", "vue-router": "^3.0.3",
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<p>{{ account._source.label }}</p> <p>{{ account._source.label }}</p>
<div v-if="isLoadingSources && results[account.id] && results[account.id].results.isLoading"> <div v-if="isLoadingSources && results[account.id] && results[account.id].results.isLoading">
<div class="progress"> <div class="progress">
<div v-if="results[account.id].results.progress === 'indeterminate'" :class="indeterminate"></div> <div v-if="results[account.id].results.progress === 'indeterminate'" class="indeterminate"></div>
<div v-else class="determinate" :style="{width: `${results[account.id].results.progress}%`}"></div> <div v-else class="determinate" :style="{width: `${results[account.id].results.progress}%`}"></div>
</div> </div>
{{ results[account.id].results.status }} {{ results[account.id].results.status }}
...@@ -24,9 +24,15 @@ ...@@ -24,9 +24,15 @@
<button <button
@click="fetch()" @click="fetch()"
:class="['waves-effect', 'waves-light', {disabled: isLoading}, 'btn-small']" :disabled="isLoading"> :class="['waves-effect', 'waves-light', {disabled: isLoading}, 'btn-small']" :disabled="isLoading">
<i class="material-icons left">refresh</i>Fetch data <i class="material-icons left">refresh</i>Fetch {{ maxDays }} days of data
</button> </button>
</h2> </h2>
<div class="row">
<div class="input-field col s3">
<input v-model="maxDays" id="maxDays" placeholder="60" type="number" min="1" step="5" max="365" class="validate">
<label class="active" for="maxDays">Period (in days)</label>
</div>
</div>
<div v-if="isLoading" class="progress"> <div v-if="isLoading" class="progress">
<div class="indeterminate"></div> <div class="indeterminate"></div>
</div> </div>
...@@ -69,6 +75,7 @@ import axios from 'axios' ...@@ -69,6 +75,7 @@ import axios from 'axios'
export default { export default {
data () { data () {
return { return {
maxDays: 60,
isLoadingSources: false, isLoadingSources: false,
isLoadingRetribute: false, isLoadingRetribute: false,
results: {}, results: {},
...@@ -152,7 +159,7 @@ export default { ...@@ -152,7 +159,7 @@ export default {
this.isLoadingSources = true this.isLoadingSources = true
accounts.forEach((a) => { accounts.forEach((a) => {
let r = {isLoading: true, progress: 'indeterminate', status: ''} let r = {isLoading: true, progress: 'indeterminate', status: ''}
let promise = a._source.fetch({account: a, store: this.$store, results: r, vue: this}) let promise = a._source.fetch({account: a, store: this.$store, results: r, vue: this, maxDays: this.maxDays})
this.$set(this.results, a.id, {account: a, promise, results: r}) this.$set(this.results, a.id, {account: a, promise, results: r})
}) })
const keys = Object.keys(this.results) const keys = Object.keys(this.results)
......
import axios from 'axios' import axios from 'axios'
import moment from 'moment'
import Form from '../components/MastodonForm.vue' import Form from '../components/MastodonForm.vue'
import parseLink from 'parse-link-header' import parseLink from 'parse-link-header'
...@@ -82,29 +83,37 @@ export default { ...@@ -82,29 +83,37 @@ export default {
store.commit('addAccount', {source: 'mastodon', raw: accountResponse.data, username: accountResponse.data.acct, domain}) store.commit('addAccount', {source: 'mastodon', raw: accountResponse.data, username: accountResponse.data.acct, domain})
router.push({path: '/'}) router.push({path: '/'})
}, },
async fetch ({account, cache, store, results, vue}) { async fetch ({account, store, results, maxDays}) {
results.status = `Fetching favorites...` results.status = `Fetching favorites...`
const token = store.state.sources.mastodon.appTokens[`${account.username}@${account.domain}`].access_token const token = store.state.sources.mastodon.appTokens[`${account.username}@${account.domain}`].access_token
const client = axios.create({ const client = axios.create({
baseURL: `https://${account.domain}`, baseURL: `https://${account.domain}`,
headers: {'Authorization': `Bearer ${token}`}, headers: {'Authorization': `Bearer ${token}`},
}) })
const maxFavorites = 400 const dateLimit = moment().subtract(maxDays, 'days')
let url = '/api/v1/favourites' let url = '/api/v1/favourites'
let handledFavorites = 0 let handledFavorites = 0
results.progress = 0 // results.progress = 0
// results.progressCount
results.accounts = {} results.accounts = {}
while (handledFavorites < maxFavorites) { let cont = true
while (cont) {
let response = await client.get(url, {params: {limit: 40}}) let response = await client.get(url, {params: {limit: 40}})
response.data.forEach((f) => { response.data.forEach((f) => {
let date = moment(f.created_at)
if (date.isBefore(dateLimit)) {
cont = false
return
}
handledFavorites += 1 handledFavorites += 1
results.progressCount += 1
let account = `webfinger:${f.account.acct}` let account = `webfinger:${f.account.acct}`
if (results.accounts[account]) { if (results.accounts[account]) {
results.accounts[account].weight += 1 results.accounts[account].weight += 1
} else { } else {
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.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, handledFavorites / maxFavorites * 100) // results.progress = Math.min(100, handledFavorites / maxFavorites * 100)
}) })
let link = response.headers.link || '' let link = response.headers.link || ''
let parsed = parseLink(link) let parsed = parseLink(link)
...@@ -113,7 +122,7 @@ export default { ...@@ -113,7 +122,7 @@ export default {
} else { } else {
break break
} }
results.status = `Fetched favorites ${handledFavorites}/${maxFavorites}` results.status = `Fetched ${handledFavorites} favorites`
} }
results.isLoading = false results.isLoading = false
return results return results
......
...@@ -17,12 +17,12 @@ export const storeConfig = { ...@@ -17,12 +17,12 @@ export const storeConfig = {
cache: {} cache: {}
}, },
mutations: { mutations: {
addAccount (state, payload) { addAccount ({state}, payload) {
const id = sources.sources[payload.source].authDataToKey(payload) const id = sources.sources[payload.source].authDataToKey(payload)
payload.id = id payload.id = id
state.accounts[id] = payload state.accounts[id] = payload
}, },
setRecursiveState (state, {key, suffix, value}) { setRecursiveState ({state}, {key, suffix, value}) {
if (suffix) { if (suffix) {
let existing = get(state, key, {}) let existing = get(state, key, {})
existing[suffix] = value existing[suffix] = value
......
...@@ -5438,6 +5438,11 @@ mocha@^5.2.0: ...@@ -5438,6 +5438,11 @@ mocha@^5.2.0:
mkdirp "0.5.1" mkdirp "0.5.1"
supports-color "5.4.0" supports-color "5.4.0"
moment@^2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
move-concurrently@^1.0.1: move-concurrently@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment