Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Jun 10, 2024
1 parent 533a0a0 commit 9afa4c0
Show file tree
Hide file tree
Showing 20 changed files with 281 additions and 179 deletions.
18 changes: 18 additions & 0 deletions api/src/libs/composedb.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ let defaultRuntime = {
property: "indexId",
},
},
embeddings: {
type: "view",
viewType: "relation",
relation: {
source: "queryConnection",
model: "Model_Embedding_ID",
property: "indexId",
},
},
},
IndexItem: {
itemId: {
Expand Down Expand Up @@ -271,6 +280,15 @@ let defaultRuntime = {
},
},
controllerDID: { type: "view", viewType: "documentAccount" },
embeddings: {
type: "view",
viewType: "relation",
relation: {
source: "queryConnection",
model: "Model_Embedding_ID",
property: "itemId",
},
},
},
Profile: {
bio: { type: "string", required: false, immutable: false },
Expand Down
30 changes: 25 additions & 5 deletions api/src/libs/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Indexer {
const itemService = new ItemService(this.definition);
const indexItem = await itemService.getIndexItemById(id, false);

if (indexItem.index.controllerDID.id !== indexItem.controllerDID.id) {
logger.warn(
`Step [0]: IndexItem is unauthorized to index: ${JSON.stringify(indexItem)}`,
);
return;
}
logger.info(
`Step [0]: IndexItem found for id: ${JSON.stringify(indexItem)}`,
);
Expand Down Expand Up @@ -121,6 +127,13 @@ class Indexer {
const itemService = new ItemService(this.definition);
const indexItem = await itemService.getIndexItemById(id, false);

if (indexItem.index.controllerDID.id !== indexItem.controllerDID.id) {
logger.warn(
`Step [0]: IndexItem is unauthorized to index: ${JSON.stringify(indexItem)}`,
);
return;
}

try {
const indexSession = await this.getIndexerSession(indexItem.index);
await indexSession.did.authenticate();
Expand Down Expand Up @@ -305,6 +318,13 @@ class Indexer {
const embedding = await embeddingService.getEmbeddingById(id);
const itemStream = await ceramic.loadStream(embedding.item.id);

if (embedding.index.controllerDID.id !== embedding.controllerDID.id) {
logger.warn(
`Step [0]: Embedding is unauthorized to index: ${JSON.stringify(indexItem)}`,
);
return;
}

const doc = {
...itemStream.content,
id: itemStream.id.toString(),
Expand All @@ -317,19 +337,19 @@ class Indexer {
indexCreatedAt: embedding.index.createdAt,
indexUpdatedAt: embedding.index.updatedAt,
indexDeletedAt: embedding.index.deletedAt,
indexOwnerDID: embedding.index.ownerDID.id,
indexControllerDID: embedding.index.controllerDID.id,
vector: embedding.vector,
};

for (let key of Object.keys(doc)) {
payload[key] = doc[key];
}

if (embedding.index.ownerDID.name) {
payload.indexOwnerName = embedding.index.ownerDID.name;
if (embedding.index.controllerDID.name) {
payload.indexOwnerName = embedding.index.controllerDID.name;
}
if (embedding.index.ownerDID.bio) {
payload.indexOwnerBio = embedding.index.ownerDID.bio;
if (embedding.index.controllerDID.bio) {
payload.indexOwnerBio = embedding.index.controllerDID.bio;
}

try {
Expand Down
2 changes: 1 addition & 1 deletion api/src/libs/lit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ export const getPKPSessionWithLIT = async (session, index) => {
export const getRolesFromSession = (index, session, definition) => {
if (
session.cacao.p.resources.indexOf("ceramic://*") > -1 &&
index.ownerDID.id == session.did.parent
index.controllerDID.id == session.did.parent
) {
return {
owner: true,
Expand Down
18 changes: 17 additions & 1 deletion api/src/services/embedding.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class EmbeddingService {
definition,
});
this.did = null;
this.indexService = new IndexService(definition);
}

setSession(session) {
Expand Down Expand Up @@ -64,7 +65,6 @@ export class EmbeddingService {
if (!data || !data.node) {
throw new Error("Invalid response data");
}
data.node.index.ownerDID = data.node.index.controllerDID;

return data.node;
} catch (error) {
Expand Down Expand Up @@ -97,6 +97,22 @@ export class EmbeddingService {
context
vector
description
item {
id
__typename
}
index {
id
title
controllerDID {
id
}
signerPublicKey
signerFunction
createdAt
updatedAt
deletedAt
}
createdAt
updatedAt
deletedAt
Expand Down
3 changes: 1 addition & 2 deletions api/src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export class IndexService {
index.did = did;
}

index.ownerDID = this.getOwnerProfile(index);
delete index.controllerDID;
index.controllerDID = this.getOwnerProfile(index);
return index;
}
async getIndexById(id) {
Expand Down
49 changes: 31 additions & 18 deletions api/src/services/item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ComposeClient } from "@composedb/client";
import { removePrefixFromKeys, getCurrentDateTime } from "../utils/helpers.js";
import { indexItemFragment } from "../types/fragments.js";
import { IndexService } from "./index.js";

const transformIndexItem = (indexItem) => {
const { __typename: type, indexedAt: _, ...rest } = indexItem.item;
Expand All @@ -20,6 +21,7 @@ export class ItemService {
definition,
});
this.did = null;
this.indexService = new IndexService(definition);
}

setSession(session) {
Expand Down Expand Up @@ -111,53 +113,64 @@ export class ItemService {
async getIndexItems(indexId, cursor = null, limit = 24) {
try {
let cursorFilter = cursor ? `after: "${cursor}",` : "";
const index = await this.indexService.getIndexById(indexId);

let { data, errors } = await this.client.executeQuery(`{
indexItemIndex(first: ${limit}, ${cursorFilter} filters: {
node(id: "${indexId}") {
... on Index {
items(
first: 10
${cursorFilter}
account: "${index.controllerDID.id}"
filters: {
where: {
indexId: { equalTo: "${indexId}"},
deletedAt: {isNull: true}
}
}, sorting: { createdAt: DESC}) {
pageInfo {
endCursor
}
edges {
node {
${indexItemFragment}
}
}
sorting: { createdAt: DESC}
) {
pageInfo {
endCursor
}
edges {
node {
${indexItemFragment}
}
}
}`);
}
}
}
}`);

// Handle GraphQL errors
if (errors) {
throw new Error(`Error getting index item: ${JSON.stringify(errors)}`);
}
// Validate the data response
if (!data || !data.indexItemIndex || !data.indexItemIndex.edges) {
if (!data || !data.node || !data.node.items || !data.node.items.edges) {
throw new Error("Invalid response data");
}

if (data.indexItemIndex.edges.length === 0) {
const items = data.node.items.edges;

if (items.length === 0) {
return {
endCursor: null,
items: [],
};
}

data.indexItemIndex.edges.map((e) => {
items.map((e) => {
e.node.item = removePrefixFromKeys(
e.node.item,
`${e.node.item.__typename}_`,
);
return e;
});
console.log(data.indexItemIndex.edges);
return {
//Todo fix itemId to id
endCursor: data.indexItemIndex.pageInfo.endCursor,
items: data.indexItemIndex.edges.map((e) => transformIndexItem(e.node)),
endCursor: data.node.items.pageInfo.endCursor,
items: items.map((e) => transformIndexItem(e.node)),
};
} catch (error) {
// Log the error and rethrow it for external handling
Expand Down Expand Up @@ -233,7 +246,7 @@ export class ItemService {
const { data, errors } = await this.client.executeQuery(`{
indexItemIndex(first: ${limit}, ${cursorFilter} filters: {
where: {
itemId: { in: ["${itemIds.join('","')}"]},
itemId: { in: ${JSON.stringify(itemIds)}}},
deletedAt: {isNull: true}
}
}, sorting: { createdAt: DESC}) {
Expand Down
6 changes: 6 additions & 0 deletions api/src/types/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export const indexItemFragment = `
id
indexId
itemId
controllerDID {
id
}
modelId
createdAt
updatedAt
Expand All @@ -60,6 +63,9 @@ export const indexItemFragment = `
title
signerPublicKey
signerFunction
controllerDID {
id
}
createdAt
updatedAt
deletedAt
Expand Down
Loading

0 comments on commit 9afa4c0

Please sign in to comment.