hippofish/packages/frontend/src/pages/following-feed.vue

362 lines
10 KiB
Vue
Raw Normal View History

2024-09-30 18:06:00 +02:00
<!--
SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->
<template>
2024-10-03 02:20:51 +02:00
<div :class="$style.root">
2024-10-03 04:00:32 +02:00
<MkPageHeader v-model:tab="currentTab" :class="$style.header" :tabs="headerTabs" :actions="headerActions" @update:tab="onChangeTab"/>
2024-10-03 02:20:51 +02:00
<div :class="$style.notes">
<MkHorizontalSwipe v-model:tab="currentTab" :tabs="headerTabs">
<MkPullToRefresh :refresher="() => reloadLatestNotes()">
<MkPagination ref="latestNotesPaging" :pagination="latestNotesPagination" @init="onListReady">
<template #empty>
<div class="_fullinfo">
<img :src="infoImageUrl" class="_ghost" :alt="i18n.ts.noNotes" aria-hidden="true"/>
<div>{{ i18n.ts.noNotes }}</div>
</div>
</template>
<template #default="{ items: notes }">
<MkDateSeparatedList v-slot="{ item: note }" :items="notes" :class="$style.panel" :noGap="true">
2024-10-03 06:45:49 +02:00
<FollowingFeedEntry v-if="!isHardMuted(note)" :isMuted="isSoftMuted(note)" :note="note" @select="userSelected"/>
2024-10-03 02:20:51 +02:00
</MkDateSeparatedList>
</template>
</MkPagination>
</MkPullToRefresh>
</MkHorizontalSwipe>
</div>
<div v-if="isWideViewport" :class="$style.user">
<MkHorizontalSwipe v-model:tab="currentTab" :tabs="headerTabs">
<MkPullToRefresh :refresher="() => reloadUserNotes()">
<div v-if="selectedUser" :class="$style.userInfo">
<MkUserInfo :class="$style.userInfo" class="user" :user="selectedUser"/>
<MkNotes :noGap="true" :pagination="userNotesPagination"/>
</div>
<div v-else-if="selectedUserError" :class="$style.panel">{{ selectedUserError }}</div>
<MkLoading v-else-if="selectedUserId"/>
</MkPullToRefresh>
</MkHorizontalSwipe>
</div>
</div>
2024-09-30 18:06:00 +02:00
</template>
<script lang="ts">
export type FollowingFeedTab = typeof followingTab | typeof mutualsTab;
export const followingTab = 'following' as const;
export const mutualsTab = 'mutuals' as const;
</script>
<script lang="ts" setup>
2024-10-02 04:21:08 +02:00
import { computed, Ref, ref, shallowRef } from 'vue';
import * as Misskey from 'misskey-js';
2024-09-30 18:06:00 +02:00
import { definePageMetadata } from '@/scripts/page-metadata.js';
import { i18n } from '@/i18n.js';
import MkHorizontalSwipe from '@/components/MkHorizontalSwipe.vue';
import MkPullToRefresh from '@/components/MkPullToRefresh.vue';
import MkPagination, { Paging } from '@/components/MkPagination.vue';
import { infoImageUrl } from '@/instance.js';
import MkDateSeparatedList from '@/components/MkDateSeparatedList.vue';
import { Tab } from '@/components/global/MkPageHeader.tabs.vue';
2024-09-30 19:56:10 +02:00
import { PageHeaderItem } from '@/types/page-header.js';
import FollowingFeedEntry from '@/components/FollowingFeedEntry.vue';
2024-10-02 00:28:04 +02:00
import MkNotes from '@/components/MkNotes.vue';
2024-10-02 04:21:08 +02:00
import MkUserInfo from '@/components/MkUserInfo.vue';
import { misskeyApi } from '@/scripts/misskey-api.js';
2024-10-02 17:38:21 +02:00
import { useRouter } from '@/router/supplier.js';
import * as os from '@/os.js';
2024-10-03 02:32:54 +02:00
import MkPageHeader from '@/components/global/MkPageHeader.vue';
2024-10-03 06:45:49 +02:00
import { $i } from '@/account.js';
2024-10-03 06:43:45 +02:00
import MkLoading from '@/components/global/MkLoading.vue';
2024-10-03 06:45:49 +02:00
import { getNoteText } from '@/scripts/check-word-mute.js';
2024-09-30 18:06:00 +02:00
const props = withDefaults(defineProps<{
initialTab?: FollowingFeedTab,
}>(), {
initialTab: followingTab,
});
2024-10-02 06:45:41 +02:00
const router = useRouter();
2024-09-30 18:06:00 +02:00
// Vue complains, but we *want* to lose reactivity here.
2024-10-02 06:44:26 +02:00
// Otherwise, the user would be unable to change the tab.
2024-09-30 18:06:00 +02:00
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
2024-10-02 04:21:08 +02:00
const currentTab: Ref<FollowingFeedTab> = ref(props.initialTab);
const mutualsOnly: Ref<boolean> = computed(() => currentTab.value === mutualsTab);
2024-09-30 18:06:00 +02:00
2024-10-02 06:45:41 +02:00
// We have to disable the per-user feed on small displays, and it must be done through JS instead of CSS.
2024-10-02 17:38:21 +02:00
// Otherwise, the second column will waste resources in the background.
const wideViewportQuery = window.matchMedia('(min-width: 750px)');
const isWideViewport: Ref<boolean> = ref(wideViewportQuery.matches);
wideViewportQuery.addEventListener('change', () => isWideViewport.value = wideViewportQuery.matches);
2024-10-02 06:45:41 +02:00
2024-10-02 04:21:08 +02:00
const selectedUserError: Ref<string> = ref('');
const selectedUserId: Ref<string> = ref('');
const selectedUser: Ref<Misskey.entities.UserDetailed | null> = ref(null);
2024-10-02 00:28:04 +02:00
2024-10-02 06:45:41 +02:00
async function userSelected(user: Misskey.entities.UserLite): Promise<void> {
2024-10-02 17:38:21 +02:00
if (isWideViewport.value) {
2024-10-02 06:45:41 +02:00
await showUserNotes(user.id);
} else {
if (user.host) {
router.push(`/@${user.username}@${user.host}`);
} else {
router.push(`/@${user.username}`);
}
}
}
async function showUserNotes(userId: string): Promise<void> {
2024-10-02 04:21:08 +02:00
selectedUserError.value = '';
2024-10-02 00:28:04 +02:00
selectedUserId.value = userId;
2024-10-02 04:21:08 +02:00
selectedUser.value = null;
if (userId) {
2024-10-03 02:26:22 +02:00
// Wait for 1 second to match the animation effects in MkHorizontalSwipe, MkPullToRefresh, and MkPagination.
// Otherwise, the page appears to load "backwards".
await new Promise(resolve => setTimeout(resolve, 1000));
// We need a User entity, but the pagination returns only UserLite.
// An additional request is needed to "upgrade" the object.
2024-10-02 04:21:08 +02:00
await misskeyApi('users/show', { userId })
.then(user => selectedUser.value = user)
.catch(error => {
console.error('Error fetching user info', error);
2024-10-02 06:09:10 +02:00
return selectedUserError.value =
typeof(error) === 'string'
? String(error)
: JSON.stringify(error);
2024-10-02 04:21:08 +02:00
});
}
2024-10-02 00:28:04 +02:00
}
2024-10-02 06:20:39 +02:00
async function reloadLatestNotes() {
await latestNotesPaging.value?.reload();
2024-10-02 00:28:04 +02:00
}
2024-10-02 06:20:39 +02:00
async function reloadUserNotes() {
2024-10-02 06:45:41 +02:00
await showUserNotes(selectedUserId.value);
2024-10-02 00:28:04 +02:00
}
2024-09-30 18:06:00 +02:00
2024-10-02 06:20:39 +02:00
async function reload() {
2024-10-02 00:28:04 +02:00
await Promise.all([
2024-10-02 06:20:39 +02:00
reloadLatestNotes(),
reloadUserNotes(),
2024-10-02 00:28:04 +02:00
]);
2024-09-30 18:06:00 +02:00
}
async function onListReady(): Promise<void> {
2024-10-02 06:20:39 +02:00
if (!selectedUserId.value && latestNotesPaging.value?.items.size) {
// This just gets the first user ID
2024-10-02 06:20:39 +02:00
const selectedNote: Misskey.entities.Note = latestNotesPaging.value.items.values().next().value;
2024-10-02 06:45:41 +02:00
await showUserNotes(selectedNote.userId);
}
}
2024-10-03 02:26:22 +02:00
async function onChangeTab(): Promise<void> {
await showUserNotes('');
}
2024-10-03 06:45:49 +02:00
const softMutePatterns = ref(buildMutePatterns($i?.mutedWords));
const hardMutePatterns = ref(buildMutePatterns($i?.hardMutedWords));
function buildMutePatterns(mutedWords: (string | string[])[] | undefined): RegExp[] {
if (!mutedWords || mutedWords.length < 1) {
return [];
}
// flags -> pattern[]
const patternMap = new Map<string, Set<string>>();
for (const mute of mutedWords) {
let flags: string;
let patterns: string[];
2024-10-03 06:49:47 +02:00
// Translate the pattern, which can be a raw string or a regular expression.
// For unknown reasons, raw strings are expressed as an array and expressions are plain strings.
2024-10-03 06:45:49 +02:00
if (!mute) {
continue;
} else if (Array.isArray(mute)) {
patterns = mute;
flags = 'i';
} else {
const match = mute.match(/^\/(.+)\/(.*)$/);
if (!match) {
continue;
} else {
patterns = [match[1]];
flags = match[2];
}
}
2024-10-03 06:49:47 +02:00
// Group the patterns based on shared flags
2024-10-03 06:45:49 +02:00
let flagPatterns = patternMap.get(flags);
if (!flagPatterns) {
flagPatterns = new Set<string>();
patternMap.set(flags, flagPatterns);
}
for (const pattern of patterns) {
flagPatterns.add(pattern);
}
}
2024-10-03 06:49:47 +02:00
// Parse all the patterns into regular expressions
2024-10-03 06:45:49 +02:00
return Array
.from(patternMap)
.map(([flag, patterns]) => {
const pattern = Array.from(patterns).map(p => `(${p})`).join('|');
return new RegExp(pattern, flag);
});
}
// Adapted from MkNote.ts
function isSoftMuted(note: Misskey.entities.Note): boolean {
return isMuted(note, softMutePatterns.value);
}
function isHardMuted(note: Misskey.entities.Note): boolean {
return isMuted(note, hardMutePatterns.value);
}
function isMuted(note: Misskey.entities.Note, mutes: RegExp[]): boolean {
if (mutes.length < 1) return false;
return checkMute(note, mutes)
|| checkMute(note.reply, mutes)
|| checkMute(note.renote, mutes);
}
// Adapted from check-word-mute.ts
function checkMute(note: Misskey.entities.Note | undefined | null, mutes: RegExp[]): boolean {
if (!note) {
return false;
}
const noteText = getNoteText(note);
return mutes.some(p => p.test(noteText));
}
2024-10-02 06:20:39 +02:00
const latestNotesPaging = shallowRef<InstanceType<typeof MkPagination>>();
const latestNotesPagination: Paging<'notes/following'> = {
2024-09-30 18:06:00 +02:00
endpoint: 'notes/following' as const,
limit: 20,
params: computed(() => ({
mutualsOnly: mutualsOnly.value,
})),
};
const withUserRenotes = ref(false);
const withUserReplies = ref(true);
2024-10-02 06:20:39 +02:00
const userNotesPagination: Paging<'users/notes'> = {
2024-10-02 00:28:04 +02:00
endpoint: 'users/notes' as const,
limit: 10,
params: computed(() => ({
userId: selectedUserId.value,
withRenotes: withUserRenotes.value,
withReplies: withUserReplies.value,
2024-10-02 00:28:04 +02:00
})),
};
2024-10-02 23:08:12 +02:00
const headerActions = computed(() => isWideViewport.value ? [
2024-09-30 19:56:10 +02:00
{
icon: 'ti ti-refresh',
text: i18n.ts.reload,
2024-10-02 06:20:39 +02:00
handler: () => reload(),
2024-10-02 23:08:12 +02:00
} satisfies PageHeaderItem,
{
icon: 'ti ti-dots',
text: i18n.ts.options,
handler: (ev) => {
os.popupMenu([
{
type: 'switch',
text: i18n.ts.showRenotes,
ref: withUserRenotes,
}, {
type: 'switch',
text: i18n.ts.showRepliesToOthersInTimeline,
ref: withUserReplies,
},
], ev.currentTarget ?? ev.target);
},
2024-10-02 23:08:12 +02:00
} satisfies PageHeaderItem,
] : []);
2024-09-30 19:56:10 +02:00
2024-09-30 18:06:00 +02:00
const headerTabs = computed(() => [
{
key: followingTab,
2024-10-02 17:38:21 +02:00
icon: 'ph-user-check ph-bold ph-lg',
2024-09-30 18:06:00 +02:00
title: i18n.ts.following,
} satisfies Tab,
{
key: mutualsTab,
2024-10-02 17:38:21 +02:00
icon: 'ph-user-switch ph-bold ph-lg',
2024-09-30 18:06:00 +02:00
title: i18n.ts.mutuals,
} satisfies Tab,
]);
definePageMetadata(() => ({
title: i18n.ts.following,
2024-10-02 17:38:21 +02:00
icon: 'ph-user-check ph-bold ph-lg',
2024-09-30 18:06:00 +02:00
}));
</script>
<style lang="scss" module>
2024-10-03 02:20:51 +02:00
.root {
display: grid;
grid-template-columns: min-content 1fr min-content;
grid-template-rows: min-content 1fr;
grid-template-areas:
"header header header"
2024-10-03 05:35:40 +02:00
"lm notes rm";
2024-10-03 02:20:51 +02:00
gap: 12px;
height: 100vh;
max-width: 1200px;
overflow: hidden;
2024-10-02 00:28:04 +02:00
}
2024-10-03 02:20:51 +02:00
.header {
grid-area: header;
2024-10-02 00:28:04 +02:00
}
2024-10-03 02:20:51 +02:00
.notes {
grid-area: notes;
overflow: auto;
2024-10-02 00:28:04 +02:00
}
2024-10-03 02:20:51 +02:00
.user {
grid-area: user;
overflow: auto;
2024-10-02 00:28:04 +02:00
}
2024-10-03 02:20:51 +02:00
.userInfo {
margin-bottom: 12px;
2024-10-02 00:28:04 +02:00
}
2024-10-03 02:20:51 +02:00
@container (min-width: 751px) {
.root {
grid-template-columns: min-content 4fr 6fr min-content;
grid-template-rows: min-content 1fr;
grid-template-areas:
"header header header header"
"lm notes user rm";
gap: 24px;
2024-10-02 00:28:04 +02:00
}
2024-10-03 02:20:51 +02:00
.userInfo {
margin-bottom: 24px;
2024-10-02 00:28:04 +02:00
}
}
2024-10-02 04:21:08 +02:00
2024-10-03 02:20:51 +02:00
.panel {
background: var(--panel);
2024-10-02 04:21:08 +02:00
}
2024-09-30 18:06:00 +02:00
</style>