Skip to content

Commit

Permalink
Merge pull request #19 from drawpitech/add-users
Browse files Browse the repository at this point in the history
feat: add users (list users)
  • Loading branch information
nailec1911 authored Apr 17, 2024
2 parents 12d9f47 + a13aae0 commit 4ea7bb1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/server/cmds/cmd_logout.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void cmd_logout(server_t *server, client_t *client)

if (client->user == NULL)
return;
client->user->status = client->user->status - 1;
printf("-->%d\n", client->user->status);
client->user->status -= 1;
if (client->user->status < 0)
client->user->status = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/server/cmds/cmd_subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
static team_t *get_team(server_t *server, client_t *client, char *uuid_str)
{
uuid_t uuid = {0};
team_t *team = NULL;

uuid_parse(uuid_str, uuid);
for (size_t i = 0; i < server->teams.size; i++) {
if (uuid_compare(server->teams.arr[i].uuid, uuid) == 0)
return &server->teams.arr[i];
}
team = get_team_by_uuid(server, uuid);
if (team)
return team;
dprintf(client->fd, "511");
fsync(client->fd);
write(client->fd, uuid_str, UUID_STR_LEN);
Expand Down
8 changes: 4 additions & 4 deletions src/server/cmds/cmd_subscribed.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
static team_t *get_team(server_t *server, client_t *client, char *uuid_str)
{
uuid_t uuid = {0};
team_t *team = NULL;

uuid_parse(uuid_str, uuid);
for (size_t i = 0; i < server->teams.size; i++) {
if (uuid_compare(server->teams.arr[i].uuid, uuid) == 0)
return &server->teams.arr[i];
}
team = get_team_by_uuid(server, uuid);
if (team)
return team;
dprintf(client->fd, "511");
fsync(client->fd);
write(client->fd, uuid_str, UUID_STR_LEN);
Expand Down
8 changes: 4 additions & 4 deletions src/server/cmds/cmd_unsubscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
static team_t *get_team(server_t *server, client_t *client, char *uuid_str)
{
uuid_t uuid = {0};
team_t *team = NULL;

uuid_parse(uuid_str, uuid);
for (size_t i = 0; i < server->teams.size; i++) {
if (uuid_compare(server->teams.arr[i].uuid, uuid) == 0)
return &server->teams.arr[i];
}
team = get_team_by_uuid(server, uuid);
if (team)
return team;
dprintf(client->fd, "511");
fsync(client->fd);
write(client->fd, uuid_str, UUID_STR_LEN);
Expand Down
41 changes: 41 additions & 0 deletions src/server/cmds/cmd_users.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
** EPITECH PROJECT, 2024
** my_teams
** File description:
** cmd_users
*/

/*
** EPITECH PROJECT, 2024
** my_teams
** File description:
** cmd_list
*/

#include <myteams/logging_client.h>
#include <myteams/logging_server.h>
#include <stdio.h>
#include <unistd.h>
#include <uuid/uuid.h>

#include "cmds/cmds_utils.h"
#include "command.h"
#include "ressources_infos.h"
#include "server.h"

void cmd_users(server_t *server, client_t *client)
{
user_info_t info = {0};

if (!server || !client || !is_logged_in(client))
return;
dprintf(client->fd, "225");
fsync(client->fd);
write(client->fd, &server->users.size, sizeof(server->users.size));
for (size_t i = 0; i < server->users.size; i++) {
write(
client->fd,
user_to_info(&server->users.arr[i], &info, NULL),
sizeof(info));
}
}
3 changes: 2 additions & 1 deletion src/server/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void cmd_login(server_t *server, client_t *client);
void cmd_logout(server_t *server, client_t *client);
void cmd_use(server_t *server, client_t *client);
void cmd_user(server_t *server, client_t *client);
void cmd_users(server_t *server, client_t *client);
void cmd_list(server_t *server, client_t *client);

void cmd_subscribe(server_t *server, client_t *client);
Expand Down Expand Up @@ -47,7 +48,7 @@ static const struct cmd_s {
{"UNSUBSCRIBE", cmd_unsubscribe, "unsubscribe from a team"},
{"USE", cmd_use, "sets the command context to a team/channel/thread"},
{"USER", cmd_user, "get details about the requested user"},
{"USERS", NULL, "get the list of all users that exist on the domain"},
{"USERS", cmd_users, "get the list of all users that exist on the domain"},
};

void exec_command(server_t *server, client_t *client);
9 changes: 5 additions & 4 deletions src/server/getters.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <string.h>
#include <uuid/uuid.h>

#include "server.h"

Expand All @@ -15,7 +16,7 @@ user_t *get_user_by_uuid(server_t *server, uuid_t uuid)

for (size_t i = 0; i < server->clients.size; i++) {
user = server->clients.arr[i].user;
if (user != NULL && memcmp(user->uuid, uuid, sizeof(uuid_t)) == 0)
if (user != NULL && uuid_compare(user->uuid, uuid) == 0)
return user;
}
return NULL;
Expand All @@ -27,7 +28,7 @@ team_t *get_team_by_uuid(server_t *server, uuid_t uuid)

for (size_t i = 0; i < server->teams.size; i++) {
team = &server->teams.arr[i];
if (memcmp(team->uuid, uuid, sizeof(uuid_t)) == 0)
if (uuid_compare(team->uuid, uuid) == 0)
return team;
}
return NULL;
Expand All @@ -39,7 +40,7 @@ channel_t *get_channel_by_uuid(team_t *team, uuid_t uuid)

for (size_t i = 0; i < team->channels.size; i++) {
channel = &team->channels.arr[i];
if (memcmp(channel->uuid, uuid, sizeof(uuid_t)) == 0)
if (uuid_compare(channel->uuid, uuid) == 0)
return channel;
}
return NULL;
Expand All @@ -51,7 +52,7 @@ thread_t *get_thread_by_uuid(channel_t *channel, uuid_t uuid)

for (size_t i = 0; i < channel->threads.size; i++) {
thread = &channel->threads.arr[i];
if (memcmp(thread->thread_uuid, uuid, sizeof(uuid_t)) == 0)
if (uuid_compare(thread->thread_uuid, uuid) == 0)
return thread;
}
return NULL;
Expand Down

0 comments on commit 4ea7bb1

Please sign in to comment.