urbit/pkg/npm/api/graph/lib.ts

305 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-02-25 06:54:00 +03:00
import _ from 'lodash';
2021-04-22 17:17:39 +03:00
import { GroupPolicy, makeResource, Resource, resourceFromPath } from '../groups';
2021-02-25 06:54:00 +03:00
import { deSig, unixToDa } from '../lib';
import { Enc, Path, Patp, PatpNoSig, Poke, Thread } from '../lib/types';
2021-04-22 17:17:39 +03:00
import { Content, Graph, GraphChildrenPoke, GraphNode, GraphNodePoke, Post } from './types';
export const GRAPH_UPDATE_VERSION: number = 1;
2021-02-25 06:54:00 +03:00
export const createBlankNodeWithChildPost = (
ship: PatpNoSig,
parentIndex: string = '',
childIndex: string = '',
contents: Content[]
): any => { // TODO should be GraphNode
const date = unixToDa(Date.now()).toString();
const nodeIndex = parentIndex + '/' + date;
const childGraph: GraphChildrenPoke = {};
childGraph[childIndex] = {
post: {
author: `~${ship}`,
index: nodeIndex + '/' + childIndex,
'time-sent': Date.now(),
contents,
hash: null,
signatures: []
},
children: null
};
return {
post: {
author: `~${ship}`,
index: nodeIndex,
'time-sent': Date.now(),
contents: [],
hash: null,
signatures: []
},
children: childGraph
};
};
2021-04-22 17:17:39 +03:00
export const markPending = (nodes: any): any => {
Object.keys(nodes).forEach((key) => {
nodes[key].post.author = deSig(nodes[key].post.author);
nodes[key].post.pending = true;
if (nodes[key].children) {
nodes[key].children = markPending(nodes[key].children);
}
2021-02-25 06:54:00 +03:00
});
2021-04-22 17:17:39 +03:00
return nodes;
2021-02-25 06:54:00 +03:00
};
export const createPost = (
ship: PatpNoSig,
contents: Content[],
parentIndex: string = '',
childIndex:string = 'DATE_PLACEHOLDER'
): Post => {
if (childIndex === 'DATE_PLACEHOLDER') {
childIndex = unixToDa(Date.now()).toString();
}
return {
author: `~${ship}`,
index: parentIndex + '/' + childIndex,
'time-sent': Date.now(),
contents,
hash: null,
signatures: []
};
};
function moduleToMark(mod: string): string | undefined {
if(mod === 'link') {
return 'graph-validator-link';
}
if(mod === 'publish') {
return 'graph-validator-publish';
}
if(mod === 'chat') {
return 'graph-validator-chat';
}
return undefined;
}
2021-04-22 17:17:39 +03:00
const storeAction = <T>(data: T, version: number = GRAPH_UPDATE_VERSION): Poke<T> => ({
2021-02-25 06:54:00 +03:00
app: 'graph-store',
2021-04-22 17:17:39 +03:00
mark: `graph-update-${version}`,
2021-02-25 06:54:00 +03:00
json: data
});
export { storeAction as graphStoreAction };
const viewAction = <T>(threadName: string, action: T): Thread<T> => ({
inputMark: 'graph-view-action',
outputMark: 'json',
threadName,
body: action
});
export { viewAction as graphViewAction };
2021-04-22 17:17:39 +03:00
const hookAction = <T>(data: T, version: number = GRAPH_UPDATE_VERSION): Poke<T> => ({
2021-02-25 06:54:00 +03:00
app: 'graph-push-hook',
2021-04-22 17:17:39 +03:00
mark: `graph-update-${version}`,
2021-02-25 06:54:00 +03:00
json: data
});
export { hookAction as graphHookAction };
export const createManagedGraph = (
ship: PatpNoSig,
name: string,
title: string,
description: string,
group: Path,
mod: string
): Thread<any> => {
const associated = { group: resourceFromPath(group) };
const resource = makeResource(`~${ship}`, name);
return viewAction('graph-create', {
create: {
resource,
title,
description,
associated,
module: mod,
mark: moduleToMark(mod)
}
});
}
export const createUnmanagedGraph = (
ship: PatpNoSig,
name: string,
title: string,
description: string,
policy: Enc<GroupPolicy>,
mod: string
): Thread<any> => viewAction('graph-create', {
create: {
resource: makeResource(`~${ship}`, name),
title,
description,
associated: { policy },
module: mod,
mark: moduleToMark(mod)
}
});
export const joinGraph = (
ship: Patp,
name: string
): Thread<any> => viewAction('graph-join', {
join: {
resource: makeResource(ship, name),
ship,
}
});
export const deleteGraph = (
ship: PatpNoSig,
name: string
): Thread<any> => viewAction('graph-delete', {
"delete": {
resource: makeResource(`~${ship}`, name)
}
});
export const leaveGraph = (
ship: Patp,
name: string
): Thread<any> => viewAction('graph-leave', {
"leave": {
resource: makeResource(ship, name)
}
});
export const groupifyGraph = (
ship: Patp,
name: string,
toPath?: string
): Thread<any> => {
const resource = makeResource(ship, name);
const to = toPath && resourceFromPath(toPath);
return viewAction('graph-groupify', {
groupify: {
resource,
to
}
});
}
export const evalCord = (
cord: string
): Thread<any> => {
return ({
inputMark: 'graph-view-action',
outputMark: 'tang',
threadName: 'graph-eval',
body: {
eval: cord
}
});
}
export const addGraph = (
ship: Patp,
name: string,
graph: any,
mark: any
): Poke<any> => {
return storeAction({
'add-graph': {
resource: { ship, name },
graph,
mark
}
});
}
export const addNodes = (
ship: Patp,
name: string,
nodes: Object
2021-04-22 17:17:39 +03:00
): Thread<any> => ({
inputMark: `graph-update-${GRAPH_UPDATE_VERSION}`,
outputMark: 'graph-view-action',
threadName: 'graph-add-nodes',
body: {
2021-02-25 06:54:00 +03:00
'add-nodes': {
resource: { ship, name },
nodes
}
2021-04-22 17:17:39 +03:00
}
});
2021-02-25 06:54:00 +03:00
export const addPost = (
ship: Patp,
name: string,
post: Post
2021-04-22 17:17:39 +03:00
): Thread<any> => {
2021-02-25 06:54:00 +03:00
let nodes: Record<string, GraphNode> = {};
nodes[post.index] = {
post,
children: null
};
return addNodes(ship, name, nodes);
}
export const addNode = (
ship: Patp,
name: string,
node: GraphNode
2021-04-22 17:17:39 +03:00
): Thread<any> => {
2021-02-25 06:54:00 +03:00
let nodes: Record<string, GraphNode> = {};
nodes[node.post.index] = node;
return addNodes(ship, name, nodes);
}
2021-04-22 17:17:39 +03:00
export const createGroupFeed = (
group: Resource,
vip: any = ''
): Thread<any> => ({
inputMark: 'graph-view-action',
outputMark: 'resource',
threadName: 'graph-create-group-feed',
body: {
'create-group-feed': {
resource: group,
vip
}
}
});
export const disableGroupFeed = (
group: Resource
): Thread<any> => ({
inputMark: 'graph-view-action',
outputMark: 'json',
threadName: 'graph-disable-group-feed',
body: {
'disable-group-feed': {
resource: group
}
}
});
2021-02-25 06:54:00 +03:00
export const removeNodes = (
ship: Patp,
name: string,
indices: string[]
): Poke<any> => hookAction({
'remove-nodes': {
resource: { ship, name },
indices
}
});