summaryrefslogtreecommitdiffstats
path: root/src/components/AdminSettings.vue
blob: f8922b57dde808d053add3116a0cf20694f5529d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!--
SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
SPDX-Licence-Identifier: AGPL-3.0-or-later
-->

<template>
	<NcSettingsSection :title="t('news', 'News')"
		class="news-settings"
		doc-url="https://nextcloud.github.io/news/admin/">
		<template v-if="lastCron !== 0">
			<NcNoteCard v-if="oldExecution" type="error">
				{{ t('news', 'Last job execution ran {relativeTime}. Something seems wrong.', {relativeTime}) }}
			</NcNoteCard>

			<NcNoteCard v-else type="success">
				{{ t('news', 'Last job ran {relativeTime}.', {relativeTime}) }}
			</NcNoteCard>
		</template>
		<NcCheckboxRadioSwitch type="switch"
			:checked.sync="useCronUpdates"
			@update:checked="update('useCronUpdates', useCronUpdates)">
			{{ t("news", "Use system cron for updates") }}
		</NcCheckboxRadioSwitch>
		<p>
			<em>{{
				t("news", "Disable this if you use a custom updater.")
			}}</em>
		</p>

		<NcTextField :value.sync="autoPurgeCount"
			:label="t('news', 'Maximum read count per feed')"
			:label-visible="true"
			@update:value="update('autoPurgeCount', autoPurgeCount)" />
		<p>
			<em>{{
				t(
					"news",
					"Defines the maximum amount of articles that can be read per feed which will not be deleted by the cleanup job; if old articles reappear after being read, increase this value; negative values such as -1 will turn this feature off."
				)
			}}</em>
		</p>

		<NcCheckboxRadioSwitch type="switch"
			:checked.sync="purgeUnread"
			@update:checked="update('purgeUnread', purgeUnread)">
			{{ t("news", "Delete unread articles automatically") }}
		</NcCheckboxRadioSwitch>
		<p>
			<em>{{
				t(
					"news",
					"Enable this if you also want to delete unread articles."
				)
			}}</em>
		</p>

		<NcTextField :value.sync="maxRedirects"
			:label="t('news', 'Maximum redirects')"
			:label-visible="true"
			@update:value="update('maxRedirects', maxRedirects)" />
		<p>
			<em>{{
				t("news", "How many redirects the feed fetcher should follow.")
			}}</em>
		</p>

		<NcTextField :value.sync="feedFetcherTimeout"
			:label="t('news', 'Feed fetcher timeout')"
			:label-visible="true"
			@update:value="update('feedFetcherTimeout', feedFetcherTimeout)" />
		<p>
			<em>{{
				t(
					"news",
					"Maximum number of seconds to wait for an RSS or Atom feed to load; if it takes longer the update will be aborted."
				)
			}}</em>
		</p>

		<NcTextField :value.sync="exploreUrl"
			:label="t('news', 'Explore Service URL')"
			:label-visible="true"
			@update:value="update('exploreUrl', exploreUrl)" />
		<p>
			<em>{{
				t(
					"news",
					"If given, this service's URL will be queried for displaying the feeds in the explore feed section. To fall back to the built in explore service, leave this input empty."
				)
			}}</em>
		</p>

		<NcTextField :value.sync="updateInterval"
			:label="t('news', 'Update interval')"
			:label-visible="true"
			@update:value="update('updateInterval', updateInterval)" />
		<p>
			<em>{{
				t(
					"news",
					"Interval in seconds in which the feeds will be updated."
				)
			}}</em>
		</p>
	</NcSettingsSection>
</template>

<script>
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import NcSettingsSection from '@nextcloud/vue/dist/Components/NcSettingsSection.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
import NcNoteCard from '@nextcloud/vue/dist/Components/NcNoteCard.js'
import moment from '@nextcloud/moment'
import { loadState } from '@nextcloud/initial-state'
import { showError, showSuccess } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { confirmPassword } from '@nextcloud/password-confirmation'

/**
 * Debounce helper for method
 * TODO: Should we remove this and use library?
 *
 * @param {Function} func - The callback function
 * @param {number}     wait - Time to wait in miliseconds
 */
function debounce(func, wait) {
	let timeout

	return function executedFunction(...args) {
		clearTimeout(timeout)
		timeout = setTimeout(() => {
			func.apply(this, args)
		}, wait)
	}
}

const successMessage = debounce(() => showSuccess(t('news', 'Successfully updated news configuration')), 500)
const lastCron = loadState('news', 'lastCron')

export default {
	name: 'AdminSettings',
	components: {
		NcCheckboxRadioSwitch,
		NcSettingsSection,
		NcTextField,
		NcNoteCard,
	},
	data() {
		return {
			useCronUpdates: loadState('news', 'useCronUpdates') === '1',
			autoPurgeCount: loadState('news', 'autoPurgeCount'),
			purgeUnread: loadState('news', 'purgeUnread') === '1',
			maxRedirects: loadState('news', 'maxRedirects'),
			feedFetcherTimeout: loadState('news', 'feedFetcherTimeout'),
			exploreUrl: loadState('news', 'exploreUrl'),
			updateInterval: loadState('news', 'updateInterval'),
			relativeTime: moment(lastCron * 1000).fromNow(),
			lastCron,
		}
	},
	computed: {
		oldExecution() {
			return Date.now() / 1000 - this.lastCron > (parseInt(this.updateInterval) * 2) + 900
		},
	},
	methods: {
		async update(key, value) {
			await confirmPassword()
			const url = generateOcsUrl(
				'/apps/provisioning_api/api/v1/config/apps/{appId}/{key}',
				{
					appId: 'news',
					key,
				},
			)
			if (key === 'useCronUpdates' || key === 'purgeUnread') {
				value = value ? '1' : '0'
			}
			try {
				const { data } = await axios.post(url, {
					value,
				})
				this.handleResponse({
					status: data.ocs?.meta?.status,
				})
			} catch (e) {
				this.handleResponse({
					errorMessage: t('news', 'Unable to update news config'),
					error: e,
				})
			}
		},
		handleResponse({ status, errorMessage, error }) {
			if (status !== 'ok') {
				showError(errorMessage)
				console.error(errorMessage, error)
			} else {
				successMessage()
			}
		},
	},
}
</script>

<style lang="scss" scoped>
.news-settings {
	p {
		max-width: 700px;
		margin-top: 0.25rem;
		margin-bottom: 1rem;
	}

	.input-field {
		max-width: 350px;
	}
}
</style>