mirror of
https://github.com/jech/galene.git
synced 2024-12-22 23:35:46 +01:00
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:
parent
60470cf8cf
commit
6040d1943c
3 changed files with 111 additions and 63 deletions
|
@ -157,6 +157,7 @@ type clientMessage struct {
|
|||
Dest string `json:"dest,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Priviledged bool `json:"priviledged,omitempty"`
|
||||
Permissions *group.ClientPermissions `json:"permissions,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
Value string `json:"value,omitempty"`
|
||||
|
@ -676,9 +677,11 @@ func StartClient(conn *websocket.Conn) (err error) {
|
|||
m, e := errorToWSCloseMessage(err)
|
||||
if m != "" {
|
||||
c.write(clientMessage{
|
||||
Type: "usermessage",
|
||||
Kind: "error",
|
||||
Value: m,
|
||||
Type: "usermessage",
|
||||
Kind: "error",
|
||||
Dest: c.id,
|
||||
Priviledged: true,
|
||||
Value: m,
|
||||
})
|
||||
}
|
||||
c.close(e)
|
||||
|
@ -1065,21 +1068,24 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
|||
if err != nil {
|
||||
log.Printf("ICE: %v", err)
|
||||
}
|
||||
case "chat":
|
||||
case "chat", "usermessage":
|
||||
tm := group.ToJSTime(time.Now())
|
||||
if m.Dest == "" {
|
||||
c.group.AddToChatHistory(
|
||||
m.Id, m.Username, tm, m.Kind, m.Value,
|
||||
)
|
||||
if m.Type == "chat" {
|
||||
if m.Dest == "" {
|
||||
c.group.AddToChatHistory(
|
||||
m.Id, m.Username, tm, m.Kind, m.Value,
|
||||
)
|
||||
}
|
||||
}
|
||||
mm := clientMessage{
|
||||
Type: "chat",
|
||||
Id: m.Id,
|
||||
Dest: m.Dest,
|
||||
Username: m.Username,
|
||||
Time: tm,
|
||||
Kind: m.Kind,
|
||||
Value: m.Value,
|
||||
Type: m.Type,
|
||||
Id: m.Id,
|
||||
Dest: m.Dest,
|
||||
Username: m.Username,
|
||||
Priviledged: c.permissions.Op,
|
||||
Time: tm,
|
||||
Kind: m.Kind,
|
||||
Value: m.Value,
|
||||
}
|
||||
if m.Dest == "" {
|
||||
clients := c.group.GetClients(nil)
|
||||
|
@ -1154,7 +1160,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
|||
if !c.permissions.Op {
|
||||
return c.error(group.UserError("not authorised"))
|
||||
}
|
||||
err := setPermissions(c.group, m.Id, m.Kind)
|
||||
err := setPermissions(c.group, m.Dest, m.Kind)
|
||||
if err != nil {
|
||||
return c.error(err)
|
||||
}
|
||||
|
@ -1166,7 +1172,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
|||
if message == "" {
|
||||
message = "you have been kicked out"
|
||||
}
|
||||
err := kickClient(c.group, m.Id, message)
|
||||
err := kickClient(c.group, m.Dest, message)
|
||||
if err != nil {
|
||||
return c.error(err)
|
||||
}
|
||||
|
@ -1277,9 +1283,11 @@ func (c *webClient) error(err error) error {
|
|||
switch e := err.(type) {
|
||||
case group.UserError:
|
||||
return c.write(clientMessage{
|
||||
Type: "usermessage",
|
||||
Kind: "error",
|
||||
Value: string(e),
|
||||
Type: "usermessage",
|
||||
Kind: "error",
|
||||
Dest: c.id,
|
||||
Priviledged: true,
|
||||
Value: string(e),
|
||||
})
|
||||
default:
|
||||
return err
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue