feat(api,userbot,frontend): search peers without chats and start tracking them
This commit is contained in:
@@ -9,6 +9,7 @@ import type {
|
||||
Chat,
|
||||
ChatLinkView,
|
||||
DayCount,
|
||||
DiscoverItem,
|
||||
Folder,
|
||||
JobStatus,
|
||||
JobView,
|
||||
@@ -301,6 +302,37 @@ export function enqueueBackfill(
|
||||
});
|
||||
}
|
||||
|
||||
export function discoverPeers(
|
||||
query: string,
|
||||
remote = false
|
||||
): Promise<DiscoverItem[]> {
|
||||
return request<DiscoverItem[]>("/discover", {
|
||||
account: true,
|
||||
query: { query, remote },
|
||||
});
|
||||
}
|
||||
|
||||
export function getDiscoverItem(chatId: number): Promise<DiscoverItem> {
|
||||
return request<DiscoverItem>(`/discover/${chatId}`, { account: true });
|
||||
}
|
||||
|
||||
export function trackChat(
|
||||
chatId: number,
|
||||
backfill = true
|
||||
): Promise<DiscoverItem> {
|
||||
return request<DiscoverItem>(`/chats/${chatId}/track`, {
|
||||
method: "POST",
|
||||
body: { account_id: accounts.selectedId, backfill },
|
||||
});
|
||||
}
|
||||
|
||||
export function syncContacts(): Promise<{ job_id: number }> {
|
||||
return request<{ job_id: number }>("/contacts/sync", {
|
||||
method: "POST",
|
||||
body: { account_id: accounts.selectedId },
|
||||
});
|
||||
}
|
||||
|
||||
export function syncDialogs(): Promise<{ job_id: number }> {
|
||||
return request<{ job_id: number }>("/dialogs/sync", {
|
||||
method: "POST",
|
||||
|
||||
@@ -36,6 +36,21 @@ export interface Chat {
|
||||
title: string | null;
|
||||
}
|
||||
|
||||
export type DiscoverKind = "private" | "group" | "channel";
|
||||
|
||||
export interface DiscoverItem {
|
||||
chat_id: number;
|
||||
has_avatar: boolean;
|
||||
in_dialogs: boolean;
|
||||
is_bot: boolean;
|
||||
is_contact: boolean;
|
||||
kind: DiscoverKind;
|
||||
message_count: number;
|
||||
title: string | null;
|
||||
tracked: boolean;
|
||||
username: string | null;
|
||||
}
|
||||
|
||||
export interface EntityView {
|
||||
custom_emoji_id: string | null;
|
||||
language: string | null;
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { getDiscoverItem } from "$lib/api/endpoints";
|
||||
import type { DiscoverItem } from "$lib/api/types";
|
||||
import { accounts } from "$lib/stores/accounts.svelte";
|
||||
|
||||
function createDiscover() {
|
||||
let items = $state<Record<number, DiscoverItem>>({});
|
||||
let account: number | null = null;
|
||||
const pending = new Set<number>();
|
||||
|
||||
function syncAccount() {
|
||||
if (accounts.selectedId !== account) {
|
||||
account = accounts.selectedId;
|
||||
items = {};
|
||||
pending.clear();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
get(chatId: number): DiscoverItem | undefined {
|
||||
return items[chatId];
|
||||
},
|
||||
set(item: DiscoverItem) {
|
||||
items = { ...items, [item.chat_id]: item };
|
||||
},
|
||||
async ensure(chatId: number) {
|
||||
syncAccount();
|
||||
if (account === null || items[chatId] || pending.has(chatId)) {
|
||||
return;
|
||||
}
|
||||
pending.add(chatId);
|
||||
try {
|
||||
const item = await getDiscoverItem(chatId);
|
||||
items = { ...items, [chatId]: item };
|
||||
} finally {
|
||||
pending.delete(chatId);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const discover = createDiscover();
|
||||
@@ -1,16 +1,18 @@
|
||||
import { searchMessages } from "$lib/api/endpoints";
|
||||
import type { SearchHit } from "$lib/api/types";
|
||||
import { discoverPeers, searchMessages } from "$lib/api/endpoints";
|
||||
import type { DiscoverItem, SearchHit } from "$lib/api/types";
|
||||
import { chats } from "$lib/stores/chats.svelte";
|
||||
|
||||
const DEBOUNCE_MS = 250;
|
||||
const REMOTE_DEBOUNCE_MS = 700;
|
||||
const MIN_LENGTH = 1;
|
||||
|
||||
function createSearch() {
|
||||
let active = $state(false);
|
||||
let query = $state("");
|
||||
let messageHits = $state<SearchHit[]>([]);
|
||||
let peerResults = $state<DiscoverItem[]>([]);
|
||||
let loading = $state(false);
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let timers: ReturnType<typeof setTimeout>[] = [];
|
||||
let seq = 0;
|
||||
|
||||
const trimmed = $derived(query.trim());
|
||||
@@ -23,9 +25,12 @@ function createSearch() {
|
||||
(chat.title ?? "").toLowerCase().includes(needle)
|
||||
);
|
||||
});
|
||||
const peerHits = $derived.by(() => {
|
||||
const shown = new Set(chatHits.map((chat) => chat.chat_id));
|
||||
return peerResults.filter((item) => !shown.has(item.chat_id));
|
||||
});
|
||||
|
||||
async function run(value: string) {
|
||||
const current = ++seq;
|
||||
async function runMessages(value: string, current: number) {
|
||||
try {
|
||||
const hits = await searchMessages(value);
|
||||
if (current === seq) {
|
||||
@@ -42,21 +47,46 @@ function createSearch() {
|
||||
}
|
||||
}
|
||||
|
||||
function schedule() {
|
||||
if (timer) {
|
||||
async function runPeers(value: string, current: number, remote: boolean) {
|
||||
try {
|
||||
const items = await discoverPeers(value, remote);
|
||||
if (current === seq) {
|
||||
peerResults = items;
|
||||
}
|
||||
} catch {
|
||||
if (current === seq && !remote) {
|
||||
peerResults = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearTimers() {
|
||||
for (const timer of timers) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timers = [];
|
||||
}
|
||||
|
||||
function schedule() {
|
||||
clearTimers();
|
||||
const value = trimmed;
|
||||
const current = ++seq;
|
||||
if (value.length < MIN_LENGTH) {
|
||||
seq++;
|
||||
messageHits = [];
|
||||
peerResults = [];
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
loading = true;
|
||||
timer = setTimeout(() => {
|
||||
run(value).catch(() => undefined);
|
||||
}, DEBOUNCE_MS);
|
||||
timers.push(
|
||||
setTimeout(() => {
|
||||
runMessages(value, current).catch(() => undefined);
|
||||
runPeers(value, current, false).catch(() => undefined);
|
||||
}, DEBOUNCE_MS),
|
||||
setTimeout(() => {
|
||||
runPeers(value, current, true).catch(() => undefined);
|
||||
}, REMOTE_DEBOUNCE_MS)
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -78,6 +108,9 @@ function createSearch() {
|
||||
get chatHits() {
|
||||
return chatHits;
|
||||
},
|
||||
get peerHits() {
|
||||
return peerHits;
|
||||
},
|
||||
open() {
|
||||
active = true;
|
||||
},
|
||||
@@ -89,11 +122,10 @@ function createSearch() {
|
||||
active = false;
|
||||
query = "";
|
||||
messageHits = [];
|
||||
peerResults = [];
|
||||
loading = false;
|
||||
seq++;
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
clearTimers();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user