aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c36
1 files changed, 28 insertions, 8 deletions
diff --git a/db.c b/db.c
index 0de620d..416b9d3 100644
--- a/db.c
+++ b/db.c
@@ -10,7 +10,8 @@
#define SQLITE(func,...) do{if(sqlite3_##func(__VA_ARGS__)!=SQLITE_OK){die_sqlite("sqlite3_" #func);}}while(0)
-#define DATABASE_VERSION 1
+#define DATABASE_VERSION 2
+
#define PASSHASH_OPSLIMIT 3
#define PASSHASH_MEMLIMIT crypto_pwhash_MEMLIMIT_INTERACTIVE
@@ -381,16 +382,18 @@ bool db_delete_token(i64 userid,const char *token){
}
-i64 db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){
+i64 db_create_message(i64 roomid,i64 userid,i64 timestamp,i64 replyid,const char *message){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,
- "insert into Messages (room, user, time, message) "
- "values (?, ?, ?, ?)"
+ "insert into Messages (room, user, time, reply, message) "
+ "values (?, ?, ?, ?, ?)"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,userid);
SQLITE(bind_int64,stmt,3,timestamp);
- SQLITE(bind_blob,stmt,4,message,strlen(message),SQLITE_STATIC);
+ if(replyid>=0)SQLITE(bind_int64,stmt,4,replyid);
+ else SQLITE(bind_null,stmt,4);
+ SQLITE(bind_blob,stmt,5,message,strlen(message),SQLITE_STATIC);
if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
@@ -407,7 +410,7 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
sqlite3_stmt *stmt;
if(beforeid<0){
SQLITE(prepare_v2,database,
- "select id, user, time, message "
+ "select id, user, time, reply, message "
"from Messages "
"where room = ? "
"order by time desc "
@@ -417,7 +420,7 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
SQLITE(bind_int64,stmt,2,count);
} else {
SQLITE(prepare_v2,database,
- "select id, user, time, message "
+ "select id, user, time, reply, message "
"from Messages "
"where room = ? and time < (select time from Messages where id = ?) "
"order by time desc "
@@ -442,7 +445,12 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
ml.list[ml.count].roomid=roomid;
ml.list[ml.count].userid=sqlite3_column_int64(stmt,1);
ml.list[ml.count].timestamp=sqlite3_column_int64(stmt,2);
- ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,3));
+ if(sqlite3_column_type(stmt,3)==SQLITE_INTEGER){
+ ml.list[ml.count].replyid=sqlite3_column_int64(stmt,3);
+ } else {
+ ml.list[ml.count].replyid=-1; // NULL, not a reply
+ }
+ ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,4));
ml.count++;
}
@@ -452,6 +460,18 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
return ml;
}
+bool db_message_exists(i64 roomid, i64 msgid) {
+ sqlite3_stmt *stmt;
+ SQLITE(prepare_v2, database,
+ "select id from Messages where id = ? and room = ?",
+ -1, &stmt, NULL);
+ SQLITE(bind_int64, stmt, 1, msgid);
+ SQLITE(bind_int64, stmt, 2, roomid);
+ bool success = sqlite3_step(stmt) == SQLITE_ROW;
+ SQLITE(finalize, stmt);
+ return success;
+}
+
void db_nullify_name_id(struct db_name_id ni){
if(ni.name)free(ni.name);