-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
display rooms and implement simple message sending
- Loading branch information
Karl
committed
Nov 27, 2024
1 parent
228caa9
commit b6b5c52
Showing
6 changed files
with
254 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import type { Conversation } from "@dm3-org/dm3-js-sdk"; | ||
import { MessageIndicator } from "@dm3-org/dm3-js-sdk"; | ||
|
||
export type ChatRoom = { | ||
roomId: string; | ||
roomName: string; | ||
avatar: string; | ||
unreadCount: number; | ||
index: number; | ||
lastMessage: { | ||
_id: string; | ||
content: string; | ||
senderId: string; | ||
username: string; | ||
timestamp: string; | ||
saved: boolean; | ||
distributed: boolean; | ||
seen: boolean; | ||
new: boolean; | ||
}; | ||
users: { | ||
_id: string; | ||
username: string; | ||
avatar: string; | ||
status: { | ||
state: string; | ||
lastChanged: string; | ||
}; | ||
}[]; | ||
typingUsers: string[]; | ||
}; | ||
|
||
|
||
export function transformToRooms(inputData: Conversation[]): ChatRoom[] { | ||
// const initialisedMessages = await Promise.all(inputData.map((data) => { data.messages.init() })); | ||
|
||
const rooms = inputData.map((data, index) => { | ||
const { contact, messages } = data; | ||
|
||
const allParticipants = messages.participants.map((participant) => { | ||
return { | ||
_id: participant.account.ensName, | ||
username: participant.name, | ||
avatar: participant.image || "assets/imgs/sender.png", // TODO: get from contacts list | ||
status: { | ||
state: (new Date().getTime() - new Date(participant.updatedAt).getTime()) < 3600000 ? "online" : "offline", | ||
lastChanged: new Date(participant.updatedAt).toLocaleString(), | ||
}, | ||
} | ||
}); | ||
|
||
const lastMessage = messages.list[messages.list.length - 1]; | ||
|
||
return { | ||
roomId: `${contact.account.ensName}`, | ||
roomName: contact.name, | ||
avatar: contact.image || "assets/imgs/people.png", | ||
unreadCount: 0, // Default value, can be adjusted | ||
index: index, | ||
lastMessage: { | ||
_id: `${lastMessage.envelop.id}`, | ||
content: `${lastMessage?.envelop.message.message}`|| "No last message received", // Placeholder | ||
senderId: lastMessage?.envelop.message.metadata.from, | ||
username: contact.name, | ||
timestamp: new Date(lastMessage?.envelop.message.metadata.timestamp).toLocaleString(), | ||
// TODO: check if states are correct | ||
saved: lastMessage?.indicator === MessageIndicator.RECEIVED, | ||
distributed: lastMessage?.indicator === MessageIndicator.SENT, | ||
seen: lastMessage?.indicator === MessageIndicator.READED, | ||
new: lastMessage?.indicator !== MessageIndicator.READED, | ||
}, | ||
users: allParticipants, | ||
typingUsers: [], // Default empty | ||
}; | ||
}); | ||
|
||
return rooms; | ||
} | ||
|
||
export function transformToMessages(messagesData: any[]): any[] { | ||
return messagesData.map((message) => { | ||
console.log('message', message); | ||
|
||
// Ensure _id and senderId are valid | ||
const _id = message.envelop.id ? String(message.envelop.id) : "unknown_id"; | ||
const senderId = message.envelop.message.metadata.from ? String(message.envelop.message.metadata.from) : "unknown_sender"; | ||
|
||
return { | ||
_id: _id, | ||
indexId: message.indexId || null, // Assuming indexId is available or null | ||
content: message.envelop.message.message || "No content", | ||
senderId: senderId, | ||
username: message.senderName || "Unknown", // Assuming senderName is available | ||
avatar: message.senderAvatar || "assets/imgs/default.png", // Assuming senderAvatar is available | ||
date: new Date(message.envelop.message.metadata.timestamp).toLocaleDateString(), | ||
timestamp: new Date(message.envelop.message.metadata.timestamp).toLocaleTimeString(), | ||
system: message.system || false, | ||
saved: message.indicator === MessageIndicator.RECEIVED, | ||
distributed: message.indicator === MessageIndicator.SENT, | ||
seen: message.indicator === MessageIndicator.READED, | ||
deleted: message.deleted || false, | ||
failure: message.failure || false, | ||
disableActions: message.disableActions || false, | ||
disableReactions: message.disableReactions || false, | ||
files: message.files || [], | ||
reactions: message.reactions || {}, | ||
replyMessage: message.replyMessage || null, | ||
}; | ||
}); | ||
} |
151 changes: 35 additions & 116 deletions
151
packages/messenger-vue-demo/src/components/Dm3Chat.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,62 @@ | ||
<template> | ||
{{ rooms }} | ||
{{ messages }} | ||
<!-- <vue-advanced-chat | ||
:current-user-id="currentUserId" | ||
<div v-if="!isReady" class="loading-animation"> | ||
<p>Loading...</p> | ||
</div> | ||
<vue-advanced-chat | ||
:loading-rooms="!isReady" | ||
:current-user-id="loggedInAccount" | ||
:rooms="JSON.stringify(rooms)" | ||
:messages="JSON.stringify(messages)" | ||
:room-actions="JSON.stringify(roomActions)" | ||
:messages-loaded="messagesLoaded" | ||
:rooms-loaded="roomsLoaded" | ||
/> --> | ||
<button @click="startTestConversation">Start test conversation</button> | ||
isReady: {{ isReady }} | ||
@fetch-messages="fetchMessages($event.detail[0])" | ||
@send-message="sendMessage($event.detail[0])" | ||
/> | ||
<button :disabled="!isReady" @click="startTestConversation">Start test conversation</button> | ||
|
||
<details> | ||
<summary>Conversations Preview</summary> | ||
<pre>{{ JSON.stringify(conversationsPreview, null, 2) }}</pre> | ||
</details> | ||
<details> | ||
<summary>Rooms</summary> | ||
<pre>{{ JSON.stringify(rooms, null, 2) }}</pre> | ||
</details> | ||
<details> | ||
<summary>Messages</summary> | ||
<pre>{{ JSON.stringify(messages, null, 2) }}</pre> | ||
</details> | ||
</template> | ||
|
||
<script setup> | ||
import { onMounted, ref } from 'vue' | ||
import { register } from 'vue-advanced-chat' | ||
import { useDm3Chat } from '../composables/chat' | ||
register() | ||
const roomsLoaded = ref(false); | ||
const messagesLoaded = ref(false); | ||
const { rooms, messages, init, startTestConversation, isReady } = useDm3Chat(); | ||
const { | ||
messages, | ||
init, | ||
startTestConversation, | ||
isReady, | ||
conversationsPreview, | ||
rooms, | ||
fetchMessages, | ||
roomsLoaded, | ||
messagesLoaded, | ||
sendMessage | ||
} = useDm3Chat(); | ||
onMounted(() => { | ||
init(); | ||
}) | ||
const currentUserId = '1234' | ||
// const rooms = [ | ||
// { | ||
// roomId: '1', | ||
// roomName: 'Room 1', | ||
// avatar: 'https://i.pravatar.cc/300?u=1', | ||
// unreadCount: 4, | ||
// index: 3, | ||
// lastMessage: { | ||
// _id: 'xyz', | ||
// content: 'Last message received', | ||
// senderId: '1234', | ||
// username: 'John Doe', | ||
// timestamp: '10:20', | ||
// saved: true, | ||
// distributed: false, | ||
// seen: false, | ||
// new: true | ||
// }, | ||
// users: [ | ||
// { | ||
// _id: '1234', | ||
// username: 'John Doe', | ||
// avatar: 'https://i.pravatar.cc/300?u=1234', | ||
// status: { | ||
// state: 'online', | ||
// lastChanged: 'today, 14:30' | ||
// } | ||
// }, | ||
// { | ||
// _id: '4321', | ||
// username: 'John Snow', | ||
// avatar: 'https://i.pravatar.cc/300?u=4321', | ||
// status: { | ||
// state: 'offline', | ||
// lastChanged: '14 July, 20:00' | ||
// } | ||
// } | ||
// ], | ||
// typingUsers: [ 4321 ] | ||
// } | ||
// ] | ||
// const messages=[ | ||
// { | ||
// _id: '7890', | ||
// indexId: 12092, | ||
// content: 'Message 1', | ||
// senderId: '1234', | ||
// username: 'John Doe', | ||
// avatar: 'https://i.pravatar.cc/300?u=1234', | ||
// date: '13 November', | ||
// timestamp: '10:20', | ||
// system: false, | ||
// saved: true, | ||
// distributed: true, | ||
// seen: true, | ||
// deleted: false, | ||
// failure: true, | ||
// disableActions: false, | ||
// disableReactions: false, | ||
// // files: [ | ||
// // { | ||
// // name: 'My File', | ||
// // size: 67351, | ||
// // type: 'png', | ||
// // audio: true, | ||
// // duration: 14.4, | ||
// // url: 'https://firebasestorage.googleapis.com/...', | ||
// // preview: 'data:image/png;base64,iVBORw0KGgoAA...', | ||
// // progress: 88 | ||
// // } | ||
// // ], | ||
// reactions: { | ||
// '😁': [ | ||
// '1234', // USER_ID | ||
// '4321' | ||
// ], | ||
// '🥰': [ | ||
// '1234' | ||
// ] | ||
// }, | ||
// replyMessage: { | ||
// content: 'Reply Message', | ||
// senderId: '4321', | ||
// // files: [ | ||
// // { | ||
// // name: 'My Replied File', | ||
// // size: 67351, | ||
// // type: 'png', | ||
// // audio: true, | ||
// // duration: 14.4, | ||
// // url: 'https://firebasestorage.googleapis.com/...', | ||
// // preview: 'data:image/png;base64,iVBORw0KGgoAA...' | ||
// // } | ||
// // ] | ||
// }, | ||
// } | ||
// ] | ||
const roomActions = [ | ||
{ name: 'inviteUser', title: 'Invite User' }, | ||
{ name: 'removeUser', title: 'Remove User' }, | ||
{ name: 'deleteRoom', title: 'Delete Room' } | ||
] | ||
onMounted(() => { | ||
setTimeout(() => { | ||
messagesLoaded.value = true; | ||
roomsLoaded.value = true; | ||
}, 1000); | ||
}) | ||
</script> |
Oops, something went wrong.