fix(api): directly use thread client to generate bucket thread if not exist

Signed-off-by: Andrew Hill <andrew@textile.io>
This commit is contained in:
Andrew Hill 2020-09-22 14:31:19 -07:00
parent fdd7ef58b4
commit 157151ef57
No known key found for this signature in database
GPG Key ID: B8929C9860377979

View File

@ -6,7 +6,7 @@ import * as Social from "~/node_common/social";
import JWT from "jsonwebtoken";
import BCrypt from "bcrypt";
import { Buckets, PrivateKey, Pow } from "@textile/hub";
import { Buckets, PrivateKey, Pow, Client } from "@textile/hub";
const BUCKET_NAME = "data";
@ -128,11 +128,34 @@ export const getBucketAPIFromUserToken = async (token, user) => {
const buckets = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
await buckets.getToken(identity);
<<<<<<< HEAD
// TODO(jim): Put this call into a file for all Textile related calls.
let target;
console.log(`[buckets] getOrCreate`);
try {
target = await buckets.getOrCreate(BUCKET_NAME);
// Create a threads client
const client = new Client(buckets.context)
try {
// Get a default thread to store our buckets
const res = await client.getThread('buckets')
buckets.withThread(res.id.toString())
} catch (error) {
if (error.message !== 'Thread not found') {
throw new Error(error.message)
}
const newId = ThreadID.fromRandom()
await client.newDB(newId, 'buckets')
threadID = newId.toString()
buckets.withThread(threadID)
}
const roots = await buckets.list()
let root = roots.find((bucket) => bucket.name === BUCKET_NAME)
if (!root) {
const created = await this.create(BUCKET_NAME)
root = created.root
}
} catch (e) {
Social.sendTextileSlackMessage({
file: "/node_common/utilities.js",
@ -142,16 +165,14 @@ export const getBucketAPIFromUserToken = async (token, user) => {
functionName: `buckets.getOrCreate`,
});
return { buckets: null, bucketKey: null, bucketRoot: null };
return {
buckets,
bucketKey: root.key,
bucketRoot: root,
bucketName: BUCKET_NAME,
};
}
console.log(`[buckets] getOrCreate succes!`);
return {
buckets,
bucketKey: target.root.key,
bucketRoot: target,
bucketName: BUCKET_NAME,
};
};
export const getFileName = (s) => {