Skip to content

Commit

Permalink
gear: import from @urbit/api
Browse files Browse the repository at this point in the history
- copied lib and types from @urbit/api repo to gear/
- migrated webterm types (see: urbit/webterm#3)
- rename S3 --> Storage
  • Loading branch information
tomholford committed May 19, 2023
1 parent 55366c3 commit 9228e89
Show file tree
Hide file tree
Showing 39 changed files with 3,213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gear/contacts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './lib';
109 changes: 109 additions & 0 deletions gear/contacts/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

import { Patp, Poke, Scry } from '../lib';
import {
Contact,
ContactUpdateAdd,
ContactUpdateEdit,
ContactUpdateRemove,
ContactEditField,
ContactShare,
ContactUpdate,
ContactUpdateAllowShips,
ContactUpdateAllowGroup,
ContactUpdateSetPublic
} from './types';

export const CONTACT_UPDATE_VERSION = 0;

const storeAction = <T extends ContactUpdate>(data: T, version: number = CONTACT_UPDATE_VERSION): Poke<T> => ({
app: 'contact-store',
mark: `contact-update-${version}`,
json: data
});

export { storeAction as contactStoreAction };

export const addContact = (ship: Patp, contact: Contact): Poke<ContactUpdateAdd> => {
contact['last-updated'] = Date.now();

return storeAction({
add: { ship, contact }
});
};

export const removeContact = (ship: Patp): Poke<ContactUpdateRemove> =>
storeAction({
remove: { ship }
});

export const share = (recipient: Patp, version: number = CONTACT_UPDATE_VERSION): Poke<ContactShare> => ({
app: 'contact-push-hook',
mark: 'contact-share',
json: { share: recipient }
});

export const editContact = (
ship: Patp,
editField: ContactEditField
): Poke<ContactUpdateEdit> =>
storeAction({
edit: {
ship,
'edit-field': editField,
timestamp: Date.now()
}
});

export const allowShips = (
ships: Patp[]
): Poke<ContactUpdateAllowShips> => storeAction({
allow: {
ships
}
});

export const allowGroup = (
ship: string,
name: string
): Poke<ContactUpdateAllowGroup> => storeAction({
allow: {
group: { ship, name }
}
});

export const setPublic = (
setPublic: any
): Poke<ContactUpdateSetPublic> => {
return storeAction({
'set-public': setPublic
});
};

export const retrieve = (
ship: string
) => {
const resource = { ship, name: '' };
return {
app: 'contact-pull-hook',
mark: 'pull-hook-action',
json: {
add: {
resource,
ship
}
}
};
};

export const fetchIsAllowed = (
entity: string,
name: string,
ship: string,
personal: boolean
): Scry => {
const isPersonal = personal ? 'true' : 'false';
return {
app: 'contact-store',
path: `/is-allowed/${entity}/${name}/${ship}/${isPersonal}`
};
};
82 changes: 82 additions & 0 deletions gear/contacts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Path, Patp } from '../lib';
import { Resource } from '../groups';

export type ContactUpdate =
| ContactUpdateAdd
| ContactUpdateRemove
| ContactUpdateEdit
| ContactUpdateInitial
| ContactUpdateAllowGroup
| ContactUpdateAllowShips
| ContactUpdateSetPublic;

export interface ContactUpdateAdd {
add: {
ship: Patp;
contact: Contact;
};
}

export interface ContactUpdateRemove {
remove: {
ship: Patp;
};
}

export interface ContactUpdateEdit {
edit: {
ship: Patp;
'edit-field': ContactEditField;
timestamp: number;
};
}

export interface ContactUpdateAllowShips {
allow: {
ships: Patp[];
}
}

export interface ContactUpdateAllowGroup {
allow: {
group: Resource;
}
}

export interface ContactUpdateSetPublic {
'set-public': boolean;
}

export interface ContactShare {
share: Patp;
}

export interface ContactUpdateInitial {
initial: Rolodex;
}

export type Rolodex = {
[p in Patp]: Contact;
};

export type Contacts = Rolodex;

export interface Contact {
nickname: string;
bio: string;
status: string;
color: string;
avatar: string | null;
cover: string | null;
groups: Path[];
'last-updated': number;
}

type ContactKeys = keyof Contact;

export type ContactEditFieldPrim = Exclude<ContactKeys, 'groups' | 'last-updated'>;

export type ContactEditField = Partial<Pick<Contact, ContactEditFieldPrim>> & {
'add-group'?: Resource;
'remove-group'?: Resource;
};
8 changes: 8 additions & 0 deletions gear/deps.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

declare module 'urbit-ob' {

/**
* Convert a @p-encoded string to a decimal-encoded string.
*/
function patp2dec(name: string): string
}
2 changes: 2 additions & 0 deletions gear/docket/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lib';
export * from './types';
65 changes: 65 additions & 0 deletions gear/docket/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Poke, Scry } from '../lib';
import { Chad } from './types';

export function chadIsRunning(chad: Chad) {
return 'glob' in chad || 'site' in chad;
}

export const scryCharges: Scry = {
app: 'docket',
path: '/charges'
};

export const scryDockets: Scry = {
app: 'docket',
path: '/dockets'
};

export const scryTreaties: Scry = {
app: 'treaty',
path: '/treaties'
};

export const scryDefaultAlly: Scry = {
app: 'treaty',
path: '/default-ally'
};

export const scryAllies: Scry = {
app: 'treaty',
path: '/allies'
};

export const scryAllyTreaties = (ship: string): Scry => ({
app: 'treaty',
path: `/treaties/${ship}`
});

/**
* Uninstall a desk, and remove docket
*/
export function docketUninstall(desk: string): Poke<string> {
return {
app: 'docket',
mark: 'docket-uninstall',
json: desk
};
}

export function docketInstall(ship: string, desk: string): Poke<any> {
return {
app: 'docket',
mark: 'docket-install',
json: `${ship}/${desk}`
};
}

export function allyShip(ship: string): Poke<any> {
return {
app: 'treaty',
mark: 'ally-update-0',
json: {
add: ship
}
};
}
Loading

0 comments on commit 9228e89

Please sign in to comment.