feat(api,userbot,frontend): search peers without chats and start tracking them

This commit is contained in:
hh committed 2026-08-05 23:46:54 +02:00
1 parent dcf95bd9d4
commit 92fd20137e
21 files changed
+936 -65

No files matched your search

+20 -4
View File
@@ -15,6 +15,7 @@
import { formatPresence } from "$lib/format/presence";
import { accounts } from "$lib/stores/accounts.svelte";
import { chats } from "$lib/stores/chats.svelte";
import { discover } from "$lib/stores/discover.svelte";
import { events } from "$lib/stores/events.svelte";
import { toasts } from "$lib/stores/toasts.svelte";
import { ui } from "$lib/stores/ui.svelte";
@@ -27,6 +28,7 @@
const isDm = $derived(chatId > 0);
const chat = $derived(chats.byId(chatId));
const discovered = $derived(discover.get(chatId));
let peer = $state<PeerView | null>(null);
let presence = $state<PresenceSample | null>(null);
let backfilling = $state(false);
@@ -46,6 +48,13 @@
}
}
$effect(() => {
if (accounts.selectedId === null) {
return;
}
discover.ensure(chatId).catch(() => undefined);
});
$effect(() => {
if (accounts.selectedId === null || !isDm) {
peer = null;
@@ -103,7 +112,9 @@
});
const fallbackTitle = $derived(
chat?.title ?? (isDm ? "Удалённый аккаунт" : `Chat ${chatId}`)
chat?.title ??
discovered?.title ??
(isDm ? "Удалённый аккаунт" : `Chat ${chatId}`)
);
const title = $derived(isDm && peer ? peerName(peer) : fallbackTitle);
const subtitle = $derived.by(() => {
@@ -116,11 +127,16 @@
}
return peer?.phone ?? `ID ${chatId}`;
}
const count = chat?.message_count ?? 0;
return count > 0 ? `${count} messages` : "group";
const count = chat?.message_count ?? discovered?.message_count ?? 0;
if (count > 0) {
return `${count} messages`;
}
return discovered?.kind === "channel" ? "channel" : "group";
});
const avatarKind = $derived(isDm ? "peer" : "chat");
const hasAvatar = $derived(chat?.has_avatar ?? Boolean(peer?.has_avatar));
const hasAvatar = $derived(
chat?.has_avatar ?? Boolean(peer?.has_avatar || discovered?.has_avatar)
);
</script>
<header class="chat-header">
@@ -8,7 +8,7 @@
import MessageBubble from "$lib/components/MessageBubble.svelte";
import MessageVersions from "$lib/components/MessageVersions.svelte";
import PinnedBar from "$lib/components/PinnedBar.svelte";
import EmptyState from "$lib/components/ui/EmptyState.svelte";
import TrackChat from "$lib/components/TrackChat.svelte";
import Icon from "$lib/components/ui/Icon.svelte";
import Spinner from "$lib/components/ui/Spinner.svelte";
import { formatDay } from "$lib/format/datetime";
@@ -450,10 +450,7 @@
{/each}
</div>
{:else if rows.length === 0}
<EmptyState
title="No messages"
description="This chat has no archived messages"
/>
<TrackChat {chatId} />
{:else}
<div class="messages-container">
{#if loadingOlder}
@@ -0,0 +1,118 @@
<script lang="ts">
import { enqueueBackfill, trackChat } from "$lib/api/endpoints";
import Button from "$lib/components/ui/Button.svelte";
import Icon from "$lib/components/ui/Icon.svelte";
import { accounts } from "$lib/stores/accounts.svelte";
import { chats } from "$lib/stores/chats.svelte";
import { discover } from "$lib/stores/discover.svelte";
import { toasts } from "$lib/stores/toasts.svelte";
interface Props {
chatId: number;
}
let { chatId }: Props = $props();
let busy = $state(false);
const item = $derived(discover.get(chatId));
const tracked = $derived(item?.tracked ?? false);
const title = $derived(item?.title ?? "Этот чат");
$effect(() => {
if (accounts.selectedId === null) {
return;
}
discover.ensure(chatId).catch(() => undefined);
});
async function track() {
if (busy) {
return;
}
busy = true;
try {
discover.set(await trackChat(chatId, true));
toasts.success("Отслеживание включено, история загружается");
chats.refresh();
} catch {
toasts.error("Не удалось включить отслеживание");
} finally {
busy = false;
}
}
async function backfill() {
if (busy) {
return;
}
busy = true;
try {
await enqueueBackfill(chatId, true);
toasts.success("Бэкфилл запущен");
} catch {
toasts.error("Не удалось запустить бэкфилл");
} finally {
busy = false;
}
}
</script>
<div class="track">
<div class="title">Здесь пока нет сообщений</div>
<p class="description">
{#if tracked}
{title}
отслеживается — новые сообщения и статистика собираются автоматически.
{:else}
Включите отслеживание, чтобы собирать сообщения, медиа и статистику по
этому чату.
{/if}
</p>
<div class="actions">
{#if tracked}
<Button variant="secondary" pill loading={busy} onclick={backfill}>
<Icon name="cloud-download" />
<span>Загрузить историю</span>
</Button>
{:else}
<Button variant="primary" pill loading={busy} onclick={track}>
<Icon name="stats" />
<span>Начать отслеживать</span>
</Button>
{/if}
</div>
</div>
<style lang="scss">
.track {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 80%;
padding: 0 1.5rem;
}
.title {
margin-bottom: 0.25rem;
font-size: 1.25rem;
text-align: center;
}
.description {
max-width: 22rem;
margin: 0 0 1rem;
font-size: 0.875rem;
color: var(--color-text-secondary);
text-align: center;
}
.actions {
display: flex;
gap: 0.5rem;
}
</style>
@@ -1,7 +1,11 @@
<script lang="ts">
import { onMount } from "svelte";
import { page } from "$app/state";
import { enqueueBackfill, syncDialogs } from "$lib/api/endpoints";
import {
enqueueBackfill,
syncContacts,
syncDialogs,
} from "$lib/api/endpoints";
import JobList from "$lib/components/jobs/JobList.svelte";
import Button from "$lib/components/ui/Button.svelte";
import Icon from "$lib/components/ui/Icon.svelte";
@@ -17,6 +21,7 @@
let filter = $state("");
let starting = $state(false);
let syncing = $state(false);
let syncingContacts = $state(false);
const availableChats = $derived(
chats.list
@@ -71,6 +76,22 @@
}
}
async function syncContactList() {
if (syncingContacts) {
return;
}
syncingContacts = true;
try {
await syncContacts();
toasts.success("Синхронизация контактов запущена");
version += 1;
} catch {
toasts.error("Не удалось синхронизировать контакты");
} finally {
syncingContacts = false;
}
}
onMount(() => {
chats.load();
});
@@ -89,6 +110,21 @@
<span>Синхронизировать диалоги</span>
</Button>
</div>
<p class="hint">
Контакты подтягиваются отдельно — тогда в поиске появятся люди, с которыми
ещё не было переписки.
</p>
<div class="action">
<Button
variant="secondary"
fluid
loading={syncingContacts}
onclick={syncContactList}
>
<Icon name="user" />
<span>Синхронизировать контакты</span>
</Button>
</div>
</section>
<section>
@@ -0,0 +1,122 @@
<script lang="ts">
import { ripple } from "$lib/actions/ripple";
import type { DiscoverItem } from "$lib/api/types";
import Avatar from "$lib/components/ui/Avatar.svelte";
interface Props {
item: DiscoverItem;
onclick: () => void;
}
let { item, onclick }: Props = $props();
const title = $derived(
item.title ?? (item.username ? `@${item.username}` : `Чат ${item.chat_id}`)
);
const avatarKind = $derived(item.chat_id > 0 ? "peer" : "chat");
const badge = $derived.by(() => {
if (item.is_bot) {
return "Бот";
}
if (item.kind === "channel") {
return "Канал";
}
if (item.kind === "group") {
return "Группа";
}
return item.is_contact ? "Контакт" : "Пользователь";
});
const status = $derived.by(() => {
if (item.message_count > 0) {
return `${item.message_count} сообщений`;
}
if (item.tracked) {
return "Отслеживается";
}
return "Нет в архиве";
});
</script>
<button type="button" class="Discover ListItem-button" use:ripple {onclick}>
<Avatar
name={title}
colorKey={item.chat_id}
avatar={{ kind: avatarKind, id: item.chat_id }}
hasAvatar={item.has_avatar}
/>
<div class="info">
<h3 class="title">{title}</h3>
<div class="subtitle">
<span class="badge">{badge}</span>
<span class="dot">·</span>
<span>{status}</span>
{#if item.username && item.title}
<span class="dot">·</span>
<span>@{item.username}</span>
{/if}
</div>
</div>
</button>
<style lang="scss">
.Discover {
cursor: pointer;
position: relative;
overflow: hidden;
display: flex;
align-items: center;
gap: 0.625rem;
width: 100%;
padding: 0.5625rem 0.5rem;
border: 0;
border-radius: 0.625rem;
text-align: start;
color: var(--color-text);
background-color: transparent;
transition: background-color 0.15s ease;
--ripple-color: var(--color-interactive-element-hover);
@media (hover: hover) {
&:hover {
background-color: var(--color-chat-hover);
}
}
}
.info {
overflow: hidden;
flex: 1;
}
.title {
overflow: hidden;
margin: 0;
font-weight: var(--font-weight-medium);
font-size: 1rem;
white-space: nowrap;
text-overflow: ellipsis;
}
.subtitle {
display: flex;
gap: 0.25rem;
overflow: hidden;
font-size: 0.875rem;
color: var(--color-text-secondary);
white-space: nowrap;
text-overflow: ellipsis;
}
.badge {
color: var(--color-primary);
}
.dot {
opacity: 0.6;
}
</style>
@@ -2,6 +2,7 @@
import { goto } from "$app/navigation";
import { page } from "$app/state";
import ChatListItem from "$lib/components/ChatListItem.svelte";
import DiscoverResultItem from "$lib/components/search/DiscoverResultItem.svelte";
import SearchMessageItem from "$lib/components/search/SearchMessageItem.svelte";
import EmptyState from "$lib/components/ui/EmptyState.svelte";
import Spinner from "$lib/components/ui/Spinner.svelte";
@@ -13,8 +14,11 @@
);
const hasChats = $derived(search.chatHits.length > 0);
const hasPeers = $derived(search.peerHits.length > 0);
const hasMessages = $derived(search.messageHits.length > 0);
const empty = $derived(!(search.loading || hasChats || hasMessages));
const empty = $derived(
!(search.loading || hasChats || hasPeers || hasMessages)
);
function openChat(chatId: number) {
search.close();
@@ -40,6 +44,13 @@
{/each}
{/if}
{#if hasPeers}
<div class="section-label">Контакты и каналы</div>
{#each search.peerHits as item (item.chat_id)}
<DiscoverResultItem {item} onclick={() => openChat(item.chat_id)} />
{/each}
{/if}
{#if search.loading && !hasMessages}
<div class="loading"><Spinner /></div>
{:else if hasMessages}