diff --git a/gear/graph/lib.ts b/gear/graph/lib.ts deleted file mode 100644 index ce187df..0000000 --- a/gear/graph/lib.ts +++ /dev/null @@ -1,534 +0,0 @@ -import { GroupPolicy, makeResource, Resource, resourceFromPath } from '../groups'; - -import { decToUd, deSig, unixToDa, Scry } from '../lib'; -import { Enc, Path, Patp, PatpNoSig, Poke, Thread } from '../lib/types'; -import { Content, GraphChildrenPoke, GraphNode, GraphNodePoke, Post } from './types'; -import { patp2dec } from 'urbit-ob'; - -export const GRAPH_UPDATE_VERSION = 3; - -export const createBlankNodeWithChildPost = ( - ship: PatpNoSig, - parentIndex = '', - childIndex = '', - contents: Content[] -): GraphNodePoke => { - 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 - }; -}; - -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); - } - }); - return nodes; -}; - -export const createPost = ( - ship: PatpNoSig, - contents: Content[], - parentIndex = '', - childIndex = '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; -} - -const storeAction = (data: T, version: number = GRAPH_UPDATE_VERSION): Poke => ({ - app: 'graph-store', - mark: `graph-update-${version}`, - json: data -}); - -export { storeAction as graphStoreAction }; - -const viewAction = (threadName: string, action: T): Thread => ({ - inputMark: 'graph-view-action', - outputMark: 'json', - threadName, - body: action -}); - -export { viewAction as graphViewAction }; - -const hookAction = (data: T, version: number = GRAPH_UPDATE_VERSION): Poke => ({ - app: 'graph-push-hook', - mark: `graph-update-${version}`, - json: data -}); - -const dmAction = (data: T): Poke => ({ - app: 'dm-hook', - mark: 'dm-hook-action', - json: data -}); - -export { hookAction as graphHookAction }; - -export const createManagedGraph = ( - ship: PatpNoSig, - name: string, - title: string, - description: string, - group: Path, - mod: string -): Thread => { - 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, - mod: string -): Thread => 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 => viewAction('graph-join', { - join: { - resource: makeResource(ship, name), - ship - } -}); - -export const deleteGraph = ( - ship: PatpNoSig, - name: string -): Thread => viewAction('graph-delete', { - 'delete': { - resource: makeResource(`~${ship}`, name) - } -}); - -export const leaveGraph = ( - ship: Patp, - name: string -): Thread => viewAction('graph-leave', { - 'leave': { - resource: makeResource(ship, name) - } -}); - -export const groupifyGraph = ( - ship: Patp, - name: string, - toPath?: string -): Thread => { - const resource = makeResource(ship, name); - const to = toPath && resourceFromPath(toPath); - - return viewAction('graph-groupify', { - groupify: { - resource, - to - } - }); -}; - -export const evalCord = ( - cord: string -): Thread => { - 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 => { - return storeAction({ - 'add-graph': { - resource: { ship, name }, - graph, - mark - } - }); -}; - -export const addNodes = ( - ship: Patp, - name: string, - nodes: Object -): Thread => ({ - inputMark: `graph-update-${GRAPH_UPDATE_VERSION}`, - outputMark: 'graph-view-action', - threadName: 'graph-add-nodes', - body: { - 'add-nodes': { - resource: { ship, name }, - nodes - } - } -}); - -export const addPost = ( - ship: Patp, - name: string, - post: Post -): Thread => { - const nodes: Record = {}; - nodes[post.index] = { - post, - children: null - }; - return addNodes(ship, name, nodes); -}; - -export const addNode = ( - ship: Patp, - name: string, - node: GraphNodePoke -): Thread => { - const nodes: Record = {}; - nodes[node.post.index] = node; - - return addNodes(ship, name, nodes); -}; - -export const createGroupFeed = ( - group: Resource, - vip: any = '' -): Thread => ({ - inputMark: 'graph-view-action', - outputMark: 'resource', - threadName: 'graph-create-group-feed', - body: { - 'create-group-feed': { - resource: group, - vip - } - } -}); - -export const disableGroupFeed = ( - group: Resource -): Thread => ({ - inputMark: 'graph-view-action', - outputMark: 'json', - threadName: 'graph-disable-group-feed', - body: { - 'disable-group-feed': { - resource: group - } - } -}); - -/** - * Set dm-hook to screen new DMs or not - * - */ -export const setScreen = (screen: boolean): Poke => dmAction({ screen }); - -/** - * Accept a pending DM request - * - * @param ship the ship to accept - */ -export const acceptDm = (ship: string) => dmAction({ - accept: ship -}); - -/** - * Decline a pending DM request - * - * @param ship the ship to accept - */ -export const declineDm = (ship: string) => dmAction({ - decline: ship -}); - -/** - * Remove posts from a set of indices - * - */ -export const removePosts = ( - ship: Patp, - name: string, - indices: string[] -): Poke => hookAction({ - 'remove-posts': { - resource: { ship, name }, - indices - } -}); - -/** - * 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 => ({ - app: 'graph-store', - mark: `graph-update-${GRAPH_UPDATE_VERSION}`, - json: { - 'remove-posts': { - resource: { ship: our, name: 'dm-inbox' }, - indices: [index] - } - } -}); - -/** - * Send a DM to a ship - * - * @param our sender - * @param ship recipient - * @param contents contents of message - */ -export const addDmMessage = (our: PatpNoSig, ship: Patp, contents: Content[]): Poke => { - 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': { - resource: { ship: `~${our}`, name: 'dm-inbox' }, - nodes: { - [post.index]: node - } - } - } - }; -}; - -const encodeIndex = (idx: string) => idx.split('/').map(decToUd).join('/'); - -/** - * Fetch all graph keys - */ -export const getKeys = (): Scry => ({ - app: 'graph-store', - path: '/keys' -}); - -/** - * 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', - path: `/graph/${ship}/${name}/node/siblings` + - `/newest/lone/${count}${encodeIndex(index)}` -}); - -/** - * Fetch nodes in a graph that are older (key is smaller) and direct - * 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', - path: `/graph/${ship}/${name}/node/siblings/older/lone/${count}${encodeIndex(index)}` -}); - -/** - * Fetch nodes in a graph that are younger (key is larger) and direct - * 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', - path: `/graph/${ship}/${name}/node/siblings/newer/lone/${count}${encodeIndex(index)}` -}); - -/** - * 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', - path: `/graph/${ship}/${name}/node/children/lone/~/~${encodeIndex(index)}` -}); - -/** - * Fetch newest nodes in a graph as a flat map, including children, - * 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, - start = '' -) => ({ - app: 'graph-store', - path: `/graph/${ship}/${name}/node/siblings` + - `/${start.length > 0 ? 'older' : 'newest'}` + - `/kith/${count}${encodeIndex(start)}` -}); - -/** - * 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', - path: `/graph/${ship}/${name}/node/firstborn${encodeIndex(index)}` -}); - -/** - * 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', - path: `/graph/${ship}/${name}/node/index/kith${encodeIndex(index)}` -}); - -/** - * 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}` -}); diff --git a/gear/graph/types.ts b/gear/graph/types.ts deleted file mode 100644 index 2e8816a..0000000 --- a/gear/graph/types.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { Patp } from '../lib'; -import { BigIntOrderedMap } from '../lib/BigIntOrderedMap'; -import { BigIntArrayOrderedMap } from '../lib/BigIntArrayOrderedMap'; - -export interface TextContent { - text: string; -} -export interface UrlContent { - url: string; -} -export interface CodeContent { - code: { - expression: string; - output: string[] | undefined; - } -} - -export interface ReferenceContent { - reference: AppReference | GraphReference | GroupReference; -} - -export interface GraphReference { - graph: { - graph: string; - group: string; - index: string; - } -} - -export interface GroupReference { - group: string; -} - -export interface AppReference { - app: { - ship: string; - desk: string; - path: string; - } -} - -export interface MentionContent { - mention: string; - emphasis?: 'bold' | 'italic'; -} -export type Content = - | TextContent - | UrlContent - | CodeContent - | ReferenceContent - | MentionContent; - -export interface Post { - author: Patp; - contents: Content[]; - hash: string | null; - index: string; - pending?: boolean; - signatures: string[]; - 'time-sent': number; -} - -export interface GraphNodePoke { - post: Post; - children: GraphChildrenPoke | null; -} - -export interface GraphChildrenPoke { - [k: string]: GraphNodePoke; -} - -export interface GraphNode { - children: Graph | null; - post: Post; -} - -export interface FlatGraphNode { - children: null; - post: Post; -} - -export type Graph = BigIntOrderedMap; - -export type Graphs = { [rid: string]: Graph }; - -export type FlatGraph = BigIntArrayOrderedMap; - -export type FlatGraphs = { [rid: string]: FlatGraph }; - -export type ThreadGraphs = { - [rid: string]: { - [index: string]: FlatGraph; - } -}; diff --git a/gear/lib/BigIntArrayOrderedMap.ts b/gear/lib/BigIntArrayOrderedMap.ts deleted file mode 100644 index 79e998c..0000000 --- a/gear/lib/BigIntArrayOrderedMap.ts +++ /dev/null @@ -1,152 +0,0 @@ -import produce, { immerable, castDraft, setAutoFreeze, enablePatches } from 'immer'; -import bigInt, { BigInteger } from 'big-integer'; - -setAutoFreeze(false); - -enablePatches(); - -export function stringToArr(str: string) { - return str.split('/').slice(1).map((ind) => { - return bigInt(ind); - }); -} - -export function arrToString(arr: BigInteger[]) { - let string = ''; - arr.forEach((key) => { - string = string + `/${key.toString()}`; - }); - return string; -} - -function sorted(a: BigInteger[], b: BigInteger[], reversed = false) { - const getSort = sortBigIntArr(a, b); - if (reversed) { - return getSort * -1; - } else { - return getSort; - } -} - -export function sortBigIntArr(a: BigInteger[], b: BigInteger[]) { - const aLen = a.length; - const bLen = b.length; - - const aCop = a.slice(0); - const bCop = b.slice(0); - aCop.reverse(); - bCop.reverse(); - - let i = 0; - while (i < aLen && i < bLen) { - if (aCop[i].lt(bCop[i])) { - return 1; - } else if (aCop[i].gt(bCop[i])) { - return -1; - } else { - i++; - } - } - - return bLen - aLen; -} - -export class BigIntArrayOrderedMap implements Iterable<[BigInteger[], V]> { - root: Record = {} - cachedIter: [BigInteger[], V][] | null = null; - [immerable] = true; - reversed = false; - - constructor(items: [BigInteger[], V][] = [], reversed = false) { - items.forEach(([key, val]) => { - this.set(key, val); - }); - this.reversed = reversed; - } - - get size() { - return Object.keys(this.root).length; - } - - get(key: BigInteger[]) { - return this.root[arrToString(key)] ?? null; - } - - gas(items: [BigInteger[], V][]) { - return produce(this, (draft) => { - items.forEach(([key, value]) => { - draft.root[arrToString(key)] = castDraft(value); - }); - draft.generateCachedIter(); - }, - (patches) => { - // console.log(`gassed with ${JSON.stringify(patches, null, 2)}`); - }); - } - - set(key: BigInteger[], value: V) { - return produce(this, (draft) => { - draft.root[arrToString(key)] = castDraft(value); - draft.cachedIter = null; - }); - } - - clear() { - return produce(this, (draft) => { - draft.cachedIter = []; - draft.root = {}; - }); - } - - has(key: BigInteger[]) { - return arrToString(key) in this.root; - } - - delete(key: BigInteger[]) { - const result = produce(this, (draft) => { - delete draft.root[arrToString(key)]; - draft.cachedIter = null; - }); - return result; - } - - [Symbol.iterator](): IterableIterator<[BigInteger[], V]> { - let idx = 0; - const result = this.generateCachedIter(); - return { - [Symbol.iterator]: this[Symbol.iterator], - next: (): IteratorResult<[BigInteger[], V]> => { - if (idx < result.length) { - return { value: result[idx++], done: false }; - } - return { done: true, value: null }; - } - }; - } - - peekLargest() { - const sorted = Array.from(this); - return sorted[0] as [BigInteger[], V] | null; - } - - peekSmallest() { - const sorted = Array.from(this); - return sorted[sorted.length - 1] as [BigInteger[], V] | null; - } - - keys() { - return Array.from(this).map(([k,v]) => k); - } - - generateCachedIter() { - if(this.cachedIter) { - return [...this.cachedIter]; - } - const result = Object.keys(this.root).map((key) => { - return [stringToArr(key), this.root[key]] as [BigInteger[], V]; - }).sort(([a], [b]) => sorted(a, b, this.reversed)); - this.cachedIter = result; - return [...result]; - } -} - diff --git a/gear/lib/BigIntOrderedMap.ts b/gear/lib/BigIntOrderedMap.ts deleted file mode 100644 index 5e3661d..0000000 --- a/gear/lib/BigIntOrderedMap.ts +++ /dev/null @@ -1,117 +0,0 @@ -import produce, { immerable, castDraft, setAutoFreeze, enablePatches } from 'immer'; -import bigInt, { BigInteger } from 'big-integer'; - -setAutoFreeze(false); - -enablePatches(); - -function sortBigInt(a: BigInteger, b: BigInteger) { - if (a.lt(b)) { - return 1; - } else if (a.eq(b)) { - return 0; - } else { - return -1; - } -} -export class BigIntOrderedMap implements Iterable<[BigInteger, V]> { - root: Record = {} - cachedIter: [BigInteger, V][] | null = null; - [immerable] = true; - - constructor(items: [BigInteger, V][] = []) { - items.forEach(([key, val]) => { - this.set(key, val); - }); - } - - get size() { - if(this.cachedIter) { - return this.cachedIter.length; - } - return this.generateCachedIter().length; - } - - get(key: BigInteger) { - return this.root[key.toString()] ?? null; - } - - gas(items: [BigInteger, V][]) { - return produce(this, (draft) => { - items.forEach(([key, value]) => { - draft.root[key.toString()] = castDraft(value); - }); - draft.cachedIter = null; - }, - (patches) => { - // console.log(`gassed with ${JSON.stringify(patches, null, 2)}`); - }); - } - - set(key: BigInteger, value: V) { - return produce(this, (draft) => { - draft.root[key.toString()] = castDraft(value); - draft.cachedIter = null; - }); - } - - clear() { - return produce(this, (draft) => { - draft.cachedIter = []; - draft.root = {}; - }); - } - - has(key: BigInteger) { - return key.toString() in this.root; - } - - delete(key: BigInteger) { - const result = produce(this, (draft) => { - delete draft.root[key.toString()]; - draft.cachedIter = null; - }); - return result; - } - - [Symbol.iterator](): IterableIterator<[BigInteger, V]> { - let idx = 0; - const result = this.generateCachedIter(); - return { - [Symbol.iterator]: this[Symbol.iterator], - next: (): IteratorResult<[BigInteger, V]> => { - if (idx < result.length) { - return { value: result[idx++], done: false }; - } - return { done: true, value: null }; - } - }; - } - - peekLargest() { - const sorted = Array.from(this); - return sorted[0] as [BigInteger, V] | null; - } - - peekSmallest() { - const sorted = Array.from(this); - return sorted[sorted.length - 1] as [BigInteger, V] | null; - } - - keys() { - return Array.from(this).map(([k,v]) => k); - } - - generateCachedIter() { - if(this.cachedIter) { - return [...this.cachedIter]; - } - const result = Object.keys(this.root).map((key) => { - const num = bigInt(key); - return [num, this.root[key]] as [BigInteger, V]; - }).sort(([a], [b]) => sortBigInt(a,b)); - this.cachedIter = result; - return [...result]; - } -} - diff --git a/gear/lib/index.ts b/gear/lib/index.ts deleted file mode 100644 index 4fed660..0000000 --- a/gear/lib/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './lib'; -export * from './types'; \ No newline at end of file diff --git a/gear/lib/lib.ts b/gear/lib/lib.ts deleted file mode 100644 index c57d0a2..0000000 --- a/gear/lib/lib.ts +++ /dev/null @@ -1,259 +0,0 @@ -import bigInt, { BigInteger } from "big-integer"; - -import { Resource } from "../groups/types"; -import { Post, GraphNode } from "../graph/types"; - -const DA_UNIX_EPOCH = bigInt("170141184475152167957503069145530368000"); // `@ud` ~1970.1.1 - -const DA_SECOND = bigInt("18446744073709551616"); // `@ud` ~s1 - -function chunk(arr: T[], size: number): T[][] { - let chunk: T[] = []; - let newArray = [chunk]; - - for (let i = 0;i < arr.length;i++) { - if (chunk.length < size) { - chunk.push(arr[i]) - } else { - chunk = [arr[i]] - newArray.push(chunk) - } - } - - return newArray; -} - -function dropWhile(arr: T[], pred: (x: T) => boolean): T[] { - const newArray = arr.slice(); - - for (const item of arr) { - if (pred(item)) { - newArray.shift(); - } else { - return newArray; - } - } - - return newArray; -} - -/** - * Given a bigint representing an urbit date, returns a unix timestamp. - * - * @param {BigInteger} da The urbit date - * - * @return {number} The unix timestamp - */ -export function daToUnix(da: BigInteger): number { - // ported from +time:enjs:format in hoon.hoon - const offset = DA_SECOND.divide(bigInt(2000)); - const epochAdjusted = offset.add(da.subtract(DA_UNIX_EPOCH)); - - return Math.round( - epochAdjusted.multiply(bigInt(1000)).divide(DA_SECOND).toJSNumber() - ); -} - -/** - * Given a unix timestamp, returns a bigint representing an urbit date - * - * @param {number} unix The unix timestamp - * - * @return {BigInteger} The urbit date - */ -export function unixToDa(unix: number): BigInteger { - const timeSinceEpoch = bigInt(unix).multiply(DA_SECOND).divide(bigInt(1000)); - return DA_UNIX_EPOCH.add(timeSinceEpoch); -} - - -export function makePatDa(patda: string): BigInteger { - return bigInt(udToDec(patda)); -} - -export function udToDec(ud: string): string { - return ud.replace(/\./g, ""); -} - -export function decToUd(str: string): string { - const transform = chunk(str.split('').reverse(), 3) - .map(group => group.reverse().join('')) - .reverse() - .join('.') - return transform.replace(/^[0\.]+/g, ''); -} - -export function resourceAsPath(resource: Resource): string { - const { name, ship } = resource; - return `/ship/~${ship}/${name}`; -} - -export function uuid(): string { - let str = "0v"; - str += Math.ceil(Math.random() * 8) + "."; - for (let i = 0; i < 5; i++) { - let _str = Math.ceil(Math.random() * 10000000).toString(32); - _str = ("00000" + _str).substr(-5, 5); - str += _str + "."; - } - - return str.slice(0, -1); -} - -/* - Goes from: - ~2018.7.17..23.15.09..5be5 // urbit @da - To: - (javascript Date object) -*/ -export function daToDate(st: string): Date { - const dub = function (n: string) { - return parseInt(n) < 10 ? "0" + parseInt(n) : n.toString(); - }; - const da = st.split(".."); - const bigEnd = da[0].split("."); - const lilEnd = da[1].split("."); - const ds = `${bigEnd[0].slice(1)}-${dub(bigEnd[1])}-${dub(bigEnd[2])}T${dub( - lilEnd[0] - )}:${dub(lilEnd[1])}:${dub(lilEnd[2])}Z`; - return new Date(ds); -} - -/* - Goes from: - (javascript Date object) - To: - ~2018.7.17..23.15.09..5be5 // urbit @da -*/ - -export function dateToDa(d: Date, mil: boolean = false): string { - const fil = function (n: number) { - return n >= 10 ? n : "0" + n; - }; - return ( - `~${d.getUTCFullYear()}.` + - `${d.getUTCMonth() + 1}.` + - `${fil(d.getUTCDate())}..` + - `${fil(d.getUTCHours())}.` + - `${fil(d.getUTCMinutes())}.` + - `${fil(d.getUTCSeconds())}` + - `${mil ? "..0000" : ""}` - ); -} - -export function preSig(ship: string): string { - if (!ship) { - return ''; - } - - if (ship.trim().startsWith('~')) { - return ship.trim(); - } - - return '~'.concat(ship.trim()); -} - -export function deSig(ship: string): string | null { - if (!ship) { - return null; - } - return ship.replace("~", ""); -} - -// trim patps to match dojo, chat-cli -export function cite(ship: string) { - let patp = ship, - shortened = ''; - if (patp === null || patp === '') { - return null; - } - if (patp.startsWith('~')) { - patp = patp.substr(1); - } - // comet - if (patp.length === 56) { - shortened = '~' + patp.slice(0, 6) + '_' + patp.slice(50, 56); - return shortened; - } - // moon - if (patp.length === 27) { - shortened = '~' + patp.slice(14, 20) + '^' + patp.slice(21, 27); - return shortened; - } - return `~${patp}`; -} - - -export function uxToHex(ux: string) { - if (ux.length > 2 && ux.substr(0, 2) === '0x') { - const value = ux.substr(2).replace('.', '').padStart(6, '0'); - return value; - } - - const value = ux.replace('.', '').padStart(6, '0'); - return value; -} - -export const hexToUx = (hex: string): string => { - const nonZeroChars = dropWhile(hex.split(''), y => y === '0'); - const ux = chunk(nonZeroChars.reverse(), 4).map(x => { - return x.reverse().join(''); - }).reverse().join('.') || '0'; - - return `0x${ux}`; -}; - - -// encode the string into @ta-safe format, using logic from +wood. -// for example, 'some Chars!' becomes '~.some.~43.hars~21.' -// -export function stringToTa(str: string): string { - let out = ""; - for (let i = 0; i < str.length; i++) { - const char = str[i]; - let add = ""; - switch (char) { - case " ": - add = "."; - break; - case ".": - add = "~."; - break; - case "~": - add = "~~"; - break; - default: - const charCode = str.charCodeAt(i); - if ( - (charCode >= 97 && charCode <= 122) || // a-z - (charCode >= 48 && charCode <= 57) || // 0-9 - char === "-" - ) { - add = char; - } else { - // TODO behavior for unicode doesn't match +wood's, - // but we can probably get away with that for now. - add = "~" + charCode.toString(16) + "."; - } - } - out = out + add; - } - return "~." + out; -} - -export const buntPost = (): Post => ({ - author: '', - contents: [], - hash: null, - index: '', - signatures: [], - 'time-sent': 0 -}); - -export function makeNodeMap(posts: Post[]): Record { - const nodes: Record = {}; - posts.forEach((p: Post) => { - nodes[String(p.index)] = { children: null, post: p }; - }); - return nodes; -} diff --git a/gear/lib/types.ts b/gear/lib/types.ts deleted file mode 100644 index 4c701ea..0000000 --- a/gear/lib/types.ts +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Martian embassy - */ - -import { BigIntOrderedMap } from "./BigIntOrderedMap"; - -// an urbit style path rendered as string -export type Path = string; - -// patp including leading sig -export type Patp = string; - -// patp excluding leading sig -export type PatpNoSig = string; - -// @uvH encoded string -export type Serial = string; - -// jug from hoon -export type Jug = Map>; - -// name of app -export type AppName = 'chat' | 'link' | 'contacts' | 'publish' | 'graph' | 'groups'; - -export type ShipRank = 'czar' | 'king' | 'duke' | 'earl' | 'pawn'; - -export type Action = 'poke' | 'subscribe' | 'ack' | 'unsubscribe' | 'delete'; - - -export type SetElement = S extends Set<(infer T)> ? T : never; -export type MapKey = M extends Map<(infer K), any> ? K : never; -export type MapValue = M extends Map ? V : never; - -/** - * Turns sets into arrays and maps into objects so we can send them over the wire - */ -export type Enc = - S extends Set ? - Enc>[] : - S extends Map ? - { [s: string]: Enc> } : - S extends object ? - { [K in keyof S]: Enc } : - S extends BigIntOrderedMap ? - { [index: string]: T } : - S; - -export type Mark = string; - -export interface Poke { - ship?: string; // This should be handled by the http library, but is part of the spec - app: string; - mark: Mark; - json: Action; -} - -export interface Scry { - app: string; - path: string; -} - -export interface Thread { - inputMark: string; - outputMark: string; - threadName: string; - body: Action; -} diff --git a/gear/storage/index.ts b/gear/storage/index.ts deleted file mode 100644 index 4fed660..0000000 --- a/gear/storage/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './lib'; -export * from './types'; \ No newline at end of file diff --git a/gear/contacts/index.ts b/ui/gear/contacts/index.ts similarity index 100% rename from gear/contacts/index.ts rename to ui/gear/contacts/index.ts diff --git a/gear/contacts/lib.ts b/ui/gear/contacts/lib.ts similarity index 100% rename from gear/contacts/lib.ts rename to ui/gear/contacts/lib.ts diff --git a/gear/contacts/types.ts b/ui/gear/contacts/types.ts similarity index 100% rename from gear/contacts/types.ts rename to ui/gear/contacts/types.ts diff --git a/gear/deps.d.ts b/ui/gear/deps.d.ts similarity index 100% rename from gear/deps.d.ts rename to ui/gear/deps.d.ts diff --git a/gear/docket/index.ts b/ui/gear/docket/index.ts similarity index 100% rename from gear/docket/index.ts rename to ui/gear/docket/index.ts diff --git a/gear/docket/lib.ts b/ui/gear/docket/lib.ts similarity index 100% rename from gear/docket/lib.ts rename to ui/gear/docket/lib.ts diff --git a/gear/docket/types.ts b/ui/gear/docket/types.ts similarity index 100% rename from gear/docket/types.ts rename to ui/gear/docket/types.ts diff --git a/gear/groups/index.ts b/ui/gear/groups/index.ts similarity index 100% rename from gear/groups/index.ts rename to ui/gear/groups/index.ts diff --git a/gear/groups/lib.ts b/ui/gear/groups/lib.ts similarity index 100% rename from gear/groups/lib.ts rename to ui/gear/groups/lib.ts diff --git a/gear/groups/types.ts b/ui/gear/groups/types.ts similarity index 100% rename from gear/groups/types.ts rename to ui/gear/groups/types.ts diff --git a/gear/groups/update.ts b/ui/gear/groups/update.ts similarity index 100% rename from gear/groups/update.ts rename to ui/gear/groups/update.ts diff --git a/gear/groups/view.ts b/ui/gear/groups/view.ts similarity index 100% rename from gear/groups/view.ts rename to ui/gear/groups/view.ts diff --git a/gear/hark/index.ts b/ui/gear/hark/index.ts similarity index 100% rename from gear/hark/index.ts rename to ui/gear/hark/index.ts diff --git a/gear/hark/lib.ts b/ui/gear/hark/lib.ts similarity index 100% rename from gear/hark/lib.ts rename to ui/gear/hark/lib.ts diff --git a/gear/hark/types.ts b/ui/gear/hark/types.ts similarity index 100% rename from gear/hark/types.ts rename to ui/gear/hark/types.ts diff --git a/gear/hood/index.ts b/ui/gear/hood/index.ts similarity index 100% rename from gear/hood/index.ts rename to ui/gear/hood/index.ts diff --git a/gear/hood/lib.ts b/ui/gear/hood/lib.ts similarity index 100% rename from gear/hood/lib.ts rename to ui/gear/hood/lib.ts diff --git a/gear/hood/types.ts b/ui/gear/hood/types.ts similarity index 100% rename from gear/hood/types.ts rename to ui/gear/hood/types.ts diff --git a/gear/index.ts b/ui/gear/index.ts similarity index 87% rename from gear/index.ts rename to ui/gear/index.ts index d3f28be..6111e0c 100644 --- a/gear/index.ts +++ b/ui/gear/index.ts @@ -1,7 +1,5 @@ export * from './contacts'; export * as contacts from './contacts'; -export * from './graph'; -export * as graph from './graph'; export * from './groups'; export * as groups from './groups'; export * from './hark'; @@ -13,8 +11,8 @@ export * from './metadata'; export * as metadata from './metadata'; export * from './settings'; export * as settings from './settings'; -export * from './s3'; -export * as s3 from './s3'; +export * from './storage'; +export * as storage from './storage'; export * from './lib'; export * from './lib/BigIntOrderedMap'; export * from './lib/BigIntArrayOrderedMap'; diff --git a/gear/invite/index.ts b/ui/gear/invite/index.ts similarity index 100% rename from gear/invite/index.ts rename to ui/gear/invite/index.ts diff --git a/gear/invite/lib.ts b/ui/gear/invite/lib.ts similarity index 100% rename from gear/invite/lib.ts rename to ui/gear/invite/lib.ts diff --git a/gear/invite/types.ts b/ui/gear/invite/types.ts similarity index 100% rename from gear/invite/types.ts rename to ui/gear/invite/types.ts diff --git a/gear/metadata/index.ts b/ui/gear/metadata/index.ts similarity index 100% rename from gear/metadata/index.ts rename to ui/gear/metadata/index.ts diff --git a/gear/metadata/lib.ts b/ui/gear/metadata/lib.ts similarity index 100% rename from gear/metadata/lib.ts rename to ui/gear/metadata/lib.ts diff --git a/gear/metadata/types.ts b/ui/gear/metadata/types.ts similarity index 100% rename from gear/metadata/types.ts rename to ui/gear/metadata/types.ts diff --git a/gear/settings/index.ts b/ui/gear/settings/index.ts similarity index 100% rename from gear/settings/index.ts rename to ui/gear/settings/index.ts diff --git a/gear/settings/lib.ts b/ui/gear/settings/lib.ts similarity index 100% rename from gear/settings/lib.ts rename to ui/gear/settings/lib.ts diff --git a/gear/settings/types.ts b/ui/gear/settings/types.ts similarity index 100% rename from gear/settings/types.ts rename to ui/gear/settings/types.ts diff --git a/gear/graph/index.ts b/ui/gear/storage/index.ts similarity index 100% rename from gear/graph/index.ts rename to ui/gear/storage/index.ts diff --git a/gear/storage/lib.ts b/ui/gear/storage/lib.ts similarity index 100% rename from gear/storage/lib.ts rename to ui/gear/storage/lib.ts diff --git a/gear/storage/types.ts b/ui/gear/storage/types.ts similarity index 100% rename from gear/storage/types.ts rename to ui/gear/storage/types.ts diff --git a/ui/package-lock.json b/ui/package-lock.json index c51524a..5fbc4d4 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -25,7 +25,6 @@ "@tlon/sigil-js": "^1.4.4", "@tloncorp/mock-http-api": "^1.2.0", "@types/lodash": "^4.14.172", - "@urbit/api": "^2.2.0", "@urbit/http-api": "^2.4.5-debug", "big-integer": "^1.6.48", "browser-cookies": "^1.2.0", @@ -2922,18 +2921,6 @@ "url": "https://opencollective.com/typescript-eslint" } }, - "node_modules/@urbit/api": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@urbit/api/-/api-2.2.0.tgz", - "integrity": "sha512-W8kP9OT6yOK62n+4yCPO3i9QqU5xriLvZQ9WYW4SAV7ktbSrGuf2kmYbnoqfA/NybIs9Q/MbFkPewrz4XJ96Ag==", - "dependencies": { - "@babel/runtime": "^7.16.0", - "big-integer": "^1.6.48", - "core-js": "^3.19.1", - "immer": "^9.0.1", - "urbit-ob": "^5.0.1" - } - }, "node_modules/@urbit/http-api": { "version": "2.4.5-debug", "resolved": "https://registry.npmjs.org/@urbit/http-api/-/http-api-2.4.5-debug.tgz", @@ -11349,18 +11336,6 @@ "eslint-visitor-keys": "^2.0.0" } }, - "@urbit/api": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@urbit/api/-/api-2.2.0.tgz", - "integrity": "sha512-W8kP9OT6yOK62n+4yCPO3i9QqU5xriLvZQ9WYW4SAV7ktbSrGuf2kmYbnoqfA/NybIs9Q/MbFkPewrz4XJ96Ag==", - "requires": { - "@babel/runtime": "^7.16.0", - "big-integer": "^1.6.48", - "core-js": "^3.19.1", - "immer": "^9.0.1", - "urbit-ob": "^5.0.1" - } - }, "@urbit/http-api": { "version": "2.4.5-debug", "resolved": "https://registry.npmjs.org/@urbit/http-api/-/http-api-2.4.5-debug.tgz", diff --git a/ui/package.json b/ui/package.json index 4979156..ed00745 100644 --- a/ui/package.json +++ b/ui/package.json @@ -35,7 +35,6 @@ "@tlon/sigil-js": "^1.4.4", "@tloncorp/mock-http-api": "^1.2.0", "@types/lodash": "^4.14.172", - "@urbit/api": "^2.2.0", "@urbit/http-api": "^2.4.5-debug", "big-integer": "^1.6.48", "browser-cookies": "^1.2.0", diff --git a/ui/src/state/storage/index.ts b/ui/src/state/storage/index.ts index 430ee8c..8d649d2 100644 --- a/ui/src/state/storage/index.ts +++ b/ui/src/state/storage/index.ts @@ -7,7 +7,7 @@ import { reduceStateN, BaseState, } from '../base'; -import { S3Credentials } from '@urbit/api'; +import { StorageCredentials } from '../../../'; enableMapSet(); @@ -22,7 +22,7 @@ export interface BaseStorageState { currentBucket: string; region: string; }; - credentials: S3Credentials | null; + credentials: StorageCredentials | null; }; [ref: string]: unknown; }