diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2020-07-04 14:40:38 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2020-07-04 14:40:38 +0200 |
commit | 3c95917352dfd8ae43768003207e1bb1e80a7376 (patch) | |
tree | aff622cb75266898baef8f7eba721b44448e9477 | |
parent | 191fc6cf55df805d36555a26177da0eac65b6261 (diff) |
Send msgids in 'send' response and '_push message'
-rw-r--r-- | command.c | 8 | ||||
-rw-r--r-- | db.c | 4 | ||||
-rw-r--r-- | db.h | 2 | ||||
-rw-r--r-- | protocol.md | 6 | ||||
-rw-r--r-- | webclient/client.html | 8 |
5 files changed, 17 insertions, 11 deletions
@@ -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); @@ -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){ @@ -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`. - `<tag> history <roomname:word> <number:i64>` - Requests the last `<number>` messages in the given room, if the client is a member of the room. In the `history` response, `<count>` 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>` (user X) is now online with `<numonline>` sessions. -- `_push message <roomname:word> <user:word> <timestamp:i64> <message:string...>` +- `_push message <roomname:word> <user:word> <timestamp:i64> <msgid:i64> <message:string...>` - 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 <roomname:word>` 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; } |