summaryrefslogtreecommitdiff
path: root/src/client/app/mobile/views/components/user-timeline.vue
blob: e8d7adc8b5d1ab7e74297468e00169f47808dfca (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
<template>
<div class="mk-user-timeline">
	<mk-notes ref="timeline" :more="existMore ? more : null">
		<div slot="empty">
			<fa :icon="['far', 'comments']"/>
			{{ withMedia ? this.$t('no-notes-with-media') : this.$t('no-notes') }}
		</div>
	</mk-notes>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
import i18n from '../../../i18n';

const fetchLimit = 10;

export default Vue.extend({
	i18n: i18n('mobile/views/components/user-timeline.vue'),
	props: ['user', 'withMedia'],

	data() {
		return {
			fetching: true,
			existMore: false,
			moreFetching: false
		};
	},

	computed: {
		canFetchMore(): boolean {
			return !this.moreFetching && !this.fetching && this.existMore;
		}
	},

	mounted() {
		this.fetch();
	},

	methods: {
		fetch() {
			this.fetching = true;
			(this.$refs.timeline as any).init(() => new Promise((res, rej) => {
				this.$root.api('users/notes', {
					userId: this.user.id,
					withFiles: this.withMedia,
					limit: fetchLimit + 1,
					untilDate: new Date().getTime() + 1000 * 86400 * 365
				}).then(notes => {
					if (notes.length == fetchLimit + 1) {
						notes.pop();
						this.existMore = true;
					}
					res(notes);
					this.fetching = false;
					this.$emit('loaded');
				}, rej);
			}));
		},

		more() {
			if (!this.canFetchMore) return;

			this.moreFetching = true;

			const promise = this.$root.api('users/notes', {
				userId: this.user.id,
				withFiles: this.withMedia,
				limit: fetchLimit + 1,
				untilDate: new Date((this.$refs.timeline as any).tail().createdAt).getTime()
			});

			promise.then(notes => {
				if (notes.length == fetchLimit + 1) {
					notes.pop();
				} else {
					this.existMore = false;
				}
				notes.forEach(n => (this.$refs.timeline as any).append(n));
				this.moreFetching = false;
			});

			return promise;
		}
	}
});
</script>