[MOD/WIP] Try separating joins from the main ref
Some checks are pending
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run

Still need to do that for transactions, as that will easily bloat up.
This commit is contained in:
LDA 2024-08-14 13:21:41 +02:00
parent 5e15d73c44
commit 57651df1c7

View file

@ -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++)
{