urbit/pkg/npm/api/contacts/lib.ts

110 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-02-25 06:54:00 +03:00
import { Patp, Poke, Scry } from '../lib';
2021-02-25 06:54:00 +03:00
import {
Contact,
ContactUpdateAdd,
ContactUpdateEdit,
ContactUpdateRemove,
ContactEditField,
ContactShare,
ContactUpdate,
ContactUpdateAllowShips,
ContactUpdateAllowGroup,
ContactUpdateSetPublic
} from './types';
2021-02-25 06:54:00 +03:00
export const CONTACT_UPDATE_VERSION = 0;
2021-04-22 17:17:39 +03:00
const storeAction = <T extends ContactUpdate>(data: T, version: number = CONTACT_UPDATE_VERSION): Poke<T> => ({
app: 'contact-store',
2021-04-22 17:17:39 +03:00
mark: `contact-update-${version}`,
json: data
2021-02-25 06:54:00 +03:00
});
export { storeAction as contactStoreAction };
export const addContact = (ship: Patp, contact: Contact): Poke<ContactUpdateAdd> => {
contact['last-updated'] = Date.now();
2021-02-25 06:54:00 +03:00
return storeAction({
add: { ship, contact }
2021-02-25 06:54:00 +03:00
});
};
export const removeContact = (ship: Patp): Poke<ContactUpdateRemove> =>
storeAction({
remove: { ship }
2021-02-25 06:54:00 +03:00
});
2021-04-22 17:17:39 +03:00
export const share = (recipient: Patp, version: number = CONTACT_UPDATE_VERSION): Poke<ContactShare> => ({
app: 'contact-push-hook',
mark: 'contact-share',
json: { share: recipient }
2021-02-25 06:54:00 +03:00
});
export const editContact = (
ship: Patp,
editField: ContactEditField
): Poke<ContactUpdateEdit> =>
storeAction({
edit: {
ship,
'edit-field': editField,
timestamp: Date.now()
}
2021-02-25 06:54:00 +03:00
});
export const allowShips = (
ships: Patp[]
): Poke<ContactUpdateAllowShips> => storeAction({
allow: {
ships
}
});
export const allowGroup = (
ship: string,
name: string
): Poke<ContactUpdateAllowGroup> => storeAction({
allow: {
group: { ship, name }
2021-02-25 06:54:00 +03:00
}
});
export const setPublic = (
setPublic: any
): Poke<ContactUpdateSetPublic> => {
return storeAction({
'set-public': setPublic
});
};
2021-02-25 06:54:00 +03:00
export const retrieve = (
ship: string
) => {
const resource = { ship, name: '' };
return {
app: 'contact-pull-hook',
mark: 'pull-hook-action',
json: {
add: {
resource,
ship
}
}
};
};
2021-06-09 05:33:41 +03:00
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}`
};
2021-06-09 05:33:41 +03:00
};