feat(server): fill out activity stream queries
This commit is contained in:
@@ -1,29 +1,44 @@
|
||||
const appRoot = require( 'app-root-path' )
|
||||
const { validateServerRole, validateScopes } = require( `${appRoot}/modules/shared` )
|
||||
const { ForbiddenError, UserInputError, ApolloError, withFilter } = require( 'apollo-server-express' )
|
||||
const { getUserActivity, getStreamActivity, getResourceActivity, getUserTimeline } = require( '../../services/index' )
|
||||
const { getUserActivity, getStreamActivity, getResourceActivity, getUserTimeline, getActivityCountByResourceId, getActivityCountByStreamId, getActivityCountByUserId } = require( '../../services/index' )
|
||||
|
||||
|
||||
module.exports = {
|
||||
Query: {},
|
||||
User: {
|
||||
async activity( parent, args, context, info ) {
|
||||
if ( args.limit && args.limit > 100 )
|
||||
throw new UserInputError( 'Cannot return more than 100 items; please use pagination.' )
|
||||
|
||||
// TODO: cursor and total count
|
||||
let items = await getUserActivity( { userId: parent.id, timeEnd: args.timeEnd, limit: args.limit } )
|
||||
let { items, cursor } = await getUserActivity( { userId: parent.id, timeEnd: args.cursor, limit: args.limit } )
|
||||
let totalCount = await getActivityCountByUserId( { userId: parent.id } )
|
||||
|
||||
return { items }
|
||||
return { items, cursor, totalCount }
|
||||
}
|
||||
},
|
||||
|
||||
Stream: {
|
||||
async activity( parent, args, context, info ) {
|
||||
if ( args.limit && args.limit > 100 )
|
||||
throw new UserInputError( 'Cannot return more than 100 items; please use pagination.' )
|
||||
let { items, cursor } = await getStreamActivity( { streamId: parent.id, timeEnd: args.cursor, limit: args.limit } )
|
||||
let totalCount = await getActivityCountByStreamId( { streamId: parent.id } )
|
||||
|
||||
return { items, cursor, totalCount }
|
||||
}
|
||||
},
|
||||
|
||||
Branch: {
|
||||
async activity( parent, args, context, info ) {
|
||||
if ( args.limit && args.limit > 100 )
|
||||
throw new UserInputError( 'Cannot return more than 100 items; please use pagination.' )
|
||||
|
||||
let { items, cursor } = await getResourceActivity( { resourceType: 'branch', resourceId: parent.id, timeEnd: args.cursor, limit: args.limit } )
|
||||
let totalCount = await getActivityCountByResourceId( { resourceId: parent.id } )
|
||||
|
||||
return { items, cursor, totalCount }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,8 @@ module.exports = {
|
||||
dbQuery.limit( limit )
|
||||
|
||||
let results = await dbQuery.select( '*' )
|
||||
return results
|
||||
|
||||
return { items: results, cursor: results.length > 0 ? results[ results.length - 1 ].time.toISOString() : null }
|
||||
},
|
||||
|
||||
async getUserActivity( { userId, timeEnd, limit } ) {
|
||||
@@ -45,7 +46,7 @@ module.exports = {
|
||||
dbQuery.limit( limit )
|
||||
|
||||
let results = await dbQuery.select( '*' )
|
||||
return results
|
||||
return { items: results, cursor: results.length > 0 ? results[ results.length - 1 ].time.toISOString() : null }
|
||||
},
|
||||
|
||||
async getResourceActivity( { resourceType, resourceId, timeEnd, limit } ) {
|
||||
@@ -59,7 +60,7 @@ module.exports = {
|
||||
dbQuery.limit( limit )
|
||||
|
||||
let results = await dbQuery.select( '*' )
|
||||
return results
|
||||
return { items: results, cursor: results.length > 0 ? results[ results.length - 1 ].time.toISOString() : null }
|
||||
},
|
||||
|
||||
async getUserTimeline( { userId, timeEnd, limit } ) {
|
||||
@@ -81,6 +82,21 @@ module.exports = {
|
||||
`
|
||||
|
||||
let results = await knex.raw( dbRawQuery, [ userId, timeEnd, limit ] )
|
||||
return results
|
||||
return { items: results, cursor: results.length > 0 ? results[ results.length - 1 ].time.toISOString() : null }
|
||||
},
|
||||
|
||||
async getActivityCountByResourceId( { resourceId } ) {
|
||||
let [ res ] = await StreamActivity().count().where( { resourceId } )
|
||||
return parseInt( res.count )
|
||||
},
|
||||
|
||||
async getActivityCountByStreamId( { streamId } ) {
|
||||
let [ res ] = await StreamActivity().count().where( { streamId } )
|
||||
return parseInt( res.count )
|
||||
},
|
||||
|
||||
async getActivityCountByUserId( { userId } ) {
|
||||
let [ res ] = await StreamActivity().count().where( { userId } )
|
||||
return parseInt( res.count )
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user