From ad035b54f1fa58d09acc4741e331957821a20526 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Wed, 17 Feb 2021 12:23:49 +1000 Subject: [PATCH] urbit/api: bring inline with current userspace --- pkg/npm/api/contacts/index.d.ts | 77 +++++++++++------------ pkg/npm/api/contacts/index.ts | 107 +++++++++++--------------------- pkg/npm/api/graph/index.d.ts | 9 +++ pkg/npm/api/graph/index.ts | 12 ++-- pkg/npm/api/groups/index.ts | 8 ++- pkg/npm/api/groups/update.d.ts | 7 +-- pkg/npm/api/hark/index.d.ts | 2 +- pkg/npm/api/invite/index.d.ts | 19 ++++-- pkg/npm/api/invite/index.ts | 10 +-- pkg/npm/api/lib/index.d.ts | 15 ++--- pkg/npm/api/lib/util.ts | 4 +- pkg/npm/api/metadata/index.d.ts | 16 +++-- pkg/npm/api/metadata/index.ts | 36 ++++------- pkg/npm/api/tsconfig.json | 6 +- 14 files changed, 147 insertions(+), 181 deletions(-) diff --git a/pkg/npm/api/contacts/index.d.ts b/pkg/npm/api/contacts/index.d.ts index d29331be8..e03e1f440 100644 --- a/pkg/npm/api/contacts/index.d.ts +++ b/pkg/npm/api/contacts/index.d.ts @@ -1,25 +1,14 @@ import { Path, Patp } from ".."; +import {Resource} from "groups/update"; export type ContactUpdate = - | ContactUpdateCreate - | ContactUpdateDelete | ContactUpdateAdd | ContactUpdateRemove | ContactUpdateEdit | ContactUpdateInitial - | ContactUpdateContacts; - -interface ContactUpdateCreate { - create: Path; -} - -interface ContactUpdateDelete { - delete: Path; -} interface ContactUpdateAdd { add: { - path: Path; ship: Patp; contact: Contact; }; @@ -27,7 +16,6 @@ interface ContactUpdateAdd { interface ContactUpdateRemove { remove: { - path: Path; ship: Patp; }; } @@ -36,50 +24,55 @@ interface ContactUpdateEdit { edit: { path: Path; ship: Patp; - "edit-field": ContactEdit; + "edit-field": ContactEditField; + timestamp: number; }; } +interface ContactUpdateAllowShips { + allow: { + ships: Patp[]; + } +} + +interface ContactUpdateAllowGroup { + allow: { + group: Path; + } +} + +interface ContactUpdateSetPublic { + 'set-public': boolean; +} + +export interface ContactShare { + share: Patp; +} + interface ContactUpdateInitial { initial: Rolodex; } -interface ContactUpdateContacts { - contacts: { - path: Path; - contacts: Contacts; - }; -} - -// - -type ContactAvatar = ContactAvatarUrl | ContactAvatarOcts; - export type Rolodex = { - [p in Path]: Contacts; -}; - -export type Contacts = { [p in Patp]: Contact; }; -interface ContactAvatarUrl { - url: string; -} - -interface ContactAvatarOcts { - octs: string; -} export interface Contact { nickname: string; - email: string; - phone: string; - website: string; - notes: string; + bio: string; + status: string; color: string; avatar: string | null; + cover: string | null; + groups: Path[]; + 'last-updated': number; } -export type ContactEdit = { - [k in keyof Contact]: Contact[k]; +type ContactKeys = keyof Contact; + +export type ContactEditFieldPrim = Exclude; + +export type ContactEditField = Partial> & { + 'add-group'?: Resource; + 'remove-group'?: Resource; }; diff --git a/pkg/npm/api/contacts/index.ts b/pkg/npm/api/contacts/index.ts index f952fffaa..ca5ba9d01 100644 --- a/pkg/npm/api/contacts/index.ts +++ b/pkg/npm/api/contacts/index.ts @@ -1,83 +1,50 @@ import { Enc, Path, Patp, Poke } from ".."; -import { Contact, ContactEdit, ContactUpdateCreate, ContactUpdateEdit, ContactUpdateRemove } from "./index.d"; -import { GroupPolicy, Resource } from "../groups/index.d" +import { + Contact, + ContactUpdateAdd, + ContactUpdateEdit, + ContactUpdateRemove, + ContactEditField, + ContactShare, + ContactUpdate, +} from "./index.d"; -export const viewAction = (data: T): Poke => ({ - app: 'contact-view', - mark: 'json', - json: data +export const storeAction = (data: T): Poke => ({ + app: "contact-store", + mark: "contact-action", + json: data, }); -export const hookAction = (data: T): Poke => ({ - app: 'contact-hook', - mark: 'contact-action', - json: data -}); +export const add = (ship: Patp, contact: Contact): Poke => { + contact["last-updated"] = Date.now(); -export const create = ( - name: string, - policy: Enc, - title: string, - description: string -): Poke => viewAction({ // TODO which type is correct? - create: { - name, - policy, - title, - description - } -}); + return storeAction({ + add: { ship, contact }, + }); +}; -export const share = ( - recipient: Patp, - path: Patp, - ship: Patp, - contact: Contact -): Poke => viewAction({ // TODO type - share: { - recipient, - path, - ship, - contact - } -}); +export const remove = (ship: Patp): Poke => + storeAction({ + remove: { ship }, + }); -export const remove = ( - path: Path, - ship: Patp -): Poke => viewAction({ - remove: { - path, - ship - } +export const share = (recipient: Patp): Poke => ({ + app: "contact-push-hook", + mark: "contact-action", + json: { share: recipient }, }); export const edit = ( path: Path, ship: Patp, - editField: ContactEdit -): Poke => hookAction({ - edit: { - path, - ship, - 'edit-field': editField - } -}); + editField: ContactEditField +): Poke => + storeAction({ + edit: { + path, + ship, + "edit-field": editField, + timestamp: Date.now(), + }, + }); -export const invite = ( - resource: Resource, - ship: Patp, - text: string = '' -): Poke => viewAction({ // TODO type - invite: { - resource, - ship, - text - } -}); - -export const join = ( - resource: Resource -): Poke => viewAction({ // TODO type - join: resource -}); \ No newline at end of file diff --git a/pkg/npm/api/graph/index.d.ts b/pkg/npm/api/graph/index.d.ts index 17abee3cb..b98872e81 100644 --- a/pkg/npm/api/graph/index.d.ts +++ b/pkg/npm/api/graph/index.d.ts @@ -37,6 +37,15 @@ export interface Post { "time-sent": number; } +export interface GraphNodePoke { + post: Post; + children: GraphChildrenPoke | null; +} + +export interface GraphChildrenPoke { + [k: string]: GraphNodePoke; +} + export interface GraphNode { children: Graph; post: Post; diff --git a/pkg/npm/api/graph/index.ts b/pkg/npm/api/graph/index.ts index a12edb651..82d4cc986 100644 --- a/pkg/npm/api/graph/index.ts +++ b/pkg/npm/api/graph/index.ts @@ -1,20 +1,20 @@ import _ from 'lodash'; import { PatpNoSig, Patp, Poke, Thread, Path, Enc } from '..'; -import { Content, GraphNode, Post } from './index.d'; +import { Content, GraphNode, Post, GraphNodePoke, GraphChildrenPoke } from './index.d'; import { deSig, unixToDa } from '../lib/util'; import { makeResource, resourceFromPath } from '../groups/index'; -import { GroupPolicy } from '../groups'; +import { GroupPolicy } from '../groups/update.d'; export const createBlankNodeWithChildPost = ( ship: PatpNoSig, parentIndex: string = '', childIndex: string = '', contents: Content[] -): GraphNode => { +): GraphNodePoke => { const date = unixToDa(Date.now()).toString(); const nodeIndex = parentIndex + '/' + date; - const childGraph = {}; + const childGraph: GraphChildrenPoke = {}; childGraph[childIndex] = { post: { author: `~${ship}`, @@ -253,7 +253,7 @@ export const addNode = ( let nodes = {}; nodes[node.post.index] = node; - return this.addNodes(ship, name, nodes); + return addNodes(ship, name, nodes); } export const addNodes = ( @@ -367,4 +367,4 @@ export const removeNodes = ( // data: node // }); // }); -// } \ No newline at end of file +// } diff --git a/pkg/npm/api/groups/index.ts b/pkg/npm/api/groups/index.ts index 8679c6d7e..02122d3e5 100644 --- a/pkg/npm/api/groups/index.ts +++ b/pkg/npm/api/groups/index.ts @@ -12,6 +12,7 @@ import { Resource, Tag } from "./index.d"; +import { GroupPolicy } from "./update"; export const proxyAction = (data: T): Poke => ({ app: 'group-push-hook', @@ -106,6 +107,11 @@ export function makeResource(ship: string, name:string) { return { ship, name }; } +export const groupBunts = { + group: (): Group => ({ members: new Set(), tags: { role: {} }, hidden: false, policy: groupBunts.policy() }), + policy: (): GroupPolicy => ({ open: { banned: new Set(), banRanks: new Set() } }) +}; + export const joinError = ['no-perms', 'strange'] as const; export const joinResult = ['done', ...joinError] as const; -export const joinProgress = ['start', 'added', ...joinResult] as const; \ No newline at end of file +export const joinProgress = ['start', 'added', ...joinResult] as const; diff --git a/pkg/npm/api/groups/update.d.ts b/pkg/npm/api/groups/update.d.ts index 7788fdd83..00d3ac340 100644 --- a/pkg/npm/api/groups/update.d.ts +++ b/pkg/npm/api/groups/update.d.ts @@ -133,7 +133,7 @@ interface GroupUpdateRemoveTag { removeTag: { tag: Tag; resource: Resource; - ships: PatpNoSig; + ships: PatpNoSig[]; }; } @@ -174,7 +174,4 @@ export type GroupUpdate = export type GroupAction = Omit; -export const groupBunts = { - group: (): Group => ({ members: new Set(), tags: { role: {} }, hidden: false, policy: groupBunts.policy() }), - policy: (): GroupPolicy => ({ open: { banned: new Set(), banRanks: new Set() } }) -}; + diff --git a/pkg/npm/api/hark/index.d.ts b/pkg/npm/api/hark/index.d.ts index 1aee7bab7..eaa92db40 100644 --- a/pkg/npm/api/hark/index.d.ts +++ b/pkg/npm/api/hark/index.d.ts @@ -1,6 +1,6 @@ import { Post } from "../graph/index.d"; import { GroupUpdate } from "../groups/index.d"; -import { BigIntOrderedMap } from "~/logic/lib/BigIntOrderedMap"; +import BigIntOrderedMap from "../lib/BigIntOrderedMap"; export type GraphNotifDescription = "link" | "comment" | "note" | "mention"; diff --git a/pkg/npm/api/invite/index.d.ts b/pkg/npm/api/invite/index.d.ts index 01f080ef5..05b43ce8a 100644 --- a/pkg/npm/api/invite/index.d.ts +++ b/pkg/npm/api/invite/index.d.ts @@ -1,14 +1,21 @@ import { Serial, PatpNoSig, Path } from '..'; -import { Resource } from '../groups'; +import { Resource } from "../groups/update.d"; export type InviteUpdate = InviteUpdateInitial | InviteUpdateCreate | InviteUpdateDelete | InviteUpdateInvite +| InviteUpdateAccept | InviteUpdateAccepted | InviteUpdateDecline; +interface InviteUpdateAccept { + accept: { + term: string; + uid: Serial; + } +} interface InviteUpdateInitial { initial: Invites; @@ -16,19 +23,19 @@ interface InviteUpdateInitial { interface InviteUpdateCreate { create: { - path: Path; + term: string; }; } interface InviteUpdateDelete { delete: { - path: Path; + term: string; }; } interface InviteUpdateInvite { invite: { - path: Path; + term: string; uid: Serial; invite: Invite; }; @@ -36,14 +43,14 @@ interface InviteUpdateInvite { interface InviteUpdateAccepted { accepted: { - path: Path; + term: string; uid: Serial; }; } interface InviteUpdateDecline { decline: { - path: Path; + term: string; uid: Serial; }; } diff --git a/pkg/npm/api/invite/index.ts b/pkg/npm/api/invite/index.ts index 496007384..51e6ab5fb 100644 --- a/pkg/npm/api/invite/index.ts +++ b/pkg/npm/api/invite/index.ts @@ -1,7 +1,7 @@ -import { InviteAction, InviteActionAccept, InviteActionDecline } from "./index.d"; +import { InviteUpdate, InviteUpdateAccept, InviteUpdateDecline } from "./index.d"; import { Poke, Serial } from ".."; -export const action = (data: T): Poke => ({ +export const action = (data: T): Poke => ({ app: 'invite-store', mark: 'invite-action', json: data @@ -10,7 +10,7 @@ export const action = (data: T): Poke => ({ export const accept = ( app: string, uid: Serial -): Poke => action({ +): Poke => action({ accept: { term: app, uid @@ -20,9 +20,9 @@ export const accept = ( export const decline = ( app: string, uid: Serial -): Poke => action({ +): Poke => action({ decline: { term: app, uid } -}); \ No newline at end of file +}); diff --git a/pkg/npm/api/lib/index.d.ts b/pkg/npm/api/lib/index.d.ts index d92065375..f8f94ce66 100644 --- a/pkg/npm/api/lib/index.d.ts +++ b/pkg/npm/api/lib/index.d.ts @@ -2,6 +2,8 @@ * Martian embassy */ +import BigIntOrderedMap from "./BigIntOrderedMap"; + // an urbit style path rendered as string export type Path = string; @@ -20,15 +22,6 @@ export type Jug = Map>; // name of app export type AppName = 'chat' | 'link' | 'contacts' | 'publish' | 'graph'; -export function getTagFromFrond(frond: O): keyof O { - const tags = Object.keys(frond) as Array; - const tag = tags[0]; - if(!tag) { - throw new Error("bad frond"); - } - return tag; -} - export type ShipRank = 'czar' | 'king' | 'duke' | 'earl' | 'pawn'; export type Action = 'poke' | 'subscribe' | 'ack' | 'unsubscribe' | 'delete'; @@ -48,6 +41,8 @@ export type Enc = { [s: string]: Enc> } : S extends object ? { [K in keyof S]: Enc } : + S extends BigIntOrderedMap ? + { [index: string]: T } : S; export type Mark = string; @@ -64,4 +59,4 @@ export interface Thread { outputMark: string; threadName: string; body: Action; -} \ No newline at end of file +} diff --git a/pkg/npm/api/lib/util.ts b/pkg/npm/api/lib/util.ts index 2a6ba8226..c646c3a8a 100644 --- a/pkg/npm/api/lib/util.ts +++ b/pkg/npm/api/lib/util.ts @@ -140,7 +140,7 @@ export function cite(ship: string): string { let patp = ship, shortened = ""; if (patp === null || patp === "") { - return null; + return ""; } if (patp.startsWith("~")) { patp = patp.substr(1); @@ -208,4 +208,4 @@ export function numToUd(num: number): string { f.map(s => s.join('')), f.join('.') )(num.toString()) -} \ No newline at end of file +} diff --git a/pkg/npm/api/metadata/index.d.ts b/pkg/npm/api/metadata/index.d.ts index fca5f1e7b..c0a12075d 100644 --- a/pkg/npm/api/metadata/index.d.ts +++ b/pkg/npm/api/metadata/index.d.ts @@ -15,15 +15,15 @@ type ResourceAssociations = { } type MetadataUpdateAdd = { - add: Association; + add: AssociationPoke; } type MetadataUpdateUpdate = { - update: Association; + update: AssociationPoke; } type MetadataUpdateRemove = { - remove: Resource & { + remove: MdResource & { group: Path; } } @@ -42,16 +42,22 @@ export type AppAssociations = { [p in Path]: Association; } -interface Resource { +interface MdResource { resource: Path; 'app-name': AppName; } -export type Association = Resource & { +export type Association = MdResource & { group: Path; metadata: Metadata; }; +export interface AssociationPoke { + group: Path; + resource: MdResource; + metadata: Metadata; +} + export interface Metadata { color: string; creator: Patp; diff --git a/pkg/npm/api/metadata/index.ts b/pkg/npm/api/metadata/index.ts index 126c48e0c..e880f1c9d 100644 --- a/pkg/npm/api/metadata/index.ts +++ b/pkg/npm/api/metadata/index.ts @@ -8,32 +8,19 @@ export const action = (data: T): Poke => ({ }); export const add = ( - ship: PatpNoSig, appName: AppName, - appPath: Path, - groupPath: Path, - title: string, - description: string, - dateCreated: string, - color: string, - moduleName: string + resource: string, + group: string, + metadata: Metadata, ): Poke => { - const creator = `~${ship}`; return action({ add: { - 'group-path': groupPath, + group, resource: { - 'app-path': appPath, + resource, 'app-name': appName }, - metadata: { - title, - description, - color, - 'date-created': dateCreated, - creator, - 'module': moduleName - } + metadata } }); } @@ -41,15 +28,16 @@ export const add = ( export const update = ( association: Association, newMetadata: Partial -): Poke => { +): Poke => { + const { resource, metadata, group } = association; return action({ add: { - 'group-path': association['group-path'], + group, resource: { - 'app-path': association['app-path'], + resource, 'app-name': association['app-name'], }, - metadata: {...association.metadata, ...newMetadata } + metadata: {...metadata, ...newMetadata } } }); -} \ No newline at end of file +} diff --git a/pkg/npm/api/tsconfig.json b/pkg/npm/api/tsconfig.json index ca6746275..1d5a91a97 100644 --- a/pkg/npm/api/tsconfig.json +++ b/pkg/npm/api/tsconfig.json @@ -19,9 +19,7 @@ } }, "include": [ - "src/**/*" + "**/*" ], - "exclude": [ - "node_modules", - ] + "exclude": [ "node_modules" ] }