refactor(database): migrate to new modular database layer and unify data access
- Remove legacy database.ts, LocalDataSource.ts, and MessageRepository.ts - Create new src/database/ module with messageRepository, userCacheRepository, conversationRepository, and groupCacheRepository - Update all consumers to import from @/database instead of services/database - Add web platform blur handling for modal components to fix focus issues - Flatten SystemMessageItem and NotificationsScreen styles for consistent design - Add draggable slider in ChatSettingsScreen and dynamic font size support - Introduce 9 new chat color themes - Add profile screens for about, terms, and privacy policy with navigation routes - Add policy links to login and registration screens - Fix post share URL format from /posts/ to /post/
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
|
||||
import type { ConversationResponse } from '../../../types/dto';
|
||||
import { messageService } from '../../../services/messageService';
|
||||
import { deleteConversation } from '../../../services/database';
|
||||
import { conversationRepository } from '@/database';
|
||||
import type { IConversationOperations } from '../types';
|
||||
import { useMessageStore, normalizeConversationId } from '../store';
|
||||
|
||||
@@ -60,7 +60,7 @@ export class ConversationOperations implements IConversationOperations {
|
||||
store.removeConversation(conversationId);
|
||||
|
||||
// 删除本地数据库中的会话
|
||||
deleteConversation(conversationId).catch(error => {
|
||||
conversationRepository.delete(conversationId).catch(error => {
|
||||
console.error('[ConversationOperations] 删除本地会话失败:', error);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
import type { MessageResponse, MessageSegment, ConversationResponse } from '../../../types/dto';
|
||||
import { messageService } from '../../../services/messageService';
|
||||
import { saveMessage } from '../../../services/database';
|
||||
import { messageRepository } from '@/database';
|
||||
import type { IMessageSendService } from '../types';
|
||||
import { useMessageStore, mergeMessagesById } from '../store';
|
||||
|
||||
@@ -64,7 +64,7 @@ export class MessageSendService implements IMessageSendService {
|
||||
.map((s: any) => s.data?.text || '')
|
||||
.join('') || '';
|
||||
|
||||
saveMessage({
|
||||
messageRepository.saveMessage({
|
||||
id: response.id,
|
||||
conversationId,
|
||||
senderId: currentUserId || '',
|
||||
|
||||
@@ -9,13 +9,7 @@
|
||||
|
||||
import type { ConversationResponse, MessageResponse } from '../../../types/dto';
|
||||
import { messageService } from '../../../services/messageService';
|
||||
import {
|
||||
getMessagesByConversation,
|
||||
getMaxSeq,
|
||||
saveMessagesBatch,
|
||||
saveConversationsWithRelatedCache,
|
||||
getMessagesBeforeSeq,
|
||||
} from '../../../services/database';
|
||||
import { messageRepository, conversationRepository } from '@/database';
|
||||
import {
|
||||
type IConversationListPagedSource,
|
||||
SqliteConversationListPagedSource,
|
||||
@@ -213,8 +207,8 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
// 先从本地数据库加载
|
||||
if (!hasInMemoryMessages) {
|
||||
try {
|
||||
const localMessages = await getMessagesByConversation(conversationId, 20);
|
||||
const localMaxSeq = await getMaxSeq(conversationId);
|
||||
const localMessages = await messageRepository.getByConversation(conversationId, 20);
|
||||
const localMaxSeq = await messageRepository.getMaxSeq(conversationId);
|
||||
baselineMaxSeq = localMaxSeq;
|
||||
|
||||
if (localMessages.length > 0) {
|
||||
@@ -249,7 +243,7 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
store.setMessages(conversationId, mergedSnapshot);
|
||||
|
||||
// 持久化到本地
|
||||
saveMessagesBatch(snapshotMessages.map((m: any) => ({
|
||||
messageRepository.saveMessagesBatch(snapshotMessages.map((m: any) => ({
|
||||
id: m.id,
|
||||
conversationId: m.conversation_id || conversationId,
|
||||
senderId: m.sender_id,
|
||||
@@ -276,7 +270,7 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
const mergedMessages = mergeMessagesById(existingMessages, newMessages);
|
||||
store.setMessages(conversationId, mergedMessages);
|
||||
|
||||
saveMessagesBatch(newMessages.map((m: any) => ({
|
||||
messageRepository.saveMessagesBatch(newMessages.map((m: any) => ({
|
||||
id: m.id,
|
||||
conversationId: m.conversation_id || conversationId,
|
||||
senderId: m.sender_id,
|
||||
@@ -306,20 +300,20 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
|
||||
store.setMessages(conversationId, mergedMessages);
|
||||
|
||||
saveMessagesBatch(newMessages.map((m: any) => ({
|
||||
id: m.id,
|
||||
conversationId: m.conversation_id || conversationId,
|
||||
senderId: m.sender_id,
|
||||
content: m.content,
|
||||
type: m.type || 'text',
|
||||
isRead: m.is_read || false,
|
||||
createdAt: m.created_at,
|
||||
seq: m.seq,
|
||||
status: m.status || 'normal',
|
||||
segments: m.segments,
|
||||
}))).catch(error => {
|
||||
console.error('[MessageSyncService] 保存消息到本地失败:', error);
|
||||
});
|
||||
messageRepository.saveMessagesBatch(newMessages.map((m: any) => ({
|
||||
id: m.id,
|
||||
conversationId: m.conversation_id || conversationId,
|
||||
senderId: m.sender_id,
|
||||
content: m.content,
|
||||
type: m.type || 'text',
|
||||
isRead: m.is_read || false,
|
||||
createdAt: m.created_at,
|
||||
seq: m.seq,
|
||||
status: m.status || 'normal',
|
||||
segments: m.segments,
|
||||
}))).catch(error => {
|
||||
console.error('[MessageSyncService] 保存消息到本地失败:', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -348,7 +342,7 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
|
||||
try {
|
||||
// 先从本地获取
|
||||
const localMessages = await getMessagesBeforeSeq(conversationId, beforeSeq, limit);
|
||||
const localMessages = await messageRepository.getBeforeSeq(conversationId, beforeSeq, limit);
|
||||
|
||||
if (localMessages.length >= limit) {
|
||||
const formattedMessages: MessageResponse[] = [...localMessages].reverse().map(m => ({
|
||||
@@ -374,7 +368,7 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
if (response?.messages && response.messages.length > 0) {
|
||||
const serverMessages = response.messages;
|
||||
|
||||
await saveMessagesBatch(serverMessages.map((m: any) => ({
|
||||
await messageRepository.saveMessagesBatch(serverMessages.map((m: any) => ({
|
||||
id: m.id,
|
||||
conversationId: m.conversation_id || conversationId,
|
||||
senderId: m.sender_id,
|
||||
@@ -497,7 +491,7 @@ export class MessageSyncService implements IMessageSyncService {
|
||||
.map(conv => conv.group)
|
||||
.filter((group): group is NonNullable<ConversationResponse['group']> => Boolean(group));
|
||||
|
||||
saveConversationsWithRelatedCache(list, users, groups).catch(error => {
|
||||
conversationRepository.saveWithRelated(list, users, groups).catch(error => {
|
||||
console.error('[MessageSyncService] 持久化会话列表失败:', error);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
import type { ConversationResponse } from '../../../types/dto';
|
||||
import { messageService } from '../../../services/messageService';
|
||||
import { markConversationAsRead, updateConversationCacheUnreadCount } from '../../../services/database';
|
||||
import { messageRepository, conversationRepository } from '@/database';
|
||||
import type { ReadStateRecord, IReadReceiptManager } from '../types';
|
||||
import { READ_STATE_PROTECTION_DELAY } from '../constants';
|
||||
import { useMessageStore, normalizeConversationId } from '../store';
|
||||
@@ -78,7 +78,7 @@ export class ReadReceiptManager implements IReadReceiptManager {
|
||||
store.setUnreadCount(newTotalUnread, currentUnread.system);
|
||||
|
||||
// 3. 更新本地数据库
|
||||
markConversationAsRead(normalizedId).catch(console.error);
|
||||
messageRepository.markConversationAsRead(normalizedId).catch(console.error);
|
||||
|
||||
// 4. 调用 API,完成后设置延迟清除保护
|
||||
try {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { MessageResponse, UserDTO } from '../../../types/dto';
|
||||
import { api } from '../../../services/api';
|
||||
import { getUserCache, saveUserCache } from '../../../services/database';
|
||||
import { userCacheRepository } from '@/database';
|
||||
import type { IUserCacheService } from '../types';
|
||||
|
||||
export class UserCacheService implements IUserCacheService {
|
||||
@@ -17,7 +17,7 @@ export class UserCacheService implements IUserCacheService {
|
||||
*/
|
||||
async getSenderInfo(userId: string): Promise<UserDTO | null> {
|
||||
// 1. 先检查本地缓存
|
||||
const cachedUser = await getUserCache(userId);
|
||||
const cachedUser = await userCacheRepository.get(userId);
|
||||
if (cachedUser) {
|
||||
return cachedUser;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ export class UserCacheService implements IUserCacheService {
|
||||
const response = await api.get<UserDTO>(`/users/${userId}`);
|
||||
if (response.code === 0 && response.data) {
|
||||
// 缓存到本地数据库
|
||||
await saveUserCache(response.data);
|
||||
await userCacheRepository.save(response.data);
|
||||
return response.data;
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -19,7 +19,7 @@ import type {
|
||||
} from '../../../services/wsService';
|
||||
import { wsService, GroupNoticeType } from '../../../services/wsService';
|
||||
import { vibrateOnMessage } from '../../../services/messageVibrationService';
|
||||
import { saveMessage, updateMessageStatus } from '../../../services/database';
|
||||
import { messageRepository } from '@/database';
|
||||
import type { MessageResponse, ConversationResponse } from '../../../types/dto';
|
||||
import type {
|
||||
IWSMessageHandler,
|
||||
@@ -318,7 +318,7 @@ export class WSMessageHandler implements IWSMessageHandler {
|
||||
|
||||
// 异步保存到本地数据库
|
||||
const textContent = segments?.filter((s: any) => s.type === 'text').map((s: any) => s.data?.text || '').join('') || '';
|
||||
saveMessage({
|
||||
messageRepository.saveMessage({
|
||||
id,
|
||||
conversationId: normalizedConversationId,
|
||||
senderId: sender_id || '',
|
||||
@@ -358,7 +358,7 @@ export class WSMessageHandler implements IWSMessageHandler {
|
||||
this.markMessageAsRecalled(normalizedConversationId, message_id);
|
||||
this.syncConversationLastMessageOnRecall(normalizedConversationId, message_id);
|
||||
|
||||
updateMessageStatus(message_id, 'recalled', true).catch(error => {
|
||||
messageRepository.updateStatus(message_id, 'recalled', true).catch(error => {
|
||||
console.error('[WSMessageHandler] 更新本地消息撤回状态失败:', error);
|
||||
});
|
||||
}
|
||||
@@ -373,7 +373,7 @@ export class WSMessageHandler implements IWSMessageHandler {
|
||||
this.markMessageAsRecalled(normalizedConversationId, message_id);
|
||||
this.syncConversationLastMessageOnRecall(normalizedConversationId, message_id);
|
||||
|
||||
updateMessageStatus(message_id, 'recalled', true).catch(error => {
|
||||
messageRepository.updateStatus(message_id, 'recalled', true).catch(error => {
|
||||
console.error('[WSMessageHandler] 更新本地消息撤回状态失败:', error);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user