Mostra chi ha scritto ogni messaggio
I membri di un mondo, che siano persone o agenti, parlano nella stessa conversazione e ognuno di loro ha un ruolo. message.from è il nome del mittente e i nomi non sono unici (due membri possono chiamarsi entrambi Ada). message.fromId invece è l'id del mittente così come compare in world.members e indica l'unico membro che ha scritto il messaggio. Fai due volte una domanda allo staff del museo dell'esempio per vedere ogni volta un membro diverso rispondere.
import { useWorld } from './useWorld.js';
// In this fake museum, two members answer in turn.
// `from` in an answer names the member who says it.
let turn = 0;
const museum = {
name: 'Museum',
members: [
{ name: 'Ada', role: 'guide' },
{ name: 'Bob', role: 'guard' },
],
onMessage: () => {
turn += 1;
if (turn % 2 === 1) return { from: 'Ada', text: 'Of course! The paintings are on the first floor.' };
return { from: 'Bob', text: 'Sure. The exit is on your left.' };
},
};
// Who wrote a message, as "You" or as the member who wrote it, with their role.
function authorOf(message, world) {
if (message.fromMe) return 'You';
// `fromId` is the member's id, as in `members`. An older webapp gives none, and only the name shows.
const member = (world.members ?? []).find((item) => item.id === message.fromId);
if (!member) return message.from ?? 'Someone';
return `${member.name ?? 'Someone'} (${member.role ?? 'no role'})`;
}
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: museum } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
return (
<>
{world.messages.map((message) => (
<p key={message.id}><b>{authorOf(message, world)}</b>: {message.text}</p>
))}
<button onClick={() => world.send('Can you help me?')} disabled={!world.canSend}>Ask the staff</button>
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// In this fake museum, two members answer in turn.
// `from` in an answer names the member who says it.
let turn = 0;
const museum = {
name: 'Museum',
members: [
{ name: 'Ada', role: 'guide' },
{ name: 'Bob', role: 'guard' },
],
onMessage: () => {
turn += 1;
if (turn % 2 === 1) return { from: 'Ada', text: 'Of course! The paintings are on the first floor.' };
return { from: 'Bob', text: 'Sure. The exit is on your left.' };
},
};
// Who wrote a message, as "You" or as the member who wrote it, with their role.
function authorOf(message, node) {
if (message.fromMe) return 'You';
// `fromId` is the member's id, as in `members`. An older webapp gives none, and only the name shows.
const member = (node.members ?? []).find((item) => item.id === message.fromId);
if (!member) return message.from ?? 'Someone';
return `${member.name ?? 'Someone'} (${member.role ?? 'no role'})`;
}
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: museum } });
const list = document.createElement('div');
const ask = document.createElement('button');
ask.textContent = 'Ask the staff';
ask.onclick = () => world.send('Can you help me?');
document.body.append(list, ask);
world.subscribe((node) => {
ask.disabled = !node.canSend;
list.replaceChildren(...node.messages.map((message) => {
const line = document.createElement('p');
const author = document.createElement('b');
author.textContent = authorOf(message, node);
line.append(author, `: ${message.text ?? ''}`);
return line;
}));
});Come funziona
fromId contiene l'id del membro per il messaggio di un membro, l'id del mondo per le sue note e null per i messaggi della persona. members.find((member) => member.id === message.fromId) restituisce il membro con il suo name, role, type e status. Quando nessun membro ha quell'id (il caso delle note del mondo), authorOf mostra il nome in from.
- Quando due membri hanno lo stesso nome, una ricerca per
fromdà a entrambe le Ada il ruolo della prima mentre una ricerca perfromIddà a ogni membro il suo ruolo. (I due membri dell'esempio hanno nomi diversi perché ilfromdi una risposta finta sceglie un membro per nome.) - I messaggi della persona hanno
fromId: nulldi proposito. La tua vista conosce la persona solo attraversoworld.mee solo per quanto la persona ha scelto di condividere quando ha accettato la vista. - Con una webapp più vecchia che non ha
fromIdla vista mostrafromper tutti. - Quando il mondo chiede di nascondere chi ha scritto ogni messaggio, non mostrare niente di tutto questo (Nascondi i mittenti quando il mondo lo chiede).
Vedi anche
- MemberName: un pulsante con il nome e il ruolo del mittente che apre la sua scheda del profilo.
- Apri il profilo di un membro
- Raggruppa i messaggi per mittente
- Persone e ruoli