Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print files owned by current user to console #347

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/backend/pkg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ export type { UserSummary } from "./UserSummary.ts";
export type { UsernameStatus } from "./UsernameStatus.ts";
export type { UserProfile } from "./UserProfile.ts";

export type QubitServer = { new_ref: Mutation<[content: JsonValue, ], RpcResult<string>>, get_doc: Query<[ref_id: string, ], RpcResult<RefDoc>>, head_snapshot: Query<[ref_id: string, ], RpcResult<JsonValue>>, save_snapshot: Mutation<[data: RefContent, ], RpcResult<null>>, get_permissions: Query<[ref_id: string, ], RpcResult<Permissions>>, set_permissions: Mutation<[ref_id: string, new: NewPermissions, ], RpcResult<null>>, sign_up_or_sign_in: Mutation<[], RpcResult<null>>, user_by_username: Query<[username: string, ], RpcResult<UserSummary | null>>, username_status: Query<[username: string, ], RpcResult<UsernameStatus>>, get_active_user_profile: Query<[], RpcResult<UserProfile>>, set_active_user_profile: Mutation<[user: UserProfile, ], RpcResult<null>> };
export type QubitServer = { new_ref: Mutation<[content: JsonValue, ], RpcResult<string>>, get_doc: Query<[ref_id: string, ], RpcResult<RefDoc>>, head_snapshot: Query<[ref_id: string, ], RpcResult<JsonValue>>, save_snapshot: Mutation<[data: RefContent, ], RpcResult<null>>, get_permissions: Query<[ref_id: string, ], RpcResult<Permissions>>, set_permissions: Mutation<[ref_id: string, new: NewPermissions, ], RpcResult<null>>, sign_up_or_sign_in: Mutation<[], RpcResult<null>>, user_by_username: Query<[username: string, ], RpcResult<UserSummary | null>>, username_status: Query<[username: string, ], RpcResult<UsernameStatus>>, get_active_user_profile: Query<[], RpcResult<UserProfile>>, set_active_user_profile: Mutation<[user: UserProfile, ], RpcResult<null>>, get_user_refs_and_titles: Query<[username: string, ], RpcResult<Array<[string, string]>>> };
6 changes: 6 additions & 0 deletions packages/backend/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn router() -> Router<AppState> {
.handler(username_status)
.handler(get_active_user_profile)
.handler(set_active_user_profile)
.handler(get_user_refs_and_titles)
}

#[handler(mutation)]
Expand Down Expand Up @@ -119,6 +120,11 @@ async fn user_by_username(ctx: AppCtx, username: &str) -> RpcResult<Option<user:
user::user_by_username(ctx.state, username).await.into()
}

#[handler(query)]
async fn get_user_refs_and_titles(ctx: AppCtx, username: &str) -> RpcResult<Vec<(Uuid, String)>> {
user::get_user_refs_and_titles(ctx, username).await.into()
}

#[handler(query)]
async fn username_status(ctx: AppCtx, username: &str) -> RpcResult<user::UsernameStatus> {
user::username_status(ctx.state, username).await.into()
Expand Down
26 changes: 26 additions & 0 deletions packages/backend/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use regex::Regex;
use serde::{Deserialize, Serialize};
use ts_rs::TS;
use uuid::Uuid;

use super::app::{AppCtx, AppError, AppState};

Expand Down Expand Up @@ -160,6 +161,31 @@ pub fn is_username_valid(username: &str) -> bool {
&& ends_alpha.is_match(username)
}

/// Get references and titles for a given username.
pub async fn get_user_refs_and_titles(
ctx: AppCtx,
username: &str,
) -> Result<Vec<(Uuid, String)>, AppError> {
let query = sqlx::query!(
r#"
SELECT snapshots.for_ref, snapshots.content->>'name' as title
FROM snapshots
JOIN refs ON snapshots.for_ref = refs.id
JOIN permissions ON refs.id = permissions.object
JOIN users ON permissions.subject = users.id
WHERE users.username = $1
AND permissions.level = 'own'
"#,
username
);

let results = query.fetch_all(&ctx.state.db).await?;
Ok(results
.into_iter()
.map(|row| (row.for_ref, row.title.unwrap_or("untitled".to_string())))
.collect())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 6 additions & 2 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const routes: RouteDefinition[] = [
path: "/",
component: CreateModel,
},
{
path: "/user/:username",
component: lazy(() => import("./user/profile_public")), //XX: could this be eager?
},
{
path: "/model/:ref",
matchFilters: refIsUUIDFilter,
Expand All @@ -83,8 +87,8 @@ const routes: RouteDefinition[] = [
children: helpRoutes,
},
{
path: "/profile",
component: lazy(() => import("./user/profile")),
path: "/profile_settings",
component: lazy(() => import("./user/profile_settings")),
},
{
path: "*",
Expand Down
31 changes: 31 additions & 0 deletions packages/frontend/src/user/profile_public.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.section-title {
margin: 0 0 0.5rem 0;
color: #24292e;
font-size: 1.25rem;
font-weight: 500;
}
.file_list {
display: flex;
flex-direction: column;
gap: 0.5rem;
max-width: 600px;
margin: 0 auto;
}

.file_entry {
display: block;
padding: 1rem;
text-align: center;
background: white;
border-radius: 4px;
border: 1px solid #e1e4e8;
transition: all 0.2s ease;
text-decoration: none;
color: #0366d6;
box-shadow: none;
}

.file_entry:hover {
background-color: #f6f8fa;
border-color: #d0d7de;
}
40 changes: 40 additions & 0 deletions packages/frontend/src/user/profile_public.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { A, useParams } from "@solidjs/router";
import { For, Show, createEffect, createSignal } from "solid-js";
import { useApi } from "../api";
import "./profile_public.css";
import invariant from "tiny-invariant";

export default function PublicProfilePage() {
const api = useApi();
const params = useParams();

const [userModels, setUserModels] = createSignal<[string, string][]>(); //QQQ: unclear what type the RPC call should return to; Documents?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See first comment for the Rust side of this question.

createEffect(async () => {
invariant(params.username, "Must provide username in URL"); //QQ: or just return if no username?
const result = await api.rpc.get_user_refs_and_titles.query(params.username);
invariant(result.tag === "Ok", "RPC call failed");
console.log("get_refs_test", result.content);
setUserModels(result.content);
});
return (
<div>
<h3 class="section-title">{params.username}'s Documents</h3>
{/* XX: Should be display name */}
<Show when={userModels()} fallback={<div> Loading user files... </div>} keyed>
{(models) => {
return (
<div class="file_list">
<For each={models}>
{([id, title]: [string, string]) => (
<A href={`/model/${id}`} class="file_entry">
{title || "(Untitled)"}
</A>
)}
</For>
</div>
);
}}
</Show>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function UserProfilePage() {
<BrandedToolbar />
<div class="page-container">
<LoginGate>
<h2>Public profile</h2>
<h2>Change profile settings</h2>
<UserProfileForm />
</LoginGate>
</div>
Expand All @@ -26,13 +26,11 @@ export default function UserProfilePage() {
/** Form to configure user proifle. */
export function UserProfileForm() {
const api = useApi();

const [currentProfile, { refetch: refetchProfile }] = createResource(async () => {
const result = await api.rpc.get_active_user_profile.query();
invariant(result.tag === "Ok");
return result.content;
});

const [form, { Form, Field }] = createForm<UserProfile>();

createEffect(() => {
Expand Down
Loading