Skip to content

Commit

Permalink
Introduce user settings and subtexts
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Dec 19, 2024
1 parent 4d4b833 commit 6e1e699
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 4 deletions.
6 changes: 6 additions & 0 deletions engine/src/models/user/userentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ impl UserEntry {
self.oauth_sub == "$$SYSTEM$$"
}

pub async fn find_all(database: &Database) -> Result<Vec<UserEntry>, sqlx::Error> {
query_as!(UserEntry, "SELECT user_id, oauth_sub, oauth_data::text::json as \"oauth_data!: Json<Userinfo>\", nickname, created_at, updated_at FROM users")
.fetch_all(&database.pool)
.await
}

pub async fn upsert(
oauth_userinfo: &Userinfo,
nickname: Option<String>,
Expand Down
23 changes: 22 additions & 1 deletion engine/src/routes/users/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest::StatusCode;

use super::{error::HttpError, ApiTags};
use crate::{
auth::middleware::AuthUser,
auth::{middleware::AuthUser, permissions::Action},
models::user::{user::User, userentry::UserEntry},
state::AppState,
};
Expand All @@ -17,6 +17,27 @@ pub struct UserApi;

#[OpenApi]
impl UserApi {
/// /user
///
/// List all users
#[oai(path = "/user", method = "get", tag = "ApiTags::User")]
pub async fn users(
&self,
user: AuthUser,
state: Data<&Arc<AppState>>,
) -> Result<Json<Vec<User>>> {
user.check_policy("user", "", Action::Read).await?;

Ok(Json(
UserEntry::find_all(&state.database)
.await
.map_err(HttpError::from)?
.into_iter()
.map(|user| user.into())
.collect::<Vec<User>>(),
))
}

/// /user/:user_id
///
/// Get a User by `user_id`
Expand Down
38 changes: 38 additions & 0 deletions web/src/api/schema.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,44 @@ export type paths = {
patch?: never;
trace?: never;
};
"/user": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* /user
* @description List all users
*/
get: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json; charset=utf-8": components["schemas"]["User"][];
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/user/{user_id}": {
parameters: {
query?: never;
Expand Down
12 changes: 12 additions & 0 deletions web/src/api/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ export const getUserById = (user_id: number) =>
});

export const useUserById = (user_id: number) => useQuery(getUserById(user_id));

export const getUsers = () =>
queryOptions({
queryKey: ['users'],
queryFn: async () => {
const response = await apiRequest('/user', 'get', {});

return response.data;
},
});

export const useUsers = () => useQuery(getUsers());
4 changes: 4 additions & 0 deletions web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ a {
all: unset;
}

.link {
@apply text-blue-500 hover:cursor-pointer hover:underline;
}

.pre {
@apply bg-neutral-100 rounded-md p-2;
}
Expand Down
20 changes: 18 additions & 2 deletions web/src/routes/settings/_layout/build.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createFileRoute } from '@tanstack/react-router';
import { createFileRoute, Link } from '@tanstack/react-router';
import { FiHeart } from 'react-icons/fi';

import { BuildDetails } from '@/components/settings/BuildDetails';

Expand All @@ -12,5 +13,20 @@ export const Route = createFileRoute('/settings/_layout/build')({
});

function RouteComponent() {
return <BuildDetails />;
return (
<>
<BuildDetails />
<p className="text-sm text-gray-500 px-4 flex items-center gap-1">
We thank you for using open-source software.{' '}
<Link
href="https://v3x.company"
className="link"
target="_blank"
>
V3X Labs
</Link>{' '}
<FiHeart className="text-xs" />
</p>
</>
);
}
28 changes: 27 additions & 1 deletion web/src/routes/settings/_layout/users.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createFileRoute } from '@tanstack/react-router';

import { useUsers } from '@/api/user';
import { UserProfile } from '@/components/UserProfile';

export const Route = createFileRoute('/settings/_layout/users')({
component: RouteComponent,
context() {
Expand All @@ -10,5 +13,28 @@ export const Route = createFileRoute('/settings/_layout/users')({
});

function RouteComponent() {
return <div>Hello "/settings/users"!</div>;
const { data } = useUsers();

return (
<>
<ul className="space-y-2">
{data
?.filter((user) => user.user_id !== 1)
.map((user) => (
<li key={user.user_id}>
<div className="card no-padding p-2">
<UserProfile
user_id={user.user_id}
variant="full"
/>
</div>
</li>
))}
</ul>
<p className="text-sm text-gray-500 px-4">
With OAuth configured users will get automatically added here as
soon as they log in the for the first time.
</p>
</>
);
}

0 comments on commit 6e1e699

Please sign in to comment.