aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2018-10-30 18:44:42 +0100
committerMarvin Borner2018-10-30 18:44:42 +0100
commit94dcb2a098e978ff56eb711ae3c0b7abcb8d69ca (patch)
tree584210c84a27d31be6644de122dcb6865849b4e9
parent1309e462efc4a7bbdf538e9d209133c1b3c138a6 (diff)
parent39aa8530424310663c888f9e02224158961532e3 (diff)
Merge branch 'master' of github.com:texxme/Texx-Server
-rw-r--r--src/hooks/populate-user.js9
-rw-r--r--src/hooks/process-post.js35
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;
+ };
+ };
+};