aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-05-24 11:24:54 +0200
committertomsmeding <tom.smeding@gmail.com>2017-05-24 11:24:54 +0200
commit66b3a7833a86ce868a1cad19cd5bbe2a62d10980 (patch)
tree4ae27e04eb6f27bfe2f1e26b47e5864da90e578a
parente4914ccdfa4d9eb6f71f7b5cc5b4881c4e911778 (diff)
server: Only send notifs when user is actually active
-rw-r--r--command.c3
-rw-r--r--firebase.c2
-rw-r--r--user_data.c69
-rw-r--r--user_data.h4
4 files changed, 60 insertions, 18 deletions
diff --git a/command.c b/command.c
index fcbb64f..01aff9e 100644
--- a/command.c
+++ b/command.c
@@ -120,6 +120,7 @@ static bool cmd_create_room(struct conn_data *data,const char *tag,const char **
net_send_error(data->fd,tag,"Not logged in");
return false;
}
+ userdata_mark_active(data->userid,data->fd);
struct db_name_id room=db_create_room();
db_add_member(room.id,data->userid);
bool closed=net_send_name(data->fd,tag,room.name);
@@ -132,6 +133,7 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args)
net_send_error(data->fd,tag,"Not logged in");
return false;
}
+ userdata_mark_active(data->userid,data->fd);
const char *roomname=args[0];
i64 roomid=db_find_room(roomname);
if(roomid==-1){
@@ -192,6 +194,7 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){
net_send_error(data->fd,tag,"Not logged in");
return false;
}
+ userdata_mark_active(data->userid,data->fd);
const char *roomname=args[0];
const char *message=args[1];
i64 roomid=db_find_room(roomname);
diff --git a/firebase.c b/firebase.c
index 1f4fb26..6244882 100644
--- a/firebase.c
+++ b/firebase.c
@@ -127,7 +127,7 @@ void firebase_send_message(const char *room,i64 roomid,const char *user,const ch
struct db_user_list members=db_list_members(roomid);
for(i64 i=0;i<members.count;i++){
- if(userdata_online(members.list[i].id,NULL))continue;
+ if(userdata_is_active(members.list[i].id))continue;
struct db_strings_list tokens=db_user_tokens(members.list[i].id);
for(i64 j=0;j<tokens.count;j++){
token_send(tokens.list[j],room,user,msg);
diff --git a/user_data.c b/user_data.c
index e7d2e2a..b6a8012 100644
--- a/user_data.c
+++ b/user_data.c
@@ -1,13 +1,15 @@
#include <string.h>
#include <assert.h>
#include "user_data.h"
+#include "util.h"
struct user_data{
i64 userid;
int *fds;
- i64 fds_cap,fds_len;
+ i64 *last_active;
+ i64 cap,len;
};
struct hash_item{
@@ -48,23 +50,28 @@ static void remove_userdata(i64 userid){
void userdata_register(i64 userid,int fd){
struct hash_item *item=find_userdata(userid);
if(item){
- for(i64 i=0;i<item->data.fds_len;i++){
+ for(i64 i=0;i<item->data.len;i++){
if(item->data.fds[i]==fd){
die("userdata_register(%" PRIi64 ", %d) while pair already existed",userid,fd);
}
}
- if(item->data.fds_len==item->data.fds_cap){
- item->data.fds_cap*=2;
- item->data.fds=realloc(item->data.fds,item->data.fds_cap,int);
+ if(item->data.len==item->data.cap){
+ item->data.cap*=2;
+ item->data.fds=realloc(item->data.fds,item->data.cap,int);
+ item->data.last_active=realloc(item->data.last_active,item->data.cap,i64);
}
- item->data.fds[item->data.fds_len++]=fd;
+ item->data.fds[item->data.len]=fd;
+ item->data.last_active[item->data.len]=make_timestamp();
+ item->data.len++;
} else {
item=malloc(1,struct hash_item);
item->data.userid=userid;
- item->data.fds_cap=2;
- item->data.fds_len=1;
- item->data.fds=malloc(item->data.fds_cap,int);
+ item->data.cap=2;
+ item->data.len=1;
+ item->data.fds=malloc(item->data.cap,int);
item->data.fds[0]=fd;
+ item->data.last_active=malloc(item->data.cap,i64);
+ item->data.last_active[0]=make_timestamp();
item->next=user_hash[userid%USER_HASH_SIZE];
user_hash[userid%USER_HASH_SIZE]=item;
}
@@ -72,28 +79,56 @@ void userdata_register(i64 userid,int fd){
void userdata_unregister(i64 userid,int fd){
struct hash_item *item=find_userdata(userid);
- assert(item);
+ if(!item){
+ die("userdata_unregister(%" PRIi64 ", %d) while nonexistent",userid,fd);
+ }
i64 i;
- for(i=0;i<item->data.fds_len;i++){
+ for(i=0;i<item->data.len;i++){
if(item->data.fds[i]==fd)break;
}
- if(i==item->data.fds_len){
+ if(i==item->data.len){
die("userdata_unregister(%" PRIi64 ", %d) while nonexistent",userid,fd);
}
- memmove(item->data.fds+i,item->data.fds+i+1,item->data.fds_len-i-1);
- item->data.fds_len--;
- if(item->data.fds_len==0){
+ memmove(item->data.fds+i,item->data.fds+i+1,item->data.len-i-1);
+ memmove(item->data.last_active+i,item->data.last_active+i+1,item->data.len-i-1);
+ item->data.len--;
+ if(item->data.len==0){
remove_userdata(userid);
}
}
+void userdata_mark_active(i64 userid,int fd){
+ struct hash_item *item=find_userdata(userid);
+ if(!item){
+ die("userdata_mark_active(%" PRIi64 ") while nonexistent",userid);
+ }
+ i64 i;
+ for(i=0;i<item->data.len;i++){
+ if(item->data.fds[i]==fd)break;
+ }
+ if(i==item->data.len){
+ die("userdata_mark_active(%" PRIi64 ", %d) while nonexistent",userid,fd);
+ }
+ item->data.last_active[i]=make_timestamp();
+}
+
+bool userdata_is_active(i64 userid){
+ struct hash_item *item=find_userdata(userid);
+ if(!item)return false;
+ i64 last=0;
+ for(i64 i=0;i<item->data.len;i++){
+ if(item->data.last_active[i]>last)last=item->data.last_active[i];
+ }
+ return make_timestamp()-last<2*60*1000*1000;
+}
+
const int* userdata_online(i64 userid,i64 *nfds){
struct hash_item *item=find_userdata(userid);
if(!item){
if(nfds)*nfds=0;
return NULL;
}
- assert(item->data.fds_len!=0);
- if(nfds)*nfds=item->data.fds_len;
+ assert(item->data.len!=0);
+ if(nfds)*nfds=item->data.len;
return item->data.fds;
}
diff --git a/user_data.h b/user_data.h
index 094ad75..5769ae9 100644
--- a/user_data.h
+++ b/user_data.h
@@ -6,5 +6,9 @@
void userdata_register(i64 userid,int fd);
void userdata_unregister(i64 userid,int fd);
+void userdata_mark_active(i64 userid,int fd);
+
+bool userdata_is_active(i64 userid);
+
// Returns array of nfds fd's; valid until next call to (un)register
const int* userdata_online(i64 userid,i64 *nfds);