feat(web): владелец группы меняет иконку беседы (Фаза 7)

D-070: иконка группы приходит полем `icon_file_id` — у группы это её файл, у
1:1 по-прежнему аватар собеседника. Разбор беседы из REST вынесен в
`parseChannelPayload`: групповые поля и иконка не теряются, а у группы без
иконки поле очищается, чтобы не остался чужой аватар.

`api/dmIcon.ts` грузит и снимает иконку multipart-запросом (FormData уже умеет
общий `request`). В шапке групповой беседы у владельца появилось меню «Иконка
беседы»: загрузить или удалить; ответ сервера сразу кладётся в стор бесед, а
список перечитывается — снятая иконка приходит пустым полем (AGENT.md 7.8,
11.6). Строки добавлены в ru и en.
This commit is contained in:
2026-09-26 16:43:55 +03:00
parent 5320618d31
commit c429cbab30
7 changed files with 494 additions and 23 deletions
+40
View File
@@ -0,0 +1,40 @@
import { request } from './client';
import { parseChannelPayload } from './friends';
import type { Channel } from './types';
/**
* Иконка групповой беседы (AGENT.md §7.8, D-070).
*
* Иконка — обычная загрузка с назначением `dm_icon` (D-042), поэтому тело
* запроса — multipart, а `request()` уже умеет `FormData`. Менять иконку может
* только владелец беседы; ответ — обновлённая беседа в общем формате
* `dmChannelPayload`, как у переименования.
*/
function payloadChannel(value: unknown): Channel {
const channel = parseChannelPayload(value);
if (channel === null) {
throw new Error('malformed group channel payload');
}
return channel;
}
/** `POST /channels/{id}/icon` — загрузить иконку беседы (владелец). */
export async function uploadDmIcon(channelId: string, file: File): Promise<Channel> {
const form = new FormData();
form.append('file', file);
const payload = await request<{ channel?: unknown }>(
`/channels/${encodeURIComponent(channelId)}/icon`,
{ method: 'POST', form },
);
return payloadChannel(payload.channel);
}
/** `DELETE /channels/{id}/icon` — снять иконку беседы (владелец). */
export async function deleteDmIcon(channelId: string): Promise<Channel> {
const payload = await request<{ channel?: unknown }>(
`/channels/${encodeURIComponent(channelId)}/icon`,
{ method: 'DELETE' },
);
return payloadChannel(payload.channel);
}
+32 -18
View File
@@ -277,12 +277,7 @@ export async function fetchDirectChannels(signal?: AbortSignal): Promise<Channel
return [];
}
return payload.channels
.map((item) => {
const record = asRecord(item);
const channel = parseDirectChannel(item);
// Групповая беседа: имя и состав приходят полями канала, а не recipient.
return record === null || channel === null ? null : withGroupFields(channel, record);
})
.map((item) => parseChannelPayload(item))
.filter((item): item is Channel => item !== null);
}
@@ -324,6 +319,15 @@ function withGroupFields(channel: Channel, record: Record<string, unknown>): Cha
if (ownerId !== undefined) {
channel.owner_id = ownerId;
}
// Иконка группы — своя загрузка (D-070): у 1:1 в этом поле аватар
// собеседника, поэтому у группы без иконки поле очищается, а не остаётся
// чужим аватаром.
const icon = asString(record['icon_file_id']);
if (icon !== undefined) {
channel.icon_file_id = icon;
} else {
delete channel.icon_file_id;
}
if (Array.isArray(record['recipients'])) {
channel.recipients = record['recipients']
.map(parseDmRecipient)
@@ -332,6 +336,19 @@ function withGroupFields(channel: Channel, record: Record<string, unknown>): Cha
return channel;
}
/**
* Разбирает беседу из ответа API: 1:1 и группа идут общим путём, поэтому
* групповые поля и иконка не теряются (AGENT.md §7.8).
*/
export function parseChannelPayload(value: unknown): Channel | null {
const record = asRecord(value);
const channel = parseDirectChannel(value);
if (record === null || channel === null) {
return null;
}
return withGroupFields(channel, record);
}
/** `POST /users/@me/channels/group` — создать групповую беседу (7.8). */
export async function createGroupDirectChannel(
name: string,
@@ -341,12 +358,11 @@ export async function createGroupDirectChannel(
method: 'POST',
body: { name, recipient_ids: recipientIds },
});
const record = asRecord(payload.channel);
const channel = parseDirectChannel(payload.channel);
if (record === null || channel === null) {
const channel = parseChannelPayload(payload.channel);
if (channel === null) {
throw new Error('malformed group channel payload');
}
return withGroupFields(channel, record);
return channel;
}
/** `PUT /channels/{id}/recipients/{user_id}` — добавить участника в группу. */
@@ -355,12 +371,11 @@ export async function addGroupRecipient(channelId: string, userId: string): Prom
`/channels/${channelId}/recipients/${userId}`,
{ method: 'PUT' },
);
const record = asRecord(payload.channel);
const channel = parseDirectChannel(payload.channel);
if (record === null || channel === null) {
const channel = parseChannelPayload(payload.channel);
if (channel === null) {
throw new Error('malformed group channel payload');
}
return withGroupFields(channel, record);
return channel;
}
/** `DELETE /channels/{id}/recipients/{user_id}` — выйти или удалить участника. */
@@ -374,12 +389,11 @@ export async function renameGroupDirectChannel(channelId: string, name: string):
method: 'PATCH',
body: { name },
});
const record = asRecord(payload.channel);
const channel = parseDirectChannel(payload.channel);
if (record === null || channel === null) {
const channel = parseChannelPayload(payload.channel);
if (channel === null) {
throw new Error('malformed group channel payload');
}
return withGroupFields(channel, record);
return channel;
}
/** `POST /users/@me/channels` — открыть (или создать) беседу с пользователем. */
+3 -2
View File
@@ -103,8 +103,9 @@ export interface Channel {
permission_overwrites?: ChannelOverwrite[];
/**
* Поля личной беседы (`type: 'dm'`): id собеседника, его аватар и видимый
* статус. В снапшоте READY аватар приходит как `icon_file_id`, в REST-списке
* бесед — внутри `recipient`.
* статус. Аватар собеседника приходит и как `icon_file_id` (READY и
* REST-список бесед), а у группы в этом поле лежит её собственная иконка
* (AGENT.md 7.8, D-070).
*/
recipient_id?: string;
icon_file_id?: string;
@@ -1,14 +1,20 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { deleteDmIcon, uploadDmIcon } from '@/api/dmIcon';
import type { Message } from '@/api/messages';
import type { Channel } from '@/api/types';
import { ChatView } from '@/components/chat/ChatView';
import { PinsPanel } from '@/components/chat/PinsPanel';
import { SearchPanel } from '@/components/chat/SearchPanel';
import { useAuthorDirectory } from '@/components/chat/useAuthorDirectory';
import { Avatar } from '@/components/ui/Avatar';
import { ErrorNotice } from '@/components/ui/ErrorNotice';
import { Menu, MenuItem } from '@/components/ui/Menu';
import { Card } from '@/components/ui/primitives';
import { SkeletonLines } from '@/components/ui/Skeleton';
import { useCurrentUser } from '@/lib/hooks';
import { presenceDotClass } from '@/lib/presence';
import { useMessagesStore } from '@/stores/messages';
import { useSessionStore } from '@/stores/session';
@@ -27,6 +33,38 @@ export function DirectMessageView({ channelId }: { channelId: string }) {
),
);
const reloadDmChannels = useSessionStore((state) => state.reloadDmChannels);
const upsertDmChannel = useSessionStore((state) => state.upsertDmChannel);
const currentUser = useCurrentUser();
// Иконку групповой беседы меняет только владелец (D-070): у 1:1 своей
// иконки нет — там показывается аватар собеседника.
const isOwner = channel?.is_group === true && channel.owner_id === currentUser.data?.id;
const [iconMenuOpen, setIconMenuOpen] = useState(false);
const [iconError, setIconError] = useState<unknown>(null);
const iconMenuRef = useRef<HTMLButtonElement | null>(null);
const iconInput = useRef<HTMLInputElement | null>(null);
// Ответ сервера кладём в стор бесед: иконка появляется сразу и у владельца,
// а остальным участникам приходит DM_CHANNEL_UPDATE (AGENT.md 11.6). Список
// перечитываем ещё и запросом: снятая иконка приходит пустым полем, а
// слияние в сторе пустое поле не затирает.
const applyUpdated = (updated: Channel): void => {
setIconError(null);
upsertDmChannel(updated);
void reloadDmChannels();
};
const uploadIcon = useMutation({
mutationFn: (file: File) => uploadDmIcon(channelId, file),
onSuccess: applyUpdated,
onError: (cause) => setIconError(cause),
});
const removeIcon = useMutation({
mutationFn: () => deleteDmIcon(channelId),
onSuccess: applyUpdated,
onError: (cause) => setIconError(cause),
});
// Прямая ссылка на беседу: список приходит снапшотом, но после перезагрузки
// страницы его может ещё не быть — подстраховываемся REST-запросом.
@@ -115,6 +153,38 @@ export function DirectMessageView({ channelId }: { channelId: string }) {
<span className="sr-only">{t(`user.status.${status}`)}</span>
)}
<div className="ml-auto flex items-center gap-1">
{isOwner ? (
<>
<button
ref={iconMenuRef}
type="button"
aria-haspopup="menu"
aria-expanded={iconMenuOpen}
aria-label={t('friends.group.icon.menu')}
title={t('friends.group.icon.menu')}
data-testid="dm-icon-menu"
className="rounded-[var(--radius-sm)] px-1.5 text-base leading-none text-fg-muted transition-[color,background-color,transform] duration-[var(--duration-fast)] hover:bg-surface-3 hover:text-fg active:scale-95"
onClick={() => setIconMenuOpen((open) => !open)}
>
🖼
</button>
<input
ref={iconInput}
type="file"
accept="image/*"
className="hidden"
data-testid="dm-icon-input"
onChange={(event) => {
const file = event.target.files?.[0];
if (file !== undefined) {
uploadIcon.mutate(file);
}
// Иначе повторный выбор того же файла не сработает.
event.target.value = '';
}}
/>
</>
) : null}
<button
type="button"
aria-label={t('chat.search.open')}
@@ -137,6 +207,49 @@ export function DirectMessageView({ channelId }: { channelId: string }) {
</div>
</header>
{isOwner ? (
<Menu
open={iconMenuOpen}
label={t('friends.group.icon.menu')}
anchorRef={iconMenuRef}
placement="bottom"
align="right"
onClose={() => setIconMenuOpen(false)}
>
<MenuItem
icon="🖼"
data-testid="dm-icon-upload"
disabled={uploadIcon.isPending}
onClick={() => {
setIconMenuOpen(false);
iconInput.current?.click();
}}
>
{t('friends.group.icon.upload')}
</MenuItem>
{channel.icon_file_id === undefined ? null : (
<MenuItem
danger
icon="✖"
data-testid="dm-icon-remove"
disabled={removeIcon.isPending}
onClick={() => {
setIconMenuOpen(false);
removeIcon.mutate();
}}
>
{t('friends.group.icon.remove')}
</MenuItem>
)}
</Menu>
) : null}
{iconError === null ? null : (
<div className="px-4 pt-2">
<ErrorNotice error={iconError} />
</div>
)}
<SearchPanel
channelId={channel.id}
open={searchOpen}
+6 -1
View File
@@ -435,7 +435,12 @@
"membersHint": "Pick at least two friends:",
"create": "Create",
"members": "Members: {{count}}",
"open": "Open the group “{{name}}”, members: {{count}}"
"open": "Open the group “{{name}}”, members: {{count}}",
"icon": {
"menu": "Conversation icon",
"upload": "Upload icon",
"remove": "Remove icon"
}
},
"friendsSection": "Friends",
"friendsEmpty": "Your friend list is empty.",
+6 -1
View File
@@ -435,7 +435,12 @@
"membersHint": "Выберите минимум двух друзей:",
"create": "Создать",
"members": "Участников: {{count}}",
"open": "Открыть группу «{{name}}», участников: {{count}}"
"open": "Открыть группу «{{name}}», участников: {{count}}",
"icon": {
"menu": "Иконка беседы",
"upload": "Загрузить иконку",
"remove": "Удалить иконку"
}
},
"friendsSection": "Друзья",
"friendsEmpty": "Список друзей пуст.",