Skip to content

Commit

Permalink
Merge pull request #7 from indexnetwork/api-integration
Browse files Browse the repository at this point in the history
Resty
  • Loading branch information
serefyarar authored Jan 30, 2024
2 parents a2937e1 + 100ec89 commit 7c8ad42
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
17 changes: 14 additions & 3 deletions api/src/controllers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ import {IndexService} from "../services/index.js";
import {getPKPSession} from "../libs/lit/index.js";

export const listItems = async (req, res, next) => {
//Todo without embeddings, use chromadb filters, accepts query param.
const { indexId } = req.params;
const { cursor, limit } = req.query;
try {

const itemService = new ItemService();
const response = await itemService.getIndexItems(indexId, cursor, limit)

res.status(200).json(response);

} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const addItem = async (req, res, next) => {
const {indexId, itemId} = req.body;
const {indexId, itemId} = req.params;
try {

const indexService = new IndexService();
Expand All @@ -23,7 +34,7 @@ export const addItem = async (req, res, next) => {
//Queue embeddings.
};
export const removeItem = async (req, res, next) => {
const {indexId, itemId} = req.body;
const {indexId, itemId} = req.params;
try {

const indexService = new IndexService();
Expand Down
11 changes: 6 additions & 5 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,20 @@ app.delete('/indexes/:id', authCheckMiddleware, validator.params(Joi.object({
})), indexController.deleteIndex)

// Items
app.get('/items', validator.query(Joi.object({
app.get('/indexes/:indexId/items', validator.query(Joi.object({
query: Joi.string().min(1).optional(),
cursor: Joi.string().optional(),
limit: Joi.number().default(24),
})), validator.params(Joi.object({
indexId: Joi.custom(isStreamID, "Index ID").required(),
skip: Joi.number().default(0),
take: Joi.number().default(10),
})), itemController.listItems)

app.post('/items', authCheckMiddleware, validator.body(Joi.object({
app.post('/indexes/:indexId/items/:itemId', authCheckMiddleware, validator.params(Joi.object({
indexId: Joi.custom(isStreamID, "Index ID").required(),
itemId: Joi.custom(isStreamID, "Stream ID").required(),
})), itemController.addItem)

app.delete('/items', authCheckMiddleware, validator.body(Joi.object({
app.delete('/indexes/:indexId/items/:itemId', authCheckMiddleware, validator.params(Joi.object({
indexId: Joi.custom(isStreamID, "Index ID").required(),
itemId: Joi.custom(isStreamID, "Stream ID").required(),
})), itemController.removeItem)
Expand Down
6 changes: 6 additions & 0 deletions api/src/services/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ export class DIDService {
... on CeramicAccount {
profile {
id
controllerDID {
id
}
name
bio
avatar
Expand All @@ -357,6 +360,9 @@ export class DIDService {
return null
}

data.node.profile.id = data.node.profile.controllerDID.id
delete data.node.profile.controllerDID;

// Return the created profile document
return data.node.profile;

Expand Down
76 changes: 76 additions & 0 deletions api/src/services/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,82 @@ export class ItemService {
}
}

async getIndexItems(indexId, cursor=null, limit= 24) {
try {

let cursorFilter = cursor ? `after: "${cursor}",` : "";

const {data, errors} = await this.client.executeQuery(`{
indexItemIndex(first: ${limit}, ${cursorFilter} filters: {
where: {
indexId: { equalTo: "${indexId}"}
}
}, sorting: { createdAt: DESC}) {
pageInfo {
endCursor
}
edges {
node {
... on IndexItem {
id
indexId
itemId
createdAt
updatedAt
deletedAt
item {
id
__typename
... on WebPage {
title
favicon
url
content
createdAt
updatedAt
deletedAt
}
}
index {
id
title
signerPublicKey
signerFunction
createdAt
updatedAt
deletedAt
}
}
}
}
}
}`);

// 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) {
throw new Error('Invalid response data');
}

if (data.indexItemIndex.edges.length === 0) {
return null;
}

return {
endCursor: data.indexItemIndex.pageInfo.endCursor,
items: data.indexItemIndex.edges.map(e => e.node.item),
}

} catch (error) {
// Log the error and rethrow it for external handling
console.error('Exception occurred in getIndexItem:', error);
throw error;
}
}

async addItem(indexId, itemId) {

if (!this.did) {
Expand Down

0 comments on commit 7c8ad42

Please sign in to comment.