aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/hooks/process-post.js
blob: d23f451c1b2b0cdc886878ae133b6f5a024f9508 (plain) (blame)
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
28
29
30
31
32
33
34
35
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html

// eslint-disable-next-line no-unused-vars
module.exports = function (options = {}) {
  return async context => {
    return async context => {
      const {data} = context;

      // Throw an error if we didn't get a text
      if (!data.text) {
        throw new Error('A post must have a text');
      }

      // The authenticated user
      const user = context.params.user;
      // The actual message text
      const text = context.data.text
      // Posts can't be longer than 400 characters
        .substring(0, 400);

      // Override the original data (so that people can't submit additional stuff)
      context.data = {
        text,
        // Set the user id
        userId: user._id,
        // Add the current date
        createdAt: new Date().getTime()
      };

      // Best practise, hooks should always return the context
      return context;
    };
  };
};