1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-10 02:35:58 +01:00

Restructure error and chat messages.

This commit is contained in:
Juliusz Chroboczek 2020-08-12 12:17:56 +02:00
parent 7b51296262
commit 88d2a96819
3 changed files with 25 additions and 15 deletions

View file

@ -24,8 +24,8 @@ import (
type chatHistoryEntry struct { type chatHistoryEntry struct {
id string id string
user string user string
kind string
value string value string
me bool
} }
const ( const (
@ -294,7 +294,7 @@ func (g *group) clearChatHistory() {
g.history = nil g.history = nil
} }
func (g *group) addToChatHistory(id, user, value string, me bool) { func (g *group) addToChatHistory(id, user, kind, value string) {
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()
@ -303,7 +303,7 @@ func (g *group) addToChatHistory(id, user, value string, me bool) {
g.history = g.history[:len(g.history)-1] g.history = g.history[:len(g.history)-1]
} }
g.history = append(g.history, g.history = append(g.history,
chatHistoryEntry{id: id, user: user, value: value, me: me}, chatHistoryEntry{id: id, user: user, kind: kind, value: value},
) )
} }

View file

@ -681,7 +681,7 @@ function serverConnect() {
gotUser(m.id, m.username, m.del); gotUser(m.id, m.username, m.del);
break; break;
case 'chat': case 'chat':
addToChatbox(m.id, m.username, m.value, m.me); addToChatbox(m.id, m.username, m.kind, m.value);
break; break;
case 'clearchat': case 'clearchat':
resetChat(); resetChat();
@ -694,8 +694,15 @@ function serverConnect() {
case 'pong': case 'pong':
/* nothing */ /* nothing */
break; break;
case 'error': case 'usermessage':
displayError('The server said: ' + m.value); switch(m.kind) {
case 'error':
displayError('The server said: ' + m.value);
break;
default:
displayWarning('The server said: ' + m.value)
break;
}
break; break;
default: default:
console.warn('Unexpected server message', m.type); console.warn('Unexpected server message', m.type);
@ -962,10 +969,10 @@ function formatLines(lines) {
let lastMessage = {}; let lastMessage = {};
function addToChatbox(peerId, nick, message, me){ function addToChatbox(peerId, nick, kind, message){
let container = document.createElement('div'); let container = document.createElement('div');
container.classList.add('message'); container.classList.add('message');
if(!me) { if(kind !== 'me') {
let p = formatLines(message.split('\n')); let p = formatLines(message.split('\n'));
if (lastMessage.nick !== nick || lastMessage.peerId !== peerId) { if (lastMessage.nick !== nick || lastMessage.peerId !== peerId) {
let user = document.createElement('p'); let user = document.createElement('p');
@ -1124,8 +1131,8 @@ function handleInput() {
type: 'chat', type: 'chat',
id: myid, id: myid,
username: username, username: username,
kind: me ? 'me' : '',
value: message, value: message,
me: me,
}); });
} catch(e) { } catch(e) {
console.error(e); console.error(e);

View file

@ -175,7 +175,7 @@ type clientMessage struct {
Permissions clientPermission `json:"permissions,omitempty"` Permissions clientPermission `json:"permissions,omitempty"`
Group string `json:"group,omitempty"` Group string `json:"group,omitempty"`
Value string `json:"value,omitempty"` Value string `json:"value,omitempty"`
Me bool `json:"me,omitempty"` Kind string `json:"kind,omitempty"`
Offer *webrtc.SessionDescription `json:"offer,omitempty"` Offer *webrtc.SessionDescription `json:"offer,omitempty"`
Answer *webrtc.SessionDescription `json:"answer,omitempty"` Answer *webrtc.SessionDescription `json:"answer,omitempty"`
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"` Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
@ -634,7 +634,8 @@ func startClient(conn *websocket.Conn) (err error) {
// at this point, the writer is not running yet, so format // at this point, the writer is not running yet, so format
// the message ourselves // the message ourselves
conn.WriteJSON(clientMessage{ conn.WriteJSON(clientMessage{
Type: "error", Type: "usermessage",
Kind: "error",
Value: "don't put spaces in your username", Value: "don't put spaces in your username",
}) })
conn.WriteMessage(websocket.CloseMessage, conn.WriteMessage(websocket.CloseMessage,
@ -667,7 +668,8 @@ func startClient(conn *websocket.Conn) (err error) {
m, e := errorToWSCloseMessage(err) m, e := errorToWSCloseMessage(err)
if m != "" { if m != "" {
c.write(clientMessage{ c.write(clientMessage{
Type: "error", Type: "usermessage",
Kind: "error",
Value: m, Value: m,
}) })
} }
@ -727,7 +729,7 @@ func clientLoop(c *webClient, conn *websocket.Conn) error {
Id: m.id, Id: m.id,
Username: m.user, Username: m.user,
Value: m.value, Value: m.value,
Me: m.me, Kind: m.kind,
}) })
if err != nil { if err != nil {
return err return err
@ -996,7 +998,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
log.Printf("ICE: %v", err) log.Printf("ICE: %v", err)
} }
case "chat": case "chat":
c.group.addToChatHistory(m.Id, m.Username, m.Value, m.Me) c.group.addToChatHistory(m.Id, m.Username, m.Kind, m.Value)
clients := c.group.getClients(nil) clients := c.group.getClients(nil)
for _, cc := range clients { for _, cc := range clients {
cc, ok := cc.(*webClient) cc, ok := cc.(*webClient)
@ -1163,7 +1165,8 @@ func (c *webClient) error(err error) error {
switch e := err.(type) { switch e := err.(type) {
case userError: case userError:
return c.write(clientMessage{ return c.write(clientMessage{
Type: "error", Type: "usermessage",
Kind: "error",
Value: string(e), Value: string(e),
}) })
default: default: