Repository navigation
Expand file tree
/
Copy pathadapter.ts
More file actions
143 lines (114 loc) · 3.98 KB
/
Copy pathadapter.ts
File metadata and controls
143 lines (114 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type { Address, Expression, ExpressionAdapter, PublicSharing, HolochainLanguageDelegate, LanguageContext, AgentService } from "@perspect3vism/ad4m";
import type { IPFS } from "ipfs-core-types";
import { name } from "./index";
const _appendBuffer = (buffer1, buffer2) => {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
const uint8ArrayConcat = (chunks) => {
return chunks.reduce(_appendBuffer);
};
class GenericExpressionPutAdapter implements PublicSharing {
#agent: AgentService;
#holochainDna: HolochainLanguageDelegate;
#IPFS: IPFS;
constructor(context: LanguageContext) {
this.#agent = context.agent;
this.#holochainDna = context.Holochain as HolochainLanguageDelegate;
this.#IPFS = context.IPFS;
}
async createPublic(groupData: object): Promise<Address> {
let expression = this.#agent.createSignedExpression(groupData);
if (expression.data.image) {
const ipfsAddress = await this.#IPFS.add({content: expression.data.image});
// @ts-ignore
const ipfsHash = ipfsAddress.cid.toString();
expression.data.image_address = ipfsHash;
}
if (expression.data.thumbnail) {
const ipfsAddress = await this.#IPFS.add({content: expression.data.thumbnail});
// @ts-ignore
const ipfsHash = ipfsAddress.cid.toString();
expression.data.thumbnail_address = ipfsHash;
}
const res = await this.#holochainDna.call(
name,
"group_expression",
"create_expression",
{
author: expression.author,
data: {
name: expression.data.name,
description: expression.data.description,
image_address: expression.data.image_address,
thumbnail_address: expression.data.thumbnail_address
},
timestamp: expression.timestamp,
proof: expression.proof,
}
);
return res.toString("hex");
}
}
export default class GenericExpressionAdapter implements ExpressionAdapter {
#holochainDna: HolochainLanguageDelegate;
#IPFS: IPFS;
putAdapter: PublicSharing;
constructor(context: LanguageContext) {
this.#holochainDna = context.Holochain as HolochainLanguageDelegate;
this.putAdapter = new GenericExpressionPutAdapter(context);
this.#IPFS = context.IPFS;
}
async get(address: Address): Promise<Expression> {
const hash = Buffer.from(address, "hex");
const expression = await this.#holochainDna.call(
name,
"group_expression",
"get_expression_by_address",
hash
);
if (expression != null) {
let cloneRes = Object.assign({}, expression);
let imageAddr = cloneRes.data.image_address
if(imageAddr) {
const chunks = [];
const imageChunk = await this.#IPFS.cat(imageAddr);
// @ts-ignore
for await (const chunk of imageChunk) {
chunks.push(chunk)
}
const fileString = Buffer.from(uint8ArrayConcat(chunks)).toString();
cloneRes.data.image = fileString;
delete cloneRes["image_address"]
}
let thumbnailAddr = cloneRes.data.thumbnail_address;
if(thumbnailAddr) {
const chunks = [];
const thumbnailChunk = await this.#IPFS.cat(thumbnailAddr);
// @ts-ignore
for await (const chunk of thumbnailChunk) {
chunks.push(chunk)
}
const fileString = Buffer.from(uint8ArrayConcat(chunks)).toString();
cloneRes.data.thumbnail = fileString;
delete cloneRes["thumbnail_address"]
}
const expressionSer = {
author: cloneRes.author,
data: {
name: cloneRes.data.name,
description: cloneRes.data.description,
image: cloneRes.data.image,
thumbnail: cloneRes.data.thumbnail
},
timestamp: cloneRes.timestamp,
proof: cloneRes.proof,
} as Expression;
return expressionSer;
} else {
return null;
}
}
}