catchEmit

This commit is contained in:
sheychen 2019-04-17 13:36:17 +02:00
parent e95b8a606d
commit edc9a87db8
12 changed files with 77 additions and 76 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -17,6 +17,12 @@ export default {
}, },
saveOptionCouple(couple) { saveOptionCouple(couple) {
this.emit('save', couple) this.emit('save', couple)
},
catchEmit(req) {
return req.catch(err => {
this.emitError(err)
throw err
})
} }
} }
} }

View File

@ -60,14 +60,13 @@ export default {
onScroll(event) { onScroll(event) {
if(!this.loadingOlder && event.target.scrollHeight - event.target.clientHeight - event.target.scrollTop - 100 < 0) { if(!this.loadingOlder && event.target.scrollHeight - event.target.clientHeight - event.target.scrollTop - 100 < 0) {
this.loadingOlder = true this.loadingOlder = true
this.rest this.catchEmit(this.rest
.get("/timelines/home", { params: { limit: this.buffer, .get("/timelines/home", { params: { limit: this.buffer,
max_id: this.statues[this.statues.length - 1].id } }) max_id: this.statues[this.statues.length - 1].id } }))
.then(res => { .then(res => {
this.statues.push.apply(this.statues, res.data) this.statues.push.apply(this.statues, res.data)
this.loadingOlder = false this.loadingOlder = false
}) })
.catch(this.emitError)
} else if(event.target.scrollTop < 20) { } else if(event.target.scrollTop < 20) {
this.statues.splice(this.buffer) this.statues.splice(this.buffer)
} }
@ -92,19 +91,16 @@ export default {
return (!status.in_reply_to_id || this.reply) && (!status.reblog || this.reblog) return (!status.in_reply_to_id || this.reply) && (!status.reblog || this.reblog)
}, },
onStatusMark(action) { onStatusMark(action) {
this.rest.post(`/statuses/${action.id}/${action.type}`) this.catchEmit(this.rest.post(`/statuses/${action.id}/${action.type}`))
.then(action.callback) .then(action.callback)
.catch(this.emitError)
}, },
onNotificationDismiss(id) { onNotificationDismiss(id) {
this.rest.post('/notifications/dismiss', { id: id }) this.catchEmit(this.rest.post('/notifications/dismiss', { id: id }))
.then(() => this.removeNotification(id)) .then(() => this.removeNotification(id))
.catch(this.emitError)
}, },
onNotificationsClear() { onNotificationsClear() {
this.rest.post('/notifications/clear') this.catchEmit(this.rest.post('/notifications/clear'))
.then(() => this.notifications.splice(0, this.notifications.length)) .then(() => this.notifications.splice(0, this.notifications.length))
.catch(this.emitError)
}, },
setupStream() { setupStream() {
const ws = new WebSocket( const ws = new WebSocket(
@ -138,15 +134,11 @@ export default {
} }
}, },
created() { created() {
this.rest this.catchEmit(this.rest.get("/timelines/home", { params: { limit: this.buffer } }))
.get("/timelines/home", { params: { limit: this.buffer } })
.then(res => this.statues.push.apply(this.statues, res.data)) .then(res => this.statues.push.apply(this.statues, res.data))
.catch(this.emitError)
this.rest this.catchEmit(this.rest.get("/notifications", { params: { limit: this.buffer } }))
.get("/notifications", { params: { limit: this.buffer } })
.then(res => this.notifications.push.apply(this.notifications, res.data)) .then(res => this.notifications.push.apply(this.notifications, res.data))
.catch(this.emitError)
this.setupStream() this.setupStream()
} }

View File

@ -13,7 +13,7 @@
setting-boolean(:id="'showMedia'" :title="'Show medias'" :value="showMedia" @change="saveOptionCouple") setting-boolean(:id="'showMedia'" :title="'Show medias'" :value="showMedia" @change="saveOptionCouple")
client(v-if="server && token" v-bind="$props") client(v-if="server && token" v-bind="$props")
.auth(v-else) .auth(v-else)
form(@submit.prevent="setServer") form(@submit.prevent="makeAuth")
p p
label(for="server") Server: label(for="server") Server:
input#server(v-model="newServer" required) input#server(v-model="newServer" required)
@ -73,25 +73,23 @@ export default { //TODO: Use oauth
}; };
}, },
methods: { methods: {
setServer() { getMe(server, token) {
axios.get(`https://${this.newServer}/api/v1/accounts/verify_credentials`, { return this.catchEmit(axios.get(`https://${server}/api/v1/accounts/verify_credentials`, {
headers: { Authorization: "Bearer " + this.newToken }, headers: { Authorization: "Bearer " + token },
timeout: this.timeout timeout: this.timeout
}).then(() => this.saveOptions({ ...this.$props, }))
},
makeAuth() {
this.getMe(this.newServer, this.newToken)
.then(() => this.saveOptions({ ...this.$props,
server: this.newServer, token: this.newToken })) server: this.newServer, token: this.newToken }))
.catch(this.emitError)
} }
}, },
created() { created() {
if(this.server && this.token) { if(this.server && this.token) {
axios.get(`https://${this.server}/api/v1/accounts/verify_credentials`, { this.getMe(this.server, this.token)
headers: { Authorization: "Bearer " + this.token }, .then(res => this.account = res.data)
timeout: this.timeout .catch(() => this.account.display_name = 'Failed')
}).then(res => this.account = res.data)
.catch(err => {
this.emitError(err)
this.account.display_name = 'Failed'
})
} else{ } else{
this.account.display_name = 'First connection' this.account.display_name = 'First connection'
} }

View File

@ -1,38 +1,37 @@
<template lang="pug"> <template lang="pug">
div .status
.status account(v-if="withAccount" :account="status.account" :showMedia="showMedia")
account(v-if="withAccount" :account="status.account" :showMedia="showMedia")
span.text-icon.letter(v-if="status.reblog") span.text-icon.letter(v-if="status.reblog")
a.date(target="_blank" :href="status.uri") a.date(target="_blank" :href="status.uri")
from-now(:date="status.created_at" :now="now") from-now(:date="status.created_at" :now="now")
.content(:class="{ avatared: showMedia }") .content(:class="{ avatared: showMedia }")
template(v-if="!status.reblog") template(v-if="!status.reblog")
.spoiler(v-if="status.spoiler_text" @click.stop.prevent="status.sensitive = !status.sensitive"). .spoiler(v-if="status.spoiler_text" @click.stop.prevent="status.sensitive = !status.sensitive").
{{ status.spoiler_text || 'Spoiler' }} {{ status.sensitive ? '&rarr;' : '&darr;' }} {{ status.spoiler_text || 'Spoiler' }} {{ status.sensitive ? '&rarr;' : '&darr;' }}
div(v-if="!status.spoiler_text || !status.sensitive") div(v-if="!status.spoiler_text || !status.sensitive")
.text(v-html="parseEmojis(status.content, status.emojis)") .text(v-html="parseEmojis(status.content, status.emojis)")
a.media(v-for="media in status.media_attachments" :href="media.url" target="_blank") a.media(v-for="media in status.media_attachments" :href="media.url" target="_blank")
template(v-if="showMedia") template(v-if="showMedia")
img(v-if="media.type == 'image' || media.type == 'gifv'" :src="media.preview_url" :alt="media.description" :title="media.description") img(v-if="media.type == 'image' || media.type == 'gifv'" :src="media.preview_url" :alt="media.description" :title="media.description")
.gif(v-if="media.type == 'gifv'") GIF .gif(v-if="media.type == 'gifv'") GIF
template(v-else) Hidden media template(v-else) Hidden media
status.reblog(v-else :status="status.reblog" :now="now" :showMedia="showMedia") status.reblog(v-else :status="status.reblog" :now="now" :showMedia="showMedia")
.meta(v-if="!status.reblog") .meta(v-if="!status.reblog")
a.replies(@click.stop.prevent="makeReply(status)") a.replies(@click.stop.prevent="makeReply(status)")
span.text-icon span.text-icon
| {{ status.replies_count }} | {{ status.replies_count }}
a.reblogs(:class="{ colored: status.reblogged }" @click.stop.prevent="makeReblog(status)") a.reblogs(:class="{ colored: status.reblogged }" @click.stop.prevent="makeReblog(status)")
span.text-icon span.text-icon
| {{ status.reblogs_count }} | {{ status.reblogs_count }}
a.favourites(:class="{ colored: status.favourited }" @click.stop.prevent="makeFav(status)") a.favourites(:class="{ colored: status.favourited }" @click.stop.prevent="makeFav(status)")
span.text-icon span.text-icon
| {{ status.favourites_count }} | {{ status.favourites_count }}
a.fil(v-if="status.in_reply_to_id" @click.stop.prevent="showReply(status.in_reply_to_id)") a.fil(v-if="status.in_reply_to_id" @click.stop.prevent="showReply(status.in_reply_to_id)")
| Voir le fil | Voir le fil
</template> </template>
<script> <script>

View File

@ -1,7 +1,9 @@
<template lang="pug"> <template lang="pug">
.nextcloud-news(v-show="showEmpty || unreaded.length > 0 || !server || !token || !username") .nextcloud-news(v-show="showEmpty || unreaded.length > 0 || !server || !token || !username")
service-header(:emit="emit") service-header(:emit="emit")
template(#title) Nextcloud News template(#title)
| Nextcloud News
span.note(v-if="unreaded.length > 0") ({{ unreaded.length }})
template(#settings) template(#settings)
setting-int(:id="'update'" :title="'Update interval'" :value="update" @change="saveOptionCouple") setting-int(:id="'update'" :title="'Update interval'" :value="update" @change="saveOptionCouple")
setting-int(:id="'buffer'" :title="'Buffer size'" :value="buffer" @change="saveOptionCouple") setting-int(:id="'buffer'" :title="'Buffer size'" :value="buffer" @change="saveOptionCouple")
@ -78,12 +80,11 @@ export default {
}, },
methods: { methods: {
loadData() { loadData() {
this.rest.get("/items", { params: { batchSize: this.buffer, type: 3, getRead: false } }) this.catchEmit(this.rest.get("/items", { params: { batchSize: this.buffer, type: 3, getRead: false } }))
.then(res => this.unreaded = res.data.items.map(n => { .then(res => this.unreaded = res.data.items.map(n => {
n.open = false n.open = false
return n return n
})) }))
.catch(this.emitError)
}, },
removeNews(id) { removeNews(id) {
for (var i = this.unreaded.length - 1; i >= 0; i--) { for (var i = this.unreaded.length - 1; i >= 0; i--) {
@ -94,17 +95,15 @@ export default {
} }
}, },
makeRead(id) { makeRead(id) {
this.rest.put(`/items/${id}/read`) this.catchEmit(this.rest.put(`/items/${id}/read`))
.then(() => this.removeNews(id)) .then(() => this.removeNews(id))
.catch(this.emitError)
}, },
setServer() { setServer() {
axios.get(`https://${this.newServer}/index.php/apps/news/api/v1-2/folders`, { this.catchEmit(axios.get(`https://${this.newServer}/index.php/apps/news/api/v1-2/folders`, {
headers: { Authorization: 'Basic ' + btoa(this.newUsername + ':' + this.newToken) }, headers: { Authorization: 'Basic ' + btoa(this.newUsername + ':' + this.newToken) },
timeout: this.timeout timeout: this.timeout
}).then(() => this.saveOptions({ ...this.$props, })).then(() => this.saveOptions({ ...this.$props,
server: this.newServer, token: this.newToken, username: this.newUsername })) server: this.newServer, token: this.newToken, username: this.newUsername }))
.catch(this.emitError)
} }
}, },
created() { created() {

View File

@ -119,17 +119,15 @@ export default {
this.loadForecast() this.loadForecast()
}, },
getWeather(params) { getWeather(params) {
return this.rest.get('weather', { params: params }) return this.catchEmit(this.rest.get('weather', { params: params }))
.catch(this.emitError)
}, },
loadForecast() { loadForecast() {
if(this.weathers[this.selected]) { if(this.weathers[this.selected]) {
this.rest.get('forecast', { params: { this.catchEmit(this.rest.get('forecast', { params: {
id: this.weathers[this.selected].id, id: this.weathers[this.selected].id,
cnt: this.forecastLimit cnt: this.forecastLimit
}}) }}))
.then(res => this.forecast = res.data.list) .then(res => this.forecast = res.data.list)
.catch(this.emitError)
} }
}, },
formatDate(dt) { formatDate(dt) {

View File

@ -32,6 +32,11 @@ input, select, button {
font-size: 1.2em; font-size: 1.2em;
} }
.note {
font-size: .7em;
vertical-align: text-top;
}
.letter { .letter {
margin: 0 .5em; margin: 0 .5em;
} }

File diff suppressed because one or more lines are too long

View File

@ -42,6 +42,10 @@ input, select, button
font-weight: bold font-weight: bold
font-size: 1.2em font-size: 1.2em
.note
font-size: .7em
vertical-align: text-top
.letter .letter
margin: 0 .5em margin: 0 .5em