From 3c95917352dfd8ae43768003207e1bb1e80a7376 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 4 Jul 2020 14:40:38 +0200 Subject: Send msgids in 'send' response and '_push message' --- command.c | 8 ++++---- db.c | 4 +++- db.h | 2 +- protocol.md | 6 ++++-- webclient/client.html | 8 +++++--- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/command.c b/command.c index e5b8ae3..a7d5b02 100644 --- a/command.c +++ b/command.c @@ -238,13 +238,13 @@ static struct cmd_retval cmd_send(struct conn_data *data,const char *tag,const c } i64 timestamp=make_timestamp(); - db_create_message(roomid,data->userid,make_timestamp(),message); - bool closed=net_send_ok(data->fd,tag); + i64 msgid=db_create_message(roomid,data->userid,make_timestamp(),message); + bool closed=net_send_number(data->fd,tag,msgid); char *pushbuf=NULL; char *username=db_get_username(data->userid); - i64 pushbuflen=asprintf(&pushbuf,"_push message %s %s %" PRIi64 " %s\n", - roomname,username,timestamp,message); + i64 pushbuflen=asprintf(&pushbuf,"_push message %s %s %" PRIi64 " %" PRIi64 " %s\n", + roomname,username,timestamp,msgid,message); event_emit_message(timestamp,message,username,roomname); firebase_send_message(roomname,roomid,username,message); diff --git a/db.c b/db.c index 0dd16e8..811d049 100644 --- a/db.c +++ b/db.c @@ -379,7 +379,7 @@ bool db_delete_token(i64 userid,const char *token){ } -void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){ +i64 db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){ sqlite3_stmt *stmt; SQLITE(prepare_v2,database, "insert into Messages (room, user, time, message) " @@ -391,6 +391,8 @@ void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){ SQLITE(bind_blob,stmt,4,message,strlen(message),SQLITE_STATIC); if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step"); SQLITE(finalize,stmt); + + return sqlite3_last_insert_rowid(database); } struct db_message_list db_get_messages(i64 roomid,i64 count){ diff --git a/db.h b/db.h index c27b243..8d618b4 100644 --- a/db.h +++ b/db.h @@ -59,7 +59,7 @@ struct db_strings_list db_user_tokens(i64 userid); bool db_add_token(i64 userid,const char *token); bool db_delete_token(i64 userid,const char *token); -void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message); +i64 db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message); // returns msgid struct db_message_list db_get_messages(i64 roomid,i64 count); // gets latest `count` messages // if beforeid<0, same as db_get_messages struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid); diff --git a/protocol.md b/protocol.md index 2d7ec63..d4c544b 100644 --- a/protocol.md +++ b/protocol.md @@ -128,7 +128,9 @@ ranges until the end of the line). `send` message was sent from). Also marks the current session as active. - - Returns `ok` or `error`. + + The returned `number` response contains the message id of the message sent. + - Returns `number` or `error`. - ` history ` - Requests the last `` messages in the given room, if the client is a member of the room. In the `history` response, `` will be at most @@ -177,7 +179,7 @@ are listed below. (excluding user X) of all rooms user X is a member of receive a `_push online` push message stating that `` (user X) is now online with `` sessions. -- `_push message ` +- `_push message ` - Sent to all sessions of all members of a room in which a message is posted, except the session that sent the message. - `_push invite ` diff --git a/webclient/client.html b/webclient/client.html index fce719d..ab10cbc 100644 --- a/webclient/client.html +++ b/webclient/client.html @@ -112,7 +112,8 @@ function reconnect(){ if(id=="_push"){ if(type=="message"){ var r=spl.word[2],u=spl.word[3],t=new Date(+spl.word[4]/1000); - addRoomEntry(r,"message",[u,t,spl.rest[5]]); + // Ignore msgid at word[5] + addRoomEntry(r,"message",[u,t,spl.rest[6]]); } else if(type=="invite"){ var r=spl.word[2]; roomlist.push(r); @@ -135,6 +136,7 @@ function reconnect(){ var obj; if(type=="ok")fn(true); else if(type=="error")fn(null,spl.rest[2]); + else if(type=="number")fn(+spl.rest[2]); else if(type=="name")fn(spl.rest[2]); else if(type=="list")fn(spl.word.slice(3)); else if(type=="history"){ @@ -421,8 +423,8 @@ function sendMessage(roomid,text){ return; } var sentAs=username,msg=text.replace(/\n/g,""); - net_send("send "+roomid+" "+msg,function(ok,err){ - if(ok){ + net_send("send "+roomid+" "+msg,function(msgid,err){ + if(msgid!=null){ addRoomEntry(roomid,"message",[sentAs,new Date().getTime(),msg]); return; } -- cgit v1.2.3-54-g00ecf