Skip to content

Commit

Permalink
Merge branch 'dev' into new-session-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
confxsd committed Feb 6, 2024
2 parents 9e95d6d + 3d4f42a commit 08fb3b2
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 75 deletions.
130 changes: 71 additions & 59 deletions api/src/controllers/did.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,89 @@
import { DIDService } from "../services/did.js";
export const getIndexes = async (req, res, next) => {
// sendLit(req.params.id) //TODO Fix later.
export const getIndexes = async (req, res) => {

try {
const didService = new DIDService().setSession(req.session)
const { type, did } = req.params;
const indexes = await didService.getIndexes(did, type)
res.status(200).json(indexes);
} catch (error) {
res.status(500).json({ error: error.message });
}
// sendLit(req.params.id) // TODO Fix later.
try {
const didService = new DIDService();
const { type, did } = req.params;
const indexes = await didService.getIndexes(did, type);
res.status(200).json(indexes);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const addIndex = async (req, res, next) => {

if(req.params.did !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}
const type = req.params.type === 'own' ? 'owned' : 'starred';
const { indexId } = req.params;
try {
const didService = new DIDService().setSession(req.session);
const newIndex = await didService.addIndex(indexId, type)
res.status(201).json(newIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
export const addIndex = async (req, res) => {
if (req.params.did !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}
const type = req.params.type === "own" ? "owned" : "starred";
const { indexId } = req.params;
try {
const didService = new DIDService().setSession(req.session);
const newIndex = await didService.addIndex(indexId, type);
res.status(201).json(newIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const removeIndex = async (req, res, next) => {
if (req.params.did !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}

if(req.params.did !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}
const type = req.params.type === "own" ? "owned" : "starred";
const { indexId } = req.params;
try {
const didService = new DIDService().setSession(req.session);
const newIndex = await didService.removeIndex(indexId, type);
res.status(200).json(newIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

const type = req.params.type === 'own' ? 'owned' : 'starred';
const {indexId } = req.params;
try {
const didService = new DIDService().setSession(req.session)
const newIndex = await didService.removeIndex(indexId, type)
res.status(200).json(newIndex);
} catch (error) {
res.status(500).json({ error: error.message });
}
export const createProfile = async (req, res, next) => {

try {
const didService = new DIDService().setSession(req.session);
const profile = await didService.createProfile(req.body);
res.status(201).json(profile);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const getProfileByDID = async (req, res, next) => {
try {
const didService = new DIDService();
let profile = await didService.getProfile(req.params.did);

export const createProfile = async (req, res, next) => {
if(req.params.did !== req.session.did.parent) {
return res.status(500).json({ error: "Authorization error" });
}
try {
const didService = new DIDService().setSession(req.session);
const profile = await didService.createProfile(req.body)
res.status(201).json(profile);
} catch (error) {
res.status(500).json({ error: error.message });
if (!profile) {
profile = {
id: req.params.did,
};
}
};

export const getProfile = async (req, res, next) => {
try {
const didService = new DIDService()
let profile = await didService.getProfile(req.params.did)
res.status(200).json(profile);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

if(!profile){
profile = {
id: req.params.did,
}
}
export const getProfileFromSession = async (req, res, next) => {
try {
const didService = new DIDService();
let profile = await didService.getProfile(req.session.did.parent);

res.status(200).json(profile);
} catch (error) {
res.status(500).json({ error: error.message });
if (!profile) {
profile = {
id: req.params.did,
};
}

res.status(200).json(profile);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
8 changes: 6 additions & 2 deletions api/src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getIndexById = async (req, res, next) => {
const roles = getRolesFromSession(pkpSession);
Object.assign(index, {roles});
}else{
Object.assign(index, {owner: false, creator: false});
Object.assign(index, {roles: {owner: false, creator: false}});
}
}

Expand All @@ -31,11 +31,15 @@ export const createIndex = async (req, res, next) => {
const newIndex = await indexService.createIndex(req.body);

const didService = new DIDService().setSession(req.session); //Personal
const newIndexDID = await didService.addIndex(newIndex.id, "owner");
const newIndexDID = await didService.addIndex(newIndex.id, "owned");
newIndex.did = {
owned: true,
starred: false
};
newIndex.roles = {
owner: true,
creator: true
}

res.status(201).json(newIndex);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const listItems = async (req, res, next) => {
res.status(200).json(response);

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

Expand All @@ -29,7 +29,7 @@ export const addItem = async (req, res, next) => {
const item = await itemService.addItem(indexId, itemId);
res.status(201).json(item);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(400).json({ error: error.message });
}
//Queue embeddings.
};
Expand All @@ -45,7 +45,7 @@ export const removeItem = async (req, res, next) => {
await itemService.removeItem(indexId, itemId);
res.sendStatus(204);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(400).json({ error: error.message });
}
//Queue embeddings
};
4 changes: 2 additions & 2 deletions api/src/libs/lit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const getPKPSession = async (session, index) => {
throw new Error("Unauthenticated DID");
}

const sessionCacheKey = `${session.did.parent}:${index.signerFunction}`
const sessionCacheKey = `${session.did.parent}:${index.id}:${index.signerFunction}`

const existingSessionStr = await redis.hGet("sessions", sessionCacheKey);

Expand Down Expand Up @@ -184,7 +184,7 @@ export const getPKPSession = async (session, index) => {
return null;
}

if(! resp.signatures.sig1 ){
if(!resp.signatures || !resp.signatures.sig1 ){
throw new Error("No signature returned")
}

Expand Down
12 changes: 6 additions & 6 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ app.delete('/dids/:did/indexes/:indexId/:type', authCheckMiddleware, validator.p
type: Joi.string().valid('own', 'star').required(),
})), didController.removeIndex)

app.patch('/dids/:did/profile', authCheckMiddleware, validator.body(Joi.object({
app.patch('/profile', authCheckMiddleware, validator.body(Joi.object({
name: Joi.string().optional(),
bio: Joi.string().optional(),
avatar: Joi.custom(isCID, "Avatar").optional().allow(null),
}).or('name', 'bio', 'avatar')), validator.params(Joi.object({
did: Joi.custom(isDID, "DID").required(),
})), didController.createProfile)
}).or('name', 'bio', 'avatar')), didController.createProfile)

app.get('/profile', authCheckMiddleware, didController.getProfileFromSession)

app.get('/dids/:did/profile', validator.params(Joi.object({
did: Joi.custom(isDID, "DID").required(),
})), didController.getProfile)
})), didController.getProfileByDID)

// Indexes
app.get('/indexes/:id', validator.params(Joi.object({
Expand Down Expand Up @@ -210,7 +210,7 @@ app.get('/nft/:chainName/:tokenAddress', infuraController.getCollectionMetadataH
app.get('/nft/:chainName/:tokenAddress/:tokenId', infuraController.getNftMetadataHandler);
app.get('/ens/:ensName', infuraController.getWalletByENSHandler);

app.post('/site/upload_avatar', isImage.single('file'), fileController.uploadAvatar);
app.post('/profile/upload_avatar', isImage.single('file'), fileController.uploadAvatar);

app.post("/site/subscribe", validator.body(Joi.object({
email: Joi.string().email().required(),
Expand Down
6 changes: 3 additions & 3 deletions api/src/services/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export class DIDService {
const ownerDID = await getOwnerProfile(i.signerPublicKey);
return { ...i, ownerDID };
})
.sort((a, b) => {
return new Date(b.createdAt) - new Date(a.createdAt);
})
);


Expand Down Expand Up @@ -382,6 +385,3 @@ export class DIDService {
}

}



0 comments on commit 08fb3b2

Please sign in to comment.