скрытие статуса online через сокеты и поле isInvisible в профиле
This commit is contained in:
@@ -21,6 +21,7 @@ export interface User extends UserPresence {
|
||||
statusText?: string | null;
|
||||
statusEmoji?: string | null;
|
||||
statusExpiresAt?: string | null;
|
||||
isInvisible?: boolean;
|
||||
createdAt: string;
|
||||
hideStoryViews?: boolean;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export function normalizeUser(user: any): any {
|
||||
result.username = result.userName;
|
||||
}
|
||||
|
||||
// 2. Приведение avatar (Auth -> avatar, Profiles -> avatarUrl)
|
||||
|
||||
if (!result.avatarUrl && result.avatar) {
|
||||
result.avatarUrl = result.avatar;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ export function normalizeUser(user: any): any {
|
||||
result.avatar = result.avatarUrl;
|
||||
}
|
||||
|
||||
// 3. Приведение bio (Settings -> bio, Profiles -> about)
|
||||
|
||||
if (!result.bio && result.about) {
|
||||
result.bio = result.about;
|
||||
}
|
||||
@@ -33,7 +33,7 @@ export function normalizeUser(user: any): any {
|
||||
result.about = result.bio;
|
||||
}
|
||||
|
||||
// 4. Приведение status (snake_case -> camelCase)
|
||||
|
||||
if (!result.statusText && result.status_text) {
|
||||
result.statusText = result.status_text;
|
||||
}
|
||||
@@ -44,6 +44,11 @@ export function normalizeUser(user: any): any {
|
||||
result.statusExpiresAt = result.status_expires_at;
|
||||
}
|
||||
|
||||
// 5. Privacy flag
|
||||
if (typeof result.isInvisible === 'undefined' && typeof result.is_invisible !== 'undefined') {
|
||||
result.isInvisible = result.is_invisible;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ interface ChatState {
|
||||
addTypingUser: (chatId: string, userId: string) => void;
|
||||
removeTypingUser: (chatId: string, userId: string) => void;
|
||||
updateUserOnlineStatus: (userId: string, isOnline: boolean, lastSeen?: string) => void;
|
||||
applyInvisiblePrivacyMode: (enabled: boolean) => void;
|
||||
setReplyTo: (message: Message | null) => void;
|
||||
setEditingMessage: (message: Message | null) => void;
|
||||
addChat: (chat: Chat) => void;
|
||||
@@ -457,6 +458,9 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
},
|
||||
|
||||
updateUserOnlineStatus: (userId, isOnline, lastSeen) => {
|
||||
if (useAuthStore.getState().user?.isInvisible) {
|
||||
return;
|
||||
}
|
||||
set((state) => ({
|
||||
chats: state.chats.map((chat) => ({
|
||||
...chat,
|
||||
@@ -469,6 +473,22 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
}));
|
||||
},
|
||||
|
||||
applyInvisiblePrivacyMode: (enabled) => {
|
||||
if (!enabled) return;
|
||||
set((state) => ({
|
||||
chats: state.chats.map((chat) => ({
|
||||
...chat,
|
||||
members: chat.members.map((member) => ({
|
||||
...member,
|
||||
user: {
|
||||
...member.user,
|
||||
isOnline: false,
|
||||
},
|
||||
})),
|
||||
})),
|
||||
}));
|
||||
},
|
||||
|
||||
setReplyTo: (message) => set({ replyTo: message, editingMessage: null }),
|
||||
setEditingMessage: (message) => set({ editingMessage: message, replyTo: null }),
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ export default function ChatPage() {
|
||||
addTypingUser,
|
||||
removeTypingUser,
|
||||
updateUserOnlineStatus,
|
||||
applyInvisiblePrivacyMode,
|
||||
setPinnedMessage,
|
||||
removePinnedMessage,
|
||||
clearStore,
|
||||
@@ -76,6 +77,12 @@ export default function ChatPage() {
|
||||
loadChats();
|
||||
}, [loadChats]);
|
||||
|
||||
useEffect(() => {
|
||||
if (user?.isInvisible) {
|
||||
applyInvisiblePrivacyMode(true);
|
||||
}
|
||||
}, [user?.isInvisible, applyInvisiblePrivacyMode]);
|
||||
|
||||
// Обработка закрытия вкладки — отправить disconnect
|
||||
useEffect(() => {
|
||||
const handleBeforeUnload = () => {
|
||||
@@ -195,10 +202,12 @@ export default function ChatPage() {
|
||||
});
|
||||
|
||||
socket.on('user_online', (data: { userId: string }) => {
|
||||
if (useAuthStore.getState().user?.isInvisible) return;
|
||||
updateUserOnlineStatus(data.userId, true);
|
||||
});
|
||||
|
||||
socket.on('user_offline', (data: { userId: string; lastSeen?: string }) => {
|
||||
if (useAuthStore.getState().user?.isInvisible) return;
|
||||
updateUserOnlineStatus(data.userId, false, data.lastSeen);
|
||||
});
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ function ChatListItem({ chat, isActive }: ChatListItemProps) {
|
||||
? otherMember?.user.avatarUrl || otherMember?.user.avatar || null
|
||||
: chat.avatarUrl || chat.avatar || null;
|
||||
|
||||
const isOnline = chat.type === 'personal' && !!otherMember?.user.isOnline;
|
||||
const isOnline = chat.type === 'personal' && !user?.isInvisible && !!otherMember?.user.isOnline;
|
||||
|
||||
// Check if someone is typing in this chat
|
||||
const typingInChat = typingUsers.filter((t) => t.chatId === chat.id && t.userId !== user?.id);
|
||||
|
||||
@@ -181,6 +181,7 @@ export default function ChatView({ onStartCall, onStartGroupCall }: { onStartCal
|
||||
? otherMember?.user.avatarUrl || otherMember?.user.avatar || null
|
||||
: chat?.avatarUrl || chat?.avatar || null;
|
||||
const isOnline = chat?.type === 'personal' && otherMember?.user.isOnline;
|
||||
const privacyExchangeMode = !!user?.isInvisible;
|
||||
|
||||
const typingInChat = typingUsers.filter((t) => t.chatId === activeChat && t.userId !== user?.id);
|
||||
|
||||
@@ -859,7 +860,9 @@ export default function ChatView({ onStartCall, onStartGroupCall }: { onStartCal
|
||||
? <span className="text-primary font-black animate-pulse">{t('typing')}</span>
|
||||
: isOnline
|
||||
? <span className="text-success">{t('online')}</span>
|
||||
: chat.type === 'personal' && otherMember?.user.lastSeen
|
||||
: chat.type === 'personal' && privacyExchangeMode
|
||||
? t('wasRecently')
|
||||
: chat.type === 'personal' && otherMember?.user.lastSeen
|
||||
? `${formatLastSeen(otherMember.user.lastSeen, lang)}`
|
||||
: chat.type === 'group'
|
||||
? `${chat.members.length} ${t('members')}`
|
||||
|
||||
@@ -19,11 +19,12 @@ export class UserApi {
|
||||
statusText?: string | null;
|
||||
statusEmoji?: string | null;
|
||||
statusExpiresAt?: string | null;
|
||||
isInvisible?: boolean;
|
||||
}) {
|
||||
// Основной путь по ТЗ: PATCH /api/user/profile
|
||||
|
||||
const payload = {
|
||||
...data,
|
||||
about: data.bio // Дублируем для совместимости
|
||||
about: data.bio
|
||||
};
|
||||
return httpClient.request<User>('/user/profile', {
|
||||
method: 'PATCH',
|
||||
@@ -32,7 +33,7 @@ export class UserApi {
|
||||
}
|
||||
|
||||
static async updateSettings(settings: any) {
|
||||
// Конечная точка в новом бэкенде: PUT /api/profiles/settings
|
||||
|
||||
return httpClient.request('/profiles/settings', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(settings),
|
||||
@@ -40,13 +41,13 @@ export class UserApi {
|
||||
}
|
||||
|
||||
static async uploadAvatar(file: File) {
|
||||
// Конечная точка в новом бэкенде: POST /api/profiles/avatar
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('avatar', file);
|
||||
return httpClient.request<User>('/profiles/avatar', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
timeout: 120_000, // Аватар может быть большим, даем время на обработку
|
||||
timeout: 120_000,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
Bell, Shield, Eye, Smile
|
||||
} from 'lucide-react';
|
||||
import { useAuthStore } from '../../../auth/application/authStore';
|
||||
import { useChatStore } from '../../../chats/application/chatStore';
|
||||
import { useLang } from '../../../../core/infrastructure/i18n';
|
||||
import { UserApi } from '../../infrastructure/userApi';
|
||||
import { getMediaUrl, getInitials } from '../../../../core/utils/utils';
|
||||
@@ -28,6 +29,7 @@ const STATUS_PRESETS = [
|
||||
|
||||
export default function SettingsPage() {
|
||||
const { user, updateUser, logout } = useAuthStore();
|
||||
const { applyInvisiblePrivacyMode } = useChatStore();
|
||||
const { t, lang, setLang } = useLang();
|
||||
|
||||
const [editing, setEditing] = useState(false);
|
||||
@@ -37,11 +39,13 @@ export default function SettingsPage() {
|
||||
birthday: user?.birthday || '',
|
||||
statusText: user?.statusText || '',
|
||||
statusEmoji: user?.statusEmoji || '💬',
|
||||
statusDuration: 'none' as 'none' | '1h' | '1d'
|
||||
statusDuration: 'none' as 'none' | '1h' | '1d',
|
||||
isInvisible: !!user?.isInvisible
|
||||
});
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [showStatusPicker, setShowStatusPicker] = useState(false);
|
||||
const [showChangePasswordModal, setShowChangePasswordModal] = useState(false);
|
||||
const [updatingInvisible, setUpdatingInvisible] = useState(false);
|
||||
|
||||
// Avatar cropping state
|
||||
const [cropModal, setCropModal] = useState<{
|
||||
@@ -60,7 +64,8 @@ export default function SettingsPage() {
|
||||
birthday: user.birthday || '',
|
||||
statusText: user.statusText || '',
|
||||
statusEmoji: user.statusEmoji || '💬',
|
||||
statusDuration: 'none'
|
||||
statusDuration: 'none',
|
||||
isInvisible: !!user.isInvisible
|
||||
});
|
||||
}
|
||||
}, [user]);
|
||||
@@ -83,6 +88,9 @@ export default function SettingsPage() {
|
||||
};
|
||||
const updatedUser = await UserApi.updateProfile(payload);
|
||||
updateUser(updatedUser);
|
||||
if (payload.isInvisible) {
|
||||
applyInvisiblePrivacyMode(true);
|
||||
}
|
||||
setEditing(false);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
@@ -134,6 +142,32 @@ export default function SettingsPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleToggleInvisible = async () => {
|
||||
if (!user || updatingInvisible) return;
|
||||
const nextInvisible = !formData.isInvisible;
|
||||
setUpdatingInvisible(true);
|
||||
try {
|
||||
const updatedUser = await UserApi.updateProfile({
|
||||
displayName: user.displayName || '',
|
||||
bio: user.bio || '',
|
||||
birthday: user.birthday || null,
|
||||
statusText: user.statusText || null,
|
||||
statusEmoji: user.statusEmoji || null,
|
||||
statusExpiresAt: user.statusExpiresAt || null,
|
||||
isInvisible: nextInvisible,
|
||||
});
|
||||
updateUser(updatedUser);
|
||||
setFormData((p) => ({ ...p, isInvisible: nextInvisible }));
|
||||
if (nextInvisible) {
|
||||
applyInvisiblePrivacyMode(true);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to update privacy status', err);
|
||||
} finally {
|
||||
setUpdatingInvisible(false);
|
||||
}
|
||||
};
|
||||
|
||||
const initials = getInitials(user?.displayName || user?.username || '??');
|
||||
|
||||
return (
|
||||
@@ -412,6 +446,34 @@ export default function SettingsPage() {
|
||||
|
||||
{/* Account Section */}
|
||||
<div className="p-6 rounded-3xl bg-white/[0.02] border border-white/5 flex flex-col justify-between">
|
||||
<button
|
||||
onClick={handleToggleInvisible}
|
||||
disabled={updatingInvisible}
|
||||
className={`w-full mb-3 flex items-center justify-between p-4 rounded-2xl border transition-all group ${
|
||||
formData.isInvisible
|
||||
? 'bg-primary/10 border-primary/30'
|
||||
: 'bg-white/5 hover:bg-white/10 border-white/10'
|
||||
} ${updatingInvisible ? 'opacity-60 cursor-not-allowed' : ''}`}
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className={`w-9 h-9 rounded-xl flex items-center justify-center transition-transform ${
|
||||
formData.isInvisible ? 'bg-primary/20 text-primary' : 'bg-white/10 text-white/70'
|
||||
}`}>
|
||||
<Eye size={16} />
|
||||
</div>
|
||||
<div className="text-left">
|
||||
<span className="block text-[11px] font-black uppercase tracking-widest text-white/80">
|
||||
{lang === 'ru' ? 'Скрывать мой статус онлайн' : 'Hide my online status'}
|
||||
</span>
|
||||
<span className="block text-[10px] text-white/40 mt-0.5">
|
||||
{lang === 'ru' ? 'Если включено, вы тоже не видите точный статус других' : 'If enabled, you also cannot see exact status of others'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className={`w-11 h-6 rounded-full p-1 transition-colors ${formData.isInvisible ? 'bg-primary' : 'bg-white/15'}`}>
|
||||
<div className={`w-4 h-4 rounded-full bg-white transition-transform ${formData.isInvisible ? 'translate-x-5' : 'translate-x-0'}`} />
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setShowChangePasswordModal(true)}
|
||||
className="w-full mb-3 flex items-center justify-between p-4 rounded-2xl bg-white/5 hover:bg-white/10 border border-white/10 transition-all group"
|
||||
|
||||
Reference in New Issue
Block a user