mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 10:36:22 +08:00
feat(core): view selfhosted shared docs in electron (#9365)
This commit is contained in:
@@ -55,6 +55,12 @@ export class ServersService extends Service {
|
||||
);
|
||||
}
|
||||
|
||||
serverByBaseUrl$(url: string) {
|
||||
return this.servers$.map(servers =>
|
||||
servers.find(server => server.baseUrl === url)
|
||||
);
|
||||
}
|
||||
|
||||
private readonly serverPool = new ObjectPool<string, Server>({
|
||||
onDelete(obj) {
|
||||
obj.dispose();
|
||||
|
||||
@@ -31,8 +31,18 @@ export class ShareReader extends Entity {
|
||||
|
||||
loadShare = effect(
|
||||
switchMap(
|
||||
({ workspaceId, docId }: { workspaceId: string; docId: string }) => {
|
||||
return fromPromise(this.store.loadShare(workspaceId, docId)).pipe(
|
||||
({
|
||||
serverId,
|
||||
workspaceId,
|
||||
docId,
|
||||
}: {
|
||||
serverId: string;
|
||||
workspaceId: string;
|
||||
docId: string;
|
||||
}) => {
|
||||
return fromPromise(
|
||||
this.store.loadShare(serverId, workspaceId, docId)
|
||||
).pipe(
|
||||
mergeMap(data => {
|
||||
if (!data) {
|
||||
this.data$.next(null);
|
||||
|
||||
@@ -5,7 +5,7 @@ export { ShareReaderService } from './services/share-reader';
|
||||
|
||||
import { type Framework } from '@toeverything/infra';
|
||||
|
||||
import { RawFetchProvider, WorkspaceServerService } from '../cloud';
|
||||
import { ServersService, WorkspaceServerService } from '../cloud';
|
||||
import { DocScope, DocService } from '../doc';
|
||||
import {
|
||||
WorkspaceLocalCache,
|
||||
@@ -26,7 +26,7 @@ export function configureShareDocsModule(framework: Framework) {
|
||||
framework
|
||||
.service(ShareReaderService)
|
||||
.entity(ShareReader, [ShareReaderStore])
|
||||
.store(ShareReaderStore, [RawFetchProvider])
|
||||
.store(ShareReaderStore, [ServersService])
|
||||
.scope(WorkspaceScope)
|
||||
.service(ShareDocsListService, [WorkspaceService])
|
||||
.store(ShareDocsStore, [WorkspaceServerService])
|
||||
|
||||
@@ -2,36 +2,31 @@ import { ErrorNames, UserFriendlyError } from '@affine/graphql';
|
||||
import type { DocMode } from '@blocksuite/affine/blocks';
|
||||
import { Store } from '@toeverything/infra';
|
||||
|
||||
import type { RawFetchProvider } from '../../cloud';
|
||||
import type { ServersService } from '../../cloud';
|
||||
import { isBackendError } from '../../cloud';
|
||||
|
||||
export class ShareReaderStore extends Store {
|
||||
constructor(private readonly rawFetch?: RawFetchProvider) {
|
||||
constructor(private readonly serversService: ServersService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async loadShare(workspaceId: string, docId: string) {
|
||||
if (!this.rawFetch) {
|
||||
throw new Error('No Fetch Service');
|
||||
async loadShare(serverId: string, workspaceId: string, docId: string) {
|
||||
const server = this.serversService.server$(serverId).value;
|
||||
if (!server) {
|
||||
throw new Error(`Server ${serverId} not found`);
|
||||
}
|
||||
try {
|
||||
const docResponse = await this.rawFetch.fetch(
|
||||
const docResponse = await server.fetch(
|
||||
`/api/workspaces/${workspaceId}/docs/${docId}`
|
||||
);
|
||||
if (docResponse.status !== 200) {
|
||||
throw new Error('Failed to fetch workspace');
|
||||
}
|
||||
const publishMode = docResponse.headers.get(
|
||||
'publish-mode'
|
||||
) as DocMode | null;
|
||||
const docBinary = await docResponse.arrayBuffer();
|
||||
|
||||
const workspaceResponse = await this.rawFetch.fetch(
|
||||
const workspaceResponse = await server.fetch(
|
||||
`/api/workspaces/${workspaceId}/docs/${workspaceId}`
|
||||
);
|
||||
if (workspaceResponse.status !== 200) {
|
||||
throw new Error('Failed to fetch workspace');
|
||||
}
|
||||
const workspaceBinary = await workspaceResponse.arrayBuffer();
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user