monkeeShark/src/client/app/mobile/views/pages/home.timeline.vue

191 lines
4.5 KiB
Vue
Raw Normal View History

2018-04-26 05:01:41 +00:00
<template>
<div>
<ui-container v-if="src == 'home' && alone" :show-header="false" style="margin-bottom:8px;">
<div class="zrzngnxs">
<p>{{ $t('@.empty-timeline-info.follow-users-to-make-your-timeline') }}</p>
<router-link to="/explore">{{ $t('@.empty-timeline-info.explore') }}</router-link>
</div>
</ui-container>
2018-04-26 05:01:41 +00:00
<mk-notes ref="timeline" :more="existMore ? more : null">
<div slot="empty">
<fa :icon="['far', 'comments']"/>{{ $t('empty') }}
2018-04-26 05:01:41 +00:00
</div>
</mk-notes>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import i18n from '../../../i18n';
2018-04-26 05:01:41 +00:00
const fetchLimit = 10;
export default Vue.extend({
i18n: i18n('mobile/views/pages/home.timeline.vue'),
2018-04-26 05:01:41 +00:00
props: {
src: {
type: String,
required: true
},
tagTl: {
required: false
2018-04-26 05:01:41 +00:00
}
},
data() {
return {
fetching: true,
moreFetching: false,
existMore: false,
streamManager: null,
2018-04-26 05:01:41 +00:00
connection: null,
unreadCount: 0,
date: null,
baseQuery: {
includeMyRenotes: this.$store.state.settings.showMyRenotes,
includeRenotedMyNotes: this.$store.state.settings.showRenotedMyNotes,
includeLocalRenotes: this.$store.state.settings.showLocalRenotes
},
query: {},
endpoint: null
2018-04-26 05:01:41 +00:00
};
},
computed: {
alone(): boolean {
2018-05-27 04:49:09 +00:00
return this.$store.state.i.followingCount == 0;
2018-04-26 05:01:41 +00:00
},
canFetchMore(): boolean {
return !this.moreFetching && !this.fetching && this.existMore;
}
},
mounted() {
const prepend = note => {
(this.$refs.timeline as any).prepend(note);
};
if (this.src == 'tag') {
this.endpoint = 'notes/search_by_tag';
this.query = {
query: this.tagTl.query
};
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.connectToChannel('hashtag', { q: this.tagTl.query });
this.connection.on('note', prepend);
} else if (this.src == 'home') {
this.endpoint = 'notes/timeline';
const onChangeFollowing = () => {
this.fetch();
};
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.useSharedConnection('homeTimeline');
this.connection.on('note', prepend);
this.connection.on('follow', onChangeFollowing);
this.connection.on('unfollow', onChangeFollowing);
} else if (this.src == 'local') {
this.endpoint = 'notes/local-timeline';
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.useSharedConnection('localTimeline');
this.connection.on('note', prepend);
} else if (this.src == 'hybrid') {
this.endpoint = 'notes/hybrid-timeline';
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.useSharedConnection('hybridTimeline');
this.connection.on('note', prepend);
} else if (this.src == 'global') {
this.endpoint = 'notes/global-timeline';
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.useSharedConnection('globalTimeline');
this.connection.on('note', prepend);
} else if (this.src == 'mentions') {
this.endpoint = 'notes/mentions';
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.useSharedConnection('main');
this.connection.on('mention', prepend);
} else if (this.src == 'messages') {
this.endpoint = 'notes/mentions';
this.query = {
visibility: 'specified'
};
const onNote = note => {
if (note.visibility == 'specified') {
prepend(note);
}
};
2018-11-08 23:13:34 +00:00
this.connection = this.$root.stream.useSharedConnection('main');
this.connection.on('mention', onNote);
2018-04-26 05:01:41 +00:00
}
this.fetch();
},
beforeDestroy() {
this.connection.dispose();
2018-04-26 05:01:41 +00:00
},
methods: {
fetch() {
this.fetching = true;
(this.$refs.timeline as any).init(() => new Promise((res, rej) => {
2018-11-08 23:13:34 +00:00
this.$root.api(this.endpoint, Object.assign({
2018-04-26 05:01:41 +00:00
limit: fetchLimit + 1,
untilDate: this.date ? this.date.getTime() : undefined
}, this.baseQuery, this.query)).then(notes => {
2018-04-26 05:01:41 +00:00
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;
2018-11-08 23:13:34 +00:00
const promise = this.$root.api(this.endpoint, Object.assign({
2018-04-26 05:01:41 +00:00
limit: fetchLimit + 1,
untilId: (this.$refs.timeline as any).tail().id
}, this.baseQuery, this.query));
2018-05-26 14:53:22 +00:00
promise.then(notes => {
2018-04-26 05:01:41 +00:00
if (notes.length == fetchLimit + 1) {
notes.pop();
} else {
this.existMore = false;
}
for (const n of notes) {
(this.$refs.timeline as any).append(n);
}
2018-04-26 05:01:41 +00:00
this.moreFetching = false;
});
2018-05-26 14:53:22 +00:00
return promise;
2018-04-26 05:01:41 +00:00
},
focus() {
(this.$refs.timeline as any).focus();
},
warp(date) {
this.date = date;
this.fetch();
}
}
});
</script>
<style lang="stylus" scoped>
.zrzngnxs
padding 16px
text-align center
font-size 14px
> p
margin 0 0 8px 0
</style>