Compare commits

..
1 Commits
Author SHA1 Message Date
renovate Bot 6e46b210d5 chore(deps): update ghcr.io/astral-sh/uv:python3.13-trixie-slim docker digest to 331fbb1
renovate/stability-days Updates have met minimum release age requirement
2026-10-22 04:02:47 +00:00
37 changed files with 95 additions and 1031 deletions

No files matched your search

+4 -5
View File
@@ -298,11 +298,10 @@ async def get_feed(
"""Deleted and edited messages and profile changes across chats, newest first. """Deleted and edited messages and profile changes across chats, newest first.
`kind` picks deleted, edited, profile, messages (deleted and edited) or all; `kind` picks deleted, edited, profile, messages (deleted and edited) or all;
`since` keeps items at or after that time; `chat_id` or `chat_kind` (dm `since` keeps items at or after that time; `chat_id` or `chat_kind` (dm,
with people, bot, group, channel) narrow it down. Edited items carry group, channel) narrow it down. Edited items carry `before` and `after`
`before` and `after` texts. A deleted item with `message_count` above 1 is texts. A deleted item with `message_count` above 1 is a bulk deletion in
a bulk deletion in one chat; read its messages with `get_deleted_messages` one chat; read its messages with `get_deleted_messages` for that chat.
for that chat.
Profile items cover people in private chats or contacts and chats in the Profile items cover people in private chats or contacts and chats in the
dialog list: `detail` maps `name`, `username`, `photo` (unique file ids) or dialog list: `detail` maps `name`, `username`, `photo` (unique file ids) or
`title` to `[old, new]`, changes a day apart or closer are merged. Pass `title` to `[old, new]`, changes a day apart or closer are merged. Pass
+1 -8
View File
@@ -29,7 +29,6 @@ async def list_feed(
kind: Annotated[FeedKind, Query()] = FeedKind.ALL, kind: Annotated[FeedKind, Query()] = FeedKind.ALL,
chat_id: Annotated[int | None, Query()] = None, chat_id: Annotated[int | None, Query()] = None,
chat_kind: Annotated[FeedChatKind | None, Query()] = None, chat_kind: Annotated[FeedChatKind | None, Query()] = None,
folder_id: Annotated[int | None, Query()] = None,
since: Annotated[datetime | None, Query()] = None, since: Annotated[datetime | None, Query()] = None,
cursor: Cursor = None, cursor: Cursor = None,
limit: Limit = DEFAULT_LIMIT, limit: Limit = DEFAULT_LIMIT,
@@ -37,13 +36,7 @@ async def list_feed(
return await feed.list_feed( return await feed.list_feed(
pool, pool,
account_id, account_id,
FeedFilter( FeedFilter(kind=kind, chat_id=chat_id, chat_kind=chat_kind, since=since),
kind=kind,
chat_id=chat_id,
chat_kind=chat_kind,
folder_id=folder_id,
since=since,
),
limit=limit, limit=limit,
cursor=cursor, cursor=cursor,
) )
+2 -19
View File
@@ -5,7 +5,7 @@ from dishka.integrations.fastapi import DishkaRoute, FromDishka
from fastapi import APIRouter, HTTPException, Query from fastapi import APIRouter, HTTPException, Query
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from utils.files import content_disposition, sniff_media_kind, story_file_name from utils.files import content_disposition, story_file_name
from utils.read import peers from utils.read import peers
from utils.read.models import DEFAULT_LIMIT, Page, StoryView from utils.read.models import DEFAULT_LIMIT, Page, StoryView
from utils.storage import ContentAddressedStorage from utils.storage import ContentAddressedStorage
@@ -17,34 +17,17 @@ AccountId = Annotated[int, Query()]
_STORY_MIME = {"photo": "image/jpeg", "video": "video/mp4"} _STORY_MIME = {"photo": "image/jpeg", "video": "video/mp4"}
async def _with_kind(
pool: asyncpg.Pool,
storage: ContentAddressedStorage,
account_id: int,
story: StoryView,
) -> StoryView:
if story.media_kind or not story.storage_key:
return story
if not storage.exists(story.storage_key):
return story
kind = sniff_media_kind(storage.head(story.storage_key))
await peers.set_story_kind(pool, account_id, story.peer_id, story.story_id, kind)
return story.model_copy(update={"media_kind": kind})
@router.get("/stories") @router.get("/stories")
async def list_stories( async def list_stories(
pool: FromDishka[asyncpg.Pool], pool: FromDishka[asyncpg.Pool],
storage: FromDishka[ContentAddressedStorage],
account_id: AccountId, account_id: AccountId,
peer_id: Annotated[int | None, Query()] = None, peer_id: Annotated[int | None, Query()] = None,
limit: Annotated[int, Query()] = DEFAULT_LIMIT, limit: Annotated[int, Query()] = DEFAULT_LIMIT,
offset: Annotated[int, Query()] = 0, offset: Annotated[int, Query()] = 0,
) -> list[StoryView]: ) -> list[StoryView]:
stories = await peers.get_stories( return await peers.get_stories(
pool, account_id, Page(limit=limit, offset=offset), peer_id=peer_id pool, account_id, Page(limit=limit, offset=offset), peer_id=peer_id
) )
return [await _with_kind(pool, storage, account_id, story) for story in stories]
@router.get("/stories/{peer_id}/{story_id}/media") @router.get("/stories/{peer_id}/{story_id}/media")
@@ -6,7 +6,7 @@ _UPSERT_STORY = """
INSERT INTO stories INSERT INTO stories
(account_id, peer_id, story_id, date, expire_date, caption, media_kind, (account_id, peer_id, story_id, date, expire_date, caption, media_kind,
storage_key, file_size, downloaded, views, pinned, deleted, raw, observed_at) storage_key, file_size, downloaded, views, pinned, deleted, raw, observed_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, false, $13::jsonb, now()) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14::jsonb, now())
ON CONFLICT (account_id, peer_id, story_id) DO UPDATE SET ON CONFLICT (account_id, peer_id, story_id) DO UPDATE SET
date = EXCLUDED.date, date = EXCLUDED.date,
expire_date = EXCLUDED.expire_date, expire_date = EXCLUDED.expire_date,
@@ -17,17 +17,10 @@ ON CONFLICT (account_id, peer_id, story_id) DO UPDATE SET
downloaded = stories.downloaded OR EXCLUDED.downloaded, downloaded = stories.downloaded OR EXCLUDED.downloaded,
views = EXCLUDED.views, views = EXCLUDED.views,
pinned = EXCLUDED.pinned, pinned = EXCLUDED.pinned,
deleted = false, deleted = EXCLUDED.deleted,
raw = EXCLUDED.raw raw = EXCLUDED.raw
""" """
_MARK_DELETED = """
INSERT INTO stories
(account_id, peer_id, story_id, downloaded, pinned, deleted, raw, observed_at)
VALUES ($1, $2, $3, false, false, true, '{}'::jsonb, now())
ON CONFLICT (account_id, peer_id, story_id) DO UPDATE SET deleted = true
"""
async def is_downloaded( async def is_downloaded(
pool: asyncpg.Pool, account_id: int, peer_id: int, story_id: int pool: asyncpg.Pool, account_id: int, peer_id: int, story_id: int
@@ -58,6 +51,7 @@ async def upsert_story( # noqa: PLR0913
raw: str, raw: str,
*, *,
pinned: bool, pinned: bool,
deleted: bool,
downloaded: bool, downloaded: bool,
) -> None: ) -> None:
await pool.execute( await pool.execute(
@@ -74,11 +68,6 @@ async def upsert_story( # noqa: PLR0913
downloaded, downloaded,
views, views,
pinned, pinned,
deleted,
raw, raw,
) )
async def mark_deleted(
pool: asyncpg.Pool, account_id: int, peer_id: int, story_id: int
) -> None:
await pool.execute(_MARK_DELETED, account_id, peer_id, story_id)
@@ -17,18 +17,13 @@ def story_peer_id(story: Story) -> int:
async def save_story(client: Client, capture: CaptureContext, story: Story) -> None: async def save_story(client: Client, capture: CaptureContext, story: Story) -> None:
peer_id = story_peer_id(story) peer_id = story_peer_id(story)
if story.deleted:
await repository.mark_deleted(
capture.pool, capture.account_id, peer_id, story.id
)
return
storage_key: str | None = None storage_key: str | None = None
file_size: int | None = None file_size: int | None = None
downloaded = False downloaded = False
stored = await repository.is_downloaded( stored = await repository.is_downloaded(
capture.pool, capture.account_id, peer_id, story.id capture.pool, capture.account_id, peer_id, story.id
) )
if not (stored or story.media is None): if not (stored or story.deleted or story.media is None):
data = await download_bytes(client, story) data = await download_bytes(client, story)
if data is not None: if data is not None:
storage_key = capture.storage.put(data) storage_key = capture.storage.put(data)
@@ -48,5 +43,6 @@ async def save_story(client: Client, capture: CaptureContext, story: Story) -> N
story.views, story.views,
raw_json(story.raw), raw_json(story.raw),
pinned=bool(story.pinned), pinned=bool(story.pinned),
deleted=bool(story.deleted),
downloaded=downloaded, downloaded=downloaded,
) )
-5
View File
@@ -191,11 +191,6 @@ def story_file_name(peer_id: int, story_id: int, media_kind: str | None) -> str:
return f"story_{peer_id}_{story_id}{extension_for(media_kind or 'photo', None)}" return f"story_{peer_id}_{story_id}{extension_for(media_kind or 'photo', None)}"
def sniff_media_kind(head: bytes) -> str:
is_video = head[4:8] == b"ftyp" or head.startswith(b"\x1aE\xdf\xa3")
return "video" if is_video else "photo"
def resolve_mime( def resolve_mime(
kind: str | None, mime: str | None, file_name: str | None = None kind: str | None, mime: str | None, file_name: str | None = None
) -> str: ) -> str:
+4 -41
View File
@@ -5,7 +5,6 @@ from enum import StrEnum
import asyncpg import asyncpg
from utils.policy import repository as policy_repository
from utils.read import chats, peers from utils.read import chats, peers
from utils.read.accounts import self_user_id from utils.read.accounts import self_user_id
from utils.read.models import ( from utils.read.models import (
@@ -46,13 +45,6 @@ LEFT JOIN LATERAL (
) p ON true ) p ON true
""" """
_IS_BOT = """EXISTS (
SELECT 1 FROM peers p
WHERE p.account_id = feed_items.account_id AND p.peer_id = feed_items.chat_id
AND COALESCE((p.raw->>'is_bot')::bool, (p.raw->>'bot')::bool,
p.raw->>'type' = 'ChatType.BOT', false)
)"""
_UNSEEN = """ _UNSEEN = """
SELECT count(*) FILTER (WHERE kind = 'deleted') AS deleted, SELECT count(*) FILTER (WHERE kind = 'deleted') AS deleted,
count(*) FILTER (WHERE kind = 'edited') AS edited count(*) FILTER (WHERE kind = 'edited') AS edited
@@ -77,25 +69,15 @@ class FeedKind(StrEnum):
class FeedChatKind(StrEnum): class FeedChatKind(StrEnum):
DM = "dm" DM = "dm"
BOT = "bot"
GROUP = "group" GROUP = "group"
CHANNEL = "channel" CHANNEL = "channel"
_CHAT_KIND_CLAUSES = {
FeedChatKind.DM: f"chat_kind = 'dm' AND NOT {_IS_BOT}",
FeedChatKind.BOT: f"chat_kind = 'dm' AND {_IS_BOT}",
FeedChatKind.GROUP: "chat_kind = 'group'",
FeedChatKind.CHANNEL: "chat_kind = 'channel'",
}
@dataclass(slots=True) @dataclass(slots=True)
class FeedFilter: class FeedFilter:
kind: FeedKind = FeedKind.ALL kind: FeedKind = FeedKind.ALL
chat_id: int | None = None chat_id: int | None = None
chat_kind: FeedChatKind | None = None chat_kind: FeedChatKind | None = None
folder_id: int | None = None
since: datetime | None = None since: datetime | None = None
@@ -108,34 +90,18 @@ def decode_cursor(cursor: str) -> tuple[datetime, int]:
return _EPOCH + int(micros) * _MICROSECOND, int(key) return _EPOCH + int(micros) * _MICROSECOND, int(key)
async def _folder_chat_ids(
pool: asyncpg.Pool, account_id: int, folder_id: int | None
) -> list[int] | None:
if folder_id is None:
return None
folder = await policy_repository.get_folder(pool, account_id, folder_id)
if folder is None:
return []
return await chats.folder_chat_ids(pool, account_id, folder)
def _where( def _where(
account_id: int, account_id: int, feed: FeedFilter, cursor: str | None
feed: FeedFilter,
folder_chat_ids: list[int] | None,
cursor: str | None,
) -> tuple[str, list[object]]: ) -> tuple[str, list[object]]:
params: list[object] = [account_id] params: list[object] = [account_id]
clauses = ["account_id = $1"] clauses = ["account_id = $1"]
if feed.kind is FeedKind.MESSAGES: if feed.kind is FeedKind.MESSAGES:
clauses.append("kind <> 'profile'") clauses.append("kind <> 'profile'")
if feed.chat_kind is not None:
clauses.append(_CHAT_KIND_CLAUSES[feed.chat_kind])
exact = feed.kind not in {FeedKind.ALL, FeedKind.MESSAGES} exact = feed.kind not in {FeedKind.ALL, FeedKind.MESSAGES}
conditions = ( conditions = (
("kind = {}", feed.kind.value if exact else None), ("kind = {}", feed.kind.value if exact else None),
("chat_id = {}", feed.chat_id), ("chat_id = {}", feed.chat_id),
("chat_id = ANY({}::bigint[])", folder_chat_ids), ("chat_kind = {}", feed.chat_kind.value if feed.chat_kind else None),
("at >= {}", feed.since), ("at >= {}", feed.since),
) )
for template, value in conditions: for template, value in conditions:
@@ -225,9 +191,7 @@ async def _build_items(
chat=FeedChat( chat=FeedChat(
chat_id=row["chat_id"], chat_id=row["chat_id"],
title=chat.title if chat else None, title=chat.title if chat else None,
kind=FeedChatKind.BOT kind=row["chat_kind"],
if chat and chat.is_bot and row["chat_kind"] == FeedChatKind.DM
else row["chat_kind"],
has_avatar=chat.has_avatar if chat else False, has_avatar=chat.has_avatar if chat else False,
), ),
message_id=row["message_id"], message_id=row["message_id"],
@@ -254,8 +218,7 @@ async def list_feed(
cursor: str | None = None, cursor: str | None = None,
) -> FeedPage: ) -> FeedPage:
size = max(1, min(limit, MAX_LIMIT)) size = max(1, min(limit, MAX_LIMIT))
folder_chat_ids = await _folder_chat_ids(pool, account_id, feed.folder_id) where, params = _where(account_id, feed, cursor)
where, params = _where(account_id, feed, folder_chat_ids, cursor)
params.append(size + 1) params.append(size + 1)
rows = await pool.fetch( rows = await pool.fetch(
f"SELECT {_ITEM_COLS} FROM feed_items WHERE {where} " # noqa: S608 f"SELECT {_ITEM_COLS} FROM feed_items WHERE {where} " # noqa: S608
+1 -14
View File
@@ -76,7 +76,7 @@ async def get_stories(
"SELECT peer_id, story_id, date, expire_date, caption, media_kind, " # noqa: S608 "SELECT peer_id, story_id, date, expire_date, caption, media_kind, " # noqa: S608
"storage_key, downloaded, views, pinned, deleted FROM stories " "storage_key, downloaded, views, pinned, deleted FROM stories "
f"WHERE {where} " f"WHERE {where} "
f"ORDER BY date DESC NULLS LAST, story_id DESC LIMIT ${len(params) - 1} " f"ORDER BY date DESC NULLS LAST LIMIT ${len(params) - 1} "
f"OFFSET ${len(params)}", f"OFFSET ${len(params)}",
*params, *params,
) )
@@ -95,16 +95,3 @@ async def get_story(
story_id, story_id,
) )
return StoryView(**dict(row)) if row else None return StoryView(**dict(row)) if row else None
async def set_story_kind(
pool: asyncpg.Pool, account_id: int, peer_id: int, story_id: int, kind: str
) -> None:
await pool.execute(
"UPDATE stories SET media_kind = $4 "
"WHERE account_id = $1 AND peer_id = $2 AND story_id = $3",
account_id,
peer_id,
story_id,
kind,
)
-65
View File
@@ -1,65 +0,0 @@
import re
from collections.abc import Iterator
from typing import Any
from utils.search.models import Found, FoundWhere
HIDDEN_KEYS = ("chat", "from_user", "sender_chat")
SKIPPED_KEYS = ("_", "file_id", "file_unique_id")
ENUM = re.compile(r"^[A-Z][A-Za-z]+\.[A-Z_]+$")
_HEADS: dict[str, FoundWhere] = {
"text": "text",
"caption": "text",
"web_page": "preview",
}
_LEAVES: dict[str, FoundWhere] = {"url": "link", "file_name": "file"}
type Path = tuple[str | int, ...]
def _scalars(node: object, path: Path = ()) -> Iterator[tuple[Path, str, object]]:
if isinstance(node, dict):
items = ((k, v) for k, v in node.items() if k not in SKIPPED_KEYS)
elif isinstance(node, list):
items = enumerate(node)
else:
return
for key, value in items:
if isinstance(value, dict | list):
yield from _scalars(value, (*path, key))
elif isinstance(value, str | int | float) and not isinstance(value, bool):
yield (*path, key), str(value), node
def _where(path: Path) -> FoundWhere:
head, leaf = str(path[0]), str(path[-1])
if head == "reply_markup":
return "button"
if leaf in _LEAVES:
return _LEAVES[leaf]
if head.startswith("forward"):
return "forward"
if head.startswith("reply_to"):
return "reply"
return _HEADS.get(head, "data")
def find(raw: dict[str, Any], needle: str) -> Found | None:
folded = needle.casefold()
fallback = None
for path, value, owner in _scalars(raw):
if folded not in value.casefold() or ENUM.match(value):
continue
where = _where(path)
button = owner.get("text") if isinstance(owner, dict) else None
found = Found(
where=where,
path=".".join(map(str, path)),
value=value,
button=button if where == "button" else None,
)
if where != "text":
return found
fallback = fallback or found
return fallback
+4 -17
View File
@@ -10,10 +10,7 @@ _EPOCH = datetime(1970, 1, 1, tzinfo=UTC)
_MICROSECOND = timedelta(microseconds=1) _MICROSECOND = timedelta(microseconds=1)
ChatKind = Literal["private", "group", "channel"] ChatKind = Literal["private", "group", "channel"]
HitSource = Literal["text", "stt", "version", "data"] HitSource = Literal["text", "stt", "version"]
FoundWhere = Literal[
"text", "button", "link", "forward", "reply", "file", "preview", "data"
]
def encode_cursor(date: datetime, chat_id: int, message_id: int) -> str: def encode_cursor(date: datetime, chat_id: int, message_id: int) -> str:
@@ -37,7 +34,6 @@ class SearchFilters(BaseModel):
date_from: datetime | None = None date_from: datetime | None = None
date_to: datetime | None = None date_to: datetime | None = None
regex: str | None = None regex: str | None = None
everywhere: bool = False
cursor: str | None = None cursor: str | None = None
limit: int = DEFAULT_LIMIT limit: int = DEFAULT_LIMIT
@@ -49,13 +45,6 @@ class SearchFilters(BaseModel):
return value return value
class Found(BaseModel):
where: FoundWhere
path: str
value: str
button: str | None = None
class SearchHit(BaseModel): class SearchHit(BaseModel):
chat_id: int chat_id: int
message_id: int message_id: int
@@ -67,14 +56,12 @@ class SearchHit(BaseModel):
edited_at: datetime | None edited_at: datetime | None
deleted_at: datetime | None deleted_at: datetime | None
source: HitSource source: HitSource
extracted_text: str | None = None extracted_text: str | None
version_text: str | None = None version_text: str | None
version_at: datetime | None = None version_at: datetime | None
found: Found | None = None
class SearchPage(BaseModel): class SearchPage(BaseModel):
hits: list[SearchHit] hits: list[SearchHit]
next_cursor: str | None next_cursor: str | None
terms: list[str] terms: list[str]
scanned_to: datetime | None = None
+3 -95
View File
@@ -1,12 +1,5 @@
import json
import re
from datetime import UTC, datetime, timedelta
from time import monotonic
import asyncpg import asyncpg
from utils.read.message_view import load_raw
from utils.search.found import ENUM, HIDDEN_KEYS, SKIPPED_KEYS, find
from utils.search.models import ( from utils.search.models import (
MAX_LIMIT, MAX_LIMIT,
ChatKind, ChatKind,
@@ -17,12 +10,6 @@ from utils.search.models import (
encode_cursor, encode_cursor,
) )
MIN_NEEDLE = 3
_WINDOW = timedelta(days=7)
_BUDGET_SECONDS = 2.5
_BOTTOM = -(2**63)
_LIKE_SPECIAL = re.compile(r"([\\%_])")
_PARSE = """ _PARSE = """
SELECT (regexp_replace(websearch_to_tsquery('russian', q)::text, SELECT (regexp_replace(websearch_to_tsquery('russian', q)::text,
'''((?:[^'']|''''){3,})''', '''\\1'':*', 'g')::tsquery '''((?:[^'']|''''){3,})''', '''\\1'':*', 'g')::tsquery
@@ -89,24 +76,6 @@ _WITH_MEDIA = (
_SOURCES = ("text", "stt", "version") _SOURCES = ("text", "stt", "version")
_OLDEST = "SELECT min(date) FROM message_search WHERE account_id = $1"
_VALUE_MATCH = (
'strict $.** ? (@.type() == "object").keyvalue() ? ('
+ "".join(f'@.key != "{key}" && ' for key in SKIPPED_KEYS)
+ '(@.value.type() == "string" || @.value.type() == "number") && '
f"!(@.value.string() like_regex {json.dumps(ENUM.pattern)}) && "
'@.value.string() like_regex {needle} flag "iq")'
)
_EVERYWHERE = (
f"SELECT {_COLS}, s.raw - {{hidden}}::text[] AS raw " # noqa: S608
"FROM messages s WHERE {where} "
"AND CASE WHEN s.raw::text ILIKE {like} "
"THEN jsonb_path_exists(s.raw - {hidden}::text[], {path}::jsonpath) "
f"ELSE false END ORDER BY {_KEY_ORDER} LIMIT {{limit}}"
)
class _Binder: class _Binder:
def __init__(self) -> None: def __init__(self) -> None:
@@ -161,72 +130,9 @@ def _branch(sql: str, limit: str, order: str = _KEY_ORDER, **parts: str) -> str:
async def search_messages(pool: asyncpg.Pool, filters: SearchFilters) -> SearchPage: async def search_messages(pool: asyncpg.Pool, filters: SearchFilters) -> SearchPage:
async with pool.acquire() as conn, conn.transaction(): async with pool.acquire() as conn, conn.transaction():
await conn.execute("SET LOCAL jit = off") await conn.execute("SET LOCAL jit = off")
if filters.everywhere:
return await _everywhere(conn, filters)
return await _search(conn, filters) return await _search(conn, filters)
async def _channels(conn: asyncpg.Connection, filters: SearchFilters) -> list[int]:
if filters.kind in {"group", "channel"}:
return await conn.fetchval(_CHANNELS, filters.account_id)
return []
def _data_hit(row: asyncpg.Record, needle: str) -> SearchHit:
data = dict(row)
raw = load_raw(data.pop("raw"))
return SearchHit(**data, source="data", found=find(raw, needle))
async def _everywhere(conn: asyncpg.Connection, filters: SearchFilters) -> SearchPage:
needle = (filters.query or "").strip()
floor = filters.date_from or await conn.fetchval(_OLDEST, filters.account_id)
if len(needle) < MIN_NEEDLE or floor is None:
return SearchPage(hits=[], next_cursor=None, terms=[])
channels = await _channels(conn, filters)
quoted = json.dumps(needle, ensure_ascii=False)
like = _LIKE_SPECIAL.sub(r"\\\1", quoted[1:-1])
path = _VALUE_MATCH.format(needle=quoted)
size = min(filters.limit, MAX_LIMIT)
cursor = filters.cursor
top = decode_cursor(cursor)[0] if cursor else None
hits: list[SearchHit] = []
started = monotonic()
while True:
lower = max((top or filters.date_to or datetime.now(UTC)) - _WINDOW, floor)
bind = _Binder()
conds = _conditions(
filters.model_copy(update={"cursor": cursor}), channels, bind
)
conds.append(f"s.date >= {bind(lower)}")
if top is not None:
conds.append(f"s.date <= {bind(top)}")
limit = size - len(hits)
sql = _EVERYWHERE.format(
where=" AND ".join(conds),
hidden=bind(list(HIDDEN_KEYS)),
like=bind(f"%{like}%"),
path=bind(path),
limit=bind(limit),
)
account = bind(filters.account_id)
rows = await conn.fetch(
_WITH_MEDIA.format(hits=sql, account=account), *bind.params
)
hits += [_data_hit(row, needle) for row in rows]
if len(rows) >= limit:
last = rows[-1]
top = last["date"]
cursor = encode_cursor(top, last["chat_id"], last["message_id"])
elif lower > floor:
top = lower
cursor = encode_cursor(lower, _BOTTOM, _BOTTOM)
else:
return SearchPage(hits=hits, next_cursor=None, terms=[], scanned_to=floor)
if len(hits) >= size or monotonic() - started > _BUDGET_SECONDS:
return SearchPage(hits=hits, next_cursor=cursor, terms=[], scanned_to=top)
async def _search(conn: asyncpg.Connection, filters: SearchFilters) -> SearchPage: async def _search(conn: asyncpg.Connection, filters: SearchFilters) -> SearchPage:
query = (filters.query or "").strip() query = (filters.query or "").strip()
terms: list[str] = [] terms: list[str] = []
@@ -238,7 +144,9 @@ async def _search(conn: asyncpg.Connection, filters: SearchFilters) -> SearchPag
return SearchPage(hits=[], next_cursor=None, terms=terms) return SearchPage(hits=[], next_cursor=None, terms=terms)
tsquery = parsed["tsquery"] tsquery = parsed["tsquery"]
channels = await _channels(conn, filters) channels: list[int] = []
if filters.kind in {"group", "channel"}:
channels = await conn.fetchval(_CHANNELS, filters.account_id)
bind = _Binder() bind = _Binder()
where = " AND ".join(_conditions(filters, channels, bind)) where = " AND ".join(_conditions(filters, channels, bind))
size = min(filters.limit, MAX_LIMIT) size = min(filters.limit, MAX_LIMIT)
-4
View File
@@ -24,10 +24,6 @@ class ContentAddressedStorage:
def get(self, key: str) -> bytes: def get(self, key: str) -> bytes:
return self._path(key).read_bytes() return self._path(key).read_bytes()
def head(self, key: str, size: int = 16) -> bytes:
with self._path(key).open("rb") as file:
return file.read(size)
def exists(self, key: str) -> bool: def exists(self, key: str) -> bool:
return self._path(key).exists() return self._path(key).exists()
-1
View File
@@ -284,7 +284,6 @@ export interface FeedQuery {
chat_id?: number; chat_id?: number;
chat_kind?: FeedChatKind; chat_kind?: FeedChatKind;
cursor?: string; cursor?: string;
folder_id?: number;
kind?: FeedKind; kind?: FeedKind;
limit?: number; limit?: number;
} }
+2 -22
View File
@@ -7,24 +7,7 @@ export type PolicyScopeType =
| "default_channel" | "default_channel"
| "folder" | "folder"
| "chat"; | "chat";
export type SearchSource = "text" | "stt" | "version" | "data"; export type SearchSource = "text" | "stt" | "version";
export type FoundWhere =
| "text"
| "button"
| "link"
| "forward"
| "reply"
| "file"
| "preview"
| "data";
export interface SearchFound {
button: string | null;
path: string;
value: string;
where: FoundWhere;
}
export type SearchChatKind = "private" | "group" | "channel"; export type SearchChatKind = "private" | "group" | "channel";
export type JobStatus = export type JobStatus =
@@ -245,7 +228,7 @@ export interface MessageVersion {
} }
export type FeedKind = "all" | "deleted" | "edited" | "profile"; export type FeedKind = "all" | "deleted" | "edited" | "profile";
export type FeedChatKind = "dm" | "bot" | "group" | "channel"; export type FeedChatKind = "dm" | "group" | "channel";
export interface FeedChat { export interface FeedChat {
chat_id: number; chat_id: number;
@@ -306,7 +289,6 @@ export interface SearchHit {
deleted_at: string | null; deleted_at: string | null;
edited_at: string | null; edited_at: string | null;
extracted_text: string | null; extracted_text: string | null;
found: SearchFound | null;
has_media: boolean; has_media: boolean;
media_kind: string | null; media_kind: string | null;
message_id: number; message_id: number;
@@ -320,7 +302,6 @@ export interface SearchHit {
export interface SearchPage { export interface SearchPage {
hits: SearchHit[]; hits: SearchHit[];
next_cursor: string | null; next_cursor: string | null;
scanned_to: string | null;
terms: string[]; terms: string[];
} }
@@ -331,7 +312,6 @@ export interface SearchQuery {
date_to?: string; date_to?: string;
deleted?: boolean; deleted?: boolean;
edited?: boolean; edited?: boolean;
everywhere?: boolean;
has_media?: boolean; has_media?: boolean;
kind?: SearchChatKind; kind?: SearchChatKind;
limit?: number; limit?: number;
+2 -49
View File
@@ -8,16 +8,14 @@
jumboEmojiCount, jumboEmojiCount,
linkHref, linkHref,
} from "#lib/format/entities.js"; } from "#lib/format/entities.js";
import { markText } from "#lib/format/search.js";
interface Props { interface Props {
entities: EntityView[]; entities: EntityView[];
marks?: string[];
own?: boolean; own?: boolean;
text: string; text: string;
} }
let { text, entities, marks = [], own = false }: Props = $props(); let { text, entities, own = false }: Props = $props();
const nodes = $derived(buildEntityTree(text, entities)); const nodes = $derived(buildEntityTree(text, entities));
const jumbo = $derived(entities.length === 0 ? jumboEmojiCount(text) : 0); const jumbo = $derived(entities.length === 0 ? jumboEmojiCount(text) : 0);
@@ -34,29 +32,13 @@
)} )}
{#each items as node, i (i)} {#each items as node, i (i)}
{#if node.kind === "text"} {#if node.kind === "text"}
{@render plain(node.text)} {node.text}
{:else} {:else}
{@render entity(node)} {@render entity(node)}
{/if} {/if}
{/each} {/each}
{/snippet} {/snippet}
{#snippet plain(
value: string
)}
{#if marks.length === 0}
{value}
{:else}
{#each markText(value, marks) as segment, i (i)}
{#if segment.hit}
<mark class="found" class:own>{segment.text}</mark>
{:else}
{segment.text}
{/if}
{/each}
{/if}
{/snippet}
{#snippet entity( {#snippet entity(
node: EntityNode node: EntityNode
)} )}
@@ -204,35 +186,6 @@
background-color: var(--color-primary-tint); background-color: var(--color-primary-tint);
} }
.found {
padding: 0;
border-radius: 0.25rem;
color: inherit;
background-color: transparent;
animation: found-flash 3s ease;
--found-color: color-mix(in srgb, var(--color-primary) 40%, transparent);
&.own {
--found-color: color-mix(
in srgb,
var(--color-own-links) 35%,
transparent
);
}
}
@keyframes found-flash {
0%,
70% {
background-color: var(--found-color);
}
100% {
background-color: transparent;
}
}
.spoiler { .spoiler {
cursor: pointer; cursor: pointer;
@@ -2,10 +2,8 @@
import ContextMenu from "#lib/components/ui/ContextMenu.svelte"; import ContextMenu from "#lib/components/ui/ContextMenu.svelte";
import ContextMenuItem from "#lib/components/ui/ContextMenuItem.svelte"; import ContextMenuItem from "#lib/components/ui/ContextMenuItem.svelte";
import { t } from "#lib/i18n/locale.svelte.js"; import { t } from "#lib/i18n/locale.svelte.js";
import { feed } from "#lib/stores/feed.svelte.js";
import { folders } from "#lib/stores/folders.svelte.js"; import { folders } from "#lib/stores/folders.svelte.js";
import { policyUi } from "#lib/stores/policy.svelte.js"; import { policyUi } from "#lib/stores/policy.svelte.js";
import { page } from "$app/state";
interface Tab { interface Tab {
id: number | null; id: number | null;
@@ -95,12 +93,6 @@
</button> </button>
{/snippet} {/snippet}
{#snippet menu()} {#snippet menu()}
<ContextMenuItem
icon="recent"
onselect={() => feed.open(page.url, tab.id)}
>
{t("feed.folder")}
</ContextMenuItem>
<ContextMenuItem icon="settings" onselect={() => editPolicy(tab)}> <ContextMenuItem icon="settings" onselect={() => editPolicy(tab)}>
{t("settings.policy")} {t("settings.policy")}
</ContextMenuItem> </ContextMenuItem>
@@ -1,15 +1,13 @@
<script lang="ts"> <script lang="ts">
import type { InlineButton } from "#lib/api/types.js"; import type { InlineButton } from "#lib/api/types.js";
import { buttonHasMark } from "#lib/format/search.js";
import { safeHref } from "#lib/format/url.js"; import { safeHref } from "#lib/format/url.js";
import { t } from "#lib/i18n/locale.svelte.js"; import { t } from "#lib/i18n/locale.svelte.js";
interface Props { interface Props {
marks?: string[];
rows: InlineButton[][]; rows: InlineButton[][];
} }
let { marks = [], rows }: Props = $props(); let { rows }: Props = $props();
let copied = $state<string | null>(null); let copied = $state<string | null>(null);
let resetTimer: ReturnType<typeof setTimeout> | undefined; let resetTimer: ReturnType<typeof setTimeout> | undefined;
@@ -29,14 +27,12 @@
<div class="row"> <div class="row">
{#each row as button, colIndex (colIndex)} {#each row as button, colIndex (colIndex)}
{@const key = `${rowIndex}:${colIndex}`} {@const key = `${rowIndex}:${colIndex}`}
{@const lit = marks.length > 0 && buttonHasMark(button, marks)}
{#if button.kind === "url" && button.url} {#if button.kind === "url" && button.url}
<a <a
class="button" class="button"
href={safeHref(button.url)} href={safeHref(button.url)}
rel="noopener" rel="noopener"
target="_blank" target="_blank"
class:found={lit}
> >
<span class="label">{button.text}</span> <span class="label">{button.text}</span>
<span class="corner">↗</span> <span class="corner">↗</span>
@@ -46,14 +42,13 @@
class="button" class="button"
onclick={() => copy(key, button.data ?? "")} onclick={() => copy(key, button.data ?? "")}
type="button" type="button"
class:found={lit}
> >
<span class="label" <span class="label"
>{copied === key ? t("inline.copied") : button.text}</span >{copied === key ? t("inline.copied") : button.text}</span
> >
</button> </button>
{:else} {:else}
<span class="button static" class:found={lit}> <span class="button static">
<span class="label">{button.text}</span> <span class="label">{button.text}</span>
</span> </span>
{/if} {/if}
@@ -109,27 +104,6 @@
} }
} }
.found {
animation: found-flash 3s ease;
}
@keyframes found-flash {
0%,
70% {
background-color: color-mix(
in srgb,
var(--color-primary) 30%,
var(--color-message-reaction)
);
box-shadow: inset 0 0 0 1px var(--color-primary);
}
100% {
background-color: var(--color-message-reaction);
box-shadow: inset 0 0 0 1px transparent;
}
}
.label { .label {
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
@@ -19,7 +19,6 @@
import Icon from "#lib/components/ui/Icon.svelte"; import Icon from "#lib/components/ui/Icon.svelte";
import WebPage from "#lib/components/WebPage.svelte"; import WebPage from "#lib/components/WebPage.svelte";
import { accountName, peerColorIndex, peerName } from "#lib/format/peer.js"; import { accountName, peerColorIndex, peerName } from "#lib/format/peer.js";
import { buttonHasMark, hasMark } from "#lib/format/search.js";
import { t } from "#lib/i18n/locale.svelte.js"; import { t } from "#lib/i18n/locale.svelte.js";
import { accounts } from "#lib/stores/accounts.svelte.js"; import { accounts } from "#lib/stores/accounts.svelte.js";
import { chats } from "#lib/stores/chats.svelte.js"; import { chats } from "#lib/stores/chats.svelte.js";
@@ -32,7 +31,7 @@
interface Props { interface Props {
animate: boolean; animate: boolean;
firstInGroup: boolean; firstInGroup: boolean;
highlight: string[] | null; highlighted: boolean;
isGroupChat: boolean; isGroupChat: boolean;
lastInGroup: boolean; lastInGroup: boolean;
message: MessageView; message: MessageView;
@@ -48,7 +47,7 @@
isGroupChat, isGroupChat,
firstInGroup, firstInGroup,
lastInGroup, lastInGroup,
highlight, highlighted,
animate, animate,
onjump, onjump,
onmedia, onmedia,
@@ -56,14 +55,6 @@
}: Props = $props(); }: Props = $props();
const deleted = $derived(message.deleted_at !== null); const deleted = $derived(message.deleted_at !== null);
const marks = $derived(highlight ?? []);
const markedInside = $derived(
marks.length > 0 &&
(hasMark(message.text, marks) ||
message.inline_buttons.some((row) =>
row.some((button) => buttonHasMark(button, marks))
))
);
const hasText = $derived(Boolean(message.text)); const hasText = $derived(Boolean(message.text));
const special = $derived( const special = $derived(
Boolean( Boolean(
@@ -171,7 +162,7 @@
data-message-id={message.message_id} data-message-id={message.message_id}
class:deleted class:deleted
class:first-in-group={firstInGroup} class:first-in-group={firstInGroup}
class:highlighted={highlight !== null && !markedInside} class:highlighted
class:last-in-group={lastInGroup} class:last-in-group={lastInGroup}
class:own class:own
class:with-avatar={isGroupChat} class:with-avatar={isGroupChat}
@@ -248,7 +239,6 @@
<div class="text"> <div class="text">
<EntityText <EntityText
entities={message.entities} entities={message.entities}
{marks}
{own} {own}
text={message.text ?? ""} text={message.text ?? ""}
/> />
@@ -280,7 +270,7 @@
{own} {own}
/> />
{#if message.inline_buttons.length} {#if message.inline_buttons.length}
<InlineButtons {marks} rows={message.inline_buttons} /> <InlineButtons rows={message.inline_buttons} />
{/if} {/if}
{#if hasCallbacks} {#if hasCallbacks}
<button class="detail-chip" onclick={openCallbacks} type="button"> <button class="detail-chip" onclick={openCallbacks} type="button">
+10 -17
View File
@@ -38,7 +38,6 @@
const IDLE_DELAY = 1500; const IDLE_DELAY = 1500;
const MAX_MESSAGES = 360; const MAX_MESSAGES = 360;
const SETTLE_DELAY = 400; const SETTLE_DELAY = 400;
const HIGHLIGHT_MS = 3000;
let messages = $state<MessageView[]>([]); let messages = $state<MessageView[]>([]);
let loading = $state(true); let loading = $state(true);
@@ -58,7 +57,7 @@
let mediaIndex = $state(0); let mediaIndex = $state(0);
let versionsOpen = $state(false); let versionsOpen = $state(false);
let versionsMessageId = $state<number | null>(null); let versionsMessageId = $state<number | null>(null);
let highlight = $state<{ id: number; marks: string[] } | null>(null); let highlightId = $state<number | null>(null);
let calendarOpen = $state(false); let calendarOpen = $state(false);
let calendarAnchor = $state<HTMLElement | null>(null); let calendarAnchor = $state<HTMLElement | null>(null);
let calendarMonth = $state<{ month: number; year: number } | null>(null); let calendarMonth = $state<{ month: number; year: number } | null>(null);
@@ -230,11 +229,7 @@
return messages.find((m) => holds(m, messageId))?.message_id ?? null; return messages.find((m) => holds(m, messageId))?.message_id ?? null;
} }
function jumpToMessage( function jumpToMessage(messageId: number, smooth = true) {
messageId: number,
smooth = true,
marks: string[] = []
) {
const owner = ownerOf(messageId) ?? messageId; const owner = ownerOf(messageId) ?? messageId;
const target = container?.querySelector<HTMLElement>( const target = container?.querySelector<HTMLElement>(
`[data-message-id="${owner}"]` `[data-message-id="${owner}"]`
@@ -247,13 +242,13 @@
behavior: smooth ? "smooth" : "auto", behavior: smooth ? "smooth" : "auto",
block: "center", block: "center",
}); });
highlight = { id: owner, marks }; highlightId = owner;
if (highlightTimer) { if (highlightTimer) {
clearTimeout(highlightTimer); clearTimeout(highlightTimer);
} }
highlightTimer = setTimeout(() => { highlightTimer = setTimeout(() => {
highlight = null; highlightId = null;
}, HIGHLIGHT_MS); }, 1600);
} }
function scrollToBottom() { function scrollToBottom() {
@@ -288,7 +283,7 @@
ui.clearJump(); ui.clearJump();
await loadAround(pending.messageId); await loadAround(pending.messageId);
await tick(); await tick();
jumpToMessage(pending.messageId, false, pending.marks); jumpToMessage(pending.messageId, false);
return; return;
} }
const page = await listMessages(chatId, { const page = await listMessages(chatId, {
@@ -579,14 +574,14 @@
} }
} }
async function goTo(messageId: number, marks: string[] = []) { async function goTo(messageId: number) {
try { try {
const loaded = ownerOf(messageId) !== null; const loaded = ownerOf(messageId) !== null;
if (!loaded) { if (!loaded) {
await loadAround(messageId); await loadAround(messageId);
} }
await tick(); await tick();
jumpToMessage(messageId, loaded, marks); jumpToMessage(messageId, loaded);
} catch { } catch {
toasts.error(t("thread.loadFailed")); toasts.error(t("thread.loadFailed"));
} }
@@ -598,7 +593,7 @@
return; return;
} }
ui.clearJump(); ui.clearJump();
goTo(target.messageId, target.marks); goTo(target.messageId);
}); });
$effect(() => { $effect(() => {
@@ -668,9 +663,7 @@
<MessageBubble <MessageBubble
animate={!suppressAppear} animate={!suppressAppear}
firstInGroup={row.firstInGroup} firstInGroup={row.firstInGroup}
highlight={highlight?.id === row.message.message_id highlighted={highlightId === row.message.message_id}
? highlight.marks
: null}
{isGroupChat} {isGroupChat}
lastInGroup={row.lastInGroup} lastInGroup={row.lastInGroup}
message={row.message} message={row.message}
@@ -16,7 +16,6 @@
import { formatFull, formatListDate } from "#lib/format/datetime.js"; import { formatFull, formatListDate } from "#lib/format/datetime.js";
import { import {
alertAvatar, alertAvatar,
alertHref,
alertLabel, alertLabel,
alertPreview, alertPreview,
alertTitle, alertTitle,
@@ -132,7 +131,7 @@
if (isMobile()) { if (isMobile()) {
ui.resetPanel(); ui.resetPanel();
} }
await goto(alertHref(alert)); await goto(alert.path);
} }
async function seeAll() { async function seeAll() {
@@ -1,32 +1,16 @@
<script lang="ts"> <script lang="ts">
import type { FeedChatKind, FeedKind } from "#lib/api/types.js"; import type { FeedChatKind, FeedKind } from "#lib/api/types.js";
import Icon from "#lib/components/ui/Icon.svelte";
import { t } from "#lib/i18n/locale.svelte.js"; import { t } from "#lib/i18n/locale.svelte.js";
import { FEED_CHAT_KINDS, FEED_KINDS } from "#lib/stores/feed.svelte.js"; import { FEED_CHAT_KINDS, FEED_KINDS } from "#lib/stores/feed.svelte.js";
import { folders } from "#lib/stores/folders.svelte.js";
interface Props { interface Props {
chats?: FeedChatKind | null; chats?: FeedChatKind | null;
folder?: number | null;
kind: FeedKind; kind: FeedKind;
onchats?: (chats: FeedChatKind | null) => void; onchats?: (chats: FeedChatKind | null) => void;
onfolder?: (folder: number | null) => void;
onkind: (kind: FeedKind) => void; onkind: (kind: FeedKind) => void;
} }
let { let { kind, chats = null, onkind, onchats }: Props = $props();
kind,
chats = null,
folder = null,
onkind,
onchats,
onfolder,
}: Props = $props();
const folderTitle = $derived(
folders.list.find((item) => item.folder_id === folder)?.title ??
t("feed.folder")
);
const KIND_KEYS = { const KIND_KEYS = {
all: "feed.filter.all", all: "feed.filter.all",
@@ -36,7 +20,6 @@
} as const; } as const;
const CHAT_KEYS = { const CHAT_KEYS = {
bot: "feed.chats.bot",
channel: "feed.chats.channel", channel: "feed.chats.channel",
dm: "feed.chats.dm", dm: "feed.chats.dm",
group: "feed.chats.group", group: "feed.chats.group",
@@ -72,20 +55,6 @@
{/each} {/each}
</div> </div>
{/if} {/if}
{#if folder !== null && onfolder}
<div class="group">
<button
aria-pressed="true"
class="chip on"
onclick={() => onfolder(null)}
type="button"
>
<Icon name="folder" size="0.875rem" />
{folderTitle}
<Icon name="close" size="0.875rem" />
</button>
</div>
{/if}
</div> </div>
<style lang="scss"> <style lang="scss">
@@ -109,7 +78,6 @@
cursor: pointer; cursor: pointer;
display: inline-flex; display: inline-flex;
gap: 0.25rem;
align-items: center; align-items: center;
height: 1.75rem; height: 1.75rem;
@@ -14,12 +14,11 @@
interface Props { interface Props {
chatId?: number; chatId?: number;
chats?: FeedChatKind | null; chats?: FeedChatKind | null;
folderId?: number | null;
kind: FeedKind; kind: FeedKind;
onopen: (item: FeedItem, messageId: number) => void; onopen: (item: FeedItem, messageId: number) => void;
} }
let { kind, chats = null, folderId = null, chatId, onopen }: Props = $props(); let { kind, chats = null, chatId, onopen }: Props = $props();
const PAGE = 30; const PAGE = 30;
const EMPTY_KEYS = { const EMPTY_KEYS = {
@@ -39,7 +38,6 @@
const filter = $derived<FeedQuery>({ const filter = $derived<FeedQuery>({
chat_id: chatId, chat_id: chatId,
chat_kind: chats ?? undefined, chat_kind: chats ?? undefined,
folder_id: folderId ?? undefined,
kind, kind,
limit: PAGE, limit: PAGE,
}); });
@@ -55,19 +55,12 @@
</header> </header>
<FeedFilters <FeedFilters
chats={filter.chats} chats={filter.chats}
folder={filter.folder}
kind={filter.kind} kind={filter.kind}
onchats={(chats) => feed.filter(page.url, { ...filter, chats })} onchats={(chats) => feed.filter(page.url, { ...filter, chats })}
onfolder={(folder) => feed.filter(page.url, { ...filter, folder })}
onkind={(kind) => feed.filter(page.url, { ...filter, kind })} onkind={(kind) => feed.filter(page.url, { ...filter, kind })}
/> />
<div class="feed-body custom-scroll"> <div class="feed-body custom-scroll">
<FeedList <FeedList chats={filter.chats} kind={filter.kind} onopen={open} />
chats={filter.chats}
folderId={filter.folder}
kind={filter.kind}
onopen={open}
/>
</div> </div>
</div> </div>
@@ -1,7 +1,6 @@
<script lang="ts"> <script lang="ts">
import { untrack } from "svelte"; import { untrack } from "svelte";
import { visible } from "#lib/actions/visible.js"; import { visible } from "#lib/actions/visible.js";
import SearchEverywhere from "#lib/components/search/SearchEverywhere.svelte";
import SearchMessageItem from "#lib/components/search/SearchMessageItem.svelte"; import SearchMessageItem from "#lib/components/search/SearchMessageItem.svelte";
import Button from "#lib/components/ui/Button.svelte"; import Button from "#lib/components/ui/Button.svelte";
import EmptyState from "#lib/components/ui/EmptyState.svelte"; import EmptyState from "#lib/components/ui/EmptyState.svelte";
@@ -16,9 +15,7 @@
const DEBOUNCE_MS = 250; const DEBOUNCE_MS = 250;
const pager = createHitPager(); const pager = createHitPager();
const everywhere = createHitPager();
let query = $state(""); let query = $state("");
let deep = $state(false);
let typing = $state(false); let typing = $state(false);
let timer: ReturnType<typeof setTimeout> | null = null; let timer: ReturnType<typeof setTimeout> | null = null;
@@ -26,30 +23,14 @@
page.params.chatId ? Number(page.params.chatId) : null page.params.chatId ? Number(page.params.chatId) : null
); );
const needle = $derived(query.trim());
const empty = $derived(
needle !== "" &&
!(typing || deep || pager.loading || pager.failed) &&
pager.hits.length === 0
);
function run() { function run() {
typing = false; typing = false;
deep = false; const value = query.trim();
everywhere.run(null);
pager.run( pager.run(
needle && chatId !== null ? { chat_id: chatId, query: needle } : null value && chatId !== null ? { chat_id: chatId, query: value } : null
); );
} }
function searchEverywhere() {
if (chatId === null) {
return;
}
deep = true;
everywhere.run({ chat_id: chatId, everywhere: true, query: needle });
}
$effect(() => { $effect(() => {
if (chatId !== null) { if (chatId !== null) {
untrack(run); untrack(run);
@@ -65,11 +46,11 @@
timer = setTimeout(run, DEBOUNCE_MS); timer = setTimeout(run, DEBOUNCE_MS);
} }
function openHit(messageId: number, marks: string[]) { function openHit(messageId: number) {
if (chatId === null) { if (chatId === null) {
return; return;
} }
ui.requestJump(chatId, messageId, marks); ui.requestJump(chatId, messageId);
if (isMobile()) { if (isMobile()) {
ui.closePanel(); ui.closePanel();
} }
@@ -92,9 +73,9 @@
{#each pager.hits as hit (`${hit.chat_id}:${hit.message_id}`)} {#each pager.hits as hit (`${hit.chat_id}:${hit.message_id}`)}
<SearchMessageItem <SearchMessageItem
{hit} {hit}
href={search.hitHref(hit.chat_id, hit.message_id, pager.terms)} href={search.hitHref(hit.chat_id, hit.message_id)}
inChat inChat
onclick={() => openHit(hit.message_id, pager.terms)} onclick={() => openHit(hit.message_id)}
terms={pager.terms} terms={pager.terms}
/> />
{/each} {/each}
@@ -115,19 +96,7 @@
>{t("search.retry")}</Button >{t("search.retry")}</Button
> >
</div> </div>
{/if} {:else if query.trim() && !typing}
<SearchEverywhere
href={(hit) => search.hitHref(hit.chat_id, hit.message_id, [needle])}
inChat
{needle}
onopen={(hit) => openHit(hit.message_id, [needle])}
onstart={searchEverywhere}
pager={everywhere}
ready={!(typing || pager.loading || pager.failed || pager.hasMore)}
shown={pager.hits}
started={deep}
/>
{#if empty}
<EmptyState title={t("search.nothing")} /> <EmptyState title={t("search.nothing")} />
{/if} {/if}
</div> </div>
@@ -1,208 +0,0 @@
<script lang="ts">
import { ripple } from "#lib/actions/ripple.js";
import { visible } from "#lib/actions/visible.js";
import type { SearchHit } from "#lib/api/types.js";
import SearchMessageItem from "#lib/components/search/SearchMessageItem.svelte";
import Button from "#lib/components/ui/Button.svelte";
import Icon from "#lib/components/ui/Icon.svelte";
import Spinner from "#lib/components/ui/Spinner.svelte";
import { formatDate } from "#lib/format/datetime.js";
import { t } from "#lib/i18n/locale.svelte.js";
import {
EVERYWHERE_MIN,
type HitPager,
hitKey,
} from "#lib/stores/search.svelte.js";
interface Props {
href: (hit: SearchHit) => string;
inChat?: boolean;
needle: string;
onopen?: (hit: SearchHit) => void;
onstart: () => void;
pager: HitPager;
ready: boolean;
shown: SearchHit[];
started: boolean;
}
let {
href,
inChat = false,
needle,
onopen,
onstart,
pager,
ready,
shown,
started,
}: Props = $props();
const hits = $derived.by(() => {
const seen = new Set(shown.map(hitKey));
return pager.hits.filter((hit) => {
const key = hitKey(hit);
const fresh = !seen.has(key);
seen.add(key);
return fresh;
});
});
const busy = $derived(pager.loading || pager.appending);
function retry() {
if (pager.hasMore) {
pager.more();
} else {
pager.retry();
}
}
</script>
{#if !started}
{#if ready && needle.length >= EVERYWHERE_MIN}
<button
class="start"
onclick={onstart}
type="button"
class:in-chat={inChat}
use:ripple
>
<span class="glyph"><Icon name="search" size="1.5rem" /></span>
<span class="body">
<span class="label">{t("search.everywhere")}</span>
<span class="hint">{t("search.everywhereHint")}</span>
</span>
</button>
{/if}
{:else}
{#if hits.length > 0}
<div class="section-label">{t("search.everywhereSection")}</div>
{#each hits as hit (hitKey(hit))}
<SearchMessageItem
{hit}
href={href(hit)}
{inChat}
onclick={() => onopen?.(hit)}
terms={[needle]}
/>
{/each}
{/if}
{#if busy}
<div class="status">
<Spinner size="1.25rem" />
<span>
{pager.scannedTo
? t("search.everywhereScanned", { date: formatDate(pager.scannedTo) })
: t("search.everywhereScanning")}
</span>
</div>
{:else if pager.failed}
<div class="status failed">
<span>{t("search.failed")}</span>
<Button onclick={retry} variant="text">{t("search.retry")}</Button>
</div>
{:else if pager.hasMore}
<div class="sentinel" use:visible={() => pager.more()}></div>
{:else if hits.length === 0}
<div class="status">{t("search.everywhereNothing")}</div>
{/if}
{/if}
<style lang="scss">
.start {
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;
font: inherit;
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);
}
}
&:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: -2px;
}
}
.glyph {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: 3rem;
height: 3rem;
color: var(--color-primary);
.in-chat & {
width: 2.5rem;
height: 2.5rem;
}
}
.body {
display: flex;
flex-direction: column;
gap: 0.1875rem;
min-width: 0;
}
.label {
font-size: 0.9375rem;
font-weight: var(--font-weight-medium);
color: var(--color-primary);
}
.hint {
font-size: 0.8125rem;
color: var(--color-text-secondary);
}
.section-label {
padding: 0.625rem 0.75rem 0.375rem;
font-size: 0.875rem;
font-weight: var(--font-weight-medium);
color: var(--color-primary);
}
.status {
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
padding: 1rem 0.75rem 1.5rem;
font-size: 0.875rem;
font-variant-numeric: tabular-nums;
color: var(--color-text-secondary);
&.failed {
flex-direction: column;
}
}
.sentinel {
height: 1px;
}
</style>
@@ -1,12 +1,12 @@
<script lang="ts"> <script lang="ts">
import { ripple } from "#lib/actions/ripple.js"; import { ripple } from "#lib/actions/ripple.js";
import type { FoundWhere, SearchHit } from "#lib/api/types.js"; import type { SearchHit } from "#lib/api/types.js";
import Avatar from "#lib/components/ui/Avatar.svelte"; import Avatar from "#lib/components/ui/Avatar.svelte";
import Icon from "#lib/components/ui/Icon.svelte"; import Icon from "#lib/components/ui/Icon.svelte";
import { formatFull, formatListDate } from "#lib/format/datetime.js"; import { formatFull, formatListDate } from "#lib/format/datetime.js";
import { mediaKindLabel } from "#lib/format/media.js"; import { mediaKindLabel } from "#lib/format/media.js";
import { peerName } from "#lib/format/peer.js"; import { peerName } from "#lib/format/peer.js";
import { highlight, highlightMarks } from "#lib/format/search.js"; import { highlight } from "#lib/format/search.js";
import { t } from "#lib/i18n/locale.svelte.js"; import { t } from "#lib/i18n/locale.svelte.js";
import { accounts } from "#lib/stores/accounts.svelte.js"; import { accounts } from "#lib/stores/accounts.svelte.js";
import { chats } from "#lib/stores/chats.svelte.js"; import { chats } from "#lib/stores/chats.svelte.js";
@@ -22,17 +22,6 @@
let { hit, href, inChat = false, onclick, terms }: Props = $props(); let { hit, href, inChat = false, onclick, terms }: Props = $props();
const FOUND_ICONS: Record<FoundWhere, string> = {
button: "bot-command",
data: "info",
file: "document",
forward: "forward",
link: "link",
preview: "link",
reply: "reply",
text: "search",
};
const chat = $derived(chats.byId(hit.chat_id)); const chat = $derived(chats.byId(hit.chat_id));
const ownId = $derived(accounts.selected?.tg_user_id ?? null); const ownId = $derived(accounts.selected?.tg_user_id ?? null);
const own = $derived(hit.sender_id !== null && hit.sender_id === ownId); const own = $derived(hit.sender_id !== null && hit.sender_id === ownId);
@@ -92,21 +81,7 @@
const kind = $derived( const kind = $derived(
hit.source === "stt" || !body ? mediaKindLabel(hit.media_kind) : null hit.source === "stt" || !body ? mediaKindLabel(hit.media_kind) : null
); );
const byData = $derived(hit.source === "data"); const segments = $derived(highlight(body, terms));
const segments = $derived(
byData ? highlightMarks(body, terms) : highlight(body, terms)
);
const found = $derived(
hit.found && hit.found.where !== "text" ? hit.found : null
);
const foundLabel = $derived.by(() => {
if (found === null) {
return "";
}
return found.where === "button"
? t("search.found.button", { text: found.button ?? "" })
: t(`search.found.${found.where}`);
});
</script> </script>
<a class="hit" {href} {onclick} use:ripple> <a class="hit" {href} {onclick} use:ripple>
@@ -151,21 +126,6 @@
{/each}</span {/each}</span
> >
</div> </div>
{#if found}
<div class="note found" title={found.path}>
<Icon name={FOUND_ICONS[found.where]} size="0.875rem" />
<span class="found-label">{foundLabel}:</span>
<span class="found-value"
>{#each highlightMarks(found.value, terms) as segment, index (index)}
{#if segment.hit}
<mark>{segment.text}</mark>
{:else}
{segment.text}
{/if}
{/each}</span
>
</div>
{/if}
{#if hit.source === "version"} {#if hit.source === "version"}
<div <div
class="note" class="note"
@@ -284,22 +244,6 @@
color: var(--color-text-secondary); color: var(--color-text-secondary);
} }
.found {
font-style: normal;
white-space: nowrap;
}
.found-label {
flex-shrink: 0;
color: var(--color-text);
}
.found-value {
overflow: hidden;
min-width: 0;
text-overflow: ellipsis;
}
mark { mark {
padding: 0 0.0625rem; padding: 0 0.0625rem;
border-radius: 0.25rem; border-radius: 0.25rem;
@@ -3,7 +3,6 @@
import { visible } from "#lib/actions/visible.js"; import { visible } from "#lib/actions/visible.js";
import ChatListItem from "#lib/components/ChatListItem.svelte"; import ChatListItem from "#lib/components/ChatListItem.svelte";
import DiscoverResultItem from "#lib/components/search/DiscoverResultItem.svelte"; import DiscoverResultItem from "#lib/components/search/DiscoverResultItem.svelte";
import SearchEverywhere from "#lib/components/search/SearchEverywhere.svelte";
import SearchFilters from "#lib/components/search/SearchFilters.svelte"; import SearchFilters from "#lib/components/search/SearchFilters.svelte";
import SearchMessageItem from "#lib/components/search/SearchMessageItem.svelte"; import SearchMessageItem from "#lib/components/search/SearchMessageItem.svelte";
import Button from "#lib/components/ui/Button.svelte"; import Button from "#lib/components/ui/Button.svelte";
@@ -15,7 +14,7 @@
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { page } from "$app/state"; import { page } from "$app/state";
const { everywhere, messages } = search; const { messages } = search;
let scroller = $state<HTMLDivElement | null>(null); let scroller = $state<HTMLDivElement | null>(null);
$effect(() => { $effect(() => {
@@ -26,22 +25,12 @@
}); });
}); });
$effect(() => {
const key = search.everywhereRequest;
untrack(() => search.syncEverywhere(key));
});
const activeChatId = $derived( const activeChatId = $derived(
page.params.chatId ? Number(page.params.chatId) : null page.params.chatId ? Number(page.params.chatId) : null
); );
const showChats = $derived(!search.filtered && search.chatHits.length > 0); const showChats = $derived(!search.filtered && search.chatHits.length > 0);
const showPeers = $derived(!search.filtered && search.peerHits.length > 0); const showPeers = $derived(!search.filtered && search.peerHits.length > 0);
const hasMessages = $derived(messages.hits.length > 0); const hasMessages = $derived(messages.hits.length > 0);
const needle = $derived(search.text.trim());
const searchedAll = $derived(
!(search.typing || messages.loading || messages.failed || messages.hasMore)
);
const deep = $derived(search.everywhereRequest !== "");
const empty = $derived( const empty = $derived(
!( !(
search.typing || search.typing ||
@@ -49,8 +38,7 @@
messages.failed || messages.failed ||
showChats || showChats ||
showPeers || showPeers ||
hasMessages || hasMessages
deep
) )
); );
@@ -86,9 +74,8 @@
{#each messages.hits as hit (`${hit.chat_id}:${hit.message_id}`)} {#each messages.hits as hit (`${hit.chat_id}:${hit.message_id}`)}
<SearchMessageItem <SearchMessageItem
{hit} {hit}
href={search.hitHref(hit.chat_id, hit.message_id, messages.terms)} href={search.hitHref(hit.chat_id, hit.message_id)}
onclick={() => onclick={() => ui.requestJump(hit.chat_id, hit.message_id)}
ui.requestJump(hit.chat_id, hit.message_id, messages.terms)}
terms={messages.terms} terms={messages.terms}
/> />
{/each} {/each}
@@ -111,17 +98,6 @@
</div> </div>
{/if} {/if}
<SearchEverywhere
href={(hit) => search.hitHref(hit.chat_id, hit.message_id, [needle])}
{needle}
onopen={(hit) => ui.requestJump(hit.chat_id, hit.message_id, [needle])}
onstart={() => search.searchEverywhere()}
pager={everywhere}
ready={searchedAll}
shown={messages.hits}
started={deep}
/>
{#if empty} {#if empty}
<EmptyState title={t("search.nothing")} /> <EmptyState title={t("search.nothing")} />
{/if} {/if}
+21 -78
View File
@@ -1,12 +1,8 @@
import type { InlineButton } from "#lib/api/types.js";
export interface Segment { export interface Segment {
hit: boolean; hit: boolean;
text: string; text: string;
} }
type Range = [number, number];
const WORD = /[\p{L}\p{N}_]+/gu; const WORD = /[\p{L}\p{N}_]+/gu;
const SPACE = /\s/; const SPACE = /\s/;
const PREFIX_MIN = 3; const PREFIX_MIN = 3;
@@ -24,92 +20,39 @@ function matches(word: string, terms: string[]): boolean {
); );
} }
function wordRanges(text: string, terms: string[]): Range[] { function windowed(text: string, terms: string[]): string {
return [...text.matchAll(WORD)] for (const match of text.matchAll(WORD)) {
.filter((match) => matches(match[0], terms)) if (!matches(match[0], terms)) {
.map((match) => [match.index, match.index + match[0].length]);
}
function markRanges(text: string, marks: string[]): Range[] {
const folded = fold(text);
if (folded.length !== text.length) {
return [];
}
const ranges: Range[] = [];
for (const mark of marks.map(fold)) {
if (mark.length < PREFIX_MIN) {
ranges.push(...wordRanges(text, [mark]));
continue; continue;
} }
for (let at = folded.indexOf(mark); at !== -1; ) { if (match.index <= REACH) {
ranges.push([at, at + mark.length]); return text;
at = folded.indexOf(mark, at + mark.length);
} }
} let start = match.index - LEAD;
ranges.sort((a, b) => a[0] - b[0]);
const merged: Range[] = [];
for (const [start, end] of ranges) {
const last = merged.at(-1);
if (last && start <= last[1]) {
last[1] = Math.max(last[1], end);
} else {
merged.push([start, end]);
}
}
return merged;
}
function split(text: string, ranges: Range[]): Segment[] {
const segments: Segment[] = [];
let last = 0;
for (const [start, end] of ranges) {
if (start > last) {
segments.push({ hit: false, text: text.slice(last, start) });
}
segments.push({ hit: true, text: text.slice(start, end) });
last = end;
}
if (last < text.length) {
segments.push({ hit: false, text: text.slice(last) });
}
return segments;
}
function windowed(text: string, ranges: Range[]): Segment[] {
const first = ranges[0]?.[0];
if (first === undefined || first <= REACH) {
return split(text, ranges);
}
const cut = first - LEAD;
let start = cut;
while (start > 0 && !SPACE.test(text[start - 1])) { while (start > 0 && !SPACE.test(text[start - 1])) {
start -= 1; start -= 1;
} }
if (start === 0) { return `…${text.slice(start)}`;
start = cut;
} }
const shifted = ranges.map(([a, b]): Range => [a - start, b - start]); return text;
return [{ hit: false, text: "…" }, ...split(text.slice(start), shifted)];
} }
export function highlight(text: string, terms: string[]): Segment[] { export function highlight(text: string, terms: string[]): Segment[] {
return windowed(text, wordRanges(text, terms)); const shown = windowed(text, terms);
const segments: Segment[] = [];
let last = 0;
for (const match of shown.matchAll(WORD)) {
if (!matches(match[0], terms)) {
continue;
} }
if (match.index > last) {
export function highlightMarks(text: string, marks: string[]): Segment[] { segments.push({ hit: false, text: shown.slice(last, match.index) });
return windowed(text, markRanges(text, marks));
} }
segments.push({ hit: true, text: match[0] });
export function markText(text: string, marks: string[]): Segment[] { last = match.index + match[0].length;
return split(text, markRanges(text, marks));
} }
if (last < shown.length) {
export function hasMark(text: string | null, marks: string[]): boolean { segments.push({ hit: false, text: shown.slice(last) });
return text !== null && markRanges(text, marks).length > 0;
} }
return segments;
export function buttonHasMark(button: InlineButton, marks: string[]): boolean {
return [button.text, button.data, button.url].some((value) =>
hasMark(value, marks)
);
} }
-27
View File
@@ -9,7 +9,6 @@ import type {
} from "#lib/api/watches.js"; } from "#lib/api/watches.js";
import { formatTime } from "#lib/format/datetime.js"; import { formatTime } from "#lib/format/datetime.js";
import { locale, t } from "#lib/i18n/locale.svelte.js"; import { locale, t } from "#lib/i18n/locale.svelte.js";
import { MARK_PARAM } from "#lib/stores/feed.svelte.js";
export const KIND_ICONS: Record<WatchKind, string> = { export const KIND_ICONS: Record<WatchKind, string> = {
keyword: "search", keyword: "search",
@@ -163,32 +162,6 @@ export function alertTitle(alert: AlertItem): string {
return personName(alert.peer); return personName(alert.peer);
} }
function alertMarks(alert: AlertItem): string[] {
const pattern = patternOf(alert.watch.params);
if (alert.watch.kind !== "keyword" || !pattern) {
return [];
}
if (!alert.watch.params.regex) {
return [pattern];
}
try {
const found = (alert.message?.text ?? "").matchAll(
new RegExp(pattern, "gi")
);
return [...new Set([...found].map((match) => match[0]).filter(Boolean))];
} catch {
return [];
}
}
export function alertHref(alert: AlertItem): string {
const url = new URL(alert.path, "http://app");
for (const mark of alertMarks(alert)) {
url.searchParams.append(MARK_PARAM, mark);
}
return `${url.pathname}${url.search}`;
}
export function alertLabel(alert: AlertItem): string { export function alertLabel(alert: AlertItem): string {
if (alert.watch.name) { if (alert.watch.name) {
return alert.watch.name; return alert.watch.name;
-2
View File
@@ -59,7 +59,6 @@ export const feed = {
}, },
}, },
"feed.changes": { en: "Changes", ru: "Изменения" }, "feed.changes": { en: "Changes", ru: "Изменения" },
"feed.chats.bot": { en: "Bots", ru: "Боты" },
"feed.chats.channel": { en: "Channels", ru: "Каналы" }, "feed.chats.channel": { en: "Channels", ru: "Каналы" },
"feed.chats.dm": { en: "Private", ru: "Личка" }, "feed.chats.dm": { en: "Private", ru: "Личка" },
"feed.chats.group": { en: "Groups", ru: "Группы" }, "feed.chats.group": { en: "Groups", ru: "Группы" },
@@ -93,7 +92,6 @@ export const feed = {
"feed.filter.deleted": { en: "Deleted", ru: "Удалённые" }, "feed.filter.deleted": { en: "Deleted", ru: "Удалённые" },
"feed.filter.edited": { en: "Edited", ru: "Изменённые" }, "feed.filter.edited": { en: "Edited", ru: "Изменённые" },
"feed.filter.profile": { en: "Profiles", ru: "Профили" }, "feed.filter.profile": { en: "Profiles", ru: "Профили" },
"feed.folder": { en: "Folder feed", ru: "Лента папки" },
"feed.lostText": { "feed.lostText": {
en: "The earlier text wasn't saved", en: "The earlier text wasn't saved",
ru: "Прежний текст не сохранился", ru: "Прежний текст не сохранился",
-26
View File
@@ -13,36 +13,10 @@ export const search = {
"search.clear": { en: "Clear", ru: "Очистить" }, "search.clear": { en: "Clear", ru: "Очистить" },
"search.deleted": { en: "Deleted", ru: "Удалённые" }, "search.deleted": { en: "Deleted", ru: "Удалённые" },
"search.edited": { en: "Edited", ru: "Изменённые" }, "search.edited": { en: "Edited", ru: "Изменённые" },
"search.everywhere": { en: "Search everywhere", ru: "Искать везде" },
"search.everywhereHint": {
en: "In buttons, links and message data",
ru: "В кнопках, ссылках и данных сообщений",
},
"search.everywhereNothing": {
en: "Nothing else anywhere",
ru: "Больше нигде не нашлось",
},
"search.everywhereScanned": {
en: "Searching, back to {date}",
ru: "Ищу, проверено до {date}",
},
"search.everywhereScanning": {
en: "Searching everywhere",
ru: "Ищу везде",
},
"search.everywhereSection": { en: "Everywhere", ru: "Везде" },
"search.failed": { "search.failed": {
en: "Search did not respond", en: "Search did not respond",
ru: "Поиск не ответил", ru: "Поиск не ответил",
}, },
"search.found.button": { en: "Button “{text}”", ru: "Кнопка «{text}»" },
"search.found.data": { en: "Message data", ru: "Данные сообщения" },
"search.found.file": { en: "File name", ru: "Имя файла" },
"search.found.forward": { en: "Forwarded from", ru: "Переслано от" },
"search.found.link": { en: "Link", ru: "Ссылка" },
"search.found.preview": { en: "Link preview", ru: "Превью ссылки" },
"search.found.reply": { en: "Reply", ru: "Ответ" },
"search.found.text": { en: "Text", ru: "Текст" },
"search.from": { en: "From", ru: "От кого" }, "search.from": { en: "From", ru: "От кого" },
"search.fromName": { en: "Name or @username", ru: "Имя или @username" }, "search.fromName": { en: "Name or @username", ru: "Имя или @username" },
"search.inChat": { en: "Search this chat", ru: "Поиск в чате" }, "search.inChat": { en: "Search this chat", ru: "Поиск в чате" },
+2 -2
View File
@@ -3,7 +3,7 @@ import {
type LiveAlertEvent, type LiveAlertEvent,
unseenAlerts, unseenAlerts,
} from "#lib/api/watches.js"; } from "#lib/api/watches.js";
import { alertHref, alertLine, alertTitle } from "#lib/format/watches.js"; import { alertLine, alertTitle } from "#lib/format/watches.js";
import { events } from "#lib/stores/events.svelte.js"; import { events } from "#lib/stores/events.svelte.js";
import { browser } from "$app/env"; import { browser } from "$app/env";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
@@ -49,7 +49,7 @@ function createAlerts() {
}); });
popup.onclick = () => { popup.onclick = () => {
window.focus(); window.focus();
goto(alertHref(alert)); goto(alert.path);
popup.close(); popup.close();
}; };
} }
+3 -20
View File
@@ -8,21 +8,13 @@ import { goto } from "$app/navigation";
const LIVE_DELAY = 600; const LIVE_DELAY = 600;
const KIND_PARAM = "feed"; const KIND_PARAM = "feed";
const CHATS_PARAM = "chats"; const CHATS_PARAM = "chats";
const FOLDER_PARAM = "folder";
const MESSAGE_PARAM = "message"; const MESSAGE_PARAM = "message";
export const MARK_PARAM = "mark";
export const FEED_KINDS: FeedKind[] = ["all", "deleted", "edited", "profile"]; export const FEED_KINDS: FeedKind[] = ["all", "deleted", "edited", "profile"];
export const FEED_CHAT_KINDS: FeedChatKind[] = [ export const FEED_CHAT_KINDS: FeedChatKind[] = ["dm", "group", "channel"];
"dm",
"bot",
"group",
"channel",
];
export interface FeedFilter { export interface FeedFilter {
chats: FeedChatKind | null; chats: FeedChatKind | null;
folder: number | null;
kind: FeedKind; kind: FeedKind;
} }
@@ -37,12 +29,10 @@ export function feedFilterOf(url: UrlLike): FeedFilter | null {
return null; return null;
} }
const chats = searchParams.get(CHATS_PARAM); const chats = searchParams.get(CHATS_PARAM);
const folder = Number(searchParams.get(FOLDER_PARAM));
return { return {
chats: FEED_CHAT_KINDS.includes(chats as FeedChatKind) chats: FEED_CHAT_KINDS.includes(chats as FeedChatKind)
? (chats as FeedChatKind) ? (chats as FeedChatKind)
: null, : null,
folder: Number.isInteger(folder) && folder > 0 ? folder : null,
kind: kind as FeedKind, kind: kind as FeedKind,
}; };
} }
@@ -51,16 +41,12 @@ function withFilter(url: UrlLike, filter: FeedFilter | null): URL {
const next = new URL(url.href); const next = new URL(url.href);
next.searchParams.delete(KIND_PARAM); next.searchParams.delete(KIND_PARAM);
next.searchParams.delete(CHATS_PARAM); next.searchParams.delete(CHATS_PARAM);
next.searchParams.delete(FOLDER_PARAM);
next.searchParams.delete(MESSAGE_PARAM); next.searchParams.delete(MESSAGE_PARAM);
if (filter) { if (filter) {
next.searchParams.set(KIND_PARAM, filter.kind); next.searchParams.set(KIND_PARAM, filter.kind);
if (filter.chats) { if (filter.chats) {
next.searchParams.set(CHATS_PARAM, filter.chats); next.searchParams.set(CHATS_PARAM, filter.chats);
} }
if (filter.folder !== null) {
next.searchParams.set(FOLDER_PARAM, String(filter.folder));
}
} }
return next; return next;
} }
@@ -86,7 +72,6 @@ export function messageHref(
export function withoutMessage(url: UrlLike): string { export function withoutMessage(url: UrlLike): string {
const next = new URL(url.href); const next = new URL(url.href);
next.searchParams.delete(MESSAGE_PARAM); next.searchParams.delete(MESSAGE_PARAM);
next.searchParams.delete(MARK_PARAM);
return href(next); return href(next);
} }
@@ -153,10 +138,8 @@ function createFeed() {
unseen = next; unseen = next;
} }
}, },
async open(url: UrlLike, folder: number | null = null) { async open(url: UrlLike) {
const target = href( const target = href(withFilter(url, { chats: null, kind: "all" }));
withFilter(url, { chats: null, folder, kind: "all" })
);
openedHref = target; openedHref = target;
await goto(target, { reset: false }); await goto(target, { reset: false });
}, },
+2 -49
View File
@@ -11,7 +11,6 @@ import type {
SearchQuery, SearchQuery,
} from "#lib/api/types.js"; } from "#lib/api/types.js";
import { accounts } from "#lib/stores/accounts.svelte.js"; import { accounts } from "#lib/stores/accounts.svelte.js";
import { MARK_PARAM } from "#lib/stores/feed.svelte.js";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { page } from "$app/state"; import { page } from "$app/state";
@@ -31,7 +30,6 @@ const DEBOUNCE_MS = 250;
const REMOTE_DELAY_MS = 450; const REMOTE_DELAY_MS = 450;
const CHAT_LIMIT = 30; const CHAT_LIMIT = 30;
const PAGE_SIZE = 30; const PAGE_SIZE = 30;
export const EVERYWHERE_MIN = 3;
const KINDS = new Set<string>(["private", "group", "channel"]); const KINDS = new Set<string>(["private", "group", "channel"]);
const DAY = /^\d{4}-\d{2}-\d{2}$/; const DAY = /^\d{4}-\d{2}-\d{2}$/;
const PARAMS = [ const PARAMS = [
@@ -43,7 +41,6 @@ const PARAMS = [
"from", "from",
"since", "since",
"until", "until",
"everywhere",
] as const; ] as const;
type Param = (typeof PARAMS)[number]; type Param = (typeof PARAMS)[number];
@@ -93,7 +90,6 @@ export function createHitPager() {
let hits = $state<SearchHit[]>([]); let hits = $state<SearchHit[]>([]);
let terms = $state<string[]>([]); let terms = $state<string[]>([]);
let cursor = $state<string | null>(null); let cursor = $state<string | null>(null);
let scannedTo = $state<string | null>(null);
let loading = $state(false); let loading = $state(false);
let appending = $state(false); let appending = $state(false);
let failed = $state(false); let failed = $state(false);
@@ -110,7 +106,6 @@ export function createHitPager() {
hits = request.cursor ? [...hits, ...result.hits] : result.hits; hits = request.cursor ? [...hits, ...result.hits] : result.hits;
({ terms } = result); ({ terms } = result);
cursor = result.next_cursor; cursor = result.next_cursor;
scannedTo = result.scanned_to;
} catch { } catch {
if (current === seq) { if (current === seq) {
failed = true; failed = true;
@@ -127,7 +122,6 @@ export function createHitPager() {
seq += 1; seq += 1;
base = request; base = request;
cursor = null; cursor = null;
scannedTo = null;
failed = false; failed = false;
appending = false; appending = false;
loading = request !== null; loading = request !== null;
@@ -166,21 +160,12 @@ export function createHitPager() {
run(base); run(base);
}, },
run, run,
get scannedTo() {
return scannedTo;
},
get terms() { get terms() {
return terms; return terms;
}, },
}; };
} }
export type HitPager = ReturnType<typeof createHitPager>;
export function hitKey(hit: SearchHit): string {
return `${hit.chat_id}:${hit.message_id}`;
}
function createSearch() { function createSearch() {
let open = $state(false); let open = $state(false);
let draft = $state(""); let draft = $state("");
@@ -192,9 +177,7 @@ function createSearch() {
let lastRequest = ""; let lastRequest = "";
let lastText = ""; let lastText = "";
let seq = 0; let seq = 0;
let lastEverywhere = "";
const messages = createHitPager(); const messages = createHitPager();
const everywhere = createHitPager();
const params = $derived(page.url.searchParams); const params = $derived(page.url.searchParams);
const urlQuery = $derived(params.get("q") ?? ""); const urlQuery = $derived(params.get("q") ?? "");
@@ -209,14 +192,6 @@ function createSearch() {
} }
return JSON.stringify([accounts.selectedId, toRequest(query, filters)]); return JSON.stringify([accounts.selectedId, toRequest(query, filters)]);
}); });
const everywhereRequest = $derived.by(() => {
const query = urlQuery.trim();
if (!params.has("everywhere") || query.length < EVERYWHERE_MIN) {
return "";
}
const deep = { ...toRequest(query, filters), everywhere: true };
return JSON.stringify([accounts.selectedId, deep]);
});
const peerHits = $derived.by(() => { const peerHits = $derived.by(() => {
const shown = new Set(chatHits.map((chat) => chat.chat_id)); const shown = new Set(chatHits.map((chat) => chat.chat_id));
return peerResults.filter((item) => !shown.has(item.chat_id)); return peerResults.filter((item) => !shown.has(item.chat_id));
@@ -297,9 +272,7 @@ function createSearch() {
stopTyping(); stopTyping();
lastRequest = ""; lastRequest = "";
lastText = ""; lastText = "";
lastEverywhere = "";
messages.run(null); messages.run(null);
everywhere.run(null);
runText(""); runText("");
} }
@@ -315,21 +288,14 @@ function createSearch() {
const update = Object.fromEntries(PARAMS.map((key) => [key, null])); const update = Object.fromEntries(PARAMS.map((key) => [key, null]));
write(update).catch(() => undefined); write(update).catch(() => undefined);
}, },
everywhere,
get everywhereRequest() {
return everywhereRequest;
},
get filtered() { get filtered() {
return filtered; return filtered;
}, },
get filters() { get filters() {
return filters; return filters;
}, },
hitHref(chatId: number, messageId: number, marks: string[]): string { hitHref(chatId: number, messageId: number): string {
const next = new URLSearchParams({ message: String(messageId) }); const next = new URLSearchParams({ message: String(messageId) });
for (const mark of marks) {
next.append(MARK_PARAM, mark);
}
for (const key of PARAMS) { for (const key of PARAMS) {
const value = params.get(key); const value = params.get(key);
if (value) { if (value) {
@@ -349,9 +315,6 @@ function createSearch() {
return request; return request;
}, },
reset, reset,
searchEverywhere() {
write({ everywhere: "1" }).catch(() => undefined);
},
setFilter(key: "from" | "kind" | "since" | "until", value: string | null) { setFilter(key: "from" | "kind" | "since" | "until", value: string | null) {
write({ [key]: value }).catch(() => undefined); write({ [key]: value }).catch(() => undefined);
}, },
@@ -366,11 +329,7 @@ function createSearch() {
} }
timer = setTimeout(() => { timer = setTimeout(() => {
timer = null; timer = null;
const same = draft.trim() === urlQuery.trim(); write({ q: draft.trim() ? draft : null })
write({
everywhere: same ? params.get("everywhere") : null,
q: draft.trim() ? draft : null,
})
.catch(() => undefined) .catch(() => undefined)
.finally(() => { .finally(() => {
if (timer === null) { if (timer === null) {
@@ -393,12 +352,6 @@ function createSearch() {
runText(value); runText(value);
} }
}, },
syncEverywhere(key: string) {
if (key !== lastEverywhere) {
lastEverywhere = key;
everywhere.run(key ? (JSON.parse(key)[1] as SearchQuery) : null);
}
},
get text() { get text() {
return text; return text;
}, },
+2 -3
View File
@@ -36,7 +36,6 @@ export interface PanelState {
interface JumpTarget { interface JumpTarget {
chatId: number; chatId: number;
marks: string[];
messageId: number; messageId: number;
} }
@@ -142,8 +141,8 @@ function createUi() {
: null; : null;
}, },
record, record,
requestJump(chatId: number, messageId: number, marks: string[] = []) { requestJump(chatId: number, messageId: number) {
jumpTarget = { chatId, marks, messageId }; jumpTarget = { chatId, messageId };
}, },
resetPanel() { resetPanel() {
panel = null; panel = null;
-6
View File
@@ -159,12 +159,6 @@ html.theme-transition * {
-webkit-user-select: none; -webkit-user-select: none;
user-select: none; user-select: none;
} }
input,
textarea,
select {
font-size: max(1em, 1rem) !important;
}
} }
.bg-menu-content { .bg-menu-content {
@@ -4,7 +4,7 @@
import MessageList from "#lib/components/MessageList.svelte"; import MessageList from "#lib/components/MessageList.svelte";
import { accounts } from "#lib/stores/accounts.svelte.js"; import { accounts } from "#lib/stores/accounts.svelte.js";
import { chats } from "#lib/stores/chats.svelte.js"; import { chats } from "#lib/stores/chats.svelte.js";
import { MARK_PARAM, withoutMessage } from "#lib/stores/feed.svelte.js"; import { withoutMessage } from "#lib/stores/feed.svelte.js";
import { ui } from "#lib/stores/ui.svelte.js"; import { ui } from "#lib/stores/ui.svelte.js";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { page } from "$app/state"; import { page } from "$app/state";
@@ -27,11 +27,7 @@
const messageId = Number(linkedMessage); const messageId = Number(linkedMessage);
untrack(() => { untrack(() => {
if (Number.isSafeInteger(messageId)) { if (Number.isSafeInteger(messageId)) {
ui.requestJump( ui.requestJump(chatId, messageId);
chatId,
messageId,
page.url.searchParams.getAll(MARK_PARAM)
);
} }
goto(withoutMessage(page.url), { replace: true, reset: false }); goto(withoutMessage(page.url), { replace: true, reset: false });
}); });