Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

TwitterBaby_Backend_Documentation

Jason Chia-Hsien Ho edited this page Jul 17, 2018 · 1 revision

Table of Contents

Basic Server Operation

q to shutdown server
i to reconstruct database to default (w/ some initial collections)
r to reconstruct testing database

REST API

Authorization

All routes except Login, Signup and GetWSConnection, need to have a Authorization header, which contains a JWT.

Example
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA

FetchUserInfo

Return user info for a specific user, and whether it is followed by the current user.

URL

/api/v1/userInfo/:username

Method

GET

URL Params

username = [alphanumeric]

Success Response

Code: 200 OK
Content:

{
    "userinfo": {
        "username": "TomRiddle",
        "firstname": "Tom",
        "lastname": "Riddle",
        "email": "[email protected]",
        "bio": "Hi everyone, this is Lord Voldemort.",
        "tag": "Voldemort"
    },
    "followercount": 12,
    "followingcount": 5,
    "followed": true
}

Error Response

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X GET http://localhost:1323/api/v1/userInfo/TomRiddle -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA'

Notes

# Response does not include the full list of followers and following, only the counts.


Follow

Add a specific username to the current user's following set, and add current user to that user's follower list.

URL

/api/v1/follow/:username

Method

POST

URL Params

username = [alphanumeric]

Success Response

Code: 200 OK
Content: user's following list

[
   "JasonHe",
   "MarsLee",
   "TomRiddle",
   "test115415",
   "JasonHo"
]

Error Response

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X POST http://localhost:1323/api/v1/follow/JasonHo -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA'

Unfollow

Remove a specific username from the current user's following set, and remove current user from that user's follower list.

URL

/api/v1/unfollow/:username

Method

POST

URL Params

username = [alphanumeric]

Success Response

Code: 200 OK
Content: user's following list

[
   "JasonHe",
   "TomRiddle",
   "test115415",
   "JasonHo"
]

Error Response

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X POST http://localhost:1323/api/v1/unfollow/JasonHo -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA'

Login

Login to an account associated with the email address and the password.

URL

/api/v1/login

Method

POST

Data Params

{
    "email":"[email protected]",
    "password":"test1"
}
Fields
Parameter Description Type Required
email user's email address alphanumeric Yes
password user's password alphanumeric Yes

Success Response

Code: 200 OK
Content: user data (contains a JWT)

{
    "username": "JasonHo",
    "firstname": "Jason",
    "lastname": "Hoo",
    "email": "[email protected]",
    "followers": [
        "MarsLee",
        "TomRiddle"
    ],
    "following": [
        "JasonHe",
        "MarsLee",
        "TomRiddle",
    ],
    "bio": "Jason Ho is unstoppable!",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA"
}

Error Response

Code: 401 Unauthorized (If an account associated with the email address and password is not found.)
Content:

{
    "message": "Invalid email or password."
}

Sample call:

curl -X POST http://localhost:1323/api/v1/login -H 'Content-Type: application/json' -d '{"email":"[email protected]","password":"test1"}'

Signup

Create an user instance.

URL

/api/v1/signup

Method

POST

Data Params

{
    "username":"eve",
    "firstname":"Evelyn",
    "lastname":"Clarkson",
    "password":"pass",
    "email":"[email protected]",
    "bio":"Hi, this is Evelyn!",
    "tag":"Angel",
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}
Fields
Parameter Description Type Required
username user's email address alphanumeric Yes
firstname user's firstname alphanumeric Yes
lastname user's lastname alphanumeric No
password user's password alphanumeric Yes
email user's email alphanumeric Yes
bio user's biography alphanumeric No
tag user's personal tag alphanumeric No
picture user's picture base 64 string No

Success Response

Code: 201 Created
Content: user data

{
    "username": "eve",
    "firstname": "Evelyn",
    "lastname":"Clarkson",
    "email":"[email protected]",
    "bio":"Hi, this is Evelyn!",
    "tag":"Angel",
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}

Error Response

Code: 400 Bad Request (If one of username, firstname, email, password is empty, or username, email already used.)
Content:

{
    "message": "Username or email already used."
}

Sample call:

curl -X POST http://localhost:1323/api/v1/signup -H 'Content-Type: application/json' -d '{"username":"eve","firstname":"Evelyn","password":"pass","email":"[email protected]"}'

ShowFollower

Return the follower list for a specific user, along with some followers info.

URL

/api/v1/showFollower/:username

Method

GET

URL Params

username = [alphanumeric]

Success Response

Code: 200 OK
Content: followers' info

[
    {
        "username": "JasonHo",
        "firstname": "Jason",
        "lastname": "Hoo",
        "bio": "Jason Ho is unstoppable!"
    },
    {
        "username": "TomRiddle",
        "firstname": "Tom",
        "lastname": "Riddle",
        "bio": "Hi everyone, this is Lord Voldemort."
    }
]

Error Response

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X GET http://localhost:1323/api/v1/showFollower/TomRiddle -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA'

ShowFollowing

Return the following list for a specific user, along with some followings info.

URL

/api/v1/showFollowing/:username

Method

GET

URL Params

username = [alphanumeric]

Success Response

Code: 200 OK
Content: followings' info

[
    {
        "username": "TomRiddle",
        "firstname": "Tom",
        "lastname": "Riddle",
        "bio": "Hi everyone, this is Lord Voldemort."
    },
    {
        "username": "test115415",
        "firstname": "Jason",
        "lastname": "He"
    }
]

Error Response

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X GET http://localhost:1323/api/v1/showFollowing/TomRiddle -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA'

UpdateUserInfo

Update a user's info, only firstname, lastname, bio, and tag are allowed to be modified.

URL

/api/v1/updateUserInfo

Method

POST

Data Params

{
    "firstname":"Evelyn",
    "lastname":"Clarkson",
    "bio":"Hi, this is Evelyn!",
    "tag":"Angel"
}
Fields
Parameter Description Type Required
firstname user's firstname alphanumeric Yes
lastname user's lastname alphanumeric Yes
bio user's biography alphanumeric Yes
tag user's personal tag alphanumeric Yes

Success Response

Code: 200 OK
Content: user data

{
    "username": "eve",
    "firstname": "Evelyn",
    "lastname":"Clarkson",
    "email":"[email protected]",
    "bio":"Hi, this is Evelyn!",
    "tag":"Angel",
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}

Error Response

Code: 400 Bad Request (If one or more fields are empty.)
Content:

{
    "message": "All fields must not be empty."
}

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X POST http://localhost:1323/api/v1/updateUserInfo -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA' -d '{"firstname":"Jason","lastname":"Ho","bio":"","tag":""}'

Notes

# Request body must contain all four fields, even if they are empty or not modified.


UpdateProfilePicture

Update a user's profile picture, or remove profile picture by sending an empty string.

URL

/api/v1/updateProfilePic

Method

POST

Data Params

{
    "picture":"iVBORw0KGgoAAAANSUhEUgAAACAAIrGAAAAjVBMVEXp6en///+8vLwPZ66L1fPY"
}
Fields
Parameter Description Type Required
picture user's picture base 64 string Yes

Success Response

Code: 201 Created
Content: user data

{
    "username": "eve",
    "firstname": "Evelyn",
    "lastname":"Clarkson",
    "email":"[email protected]",
    "bio":"Hi, this is Evelyn!",
    "tag":"Angel",
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}

Error Response

Code: 400 Bad Request (If the image is larger than 10 MB.)
Content:

{
    "message": "Image must be smaller than 10 MB."
}

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -X POST http://localhost:1323/api/v1/updateProfilePic -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1MzQ1NTgsInVzZXJuYW1lIjoiSmFzb25IbyJ9.0hr1JjZCKaBuL1-pn7ddzkUUbxCIKHddhsx56IUYXeA' -d '{"picture":"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAjVBMVEXp6en///+8vLwPZ66L1fPY"}'

Notes

# Image is to be encoded into base 64 string, and cannot be larger than 10 MB.


GetWSConnection

Upgrade a client connection to websocket.

URL

/api/v1/ws/:username

Method

GET

URL Params

username = [alphanumeric]
Code: 201 Created on success
Content: user data

{
    "username": "eve",
    "firstname": "Evelyn",
    "lastname":"Clarkson",
    "email":"[email protected]",
    "bio":"Hi, this is Evelyn!",
    "tag":"Angel",
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}

Error Response

Code: 400 Bad Request (If failed to make connection.)
Content:

{
    "message": "Failed to make web socket connection."
}

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User does not exist."
}

Sample call:

curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: localhost:1323" -H "Origin: http://localhost:1323" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" -H "Sec-WebSocket-Version: 13" http://localhost:1323/api/v1/ws/JasonHo

DeleteTweet

Delete a specific tweet.

URL

/api/v1/deleteTweet/:tweet

Method

DELETE

URL Params

tweet = [alphanumeric]

Success Response

Code: 200 OK
Content:

{
}

Error Response

Code: 400 Bad Request if tweet ID is malformed. Content:

{
    "message": "Malformed tweet ID."
}

Error Response

Code: 404 Not Found if the tweet is not in the database. Content:

{
    "message": "Tweet does not exist."
}

Sample call:

curl -X DELETE http://127.0.0.1:1323/api/v1/deleteTweet/59fb210c311bc3079616e46e -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDk4ODkzMDksInVzZXJuYW1lIjoiSmFzb25IbyJ9.46md5Q1-fg79egQPZ0kEqqbC3WK9jRGsnwan67y3x7s'

FetchTweets

Handle requests asking for a list of tweets posted by a specific user.

URL

/api/v1/tweetlist/:username

Method

GET

URL Params

username = [alphanumeric]

page = [number]

perpage = [number]

Success Response

Code: 200 OK on success.

Content: tweet data

{
    “Page”: “1”,
    "totalpage": “2”,
    "totaltweets”:”2”,
    "tweetlist":[
	{
		"id":"59ff683957a437fc2475164f",
		"owner":"JasonHo",
		"message":"I love 	Florida!",
		"numcomment":0,
		"timestamp":"2017-11-05T14:36:25.852-05:00"
	},
	{
		"id":"59ff683957a437fc2475164f",
		"owner":"JasonHo",
		"message":"I love 	Florida!",
		"numcomment":0,
		"timestamp":"2017-11-05T14:36:25.852-05:00"
	}
     ]
}

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User [username] does not exist."
}

Sample call:

curl -X GET 'http://127.0.0.1:1323/api/v1/tweetlist/JasonHo?page=1&perpage=6' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTAwNjMzOTksInVzZXJuYW1lIjoiSmFzb25IbyJ9.WyNqjjuWBGd4oeiM68l2mRqRMXO-asdb7by7b7imKdU'

FetchTweetTimeLine

Handle requests asking for a list of tweets timeline from the following list of specific user.

URL

/api/v1/tweettimeline/:username

Method

GET

URL Params

username = [alphanumeric]

page = [number]

perpage = [number]

Success Response

Code: 200 OK on success.

Content: tweetTimeline data

{
    “Page”: “1”,
    "totalpage": “2”,
    "totaltweets”:”2”,
    "tweetlist":[
	{
		"id":"59ff683957a437fc2475164f",
		"owner":"JasonHo",
		"message":"I love 	Florida!",
		"numcomment":0,
		"timestamp":"2017-11-05T14:36:25.852-05:00"
	},
	{
		"id":"59ff683957a437fc2475164f",
		"owner":"JasonHo",
		"message":"I love 	Florida!",
		"numcomment":0,
		"timestamp":"2017-11-05T14:36:25.852-05:00"
	}
     ]
}

Code: 404 Not Found (If the user is not in the database.)
Content:

{
    "message": "User [username] does not exist."
}

Sample call:

curl -X GET 'http://127.0.0.1:1323/api/v1/tweettimeline/JasonHo?page=1&perpage=24' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDk4ODkzMDksInVzZXJuYW1lIjoiSmFzb25IbyJ9.46md5Q1-fg79egQPZ0kEqqbC3WK9jRGsnwan67y3x7s'

NewTweet

Add one tweet for a specific user.

URL

/api/v1/newTweet

Method

POST

Data Params

{
    "message":"Test comment"
    "picture":"iVBORw0KGgoAAAANSUhEUgAAACAAIrGAAAAjVBMVEXp6en///+8vLwPZ66L1fPY"
}
Fields
Parameter Description Type Required
picture user's picture base 64 string Yes

Success Response

Code: 201 Created
Content: tweet data

{
    “owner”: “MarsLee”,
    “Message”: “Hi wow”,
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}

Error Response

Code: 400 Bad Request (If the image is larger than 10 MB.)
Content:

{
    "message": "Image must be smaller than 10 MB."
}

Code: 400 Bad Request (If the image is larger than 10 MB.)
Content:

{
    "message": "Message cannot be empty."
}

Sample call:

curl -X POST http://127.0.0.1:1323/api/v1/newTweet -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTAwNjMzOTksInVzZXJuYW1lIjoiSmFzb25IbyJ9.WyNqjjuWBGd4oeiM68l2mRqRMXO-asdb7by7b7imKdU' -d '{"message":"Test comment", "picture":"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAjVBMVEXp6en///+8vLwPZ66L1fPY"}'

Notes

# Image is to be encoded into base 64 string, and cannot be larger than 10 MB.


NewComment

Add one comment for a specific tweet.

URL

/api/v1/newcomment/:tweet

Method

POST

URL Params

tweet = [alphanumeric]

Data Params

{
    "message”:”test message“
}

Success Response

Code: 200 Created comment Content: comment data

{
    "FromTweetID": "59fdd076311bc30ffb5592b1 ",
    "FromUsername": “MarsLee”,
    "Message”:”Test test test“
}

Error Response

Code: 400 Bad Request (Bad Request if the content of the comment is empty.)
Content:

{
    "message": "Message cannot be empty."
}

Code: 404 Not Found (Not Found if the tweetID is not in the database.)
Content:

{
    "message": "Invalid tweet ID"
}

Sample call:

curl -X POST http://127.0.0.1:1323/api/v1/newcomment/59fdd076311bc30ffb5592b1 -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTAwNjMzOTksInVzZXJuYW1lIjoiSmFzb25IbyJ9.WyNqjjuWBGd4oeiM68l2mRqRMXO-asdb7by7b7imKdU' -d '{"message":"Are your kidding me?"}'

FetchComment

Handle requests asking for a list of comment depend on a specific tweet.

URL

/api/v1/fetchcomment/:tweet

Method

GET

URL Params

tweet = [alphanumeric]

Success Response

Code: 200 OK on success.

Content: tweet data

{
    “totalcomment”: “2”,
    "CommentList":[
	{
    		"_id": {
        			"$oid": "59ffc54c9d1fa261b27901a3"
    		},
    		"fromtweetid": "59fdd076311bc30ffb5592b1",
	    	"fromusername": "JasonHo",
	    	"message": "Comment2",
	    	"timestamp": {
	        		"$date": "2017-11-06T02:13:32.915Z"
	   	 }
	},
	{
    		"_id": {
        			"$oid": "59ffc54c9d1fa261b27901a3"
    		},
    		"fromtweetid": "59fdd076311bc30ffb5592b1",
	    	"fromusername": "JasonHo",
	    	"message": "Comment2",
	    	"timestamp": {
	        		"$date": "2017-11-06T02:13:32.915Z"
	   	 }
	},
     ]
}

Code: 404 Not Found (if the tweetID is not in the database.)
Content:

{
    "message": "Invalid tweet ID"
}

Sample call:

curl -X GET http://127.0.0.1:1323/api/v1/fetchcomment/59fdd076311bc30ffb5592b1 -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTAwNjMzOTksInVzZXJuYW1lIjoiSmFzb25IbyJ9.WyNqjjuWBGd4oeiM68l2mRqRMXO-asdb7by7b7imKdU'

ReTweet

Retweet a tweet.

URL

/api/v1/reTweet

Method

POST

Data Params

{
    "message":"Test retweet",
   "Idretweet":"59feca729d1fa25e894ad8d0"
}

Success Response

Code: Created on success, along with the retweet data. Content: user data

{
    “owner”: “MarsLee”,
    “message”: “test”,
    "ownerretweet”:”JasonHo”,
    "messageretweet”:”Retweet message”,
    "picture":"GVk/1ysJQC/DxcdC2QMQ6J/tylP77IEUsm3WnppWFoQsVuuNp/VqQRAMm8jT"
}

Error Response

Code: 400 Bad Request (If the image is larger than 10 MB.)
Content:

{
    "message": "Image must be smaller than 10 MB."
}

Code: 400 Bad Request (if the content of the retweet is empty.)
Content:

{
    "message": "Message cannot be empty."
}

Code: 404 Not Found (if the tweet is not in the database.)
Content:

{
    "message": "Invalid tweet ID"
}

Sample call:

curl -X POST http://127.0.0.1:1323/api/v1/reTweet -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTE1NzI3MDcsInVzZXJuYW1lIjoiSmFzb25IbyJ9.Ic1GsRvF3gxmUtbRhlSaFFF8JQVtnLLNYw0PYZeeFJY' -d '{"Idretweet":"59feca729d1fa25e894ad8d0", "message":"Test retweet"}'

Notifications

On Connect

After a websocket connection is established, the server will dump all unacked notifications to the browser, in timestamp ascending order(oldest first).


New Tweet

Whenever there is a new tweet from any of the followees of the current user, the server will push a new tweet notification to the current user's browser.

Message type

Text

Sample message

"New tweets."


New Follower

Whenever a new follower follows the current user, the server will push a follow notification to the current user's browser, containing the timestamp and the follower.

Message type

JSON

Sample message

{
    "timestamp":"2017-11-04T18:13:03.902-04:00",
    "type":"Follow",
    "detail":"TomRiddle"
}  

Clear Notifications

If a client sends a clear notifications message to the server, the server will then remove all notifications from the unacked notification list.

Message type

Text

Sample message

"Clear notifications."

Clone this wiki locally