mixit/compiler/src/nextcloud-news/main.vue

114 lines
3.6 KiB
Vue
Raw Normal View History

2019-04-14 14:52:12 +00:00
<template lang="pug">
2019-04-29 14:10:28 +00:00
.nextcloud-news(v-show="showService")
2019-04-17 10:08:30 +00:00
service-header(:emit="emit")
2019-04-17 11:36:17 +00:00
template(#title)
2019-04-29 14:10:28 +00:00
| {{ serviceName }}
span.note(v-if="hasNews") ({{ news.get().length }})
2019-04-16 12:04:25 +00:00
template(#settings)
2019-04-29 14:10:28 +00:00
setting-int(:id="'update'" :title="'Update interval'" :value="params.update" @change="saveOptionCouple")
setting-int(:id="'buffer'" :title="'Buffer size'" :value="params.buffer" @change="saveOptionCouple")
setting-boolean(:id="'showEmpty'" :title="'Show empty'" :value="params.showEmpty" @change="saveOptionCouple")
loadable-block.unreaded(:loadable="news")
template(#success)
.news(v-for="line in news.get()")
a(:href="line.url" target="_blank")
from-now.date(:date="line.pubDate * 1000" :now="now")
span.read(@click.stop="makeRead(line.id)") &#128065;
span.title(@click.stop="line.open = !line.open") {{ line.author }} {{ line.title }}
.content(v-if="line.open && line.body") {{ line.body }}
template(#error)
form(@submit.prevent="makeAuth")
p
label(for="server") Server:
2019-04-29 14:10:28 +00:00
input#server(v-model="newAuth.server" required)
p
label(for="username") Username:
2019-04-29 14:10:28 +00:00
input#username(v-model="newAuth.username" required)
p
label(for="token") Token:
2019-04-29 14:10:28 +00:00
input#token(v-model="newAuth.token" required)
p
input(type="submit" value="Connect")
2019-04-14 14:52:12 +00:00
</template>
<script>
2019-04-18 15:37:44 +00:00
/* global axios */
2019-04-29 14:10:28 +00:00
import connectedServiceVue from '../core/connectedService.vue'
2019-04-16 12:04:25 +00:00
import fromNowVue, { timerMinin } from '../core/fromNow.vue'
2019-04-14 14:52:12 +00:00
import Loadable from '../core/loadable/Loadable'
import Lists from '../core/Lists'
2019-04-14 14:52:12 +00:00
export default {
2019-04-18 15:37:44 +00:00
name: 'NextcloudNews',
2019-04-14 14:52:12 +00:00
components: {
fromNow: fromNowVue
},
2019-04-29 14:10:28 +00:00
extends: connectedServiceVue,
2019-04-18 15:37:44 +00:00
mixins: [ timerMinin ],
2019-04-14 14:52:12 +00:00
data() {
return {
2019-04-29 14:10:28 +00:00
rest: undefined, //NOTE: set in this.init()
news: new Loadable()
2019-04-18 15:37:44 +00:00
}
2019-04-14 14:52:12 +00:00
},
computed: {
2019-04-29 14:10:28 +00:00
params() {
return { timeout: 5000, buffer: -1, update: 5 * 60, showEmpty: true, ...this.options }
},
isSetup() {
return this.auth && this.auth.server && this.auth.username && this.auth.token
},
connector() {
return this.news
},
serviceName() {
return 'Nextcloud News'
},
hasNews() {
return this.news.isSuccess() && this.news.get().length > 0
},
2019-04-29 14:10:28 +00:00
showService() {
return this.params.showEmpty || this.hasNews || !this.isSetup
}
},
2019-04-14 14:52:12 +00:00
methods: {
loadData() {
this.news.load(
2019-04-29 14:10:28 +00:00
this.catchEmit(this.rest.get('/items', { params: { batchSize: this.params.buffer, type: 3, getRead: false } })),
res => res.data.items.map(n => {
2019-04-14 14:52:12 +00:00
n.open = false
return n
})
)
2019-04-14 14:52:12 +00:00
},
removeNews(id) {
Lists.removeFirst(this.news.get(), n => n.id === id)
2019-04-14 14:52:12 +00:00
},
makeRead(id) {
2019-04-17 11:36:17 +00:00
this.catchEmit(this.rest.put(`/items/${id}/read`))
2019-04-14 14:52:12 +00:00
.then(() => this.removeNews(id))
},
2019-04-29 14:10:28 +00:00
load() {
this.rest = axios.create({
baseURL: `https://${this.auth.server}/index.php/apps/news/api/v1-2/`,
timeout: this.params.timeout,
headers: {
Authorization: 'Basic ' + btoa(this.auth.username + ':' + this.auth.token)
}
}) //NOTE: required by this.params
this.loadData()
2019-04-29 14:10:28 +00:00
if(this.params.update > 0)
setInterval(this.loadData, this.params.update * 1000)
},
2019-04-29 14:10:28 +00:00
checkAuth({ server, username, token }){
return axios.get(`https://${server}/index.php/apps/news/api/v1-2/folders`, {
headers: { Authorization: 'Basic ' + btoa(username + ':' + token) },
timeout: this.params.timeout
})
2019-04-14 14:52:12 +00:00
}
}
}
</script>