on 281dbe3 help sign in
102 lines 2.9 kB history raw
1import UrbitApi from '@urbit/http-api';
2import type { ContactUpdate, Patp, Group, Contact, Rolodex } from '@urbit/api';
3import _get from 'lodash/get';
4import { compose } from 'lodash/fp';
6import { normalizeId } from './id';
8console.log(`Initializing Urbit API at ${Date()}`);
9const api: UrbitApi = new UrbitApi('', '', window.desk);
10api.ship = window.ship;
11// api.connect();
13export { api };
15export function scry<T = any>(path: string): Promise<T> {
16 return api.scry<T>({ app: 'astrolabe', path });
19export function getPoint(patp: string): Promise<any> {
20 return scry<any>(`/point/${patp}`);
23export function getSpawnedPoints(patp: string): Promise<any> {
24 return scry<any>(`/point/${patp}/spawned`);
27export function getLockedSpawnedPoints(patp: string): Promise<any> {
28 return scry<any>(`/point/${patp}/spawned/locked`);
31export function getUnlockedSpawnedPoints(patp: string): Promise<any> {
32 return scry<any>(`/point/${patp}/spawned/unlocked`);
35export function getPeers(): Promise<any> {
36 return scry<any>(`/peers`);
39export async function searchPoints(search: string, mode: ('patp' | 'sigil')): Promise<Patp[]> {
40 const { points } = await scry<any>(`/search/${mode}/${search}`);
41 return points.map(normalizeId);
44export function getChartData(old = false): Promise<any> {
45 return scry<any>('/chart-data' + (old ? '-old' : ''));
48window['gcd'] = getChartData;
50export function getDoc(path: string): Promise<string> {
51 return scry<string>(`/doc/${path}`);
54export function initialContacts(json: ContactUpdate, contacts: Rolodex): Rolodex {
55 const data = _get(json, 'initial', false);
56 if (data) {
57 contacts = data.rolodex;
58 }
59 return contacts;
60};
62export function addContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
63 const data = _get(json, 'add', false);
64 if (data) {
65 contacts[data.ship] = data.contact;
66 }
67 return contacts;
68};
70export function removeContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
71 const data = _get(json, 'remove', false);
72 if (
73 data &&
74 (data.ship in contacts)
75 ) {
76 delete contacts[data.ship];
77 }
78 return contacts;
79};
81export function handleContactUpdateEvent(updateContacts) {
82 return ({ 'contact-update': contactUpdate }) => {
83 console.log(`received contact-update: ${Object.keys(contactUpdate)}`)
84 const reducers = [initialContacts, addContact, removeContact];
85 const reducer = compose(reducers.map(r => sta => r(contactUpdate, sta)));
86 updateContacts(reducer);
87 };
90export function subscribeToContacts(e: (data: any) => void): Promise<any> {
91 return api.subscribe({
92 app: 'contact-store',
93 path: '/all',
94 event: handleContactUpdateEvent(e),
95 err: () => {
96 console.error(`Subscription to contact-store/all just get "err"`);
97 },
98 quit: () => {
99 console.error(`Subscription to contact-store/all just get "quit"`);
100 }
101 })