diff options
-rw-r--r-- | src/hooks/populate-user.js | 9 | ||||
-rw-r--r-- | src/hooks/process-post.js | 35 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/hooks/populate-user.js b/src/hooks/populate-user.js new file mode 100644 index 0000000..dfa58cf --- /dev/null +++ b/src/hooks/populate-user.js @@ -0,0 +1,9 @@ +// 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 context; + }; +}; diff --git a/src/hooks/process-post.js b/src/hooks/process-post.js new file mode 100644 index 0000000..d23f451 --- /dev/null +++ b/src/hooks/process-post.js @@ -0,0 +1,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; + }; + }; +}; |