aboutsummaryrefslogtreecommitdiff
path: root/services/feed/feed.class.ts
blob: 8c7cd0b53c2389dee90f894123929c61a03f62c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import _ from 'lodash';
import { Application } from '@feathersjs/express';
import { Params } from '@feathersjs/feathers';
import { Poll } from 'which-types';

import { PollSchema } from '../../models/polls/poll.schema';
import PollModel from '../../models/polls/poll.model';


export default class Feed {
  app!: Application;

  async find(params: Params): Promise<Poll[]> {
    return this.app.service('polls')
      .find(params)
      .then( // Move new verified polls on top
        (polls: Poll[]) => _.sortBy(polls, poll => poll.author.verified && !poll.userChoice)
      ).then( // But all seen posts go down
        (polls: Poll[]) => _.sortBy(polls, poll => !!poll.userChoice)
      );
  }

  setup (app: Application) {
    this.app = app;
  }
}