fix: messageId not found

This commit is contained in:
molvqingtai 2024-09-17 02:40:59 +08:00
parent 6fd42ef6d5
commit bb9eccd31c
3 changed files with 22 additions and 15 deletions

View file

@ -1,6 +1,5 @@
import { Remesh } from 'remesh' import { Remesh } from 'remesh'
import { ListModule } from 'remesh/modules/list' import { ListModule } from 'remesh/modules/list'
import { nanoid } from 'nanoid'
import { IndexDBStorageExtern } from '@/domain/externs/Storage' import { IndexDBStorageExtern } from '@/domain/externs/Storage'
import StorageEffect from '@/domain/modules/StorageEffect' import StorageEffect from '@/domain/modules/StorageEffect'
@ -51,11 +50,10 @@ const MessageListDomain = Remesh.domain({
const CreateItemCommand = domain.command({ const CreateItemCommand = domain.command({
name: 'MessageList.CreateItemCommand', name: 'MessageList.CreateItemCommand',
impl: (_, message: Omit<Message, 'id'>) => { impl: (_, message: Message) => {
const newMessage = { ...message, id: nanoid() }
return [ return [
MessageListModule.command.AddItemCommand(newMessage), MessageListModule.command.AddItemCommand(message),
CreateItemEvent(newMessage), CreateItemEvent(message),
ChangeListEvent(), ChangeListEvent(),
SyncToStorageEvent() SyncToStorageEvent()
] ]

View file

@ -5,6 +5,7 @@ import { PeerRoomExtern } from '@/domain/externs/PeerRoom'
import MessageListDomain from '@/domain/MessageList' import MessageListDomain from '@/domain/MessageList'
import UserInfoDomain from '@/domain/UserInfo' import UserInfoDomain from '@/domain/UserInfo'
import { callbackToObservable, desert, stringToHex } from '@/utils' import { callbackToObservable, desert, stringToHex } from '@/utils'
import { nanoid } from 'nanoid'
export enum MessageType { export enum MessageType {
Like = 'like', Like = 'like',
@ -14,16 +15,17 @@ export enum MessageType {
export interface LikeMessage extends MessageUser { export interface LikeMessage extends MessageUser {
type: MessageType.Like type: MessageType.Like
messageId: string id: string
} }
export interface HateMessage extends MessageUser { export interface HateMessage extends MessageUser {
type: MessageType.Hate type: MessageType.Hate
messageId: string id: string
} }
export interface TextMessage extends MessageUser { export interface TextMessage extends MessageUser {
type: MessageType.Text type: MessageType.Text
id: string
body: string body: string
} }
@ -43,8 +45,10 @@ const RoomDomain = Remesh.domain({
name: 'RoomSendTextMessageCommand', name: 'RoomSendTextMessageCommand',
impl: ({ get }, message: string) => { impl: ({ get }, message: string) => {
const { id: userId, name: username, avatar: userAvatar } = get(userInfoDomain.query.UserInfoQuery())! const { id: userId, name: username, avatar: userAvatar } = get(userInfoDomain.query.UserInfoQuery())!
const id = nanoid()
return [ return [
messageListDomain.command.CreateItemCommand({ messageListDomain.command.CreateItemCommand({
id,
body: message, body: message,
date: Date.now(), date: Date.now(),
userId, userId,
@ -53,7 +57,7 @@ const RoomDomain = Remesh.domain({
likeUsers: [], likeUsers: [],
hateUsers: [] hateUsers: []
}), }),
SendTextMessageEvent({ body: message, userId, username, userAvatar, type: MessageType.Text }) SendTextMessageEvent({ id, body: message, userId, username, userAvatar, type: MessageType.Text })
] ]
} }
}) })
@ -76,7 +80,7 @@ const RoomDomain = Remesh.domain({
userAvatar userAvatar
}) })
}), }),
SendLikeMessageEvent({ messageId, userId, username, userAvatar, type: MessageType.Like }) SendLikeMessageEvent({ id: messageId, userId, username, userAvatar, type: MessageType.Like })
] ]
} }
}) })
@ -100,7 +104,7 @@ const RoomDomain = Remesh.domain({
userAvatar userAvatar
}) })
}), }),
SendHateMessageEvent({ messageId, userId, username, userAvatar, type: MessageType.Hate }) SendHateMessageEvent({ id: messageId, userId, username, userAvatar, type: MessageType.Hate })
] ]
} }
}) })
@ -151,10 +155,9 @@ const RoomDomain = Remesh.domain({
const onMessage$ = callbackToObservable<RoomMessage>(peerRoom.onMessage.bind(peerRoom)) const onMessage$ = callbackToObservable<RoomMessage>(peerRoom.onMessage.bind(peerRoom))
return onMessage$.pipe( return onMessage$.pipe(
map((message) => { map((message) => {
console.log(message)
switch (message.type) { switch (message.type) {
case 'text': case 'text':
console.log(message)
return messageListDomain.command.CreateItemCommand({ return messageListDomain.command.CreateItemCommand({
...message, ...message,
date: Date.now(), date: Date.now(),
@ -162,7 +165,7 @@ const RoomDomain = Remesh.domain({
hateUsers: [] hateUsers: []
}) })
case 'like': { case 'like': {
const _message = get(messageListDomain.query.ItemQuery(message.messageId)) const _message = get(messageListDomain.query.ItemQuery(message.id))
return messageListDomain.command.UpdateItemCommand({ return messageListDomain.command.UpdateItemCommand({
..._message, ..._message,
likeUsers: desert(_message.likeUsers, 'userId', { likeUsers: desert(_message.likeUsers, 'userId', {
@ -173,7 +176,7 @@ const RoomDomain = Remesh.domain({
}) })
} }
case 'hate': { case 'hate': {
const _message = get(messageListDomain.query.ItemQuery(message.messageId)) const _message = get(messageListDomain.query.ItemQuery(message.id))
return messageListDomain.command.UpdateItemCommand({ return messageListDomain.command.UpdateItemCommand({
..._message, ..._message,
hateUsers: desert(_message.hateUsers, 'userId', { hateUsers: desert(_message.hateUsers, 'userId', {

View file

@ -20,18 +20,24 @@ class PeerRoom {
this.room = joinRoom({ appId: this.appId }, roomId) this.room = joinRoom({ appId: this.appId }, roomId)
this.room?.onPeerJoin((peerId) => console.log(`${peerId} joined`)) this.room?.onPeerJoin((peerId) => console.log(`${peerId} joined`))
this.room?.onPeerLeave((peerId) => console.log(`${peerId} left`))
console.log(this.room.getPeers()) console.log(this.room.getPeers())
return this.room return this.room
} }
async sendMessage<T extends PeerMessage>(message: T) { async sendMessage<T extends PeerMessage>(message: T) {
if (!this.room) {
throw new Error('Room not joined')
}
const [send] = this.room!.makeAction('MESSAGE') const [send] = this.room!.makeAction('MESSAGE')
return await send(message as DataPayload) return await send(message as DataPayload)
} }
onMessage<T extends PeerMessage>(callback: (message: T) => void) { onMessage<T extends PeerMessage>(callback: (message: T) => void) {
if (!this.room) {
throw new Error('Room not joined')
}
const [, on] = this.room!.makeAction('MESSAGE') const [, on] = this.room!.makeAction('MESSAGE')
on((message) => callback(message as T)) on((message) => callback(message as T))
} }