-
Notifications
You must be signed in to change notification settings - Fork 0
/
linksAdapter.ts
84 lines (66 loc) · 2.45 KB
/
linksAdapter.ts
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
import type { Expression, LinksAdapter, NewLinksObserver, LanguageContext, LinkQuery } from "@perspect3vism/ad4m";
import type { DID } from "@perspect3vism/ad4m/lib/DID";
import axios from 'axios'
export class DbStoreLinkAdapter implements LinksAdapter {
linkCallback?: NewLinksObserver
languageHash: string
constructor(context: LanguageContext) {
this.languageHash = context.storageDirectory.split("/")[context.storageDirectory.split("/").length - 2];
}
writable(): boolean {
return true;
}
public(): boolean {
return false;
}
async others(): Promise<DID[]> {
return []
}
async addLink(link: Expression): Promise<void> {
const data = prepareExpressionLink(link);
data["graph"] = this.languageHash;
console.warn("CENTRALIZED LINK LANGUAGE POSTING DATA", data);
await axios.post("https://centralized-link-store-production.up.railway.app/addLink", data)
}
async updateLink(
oldLinkExpression: Expression,
newLinkExpression: Expression
): Promise<void> {
const oldLinkData = prepareExpressionLink(oldLinkExpression);
oldLinkData["graph"] = this.languageHash;
const newLinkData = prepareExpressionLink(newLinkExpression);
newLinkData["graph"] = this.languageHash;
await axios.post("https://centralized-link-store-production.up.railway.app/removeLink", oldLinkData);
await axios.post("https://centralized-link-store-production.up.railway.app/addLink", newLinkData);
}
async removeLink(link: Expression): Promise<void> {
const linkData = prepareExpressionLink(link);
linkData["graph"] = this.languageHash;
await axios.post("https://centralized-link-store-production.up.railway.app/removeLink", linkData);
}
async getLinks(query: LinkQuery): Promise<Expression[]> {
query["graph"] = this.languageHash;
console.log("CENTRALIZED LINK LANGUAGE QUERYING DATA", query);
let links = await axios.post("https://centralized-link-store-production.up.railway.app/getLinks", query);
console.log("Got result", links.data);
//@ts-ignore
return links.data;
}
addCallback(callback: NewLinksObserver): number {
this.linkCallback = callback;
return 1;
}
}
function prepareExpressionLink(link: Expression): object {
const data = Object.assign(link);
if (data.data.source == "") {
data.data.source = null;
}
if (data.data.target == "") {
data.data.target = null;
}
if (data.data.predicate == "") {
data.data.predicate = null;
}
return data;
}