Skip to content

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.

jsx
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>
		</>
	);
}
js
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;
	}));
});
Live, on a fake world
Open

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 from dà a entrambe le Ada il ruolo della prima mentre una ricerca per fromId dà a ogni membro il suo ruolo. (I due membri dell'esempio hanno nomi diversi perché il from di una risposta finta sceglie un membro per nome.)
  • I messaggi della persona hanno fromId: null di proposito. La tua vista conosce la persona solo attraverso world.me e solo per quanto la persona ha scelto di condividere quando ha accettato la vista.
  • Con una webapp più vecchia che non ha fromId la vista mostra from per 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 ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.