textiot
Version:
A framework for building web and native (IoT) Dapps on the IPFS network
208 lines (207 loc) • 7.72 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../core/api");
const snapshots_1 = __importDefault(require("./snapshots"));
const schemas_1 = __importDefault(require("./schemas"));
/**
* Threads is an API module for managing Textile threads
*
* Threads are distributed sets of encrypted files between peers governed by build-in or
* custom Schemas.
*
* Thread type controls read (R), annotate (A), and write (W) access:
*
* private --> initiator: RAW, whitelist:
* readonly --> initiator: RAW, whitelist: R
* public --> initiator: RAW, whitelist: RA
* open --> initiator: RAW, whitelist: RAW
*
* Thread sharing style controls if (Y/N) a thread can be shared:
*
* notshared --> initiator: N, whitelist: N
* inviteonly --> initiator: Y, whitelist: N
* shared --> initiator: Y, whitelist: Y
*
* @extends API
*/
class Threads extends api_1.API {
constructor(opts = api_1.DEFAULT_API_OPTIONS) {
super(opts);
this.snapshots = new snapshots_1.default(opts);
this.schemas = new schemas_1.default(opts);
}
/**
* Adds and joins a new thread
*
* @param name The name of the new thread
* @param key A locally unique key used by an app to identify this thread on recovery
* @param type The type of thread, must be one of 'private' (default), 'read_only', 'public',
* or 'open'
* @param sharing The sharing style of thread, must be one of 'notshared'
* (default), 'invite_only', or 'shared'
* @param whitelist An array of contact addresses. When supplied, the thread will not allow
* additional peers, useful for 1-1 chat/file sharing or private threads.
* @param schema Schema ID for the new thread
* @returns The newly generated thread info
*/
add(name, schema, key, type, sharing, whitelist) {
return __awaiter(this, void 0, void 0, function* () {
let targetSchema;
// Attempt to create the schema on the fly
if (schema && typeof schema === 'object') {
const fileIndex = yield this.schemas.add(schema);
targetSchema = fileIndex.hash;
}
else if (schema && typeof schema === 'string') {
// check if it is one of the default schemas
const known = yield this.schemas.defaultByName(schema);
if (known) {
// if one of the default, add it or get it to find the hash
const added = yield this.schemas.add(known);
targetSchema = added.hash;
}
else {
// @todo - perhaps a specific warning if schema here doesn't match a hash len
targetSchema = schema;
}
}
const response = yield this.sendPost('threads', [name], {
schema: targetSchema || '',
key: key || '',
type: type || 'private',
sharing: sharing || 'not_shared',
whitelist: (whitelist || []).join(',')
});
return response.json();
});
}
/**
* Adds or updates a thread directly, usually from a backup
*
* @param thread ID of the thread
* @param info Thread object
*/
addOrUpdate(thread, info) {
return __awaiter(this, void 0, void 0, function* () {
this.sendPut(`threads/${thread}`, undefined, undefined, info);
});
}
/**
* Retrieve a thread by ID
*
* @param thread ID of the thread
* @returns A thread object
*/
get(thread) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`threads/${thread}`);
return response.json();
});
}
/**
* Retrieve a thread by Key
*
* @param key Key of the thread
* @returns A thread object
*/
getByKey(key) {
return __awaiter(this, void 0, void 0, function* () {
// @todo: update with https://github.com/textileio/go-textile/issues/712
const response = yield this.sendGet('threads');
const threads = yield response.json();
return threads.items.find((thread) => thread.key === key);
});
}
/**
* Retrieve threads by Name
*
* @param name Name of the thread
* @returns An array thread objects
*/
getByName(name) {
return __awaiter(this, void 0, void 0, function* () {
// @todo: update with https://github.com/textileio/go-textile/issues/712
const response = yield this.sendGet('threads');
const threads = yield response.json();
return threads.items.filter((thread) => thread.name === name);
});
}
/**
* Lists all local threads
*
* @returns An array of threads
*/
list() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet('threads');
return response.json();
});
}
/**
* Leave and remove a thread by ID
*
* @param thread ID of the thread
* @returns Whether the thread removal was successfull
*/
remove(thread) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendDelete(`threads/${thread}`);
return response.status === 204;
});
}
/**
* Leave and remove a thread by Key
*
* @param key thread.key of the thread
* @returns Whether the thread removal was successfull
*/
removeByKey(key) {
return __awaiter(this, void 0, void 0, function* () {
const thread = yield this.getByKey(key);
if (!thread) {
return false;
}
const response = yield this.sendDelete(`threads/${thread.id}`);
return response.status === 204;
});
}
/**
* Renames a thread
*
* Note: Only initiators can rename a thread.
* @param thread ID of the thread
* @param name New name for the thread
* @returns Whether the rename was successfully
*/
rename(thread, name) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendPut(`threads/${thread}/name`);
return response.status === 204;
});
}
/**
* List all peers in a thread
*
* @param thread ID of the thread (default is 'default').
* @returns An array of thread contacts
*/
peers(thread) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.sendGet(`threads/${thread || 'default'}/peers`);
return response.json();
});
}
}
exports.default = Threads;
;