Selaa lähdekoodia

Add the multi-view route

Matt Coles 9 vuotta sitten
vanhempi
commit
b43ca734ca
3 muutettua tiedostoa jossa 67 lisäystä ja 1 poistoa
  1. 26 1
      README.md
  2. 37 0
      presenters/user-controller.js
  3. 4 0
      routes/user/view.js

+ 26 - 1
README.md

@@ -96,7 +96,32 @@ The error codes are as follows, `1` indicates the username or password was
96 96
 invalid and `2` indicates that the login request was malformed.
97 97
 
98 98
 ### /user/view/
99
-As yet unimplemented but will eventually return all of the users.
99
+To view all the public information for all users at once, a `GET` request should
100
+be sent with no data, and the returned response will look like this:
101
+```javascript
102
+{
103
+    "users": [
104
+        {
105
+            "username": "test1",
106
+            "societies": [
107
+                "TestSociety2"
108
+            ],
109
+            "friends": [],
110
+            "accepted_events": []
111
+        },
112
+        {
113
+            "username": "test2",
114
+            "societies": [
115
+                "TestSociety2"
116
+            ],
117
+            "friends": [],
118
+            "accepted_events": []
119
+        },
120
+        { ... } // More items here
121
+    ]
122
+}
123
+```
124
+There are no error codes for this route.
100 125
 
101 126
 ### /user/view/:user
102 127
 To view the public information for any given `:user`, a `GET` request should be

+ 37 - 0
presenters/user-controller.js

@@ -39,6 +39,43 @@ module.exports = {
39 39
     });
40 40
   },
41 41
 
42
+  get_all_public_infos: function (complete) {
43
+    var self = this;
44
+    var stream = redis.scanStream({
45
+      match: "user:*"
46
+    });
47
+    var usernames = [];
48
+    stream.on('data', function (keys) {
49
+      keys.map(function (key) {
50
+        usernames.push(key.split(":")[1]);
51
+      });
52
+    });
53
+    stream.on('end', function () {
54
+      var user_objects = [];
55
+      if (usernames.length === 0) {
56
+        complete({
57
+          "users": []
58
+        })
59
+      } else {
60
+        for (var ii = 0; ii < usernames.length; ii++) {
61
+          self.get_public_user_info(usernames[ii], function (response) {
62
+            user_objects.push(response.user);
63
+            if (user_objects.length === usernames.length) {
64
+              user_objects.sort(function(a, b) {
65
+                var textA = a.username.toLowerCase();
66
+                var textB = b.username.toLowerCase();
67
+                return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
68
+              });
69
+              complete({
70
+                "users": user_objects
71
+              })
72
+            }
73
+          });
74
+        }
75
+      }
76
+    });
77
+  },
78
+
42 79
   get_user_from_auth: function (auth, complete) {
43 80
     var auth_key = "auth-key:" + auth;
44 81
 

+ 4 - 0
routes/user/view.js

@@ -11,5 +11,9 @@ var perform = function (req, res) {
11 11
     user_controller.get_public_user_info(req.params.user.toLowerCase(), function (response) {
12 12
       res.send(response);
13 13
     });
14
+  } else {
15
+    user_controller.get_all_public_infos(function (response) {
16
+      res.send(response);
17
+    });
14 18
   }
15 19
 };