diff options
Diffstat (limited to 'services/feed')
-rw-r--r-- | services/feed/feed.class.ts | 17 | ||||
-rw-r--r-- | services/feed/feed.hooks.ts | 32 | ||||
-rw-r--r-- | services/feed/feed.service.ts | 10 |
3 files changed, 59 insertions, 0 deletions
diff --git a/services/feed/feed.class.ts b/services/feed/feed.class.ts new file mode 100644 index 0000000..ceb86e6 --- /dev/null +++ b/services/feed/feed.class.ts @@ -0,0 +1,17 @@ +import { Application } from '@feathersjs/express'; +import { Params } from '@feathersjs/feathers'; +import { Poll } from 'which-types'; + + +export default class Feed { + app!: Application; + + async find(params: Params): Promise<Poll[]> { + return this.app.service('polls').find(params); + } + + setup(app: Application): void { + this.app = app; + } +} + diff --git a/services/feed/feed.hooks.ts b/services/feed/feed.hooks.ts new file mode 100644 index 0000000..54f6d61 --- /dev/null +++ b/services/feed/feed.hooks.ts @@ -0,0 +1,32 @@ +import _ from 'lodash'; +import { HookContext } from '@feathersjs/feathers'; +import { iff } from 'feathers-hooks-common'; +import isAuthenticated from '../../hooks/isAuthenticated'; + +const raiseNewVerifedPolls = async (context: HookContext): Promise<HookContext> => { + // Raise unseen verified polls to the very top + context.result = _.sortBy( + context.result, + poll => !(poll.author.verified && !poll.userChoice) + ); + return context; +}; + +const lowerOldPolls = async (context: HookContext): Promise<HookContext> => { + // Move all seen polls down + context.result = _.sortBy( + context.result, + poll => !!poll.userChoice + ); + return context; +}; + +export default { + after: { + find: [ + iff(isAuthenticated, raiseNewVerifedPolls), + iff(isAuthenticated, lowerOldPolls) + ] + } +}; + diff --git a/services/feed/feed.service.ts b/services/feed/feed.service.ts new file mode 100644 index 0000000..d326656 --- /dev/null +++ b/services/feed/feed.service.ts @@ -0,0 +1,10 @@ +import { Application } from '@feathersjs/express'; +import Feed from './feed.class'; + +import hooks from './feed.hooks'; + +export default (app: Application): void => { + app.use('/feed', new Feed()); + app.service('feed').hooks(hooks); +}; + |