on 262ca0e help sign in
92 lines 2.6 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 function scry<T = any>(path: string): Promise<T> {
14 return api.scry<T>({ app: 'astrolabe', path });
17export function getPoint(patp: string): Promise<any> {
18 return scry<any>(`/point/${patp}`);
21export function getSpawnedPoints(patp: string): Promise<any> {
22 return scry<any>(`/point/${patp}/spawned`);
25export function getPeers(): Promise<any> {
26 return scry<any>(`/peers`);
29export async function searchPoints(search: string): Promise<Patp[]> {
30 const { points } = await scry<any>(`/search/${search}`);
31 return points.map(normalizeId);
34export function getChartData(old = false): Promise<any> {
35 return scry<any>('/chart-data' + (old ? '-old' : ''));
38window['gcd'] = getChartData;
40export function getDoc(path: string): Promise<string> {
41 return scry<string>(`/doc/${path}`);
44export function initialContacts(json: ContactUpdate, contacts: Rolodex): Rolodex {
45 const data = _get(json, 'initial', false);
46 if (data) {
47 contacts = data.rolodex;
48 }
49 return contacts;
50};
52export function addContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
53 const data = _get(json, 'add', false);
54 if (data) {
55 contacts[data.ship] = data.contact;
56 }
57 return contacts;
58};
60export function removeContact(json: ContactUpdate, contacts: Rolodex): Rolodex {
61 const data = _get(json, 'remove', false);
62 if (
63 data &&
64 (data.ship in contacts)
65 ) {
66 delete contacts[data.ship];
67 }
68 return contacts;
69};
71export function handleContactUpdateEvent(updateContacts) {
72 return ({ 'contact-update': contactUpdate }) => {
73 console.log(`received contact-update: ${Object.keys(contactUpdate)}`)
74 const reducers = [initialContacts, addContact, removeContact];
75 const reducer = compose(reducers.map(r => sta => r(contactUpdate, sta)));
76 updateContacts(reducer);
77 };
80export function subscribeToContacts(e: (data: any) => void): Promise<any> {
81 return api.subscribe({
82 app: 'contact-store',
83 path: '/all',
84 event: handleContactUpdateEvent(e),
85 err: () => {
86 console.error(`Subscription to contact-store/all just get "err"`);
87 },
88 quit: () => {
89 console.error(`Subscription to contact-store/all just get "quit"`)
90 }
91 })