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

527 lines
11 KiB
TypeScript
Raw Normal View History

2021-04-22 17:17:39 +03:00
import { GroupPolicy, makeResource, Resource, resourceFromPath } from '../groups';
2021-02-25 06:54:00 +03:00
2021-06-10 05:01:25 +03:00
import { decToUd, deSig, unixToDa, Scry } from '../lib';
2021-02-25 06:54:00 +03:00
import { Enc, Path, Patp, PatpNoSig, Poke, Thread } from '../lib/types';
2021-07-08 03:48:44 +03:00
import { Content, GraphChildrenPoke, GraphNode, GraphNodePoke, Post } from './types';
2021-06-10 05:01:25 +03:00
import { patp2dec } from 'urbit-ob';
2021-04-22 17:17:39 +03:00
2021-07-08 03:48:44 +03:00
export const GRAPH_UPDATE_VERSION = 2;
2021-02-25 06:54:00 +03:00
export const createBlankNodeWithChildPost = (
ship: PatpNoSig,
2021-07-08 03:48:44 +03:00
parentIndex = '',
childIndex = '',
2021-02-25 06:54:00 +03:00
contents: Content[]
): GraphNodePoke => {
2021-02-25 06:54:00 +03:00
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-02-25 06:54:00 +03:00
};
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[],
2021-07-08 03:48:44 +03:00
parentIndex = '',
childIndex = 'DATE_PLACEHOLDER'
2021-02-25 06:54:00 +03:00
): 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
});
2021-06-10 05:01:25 +03:00
const dmAction = <T>(data: T): Poke<T> => ({
app: 'dm-hook',
mark: 'dm-hook-action',
json: data
});
2021-02-25 06:54:00 +03:00
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)
}
});
2021-07-08 03:48:44 +03:00
};
2021-02-25 06:54:00 +03:00
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),
2021-07-08 03:48:44 +03:00
ship
2021-02-25 06:54:00 +03:00
}
});
export const deleteGraph = (
ship: PatpNoSig,
name: string
): Thread<any> => viewAction('graph-delete', {
2021-07-08 03:48:44 +03:00
'delete': {
2021-02-25 06:54:00 +03:00
resource: makeResource(`~${ship}`, name)
}
});
export const leaveGraph = (
ship: Patp,
name: string
): Thread<any> => viewAction('graph-leave', {
2021-07-08 03:48:44 +03:00
'leave': {
2021-02-25 06:54:00 +03:00
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
}
});
2021-07-08 03:48:44 +03:00
};
2021-02-25 06:54:00 +03:00
export const evalCord = (
cord: string
): Thread<any> => {
return ({
inputMark: 'graph-view-action',
outputMark: 'tang',
threadName: 'graph-eval',
body: {
eval: cord
}
});
2021-07-08 03:48:44 +03:00
};
2021-02-25 06:54:00 +03:00
export const addGraph = (
ship: Patp,
name: string,
graph: any,
mark: any
): Poke<any> => {
return storeAction({
'add-graph': {
resource: { ship, name },
graph,
mark
}
});
2021-07-08 03:48:44 +03:00
};
2021-02-25 06:54:00 +03:00
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-07-08 03:48:44 +03:00
const nodes: Record<string, GraphNode> = {};
2021-02-25 06:54:00 +03:00
nodes[post.index] = {
post,
children: null
};
return addNodes(ship, name, nodes);
2021-07-08 03:48:44 +03:00
};
2021-02-25 06:54:00 +03:00
export const addNode = (
ship: Patp,
name: string,
2021-06-08 05:41:15 +03:00
node: GraphNodePoke
2021-04-22 17:17:39 +03:00
): Thread<any> => {
2021-07-08 03:48:44 +03:00
const nodes: Record<string, GraphNodePoke> = {};
2021-02-25 06:54:00 +03:00
nodes[node.post.index] = node;
return addNodes(ship, name, nodes);
2021-07-08 03:48:44 +03:00
};
2021-02-25 06:54:00 +03:00
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
2021-06-10 05:01:25 +03:00
/**
* Set dm-hook to screen new DMs or not
*
*/
export const setScreen = (screen: boolean): Poke<any> => dmAction({ screen });
/**
* Accept a pending DM request
*
* @param ship the ship to accept
*/
export const acceptDm = (ship: string) => dmAction({
2021-06-10 05:01:25 +03:00
accept: ship
});
/**
* Decline a pending DM request
*
* @param ship the ship to accept
*/
export const declineDm = (ship: string) => dmAction({
2021-06-10 05:01:25 +03:00
decline: ship
});
/**
* Remove posts from a set of indices
*
*/
export const removePosts = (
2021-02-25 06:54:00 +03:00
ship: Patp,
name: string,
indices: string[]
): Poke<any> => hookAction({
2021-07-08 03:48:44 +03:00
'remove-posts': {
2021-02-25 06:54:00 +03:00
resource: { ship, name },
indices
}
});
2021-06-10 05:01:25 +03:00
/**
* Remove a DM message from our inbox
*
* @remarks
* This does not remove the message from the recipients inbox
*/
export const removeDmMessage = (
our: Patp,
index: string
): Poke<any> => ({
app: 'graph-store',
mark: `graph-update-${GRAPH_UPDATE_VERSION}`,
json: {
'remove-posts': {
resource: { ship: our, name: 'dm-inbox' },
indices: [index]
}
}
});
2021-06-10 05:01:25 +03:00
/**
* Send a DM to a ship
*
* @param our sender
* @param ship recipient
* @param contents contents of message
*/
2021-06-14 04:45:18 +03:00
export const addDmMessage = (our: PatpNoSig, ship: Patp, contents: Content[]): Poke<any> => {
2021-06-10 05:01:25 +03:00
const post = createPost(our, contents, `/${patp2dec(ship)}`);
const node: GraphNode = {
post,
children: null
};
return {
app: 'dm-hook',
mark: `graph-update-${GRAPH_UPDATE_VERSION}`,
json: {
'add-nodes': {
2021-06-14 04:45:18 +03:00
resource: { ship: `~${our}`, name: 'dm-inbox' },
2021-06-10 05:01:25 +03:00
nodes: {
[post.index]: node
}
}
}
};
2021-07-08 03:48:44 +03:00
};
2021-06-10 05:01:25 +03:00
const encodeIndex = (idx: string) => idx.split('/').map(decToUd).join('/');
/**
2021-06-10 05:01:25 +03:00
* Fetch newest (larger keys) nodes in a graph under some index
*
* @param ship ship of graph
* @param name name of graph
* @param count number of nodes to load
* @param index index to query
*/
export const getNewest = (
ship: string,
name: string,
count: number,
index = ''
): Scry => ({
app: 'graph-store',
2021-07-21 05:26:33 +03:00
path: `/graph/${ship}/${name}/node/siblings` +
2021-07-23 04:11:21 +03:00
`/newest/lone/${count}${encodeIndex(index)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch nodes in a graph that are older (key is smaller) and direct
2021-06-10 05:01:25 +03:00
* siblings of some index
*
* @param ship ship of graph
* @param name name of graph
* @param count number of nodes to load
* @param index index to query
*/
export const getOlderSiblings = (
ship: string,
name: string,
count: number,
index: string
): Scry => ({
app: 'graph-store',
2021-07-09 02:26:25 +03:00
path: `/graph/${ship}/${name}/node/siblings/older/lone/${count}${encodeIndex(index)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch nodes in a graph that are younger (key is larger) and direct
2021-06-10 05:01:25 +03:00
* siblings of some index
*
* @param ship ship of graph
* @param name name of graph
* @param count number of nodes to load
* @param index index to query
*/
export const getYoungerSiblings = (
ship: string,
name: string,
count: number,
index: string
): Scry => ({
app: 'graph-store',
2021-07-09 02:26:25 +03:00
path: `/graph/${ship}/${name}/node/siblings/newer/lone/${count}${encodeIndex(index)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch all nodes in a graph under some index, without loading children
*
* @param ship ship of graph
* @param name name of graph
* @param index index to query
*/
export const getShallowChildren = (ship: string, name: string, index = '') => ({
app: 'graph-store',
2021-07-21 05:26:33 +03:00
path: `/graph/${ship}/${name}/node/children/lone/~/~${encodeIndex(index)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch newest nodes in a graph as a flat map, including children,
2021-06-10 05:01:25 +03:00
* optionally starting at a specified key
*
* @param ship ship of graph
* @param name name of graph
* @param count number of nodes to load
* @param start key to start at
*
*/
export const getDeepOlderThan = (
ship: string,
name: string,
count: number,
2021-07-21 05:26:33 +03:00
start = ''
2021-06-10 05:01:25 +03:00
) => ({
app: 'graph-store',
2021-07-09 02:26:25 +03:00
path: `/graph/${ship}/${name}/node/siblings` +
2021-07-29 02:18:54 +03:00
`/${start.length > 0 ? 'older' : 'newest'}` +
2021-07-21 05:26:33 +03:00
`/kith/${count}${encodeIndex(start)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch a flat map of a nodes ancestors and firstborn children
*
* @param ship ship of graph
* @param name name of graph
* @param index index to query
*
*/
export const getFirstborn = (
ship: string,
name: string,
index: string
): Scry => ({
app: 'graph-store',
2021-07-09 02:26:25 +03:00
path: `/graph/${ship}/${name}/node/firstborn${encodeIndex(index)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch a single node, and all it's children
*
* @param ship ship of graph
* @param name name of graph
* @param index index to query
*
*/
export const getNode = (
ship: string,
name: string,
index: string
): Scry => ({
app: 'graph-store',
2021-07-09 02:26:25 +03:00
path: `/graph/${ship}/${name}/node/index/kith${encodeIndex(index)}`
2021-06-10 05:01:25 +03:00
});
/**
* Fetch entire graph
*
* @param ship ship of graph
* @param name name of graph
*
*/
export const getGraph = (
ship: string,
name: string
): Scry => ({
app: 'graph-store',
path: `/graph/${ship}/${name}`
});