aboutsummaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
Diffstat (limited to 'services')
-rw-r--r--services/feed/feed.class.ts17
-rw-r--r--services/feed/feed.hooks.ts32
-rw-r--r--services/feed/feed.service.ts10
-rw-r--r--services/index.ts2
-rw-r--r--services/polls/polls.hooks.ts6
-rw-r--r--services/profiles/profiles.class.ts19
-rw-r--r--services/profiles/profiles.hooks.ts8
-rw-r--r--services/profiles/profiles.service.ts3
8 files changed, 81 insertions, 16 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);
+};
+
diff --git a/services/index.ts b/services/index.ts
index bdce04f..1763a17 100644
--- a/services/index.ts
+++ b/services/index.ts
@@ -4,6 +4,7 @@ import Polls from './polls/polls.service';
import Profiles from './profiles/profiles.service';
import Votes from './votes/votes.service';
import Auth from './auth/auth.service';
+import Feed from './feed/feed.service';
import tryAuthenticate from '../hooks/tryAuthenticate';
import logging from '../hooks/logging';
@@ -15,6 +16,7 @@ export default (app: Application): void => {
app.configure(Polls);
app.configure(Profiles);
app.configure(Votes);
+ app.configure(Feed);
app.hooks({
before: {
diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts
index 13d6f63..77fcc7a 100644
--- a/services/polls/polls.hooks.ts
+++ b/services/polls/polls.hooks.ts
@@ -1,8 +1,12 @@
import convertPoll from '../../hooks/convertPoll';
+import sortByDate from '../../hooks/sortByDate';
export default {
+ before: {
+ find: sortByDate
+ },
after: {
- all: [convertPoll]
+ all: convertPoll
}
};
diff --git a/services/profiles/profiles.class.ts b/services/profiles/profiles.class.ts
index 52d6b47..3461cbc 100644
--- a/services/profiles/profiles.class.ts
+++ b/services/profiles/profiles.class.ts
@@ -1,9 +1,20 @@
-import { PollSchema } from '../../models/polls/poll.schema';
-import PollModel from '../../models/polls/poll.model';
+import { Application } from '@feathersjs/express';
+import { Poll } from 'which-types';
+
export default class Profiles {
- async get(id: string): Promise<PollSchema[]> {
- return PollModel.find({ authorId: id }).lean();
+ app!: Application;
+
+ async get(id: string): Promise<Poll[]> {
+ return this.app.service('polls').find({
+ query: {
+ authorId: id
+ }
+ });
+ }
+
+ setup(app: Application): void {
+ this.app = app;
}
}
diff --git a/services/profiles/profiles.hooks.ts b/services/profiles/profiles.hooks.ts
deleted file mode 100644
index 13d6f63..0000000
--- a/services/profiles/profiles.hooks.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import convertPoll from '../../hooks/convertPoll';
-
-export default {
- after: {
- all: [convertPoll]
- }
-};
-
diff --git a/services/profiles/profiles.service.ts b/services/profiles/profiles.service.ts
index e860426..ae06cf9 100644
--- a/services/profiles/profiles.service.ts
+++ b/services/profiles/profiles.service.ts
@@ -1,10 +1,7 @@
import { Application } from '@feathersjs/express';
import Profiles from './profiles.class';
-import hooks from './profiles.hooks';
-
export default (app: Application): void => {
app.use('/profiles', new Profiles());
- app.service('profiles').hooks(hooks);
};