1
Fork 0

Add id to chat messages.

This commit is contained in:
Juliusz Chroboczek 2024-08-11 19:00:17 +02:00
parent 4fb0b3334a
commit eb72069c9b
5 changed files with 34 additions and 22 deletions

View File

@ -75,6 +75,7 @@ func (err ProtocolError) Error() string {
type ChatHistoryEntry struct {
Id string
Source string
User *string
Time time.Time
Kind string
@ -804,7 +805,7 @@ func (g *Group) ClearChatHistory() {
g.history = nil
}
func (g *Group) AddToChatHistory(id string, user *string, time time.Time, kind string, value interface{}) {
func (g *Group) AddToChatHistory(id, source string, user *string, time time.Time, kind string, value interface{}) {
g.mu.Lock()
defer g.mu.Unlock()
@ -813,7 +814,7 @@ func (g *Group) AddToChatHistory(id string, user *string, time time.Time, kind s
g.history = g.history[:len(g.history)-1]
}
g.history = append(g.history,
ChatHistoryEntry{Id: id, User: user, Time: time, Kind: kind, Value: value},
ChatHistoryEntry{Id: id, Source: source, User: user, Time: time, Kind: kind, Value: value},
)
}

View File

@ -54,7 +54,7 @@ func TestChatHistory(t *testing.T) {
}
user := "user"
for i := 0; i < 2*maxChatHistory; i++ {
g.AddToChatHistory("id", &user, time.Now(), "",
g.AddToChatHistory("id", "source", &user, time.Now(), "",
fmt.Sprintf("%v", i),
)
}

View File

@ -1197,7 +1197,8 @@ func handleAction(c *webClient, a any) error {
for _, m := range h {
err := c.write(clientMessage{
Type: "chathistory",
Source: m.Id,
Id: m.Id,
Source: m.Source,
Username: m.User,
Time: m.Time.Format(time.RFC3339),
Value: m.Value,
@ -1575,17 +1576,25 @@ func handleClientMessage(c *webClient, m clientMessage) error {
return c.error(group.UserError("not authorised"))
}
now := time.Now()
id := m.Id
if m.Type == "chat" && m.Dest == "" && id == "" {
buf := make([]byte, 8)
crand.Read(buf)
id = base64.RawURLEncoding.EncodeToString(buf)
}
now := time.Now()
if m.Type == "chat" {
if m.Dest == "" {
g.AddToChatHistory(
m.Source, m.Username, now, m.Kind, m.Value,
id, m.Source, m.Username,
now, m.Kind, m.Value,
)
}
}
mm := clientMessage{
Type: m.Type,
Id: id,
Source: m.Source,
Dest: m.Dest,
Username: m.Username,

View File

@ -2958,7 +2958,7 @@ let lastMessage = {};
* @param {string} kind
* @param {string|HTMLElement} message
*/
function addToChatbox(peerId, dest, nick, time, privileged, history, kind, message) {
function addToChatbox(id, peerId, dest, nick, time, privileged, history, kind, message) {
let row = document.createElement('div');
row.classList.add('message-row');
let container = document.createElement('div');
@ -2973,6 +2973,8 @@ function addToChatbox(peerId, dest, nick, time, privileged, history, kind, messa
if(dest)
container.classList.add('message-private');
if(id)
container.dataset.id = id;
if(peerId) {
container.dataset.peerId = peerId;
container.dataset.username = nick;
@ -3093,7 +3095,7 @@ function chatMessageMenu(elt) {
* @param {string|HTMLElement} message
*/
function localMessage(message) {
return addToChatbox(null, null, null, new Date(), false, false, '', message);
return addToChatbox(null, null, null, null, new Date(), false, false, '', message);
}
function clearChat() {
@ -3473,7 +3475,7 @@ commands.msg = {
if(!id)
throw new Error(`Unknown user ${p[0]}`);
serverConnection.chat('', id, p[1]);
addToChatbox(serverConnection.id, id, serverConnection.username,
addToChatbox(serverConnection.id, null, id, serverConnection.username,
new Date(), false, false, '', p[1]);
}
};

View File

@ -212,7 +212,7 @@ function ServerConnection() {
/**
* onchat is called whenever a new chat message is received.
*
* @type {(this: ServerConnection, id: string, dest: string, username: string, time: Date, privileged: boolean, history: boolean, kind: string, message: string) => void}
* @type {(this: ServerConnection, id: string, source: string, dest: string, username: string, time: Date, privileged: boolean, history: boolean, kind: string, message: string) => void}
*/
this.onchat = null;
/**
@ -497,7 +497,7 @@ ServerConnection.prototype.connect = function(url) {
case 'chathistory':
if(sc.onchat)
sc.onchat.call(
sc, m.source, m.dest, m.username, parseTime(m.time),
sc, m.id, m.source, m.dest, m.username, parseTime(m.time),
m.privileged, m.type === 'chathistory', m.kind,
'' + m.value,
);