From 0cfd3e87bba5faee5994dc915ab6485f0cf6832c Mon Sep 17 00:00:00 2001
From: tomsmeding <tom.smeding@gmail.com>
Date: Wed, 24 May 2017 09:43:03 +0200
Subject: server: Only send push notifs to offline users

---
 firebase.c  | 2 ++
 user_data.c | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/firebase.c b/firebase.c
index d11545f..1f4fb26 100644
--- a/firebase.c
+++ b/firebase.c
@@ -4,6 +4,7 @@
 #include <assert.h>
 #include "db.h"
 #include "firebase.h"
+#include "user_data.h"
 
 
 #define JS_PLUGIN_PATH "firebase-io/firebase-io.js"
@@ -126,6 +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;
 		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 5e7c028..e7d2e2a 100644
--- a/user_data.c
+++ b/user_data.c
@@ -90,10 +90,10 @@ void userdata_unregister(i64 userid,int fd){
 const int* userdata_online(i64 userid,i64 *nfds){
 	struct hash_item *item=find_userdata(userid);
 	if(!item){
-		*nfds=0;
+		if(nfds)*nfds=0;
 		return NULL;
 	}
 	assert(item->data.fds_len!=0);
-	*nfds=item->data.fds_len;
+	if(nfds)*nfds=item->data.fds_len;
 	return item->data.fds;
 }
-- 
cgit v1.2.3-70-g09d2