interface: expand graph api

This commit is contained in:
Liam Fitzgerald 2020-09-10 20:05:11 +10:00
parent 556759ee3f
commit 4008fb34b1
6 changed files with 92 additions and 13 deletions

View File

@ -57,4 +57,16 @@ export default class BaseApi<S extends object = {}> {
scry<T>(app: string, path: Path): Promise<T> {
return fetch(`/~/scry/${app}${path}.json`).then(r => r.json() as Promise<T>);
}
async spider<T>(inputMark: string, outputMark: string, threadName: string, body: any): Promise<T> {
const res = await fetch(`/spider/${inputMark}/${threadName}/${outputMark}.json`, {
method: 'POST',
body: JSON.stringify(body)
});
return res.json();
}
}

View File

@ -1,7 +1,9 @@
import BaseApi from './base';
import { StoreState } from '../store/type';
import { Patp, Path, PatpNoSig } from '~/types/noun';
import _ from 'lodash';
import {makeResource, resourceFromPath} from '../lib/group';
import {GroupPolicy, Enc} from '~/types';
export const createPost = (contents: Object[], parentIndex: string = '') => {
return {
@ -20,6 +22,58 @@ export default class GraphApi extends BaseApi<StoreState> {
return this.action('graph-store', 'graph-update', action)
}
private viewAction(threadName: string, action: any) {
return this.spider('graph-view-action', 'json', threadName, action);
}
createManagedGraph(name: string, title: string, description: string, app: string, group: Path) {
const associated = { group: resourceFromPath(group) };
const resource = makeResource(`~${window.ship}`, name);
return this.viewAction('graph-create', {
"create": {
resource,
title,
description,
app,
associated
}
});
}
createUnmanagedGraph(name: string, title: string, description: string, app: string, policy: Enc<GroupPolicy>) {
const resource = makeResource(`~${window.ship}`, name);
return this.viewAction('graph-create', {
"create": {
resource,
title,
description,
app,
associated: { policy }
}
});
}
joinGraph(ship: Patp, name: string) {
const resource = makeResource(ship, name);
return this.viewAction('graph-join', {
join: {
resource,
ship
}
});
}
deleteGraph(name: string) {
const resource = makeResource(`~${window.ship}`, name);
return this.viewAction('graph-delete', {
"delete": {
resource
}
});
}
addGraph(ship: Patp, name: string, graph: any, mark: any) {
this.storeAction({
'add-graph': {
@ -40,14 +94,15 @@ export default class GraphApi extends BaseApi<StoreState> {
addPost(ship: Patp, name: string, post: Object) {
let nodes = {};
const resource = { ship, name };
nodes[post.index] = {
post,
children: { empty: null }
};
this.storeAction({
return this.storeAction({
'add-nodes': {
resource: { ship, name },
resource,
nodes
}
});
@ -63,7 +118,7 @@ export default class GraphApi extends BaseApi<StoreState> {
}
removeNodes(ship: Patp, name: string, indices: string[]) {
this.storeAction({
return this.storeAction({
'remove-nodes': {
resource: { ship, name },
indices

View File

@ -13,3 +13,8 @@ export function resourceFromPath(path: Path): Resource {
const [, , ship, name] = path.split('/');
return { ship, name }
}
export function makeResource(ship: string, name:string) {
return { ship, name };
}

View File

@ -140,10 +140,21 @@ const addNodes = (json, state) => {
};
const removeNodes = (json, state) => {
const _remove = (graph, index) => {
if (index.length === 1) {
graph.delete(index[0]);
} else {
const child = graph.get(index[0]);
_remove(child.children, index.slice(1));
graph.set(index[0], child);
}
};
const data = _.get(json, 'remove-nodes', false);
if (data) {
console.log(data);
if (!(data.resource in state.graphs)) { return; }
const { ship, name } = data.resource;
const res = `${ship}/${name}`;
if (!(res in state.graphs)) { return; }
data.indices.forEach((index) => {
console.log(index);
@ -151,13 +162,7 @@ const removeNodes = (json, state) => {
let indexArr = index.split('/').slice(1).map((ind) => {
return parseInt(ind, 10);
});
if (indexArr.length === 1) {
state.graphs[data.resource].delete(indexArr[0]);
} else {
// TODO: recursive
}
_remove(state.graphs[res], indexArr);
});
}
};

View File

@ -12,6 +12,7 @@ import { LaunchState, WeatherState } from '~/types/launch-update';
import { LinkComments, LinkCollections, LinkSeen } from '~/types/link-update';
import { ConnectionStatus } from '~/types/connection';
import { BackgroundConfig, LocalUpdateRemoteContentPolicy } from '~/types/local-update';
import {Graphs} from '~/types/graph-update';
export interface StoreState {
// local state
@ -36,7 +37,7 @@ export interface StoreState {
groupKeys: Set<Path>;
permissions: Permissions;
s3: S3State;
graphs: Object;
graphs: Graphs;
graphKeys: Set<String>;

View File

@ -5,6 +5,7 @@ export * from './connection';
export * from './contact-update';
export * from './global';
export * from './group-update';
export * from './graph-update';
export * from './invite-update';
export * from './launch-update';
export * from './link-listen-update';