Skip to content

Commit

Permalink
Add namespaces to file saved object
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpeihl committed Nov 27, 2023
1 parent d62272d commit 3683587
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/shared-ux/file/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export interface FileJSON<Meta = unknown> {
* File hash information
*/
hash?: BaseFileMetadata['hash'];
/**
* The Spaces to which this saved object is shared
*/
namespaces?: string[];
}

export interface FileKindBase {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/files/server/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class File<M = unknown> implements IFile {
public readonly id: string,
private metadata: FileJSON<M>,
private readonly fileClient: FileClientImpl,
private readonly logger: Logger
private readonly logger: Logger,
public readonly namespaces?: string[]
) {}

private async updateFileState(action: Action): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/files/server/file/to_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export function serializeJSON<M = unknown>(attrs: Partial<FileJSON>): Partial<Fi
);
}

export function toJSON<M = unknown>(id: string, attrs: FileMetadata<M>): FileJSON<M> {
export function toJSON<M = unknown>(
id: string,
attrs: FileMetadata<M>,
namespaces?: string[]
): FileJSON<M> {
const {
name,
mime_type: mimeType,
Expand All @@ -61,6 +65,7 @@ export function toJSON<M = unknown>(id: string, attrs: FileMetadata<M>): FileJSO
return pickBy<FileJSON<M>>(
{
id,
namespaces,
user,
name,
mimeType,
Expand Down
17 changes: 11 additions & 6 deletions src/plugins/files/server/file_client/file_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,20 @@ export class FileClientImpl implements FileClient {
FileClientImpl.usageCounter?.incrementCounter({ counterName: this.getCounters()[counter] });
}

private instantiateFile<M = unknown>(id: string, metadata: FileMetadata<M>): File<M> {
private instantiateFile<M = unknown>(
id: string,
metadata: FileMetadata<M>,
namespaces?: string[]
): File<M> {
return new FileImpl(
id,
toJSON(id, {
...createDefaultFileAttributes(),
...metadata,
}),
this,
this.logger
this.logger,
namespaces
);
}

Expand Down Expand Up @@ -153,8 +158,8 @@ export class FileClientImpl implements FileClient {
}

public async get<M = unknown>(arg: P1<FileMetadataClient['get']>): Promise<File<M>> {
const { id, metadata } = await this.metadataClient.get(arg);
return this.instantiateFile(id, metadata as FileMetadata<M>);
const { id, namespaces, metadata } = await this.metadataClient.get(arg);
return this.instantiateFile(id, metadata as FileMetadata<M>, namespaces);
}

public async internalUpdate(id: string, metadata: Partial<FileJSON>): Promise<void> {
Expand All @@ -173,8 +178,8 @@ export class FileClientImpl implements FileClient {
const result = await this.metadataClient.find(arg);
return {
total: result.total,
files: result.files.map(({ id, metadata }) =>
this.instantiateFile(id, metadata as FileMetadata<M>)
files: result.files.map(({ id, namespaces, metadata }) =>
this.instantiateFile(id, metadata as FileMetadata<M>, namespaces)
),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class SavedObjectsFileMetadataClient implements FileMetadataClient {
const result = await this.soClient.get(this.soType, id);
return {
id: result.id,
namespaces: result.namespaces,
metadata: result.attributes as FileDescriptor['metadata'],
};
}
Expand All @@ -78,6 +79,7 @@ export class SavedObjectsFileMetadataClient implements FileMetadataClient {

return {
id: so.id,
namespaces: so.namespaces,
metadata: so.attributes as FileDescriptor['metadata'],
};
});
Expand All @@ -98,6 +100,7 @@ export class SavedObjectsFileMetadataClient implements FileMetadataClient {
return {
files: result.saved_objects.map((so) => ({
id: so.id,
namespaces: so.namespaces,
metadata: so.attributes as FileMetadata,
})),
total: result.total,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface FileDescriptor<M = unknown> {
* The file's metadata.
*/
metadata: FileMetadata<M>;
namespaces?: string[];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class InternalFileService {
const { total, files } = await this.metadataClient.find(args);
return {
total,
files: files.map(({ id, metadata }) => toJSON(id, metadata)),
files: files.map(({ id, metadata, namespaces }) => toJSON(id, metadata, namespaces)),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/files_management/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const App: FunctionComponent = () => {
hits: files.map((file) => ({
id: file.id,
updatedAt: file.updated,
namespaces: file.namespaces ?? [],
references: [],
type: 'file',
attributes: {
Expand Down

0 comments on commit 3683587

Please sign in to comment.