Allow forwarding usermessages.
They now have the same format as chat messages; the only difference is that they are not stored in the chat history. Also adds userMessage method to ServerConnection. Fix usermessages.
This commit is contained in:
+62
-33
@@ -124,9 +124,21 @@ function ServerConnection() {
|
||||
/**
|
||||
* onchat is called whenever a new chat message is received.
|
||||
*
|
||||
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: number, kind: string, message: string) => void}
|
||||
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: number, priviledged: boolean, kind: string, message: string) => void}
|
||||
*/
|
||||
this.onchat = null;
|
||||
/**
|
||||
* onusermessage is called when an application-specific message is
|
||||
* received. Id is null when the message originated at the server,
|
||||
* a user-id otherwise.
|
||||
*
|
||||
* 'kind' is typically one of 'error', 'warning', 'info' or 'mute'. If
|
||||
* 'id' is non-null, 'priviledged' indicates whether the message was
|
||||
* sent by an operator.
|
||||
*
|
||||
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: number, priviledged: boolean, kind: string, message: string) => void}
|
||||
*/
|
||||
this.onusermessage = null;
|
||||
/**
|
||||
* onclearchat is called whenever the server requests that the chat
|
||||
* be cleared.
|
||||
@@ -134,13 +146,6 @@ function ServerConnection() {
|
||||
* @type{(this: ServerConnection) => void}
|
||||
*/
|
||||
this.onclearchat = null;
|
||||
/**
|
||||
* onusermessage is called when the server sends an error or warning
|
||||
* message that should be displayed to the user.
|
||||
*
|
||||
* @type{(this: ServerConnection, kind: string, message: string) => void}
|
||||
*/
|
||||
this.onusermessage = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,6 +156,7 @@ function ServerConnection() {
|
||||
* @property {string} [dest]
|
||||
* @property {string} [username]
|
||||
* @property {string} [password]
|
||||
* @property {boolean} [priviledged]
|
||||
* @property {Object<string,boolean>} [permissions]
|
||||
* @property {string} [group]
|
||||
* @property {string} [value]
|
||||
@@ -286,7 +292,15 @@ ServerConnection.prototype.connect = async function(url) {
|
||||
case 'chat':
|
||||
if(sc.onchat)
|
||||
sc.onchat.call(
|
||||
sc, m.id, m.dest, m.username, m.time, m.kind, m.value,
|
||||
sc, m.id, m.dest, m.username, m.time,
|
||||
m.privileged, m.kind, m.value,
|
||||
);
|
||||
break;
|
||||
case 'usermessage':
|
||||
if(sc.onusermessage)
|
||||
sc.onusermessage.call(
|
||||
sc, m.id, m.dest, m.username, m.time,
|
||||
m.priviledged, m.kind, m.value,
|
||||
);
|
||||
break;
|
||||
case 'clearchat':
|
||||
@@ -301,10 +315,6 @@ ServerConnection.prototype.connect = async function(url) {
|
||||
case 'pong':
|
||||
/* nothing */
|
||||
break;
|
||||
case 'usermessage':
|
||||
if(sc.onusermessage)
|
||||
sc.onusermessage.call(sc, m.kind, m.value);
|
||||
break;
|
||||
default:
|
||||
console.warn('Unexpected server message', m.type);
|
||||
return;
|
||||
@@ -422,18 +432,53 @@ ServerConnection.prototype.newUpStream = function(id) {
|
||||
* chat sends a chat message to the server. The server will normally echo
|
||||
* the message back to the client.
|
||||
*
|
||||
* @param {string} username - The username of the sending user.
|
||||
* @param {string} username - The sender's username.
|
||||
* @param {string} kind - The kind of message, either "" or "me".
|
||||
* @param {string} message - The text of the message.
|
||||
* @param {string} dest - The id to send the message to, empty for broadcast.
|
||||
* @param {string} value - The text of the message.
|
||||
*/
|
||||
ServerConnection.prototype.chat = function(username, kind, dest, message) {
|
||||
ServerConnection.prototype.chat = function(username, kind, dest, value) {
|
||||
this.send({
|
||||
type: 'chat',
|
||||
id: this.id,
|
||||
dest: dest,
|
||||
username: username,
|
||||
kind: kind,
|
||||
value: message,
|
||||
value: value,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* userAction sends a request to act on a user.
|
||||
*
|
||||
* @param {string} kind - One of "op", "unop", "kick", "present", "unpresent".
|
||||
* @param {string} dest - The id of the user to act upon.
|
||||
* @param {string} [value] - An optional user-readable message.
|
||||
*/
|
||||
ServerConnection.prototype.userAction = function(kind, dest, value) {
|
||||
this.send({
|
||||
type: 'useraction',
|
||||
id: this.id,
|
||||
dest: dest,
|
||||
kind: kind,
|
||||
value: value,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* userMessage sends an application-specific message to a user.
|
||||
*
|
||||
* @param {string} kind - The kind of application-specific message.
|
||||
* @param {string} dest - The id of the user to send the message to.
|
||||
* @param {string} [value] - An optional parameter.
|
||||
*/
|
||||
ServerConnection.prototype.userMessage = function(kind, dest, value) {
|
||||
this.send({
|
||||
type: 'usermessage',
|
||||
id: this.id,
|
||||
dest: dest,
|
||||
kind: kind,
|
||||
value: value,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -452,22 +497,6 @@ ServerConnection.prototype.groupAction = function(kind, message) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* userAction sends a request to act on a user.
|
||||
*
|
||||
* @param {string} kind - One of "op", "unop", "kick", "present", "unpresent".
|
||||
* @param {string} id - The id of the user to act upon.
|
||||
* @param {string} [message] - An optional user-readable message.
|
||||
*/
|
||||
ServerConnection.prototype.userAction = function(kind, id, message) {
|
||||
this.send({
|
||||
type: 'useraction',
|
||||
kind: kind,
|
||||
id: id,
|
||||
value: message,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when we receive an offer from the server. Don't call this.
|
||||
*
|
||||
|
||||
+21
-10
@@ -1483,7 +1483,7 @@ let lastMessage = {};
|
||||
* @param {string} kind
|
||||
* @param {string} message
|
||||
*/
|
||||
function addToChatbox(peerId, dest, nick, time, kind, message) {
|
||||
function addToChatbox(peerId, dest, nick, time, priviledged, kind, message) {
|
||||
let userpass = getUserPass();
|
||||
let row = document.createElement('div');
|
||||
row.classList.add('message-row');
|
||||
@@ -1608,7 +1608,7 @@ commands.help = {
|
||||
let s = '';
|
||||
for(let i = 0; i < cs.length; i++)
|
||||
s = s + cs[i] + '\n';
|
||||
addToChatbox(null, null, null, Date.now(), null, s);
|
||||
addToChatbox(null, null, null, Date.now(), false, null, s);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1626,7 +1626,7 @@ commands.set = {
|
||||
let s = "";
|
||||
for(let key in settings)
|
||||
s = s + `${key}: ${JSON.stringify(settings[key])}\n`;
|
||||
addToChatbox(null, null, null, Date.now(), null, s);
|
||||
addToChatbox(null, null, null, Date.now(), false, null, s);
|
||||
return;
|
||||
}
|
||||
let p = parseCommand(r);
|
||||
@@ -1756,7 +1756,8 @@ commands.msg = {
|
||||
throw new Error(`Unknown user ${p[0]}`);
|
||||
let username = getUsername();
|
||||
serverConnection.chat(username, '', id, p[1]);
|
||||
addToChatbox(serverConnection.id, id, username, Date.now(), '', p[1]);
|
||||
addToChatbox(serverConnection.id, id, username,
|
||||
Date.now(), false, '', p[1]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2089,12 +2090,22 @@ async function serverConnect() {
|
||||
serverConnection.onpermissions = gotPermissions;
|
||||
serverConnection.onchat = addToChatbox;
|
||||
serverConnection.onclearchat = clearChat;
|
||||
serverConnection.onusermessage = function(kind, message) {
|
||||
if(kind === 'error')
|
||||
displayError(`The server said: ${message}`);
|
||||
else
|
||||
displayWarning(`The server said: ${message}`);
|
||||
}
|
||||
serverConnection.onusermessage = function(id, dest, username, time, priviledged, kind, message) {
|
||||
let from = id ? (username || 'Anonymous') : 'The Server';
|
||||
switch(kind) {
|
||||
case 'error':
|
||||
case 'warning':
|
||||
case 'info':
|
||||
if(priviledged)
|
||||
displayError(`${from} said: ${message}`, kind);
|
||||
else
|
||||
console.error(`Got unpriviledged message of kind ${kind}`);
|
||||
break;
|
||||
default:
|
||||
console.warn(`Got unknown user message ${kind}`);
|
||||
break;
|
||||
}
|
||||
};
|
||||
let url = `ws${location.protocol === 'https:' ? 's' : ''}://${location.host}/ws`;
|
||||
try {
|
||||
await serverConnection.connect(url);
|
||||
|
||||
Reference in New Issue
Block a user