import { API , DEFAULT_API_OPTIONS } from '../core/api' ; import { ApiOptions , Thread , ThreadList , ContactList } from '../models/index' ; import Snapshots from './snapshots' ; import Schemas from './schemas' ; export type ThreadType = 'private' | 'read_only' | 'public' | 'open' ; export type ThreadSharing = 'not_shared' | 'invite_only' | 'shared' ; extends API export default class Threads extends API { snapshots : Snapshots ; schemas : Schemas ; constructor ( opts = DEFAULT_API_OPTIONS ) { super ( opts ) ; this . snapshots = new Snapshots ( opts ) ; this . schemas = new Schemas ( opts ) ; } param name param key param type param sharing param whitelist param schema returns async add ( name , schema ? , key ? , type ? , sharing ? , whitelist ? ) { let targetSchema ; if ( schema && typeof schema === 'object' ) { const fileIndex = await this . schemas . add ( schema ) ; targetSchema = fileIndex . hash ; } else if ( schema && typeof schema === 'string' ) { const known = await this . schemas . defaultByName ( schema ) ; if ( known ) { const added = await this . schemas . add ( known ) ; targetSchema = added . hash ; } else { targetSchema = schema ; } } const response = await this . sendPost ( 'threads' , [ name ] , { schema : targetSchema || '' , key : key || '' , type : type || 'private' , sharing : sharing || 'not_shared' , whitelist : ( whitelist || [ ] ) . join ( ',' ) } ) ; return response . json ( ) as Promise < Thread > ; } param thread param info async addOrUpdate ( thread , info ) { this . sendPut ( `threads/${ thread }` , undefined , undefined , info ) ; } param thread returns async get ( thread ) { const response = await this . sendGet ( `threads/${ thread }` ) ; return response . json ( ) as Promise < Thread > ; } param key returns async getByKey ( key ) { const response = await this . sendGet ( 'threads' ) ; const threads = await response . json ( ) ; return threads . items . find ( ( thread ) => thread . key === key ) ; } param name returns async getByName ( name ) { const response = await this . sendGet ( 'threads' ) ; const threads = await response . json ( ) ; return threads . items . filter ( ( thread ) => thread . name === name ) ; } returns async list ( ) { const response = await this . sendGet ( 'threads' ) ; return response . json ( ) as Promise < ThreadList > ; } param thread returns async remove ( thread ) { const response = await this . sendDelete ( `threads/${ thread }` ) ; return response . status === 204 ; } param key returns async removeByKey ( key ) { const thread = await this . getByKey ( key ) ; if ( ! thread ) { return false ; } const response = await this . sendDelete ( `threads/${ thread . id }` ) ; return response . status === 204 ; } param thread param name returns async rename ( thread , name ) { const response = await this . sendPut ( `threads/${ thread }/name` ) ; return response . status === 204 ; } param thread returns async peers ( thread ) { const response = await this . sendGet ( `threads/${ thread || 'default' }/peers` ) ; return response . json ( ) as Promise < ContactList > ; } }