Skip to content

Commit

Permalink
Merge pull request #16 from brukmula/social
Browse files Browse the repository at this point in the history
Social
  • Loading branch information
samhaswon committed Jul 30, 2023
2 parents e39705c + 98513cb commit 53f2014
Show file tree
Hide file tree
Showing 15 changed files with 2,941 additions and 1,600 deletions.
140 changes: 140 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,133 @@ Example:
}
```

## Social endpoint
### Follow/friend user
Method: POST
#### '/add-friend
This endpoint requires the headers `user` to be the JWT from user authentication and `to-follow` to be the user id (UID)
of the user to follow.
#### Returns
- Successful following of the user ([201](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201))
```js
`Successfully followed user ${user.uid}, ${user.displayName}`
// Display name is not necessary, but there. It's not stored here since it can change and ∴ should be retrieved from
// the profile endpoint
```
- Server error with firebase or something else ([500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500))
```js
"Error following user"
```
- User has already been followed ([409](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409))
```js
"User already followed"
```
- Error finding the user's friend list in the database ([500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500))
```js
// This is a backend issue if you get this
"Database error when attempting to following user"
```
- Error finding the user to follow ([404](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404))
```js
"Error finding user"
```
- Invalid JWT ([500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500))
```js
"Invalid token"
```
- No UID sent to follow ([400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400))
```js
"No user to follow"
```
- No JWT sent ([400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400))
```js
"No user token was sent"
```

### Unfollow/unfriend user
Method: POST
#### '/remove-friend
This endpoint requires the headers `user` to be the JWT from user authentication and `to-unfollow` to be the user id
(UID) of the user to unfollow.
#### Returns
- Successful unfollow/unfriend ([200](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200))
```js
`Successfully unfollowed user ${user_to_unfollow}`
```
- Database error when removing user ([500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500))
```js
// The code shouldn't get here
"Error removing user."
```
- Could not find the user in the friends list to unfollow/unfriend ([404](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404))
```js
`Could not find user to unfollow (${user_to_unfollow})`
```
- Friends list is empty ([404](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404))
```js
"Friends list is, sadly, empty"
```
- Invalid JWT ([500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500))
```js
"Invalid token"
```
- No UID sent to unfollow/unfriend ([400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400))
```js
"No user to unfollow"
```
- No JWT sent ([400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400))
```js
"No user token was sent"
```

### Search users
Method: GET
#### /search-users
Search for a user of the app. Uses the query parameter `query` with a length >= 2. The search is based on both email and
display name.
#### Returns
- Valid query ([200](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200))
Example: http://localhost:3000/search-users?query=username
```json
[
{
"item": {
"uid": "5sjSwq6KClSb12a5Rp56xJrh44C3",
"email": "username@email.com",
"displayName": "Example Username",
"photoURL": "https://httpcats.com/204"
},
"refIndex": 0
},
{
"item": {
"uid": "RfNd5qDTNdeOlqq59QnXmb7xSEF3",
"email": "username2@email.com"
},
"refIndex": 4
},
{
"item": {
"uid": "AFDKGthhNKeCx5DtzbmA5XpaSk82",
"email": "sahoward42@tntech.edu",
"displayName": "newname"
},
"refIndex": 2
},
{
"item": {
"uid": "IlTFW3JUzwX8bwT5fyxYCpqwJGT2",
"email": "testuser@email.com"
},
"refIndex": 3
}
]
```
- Invalid length and other query issues ([400](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400))
```js
"Bad query"
```

## User endpoint
### Roots
Method: GET, POST
Expand Down Expand Up @@ -637,13 +764,26 @@ Method: GET
#### /profile
This endpoint requires the header `user` to be either the uid of the user to retrieve or the JWT from before.
#### Returns ([200](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200))
Passing the UID:
```json
{
"displayName":"Example User",
"uid":"5sjSwq6KClSb12a5Rp56xJrh44C3",
"photoURL": "https://httpcats.com/204.jpg"
}
```
Passing the JWT (includes friends/following list):
```json
{
"displayName": "Example Display Name",
"uid": "5sjSwq6KClSb12a5Rp56xJrh44C3",
"friends": {
"-NaS3SRrRZbvLXdUBT4Z": {
"uid": "RfNd5qDTNdeOlqq59QnXmb7xSEF3"
}
}
}
```
#### Bad requests
- Invalid/bad UID ([500](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500))
```js
Expand Down
43 changes: 25 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Modules
const cors = require('cors');
const compression = require('compression');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const createError = require('http-errors');
const express = require('express');
Expand All @@ -8,11 +11,31 @@ const path = require('path');
// Routes
const bibleRouter = require('./routes/bible');
// const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const { usersRouter, usersFirebaseInit } = require('./routes/users');
const { socialRouter, socialFirebaseInit } = require('./routes/social');

// Firebase imports
const firebase = require('firebase');
const admin = require("firebase-admin");
const { applicationDefault } = require("firebase-admin/app");
const { firebaseConfig } = require('./routes/firebaseConf');

// Firebase setup
const firebaseApp = admin.initializeApp({
credential: applicationDefault(),
databaseURL: 'https://basil-backend-47d01-default-rtdb.firebaseio.com/'
});
const db = admin.database();
firebase.initializeApp(firebaseConfig);
usersFirebaseInit(firebase, firebaseApp, db);
socialFirebaseInit(firebase, firebaseApp, db);

// App
const app = express();
const port = process.env.PORT || 3000;
app.use(helmet()); // For security policy
app.use(cors()); // For Cross-Origin Resource Sharing
app.use(compression()); // For bandwidth saving on the more intensive actions

// view engine setup
app.set('views', path.join(__dirname, 'views'));
Expand All @@ -30,28 +53,12 @@ app.use(express.static(path.join(__dirname, 'public')));
// Routes usage
// app.use('/', indexRouter);
app.use('/api', bibleRouter);
app.use('/', usersRouter);
app.use('/', usersRouter, socialRouter);

app.get('/health', (req, res) => {
res.status(200).send("Healthy: OK");
})

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

app.listen(port, () => {
console.log(`API server listening on port ${port}`);
});
Expand Down
Loading

0 comments on commit 53f2014

Please sign in to comment.