Skip to content

Commit

Permalink
Merge pull request #17779 from davelopez/24.0_restore_histories_API_b…
Browse files Browse the repository at this point in the history
…ehaviour_for_keys_query

[24.0] Restore histories API behavior for `keys` query parameter
  • Loading branch information
mvdbeek authored Mar 20, 2024
2 parents 4d33421 + 7dd3f71 commit 5714783
Show file tree
Hide file tree
Showing 16 changed files with 446 additions and 127 deletions.
51 changes: 51 additions & 0 deletions client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import { components } from "@/api/schema";
*/
export type HistorySummary = components["schemas"]["HistorySummary"];

export interface HistorySummaryExtended extends HistorySummary {
size: number;
contents_active: components["schemas"]["HistoryActiveContentCounts"];
user_id: string;
}

/**
* Contains additional details about a History.
*/
export type HistoryDetailed = components["schemas"]["HistoryDetailed"];

export type AnyHistory = HistorySummary | HistorySummaryExtended | HistoryDetailed;

/**
* Contains minimal information about a HistoryContentItem.
*/
Expand Down Expand Up @@ -125,3 +133,46 @@ export function hasDetails(entry: DatasetEntry): entry is DatasetDetails {
* Contains dataset metadata information.
*/
export type MetadataFiles = components["schemas"]["MetadataFile"][];

export function isHistorySummary(history: AnyHistory): history is HistorySummary {
return !("user_id" in history);
}

export function isHistorySummaryExtended(history: AnyHistory): history is HistorySummaryExtended {
return "contents_active" in history && "user_id" in history;
}

type QuotaUsageResponse = components["schemas"]["UserQuotaUsage"];

export interface User extends QuotaUsageResponse {
id: string;
email: string;
tags_used: string[];
isAnonymous: false;
is_admin?: boolean;
username?: string;
}

export interface AnonymousUser {
isAnonymous: true;
username?: string;
is_admin?: false;
}

export type GenericUser = User | AnonymousUser;

export function isRegisteredUser(user: User | AnonymousUser | null): user is User {
return !user?.isAnonymous;
}

export function userOwnsHistory(user: User | AnonymousUser | null, history: AnyHistory) {
return (
// Assuming histories without user_id are owned by the current user
(isRegisteredUser(user) && !hasOwner(history)) ||
(isRegisteredUser(user) && hasOwner(history) && user.id === history.user_id)
);
}

function hasOwner(history: AnyHistory): history is HistorySummaryExtended {
return "user_id" in history;
}
Loading

0 comments on commit 5714783

Please sign in to comment.