diff --git a/src/User.c b/src/User.c index ff0dcaa..3998078 100644 --- a/src/User.c +++ b/src/User.c @@ -43,6 +43,8 @@ struct User Db *db; DbRef *ref; + DbRef *inviteRef, *joinRef; + char *name; char *deviceId; }; @@ -193,7 +195,9 @@ UserUnlock(User * user) Free(user->name); Free(user->deviceId); - ret = DbUnlock(db, ref); + ret = DbUnlock(db, ref) && + DbUnlock(db, user->joinRef) && + DbUnlock(db, user->inviteRef); user->db = NULL; user->ref = NULL; Free(user); @@ -211,7 +215,10 @@ UserCreate(Db * db, char *name, char *password) /* TODO: Put some sort of password policy(like for example at least * 8 chars, or maybe check it's entropy)? */ - if (!db || (name && UserExists(db, name)) || !password || !strlen(password)) + if (!db || + (name && UserExists(db, name)) || + !password || + !strlen(password)) { /* User exists or cannot be registered, therefore, do NOT * bother */ @@ -246,8 +253,8 @@ UserCreate(Db * db, char *name, char *password) HashMapSet(json, "createdOn", JsonValueInteger(ts)); HashMapSet(json, "deactivated", JsonValueBoolean(false)); - HashMapSet(json, "invites", JsonValueObject(HashMapCreate())); - HashMapSet(json, "joins", JsonValueObject(HashMapCreate())); + user->inviteRef = DbCreate(db, 3, "users", user->name, "invites"); + user->joinRef = DbCreate(db, 3, "users", user->name, "joins"); return user; } @@ -1005,49 +1012,43 @@ void UserAddInvite(User *user, char *roomId) { HashMap *data; - HashMap *invites; if (!user || !roomId) { return; } - data = DbJson(user->ref); - invites = JsonValueAsObject(HashMapGet(data, "invites")); + data = DbJson(user->inviteRef); - JsonFree(HashMapSet(invites, roomId, JsonValueNull())); + JsonFree(HashMapSet(data, roomId, JsonValueNull())); } void UserRemoveInvite(User *user, char *roomId) { HashMap *data; - HashMap *invites; if (!user || !roomId) { return; } - data = DbJson(user->ref); - invites = JsonValueAsObject(HashMapGet(data, "invites")); + data = DbJson(user->inviteRef); - JsonFree(HashMapDelete(invites, roomId)); + JsonFree(HashMapDelete(data, roomId)); } Array * UserListInvites(User *user) { Array *arr; HashMap *data; - HashMap *invites; size_t i; if (!user) { return NULL; } - data = DbJson(user->ref); - invites = JsonValueAsObject(HashMapGet(data, "invites")); - arr = HashMapKeys(invites); + data = DbJson(user->inviteRef); + arr = HashMapKeys(data); for (i = 0; i < ArraySize(arr); i++) { ArraySet(arr, i, StrDuplicate(ArrayGet(arr, i))); @@ -1060,18 +1061,16 @@ void UserAddJoin(User *user, char *roomId) { HashMap *data; - HashMap *joins; if (!user || !roomId) { return; } - data = DbJson(user->ref); - joins = JsonValueAsObject(HashMapGet(data, "joins")); + data = DbJson(user->joinRef); - if (!HashMapGet(joins, roomId)) + if (!HashMapGet(data, roomId)) { - JsonFree(HashMapSet(joins, roomId, JsonValueNull())); + JsonFree(HashMapSet(data, roomId, JsonValueNull())); } UserNotifyUser(user); } @@ -1080,16 +1079,14 @@ void UserRemoveJoin(User *user, char *roomId) { HashMap *data; - HashMap *joins; if (!user || !roomId) { return; } - data = DbJson(user->ref); - joins = JsonValueAsObject(HashMapGet(data, "joins")); + data = DbJson(user->joinRef); - JsonFree(HashMapDelete(joins, roomId)); + JsonFree(HashMapDelete(data, roomId)); UserNotifyUser(user); } Array * @@ -1097,16 +1094,14 @@ UserListJoins(User *user) { Array *arr; HashMap *data; - HashMap *joins; size_t i; if (!user) { return NULL; } - data = DbJson(user->ref); - joins = JsonValueAsObject(HashMapGet(data, "joins")); - arr = HashMapKeys(joins); + data = DbJson(user->joinRef); + arr = HashMapKeys(data); for (i = 0; i < ArraySize(arr); i++) {