Skip to content

Commit

Permalink
Merge pull request #122 from pow-co/content-author
Browse files Browse the repository at this point in the history
API to query content by author
  • Loading branch information
owenkellogg authored Sep 19, 2023
2 parents e004d22 + 6f30d47 commit 1ba6640
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/migrations/20230919173006-add-auth-to-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
await queryInterface.addColumn('Contents', 'author_paymail', { type: Sequelize.STRING, allowNull: true });
await queryInterface.addColumn('Contents', 'author_pubkey', { type: Sequelize.STRING, allowNull: true });
},

async down (queryInterface, Sequelize) {
await queryInterface.removeColumn('Contents', 'author_paymail');
await queryInterface.removeColumn('Contents', 'author_pubkey');
}
};
4 changes: 3 additions & 1 deletion src/models/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ module.exports = (sequelize, DataTypes) => {
bmap: DataTypes.JSON,
bitchat_channel: DataTypes.STRING,
chain: DataTypes.STRING,
context_txid: DataTypes.STRING
context_txid: DataTypes.STRING,
author_paymail: DataTypes.STRING,
author_pubkey: DataTypes.STRING,
}, {
sequelize,
modelName: 'Content',
Expand Down
26 changes: 26 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ export async function buildServer(): Server {
}
})

server.route({
method: 'GET',
path: '/api/v1/authors/{identity}/contents',
handler: handlers.Authors.listContent,
options: {
description: 'List Content Signed By a Given Author',
notes: 'Returns most recent results first',
tags: ['api', 'authors'],
response: {
failAction: 'log',
schema: Joi.object({
job: schema.Job
})
},
validate: {
params: Joi.object({
identity: Joi.string().required()
}),
query: Joi.object({
limit: Joi.number().optional(),
offset: Joi.number().optional(),
})
}
}
})


server.route({
method: 'GET',
Expand Down
30 changes: 29 additions & 1 deletion src/server/handlers/authors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import models from '../../models'
import { Op } from 'sequelize'
import models, { sequelize } from '../../models'

// list authors by difficulty of mined jobs purchased
// returns {"address": "difficulty"}
Expand Down Expand Up @@ -52,3 +53,30 @@ export async function show(req, h) {
return { address, jobs }

}

export async function listContent(req, h) {

if (!req.params.identity) return h.response({ error: 'no identity provided' }).code(400)

const contents = await models.Content.findAll({
where: {
[Op.or]: [
sequelize.where(
sequelize.literal(`bmap#>>'{MAP, 0, paymail}'`), '=', req.params.identity
),
sequelize.where(
sequelize.literal(`bmap#>>'{MAP, 0, pubkey}'`), '=', req.params.identity
),

]
},
order: [
['id', 'DESC'],
],
limit: req.query.limit || 100,
offset: req.query.offset || 0
});

return { contents: contents.map(c => c.toJSON()) }

}

0 comments on commit 1ba6640

Please sign in to comment.