Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin get admin tweets #862

Open
wants to merge 90 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
6b4b881
feat: create user seedes
TCY1020 Aug 20, 2023
e97714a
feat: user & admin sigin, error-handler
TCY1020 Aug 20, 2023
66a795a
Merge pull request #1 from susu725/develop
susu725 Aug 20, 2023
4d05ad4
feat: add tweets seed
susu725 Aug 20, 2023
c48fb19
windows problem
susu725 Aug 20, 2023
f484c42
Merge pull request #2 from susu725/tweets-seed
TCY1020 Aug 20, 2023
5febf38
fix: windows problem
susu725 Aug 20, 2023
3e94119
feat: add replies seed
susu725 Aug 20, 2023
9d36ce6
Merge pull request #3 from susu725/replies-seed
TCY1020 Aug 21, 2023
7988a02
feat: create auth
TCY1020 Aug 21, 2023
37b2655
Merge branch 'authenticator'
TCY1020 Aug 21, 2023
3c8d477
fix: restore migrations to original
susu725 Aug 21, 2023
b3c19b6
fix: modify seeders field name
susu725 Aug 21, 2023
36858e8
feat: add getTweets function
susu725 Aug 21, 2023
e9460f5
feat: add getTweet function
susu725 Aug 21, 2023
46f5728
refactor: modify tweets seed
susu725 Aug 21, 2023
52a68bb
feat: add postTweet function
susu725 Aug 21, 2023
6f3b942
Merge remote-tracking branch 'origin/master' into tweets-function
susu725 Aug 21, 2023
0f908dc
fix: modify seeders field name
susu725 Aug 21, 2023
625ef04
Merge pull request #5 from susu725/tweets-function
TCY1020 Aug 21, 2023
ed95c57
feat: add getReplies function
susu725 Aug 21, 2023
f02ec05
feat: passport & auth, adminGetUser
TCY1020 Aug 21, 2023
b178bf9
Merge branch 'master' of https://github.com/susu725/twitter-api-2020
TCY1020 Aug 21, 2023
1d24688
Merge branch 'adminGetUser'
TCY1020 Aug 21, 2023
ec81bfe
style: white-space
TCY1020 Aug 21, 2023
3e12b4f
feat: reqUser has data
TCY1020 Aug 21, 2023
e5d150e
Merge pull request #6 from susu725/adminGetUser
susu725 Aug 21, 2023
cf1a0d0
Merge pull request #7 from susu725/reqUser
susu725 Aug 21, 2023
bf90b17
feat: add postReply function
susu725 Aug 21, 2023
b23c75a
Merge remote-tracking branch 'origin/master' into replies-function
susu725 Aug 21, 2023
adecc09
Merge pull request #8 from susu725/replies-function
TCY1020 Aug 21, 2023
6b28e4c
feat: add users banner Column
TCY1020 Aug 22, 2023
8e657b1
Merge branch 'addUserColumn'
TCY1020 Aug 22, 2023
be5ac83
feat: adminGetUsers
TCY1020 Aug 22, 2023
4e28213
Merge branch 'adminGetAdminTweets'
TCY1020 Aug 22, 2023
5ca9c6e
Merge pull request #10 from susu725/adminGetAdminTweets
susu725 Aug 22, 2023
4fac80c
fix: getUsers & getAdminTweets
TCY1020 Aug 22, 2023
0b4f2aa
:feat: admin deleteTweet
TCY1020 Aug 22, 2023
3890554
Merge branch 'master' of https://github.com/susu725/twitter-api-2020
TCY1020 Aug 22, 2023
55debc7
Merge branch 'adminDeleteTweet'
TCY1020 Aug 22, 2023
1a86065
refactor: modify tweet model init method
susu725 Aug 22, 2023
6b2a448
feat: modify status code
susu725 Aug 22, 2023
8749659
feat: modify config for heroku
susu725 Aug 22, 2023
7f35feb
style: white-space
TCY1020 Aug 22, 2023
e94e08a
Merge branch 'master' of https://github.com/susu725/twitter-api-2020
TCY1020 Aug 22, 2023
2ceed1a
Merge branch 'master' into adminDeleteTweet
TCY1020 Aug 22, 2023
6e82095
Merge pull request #15 from susu725/adminDeleteTweet
susu725 Aug 22, 2023
d3754ef
fix: user model
TCY1020 Aug 22, 2023
1f7b10b
feat: user getUser
TCY1020 Aug 22, 2023
adf0026
feat: add dayjs and association in getTweets
susu725 Aug 23, 2023
c6cb3a4
feat: add auth in tweets-function
susu725 Aug 23, 2023
e45d198
fix: modify UserId in postTweet from req
susu725 Aug 23, 2023
d667c3f
Merge pull request #16 from susu725/conformToTestC
susu725 Aug 23, 2023
b156bd1
Merge pull request #17 from susu725/userGetUser
susu725 Aug 23, 2023
0fffe88
Merge remote-tracking branch 'origin/master' into tweets-function
susu725 Aug 23, 2023
6600684
refactor: modify like model init method
susu725 Aug 23, 2023
2f75577
feat: add addLike and removeLike function
susu725 Aug 23, 2023
236b537
feat: update success message
susu725 Aug 23, 2023
f42c83c
refactor: modify reply model init method
susu725 Aug 23, 2023
186683a
feat: modify status code
susu725 Aug 23, 2023
93d4a5e
feat: add auth in replies-function
susu725 Aug 23, 2023
87d79e4
fix: modify UserId in postReply from req
susu725 Aug 23, 2023
c1d1f0e
Merge pull request #18 from susu725/tweets-function
TCY1020 Aug 23, 2023
095c3c9
feat: userPutUser
TCY1020 Aug 23, 2023
8ff5764
Merge branch 'userPutUser'
TCY1020 Aug 23, 2023
bb896f6
Merge pull request #19 from susu725/replies-function
TCY1020 Aug 23, 2023
b9e7559
feat: hide password
TCY1020 Aug 23, 2023
f59cf85
refactor: modify followship model init method
susu725 Aug 23, 2023
e351ee1
feat: user getUserTweets
TCY1020 Aug 23, 2023
10d2058
Merge branch 'master' of https://github.com/susu725/twitter-api-2020
TCY1020 Aug 23, 2023
6cc0c86
feat: sort
TCY1020 Aug 23, 2023
cd8c7ec
Merge pull request #21 from susu725/userPutUser
susu725 Aug 23, 2023
3926911
Merge pull request #23 from susu725/userGetUserTweets
susu725 Aug 23, 2023
c16f171
feat: add addFollowing and removeFollowing function
susu725 Aug 23, 2023
462bb1f
Merge remote-tracking branch 'origin/master' into followships-function
susu725 Aug 23, 2023
4682555
Merge pull request #24 from susu725/followships-function
TCY1020 Aug 23, 2023
4f723a6
fix: require upload in index router
susu725 Aug 23, 2023
36b19a5
Merge branch 'master' of https://github.com/susu725/twitter-api-2020
TCY1020 Aug 23, 2023
490155c
style: async/await to file-helper
TCY1020 Aug 24, 2023
5d19e21
fix: in order to comply with test
susu725 Aug 24, 2023
e02007f
Merge branch 'master' into userPutUser
TCY1020 Aug 24, 2023
eed90d0
Merge pull request #25 from susu725/userPutUser
susu725 Aug 24, 2023
10e92f5
feat: userSingUp
TCY1020 Aug 24, 2023
c09cd45
Merge branch 'userSignUp'
TCY1020 Aug 24, 2023
a960850
fix: modify table name replies to Replies
susu725 Aug 24, 2023
5c7dd48
feat: user getUserRepliedTweets
TCY1020 Aug 24, 2023
dfc497a
Merge branch 'master' of https://github.com/susu725/twitter-api-2020
TCY1020 Aug 24, 2023
76c68bd
Merge branch 'master' into userGetUserRepliedTweets
TCY1020 Aug 24, 2023
39b7a1c
style: data style
TCY1020 Aug 24, 2023
d9f31d5
style: data style
TCY1020 Aug 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT =
JWT_SECRET =
IMGUR_CLIENTID =
32 changes: 32 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
env: {
browser: true,
es2021: true
},
extends: [
'standard',
'plugin:react/recommended'
],
overrides: [
{
env: {
node: true
},
files: [
'.eslintrc.{js,cjs}'
],
parserOptions: {
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [
'react'
],
rules: {
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ typings/
.fusebox/

# DynamoDB Local files
.dynamodb/
.dynamodb/
temp
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cSpell.words": [
"blongs",
"Followship",
"Followships",
"imgur"
]
}
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: NODE_ENV=production node app.js
9 changes: 4 additions & 5 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

function getUser(req) {
return req.user;
function getUser (req) {
return req.user
}

module.exports = {
getUser,
};
getUser
}
23 changes: 16 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const express = require('express')
const helpers = require('./_helpers');
const helpers = require('./_helpers')
const routes = require('./routes')

const app = express()
const port = 3000
const port = process.env.PORT || 3000

app.use(express.urlencoded({ extended: true }))
app.use(express.json())

app.use((req, res, next) => {
res.locals.user = helpers.getUser(req)
next()
})

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
app.use(routes)

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

module.exports = app
6 changes: 1 addition & 5 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
"logging": false
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
"use_env_variable": "MYSQL_DATABASE_URL"
},
"travis": {
"username": "travis",
Expand Down
26 changes: 24 additions & 2 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const passport = require('passport')
const passportJWT = require('passport-jwt')
const JWTStrategy = passportJWT.Strategy
const ExtractJWT = passportJWT.ExtractJwt
const { User, Tweet } = require('../models')

const jwtOptions = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET
}
passport.use(new JWTStrategy(jwtOptions, async (jwtPayload, cb) => {
try {
const user = await User.findByPk(jwtPayload.id, {
include: [
Tweet,
{ model: Tweet, as: 'LikeTweets' },
{ model: User, as: 'Followings' },
{ model: User, as: 'Followers' }
]
})
cb(null, user)
} catch (err) {
cb(err)
}
}))


module.exports = passport
module.exports = passport
18 changes: 18 additions & 0 deletions controllers/admin-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const adminServices = require('../services/admin-services')

const adminController = {
signIn: (req, res, next) => {
adminServices.signIn(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
getUsers: (req, res, next) => {
adminServices.getUsers(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
getAdminTweets: (req, res, next) => {
adminServices.getAdminTweets(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
deleteTweet: (req, res, next) => {
adminServices.deleteTweet(req, (err, data) => err ? next(err) : res.status(200).json(data))
}
}

module.exports = adminController
12 changes: 12 additions & 0 deletions controllers/followship-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const followshipServices = require('../services/followship-services')

const followshipController = {
addFollowing: (req, res, next) => {
followshipServices.addFollowing(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
removeFollowing: (req, res, next) => {
followshipServices.removeFollowing(req, (err, data) => err ? next(err) : res.status(200).json(data))
}
}

module.exports = followshipController
12 changes: 12 additions & 0 deletions controllers/reply-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const replyServices = require('../services/reply-services')

const replyController = {
getReplies: (req, res, next) => {
replyServices.getReplies(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
postReply: (req, res, next) => {
replyServices.postReply(req, (err, data) => err ? next(err) : res.status(200).json(data))
}
}

module.exports = replyController
21 changes: 21 additions & 0 deletions controllers/tweet-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const tweetServices = require('../services/tweet-services')

const tweetController = {
getTweets: (req, res, next) => {
tweetServices.getTweets(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
getTweet: (req, res, next) => {
tweetServices.getTweet(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
postTweet: (req, res, next) => {
tweetServices.postTweet(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
addLike: (req, res, next) => {
tweetServices.addLike(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
removeLike: (req, res, next) => {
tweetServices.removeLike(req, (err, data) => err ? next(err) : res.status(200).json(data))
}
}

module.exports = tweetController
24 changes: 24 additions & 0 deletions controllers/user-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const userServices = require('../services/user-services')

const userController = {
signIn: (req, res, next) => {
userServices.signIn(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
signUp: (req, res, next) => {
userServices.signUp(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
getUser: (req, res, next) => {
userServices.getUser(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
putUser: (req, res, next) => {
userServices.putUser(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
getUserTweets: (req, res, next) => {
userServices.getUserTweets(req, (err, data) => err ? next(err) : res.status(200).json(data))
},
getUserRepliedTweets: (req, res, next) => {
userServices.getUserRepliedTweets(req, (err, data) => err ? next(err) : res.status(200).json(data))
}
}

module.exports = userController
7 changes: 7 additions & 0 deletions helpers/day-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const dayjs = require('dayjs')
const relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)

module.exports = {
relativeTimeFormat: createdAt => dayjs(createdAt).fromNow()
}
20 changes: 20 additions & 0 deletions helpers/file-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const imgur = require('imgur')
imgur.setClientId(process.env.IMGUR_CLIENTID)

const imgurFileHandler = async (files) => {
const uploadPromises = []
const images = [files?.avatar?.[0]?.path, files?.banner?.[0]?.path]
try {
for (const file of images) {
if (!file) return uploadPromises.push(null)
const img = await imgur.uploadFile(file)
uploadPromises.push(img?.link || null)
}
} catch (err) {
console.log(err.message)
}
return uploadPromises
}
module.exports = {
imgurFileHandler
}
31 changes: 31 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const passport = require('../config/passport')
const helpers = require('../_helpers')

const authenticated = (req, res, next) => { // 用jwt策略驗證
passport.authenticate('jwt', { session: false }, (err, user) => {
if (err || !user) return res.status(401).json({ status: 'error', message: '沒登入' })
req.user = user // 用cb之後變成req.logIn函式處理所以要自己處理驗證成功時放入req.user
next()
})(req, res, next)
}

const authenticatedAdmin = (req, res, next) => {
if (helpers.getUser(req) && helpers.getUser(req).role === 'admin') {
next()
} else {
return res.status(403).json({ status: 'error', message: '帳號不存在!' })
}
}
const authenticatedUser = (req, res, next) => {
if (helpers.getUser(req) && helpers.getUser(req).role === 'user') {
next()
} else {
return res.status(403).json({ status: 'error', message: '帳號不存在!' })
}
}

module.exports = {
authenticated,
authenticatedAdmin,
authenticatedUser
}
18 changes: 18 additions & 0 deletions middleware/error-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const apiErrorHandler = (err, req, res, next) => {
if (err instanceof Error) {
res.status(err.status || 500).json({
status: 'error',
message: `${err.name}: ${err.message}`
})
} else {
res.status(500).json({
status: 'error',
message: `${err}`
})
}
next(err)
}

module.exports = {
apiErrorHandler
}
4 changes: 4 additions & 0 deletions middleware/multer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const multer = require('multer')
// 使用者上傳的圖片站存到temp資料夾中
const upload = multer({ dest: 'temp/' })
module.exports = upload
8 changes: 4 additions & 4 deletions migrations/20190115071418-create-followship.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Followships', {
Expand All @@ -22,9 +22,9 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
});
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Followships');
return queryInterface.dropTable('Followships')
}
};
}
8 changes: 4 additions & 4 deletions migrations/20190115071419-create-like.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Likes', {
Expand All @@ -22,9 +22,9 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
});
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Likes');
return queryInterface.dropTable('Likes')
}
};
}
8 changes: 4 additions & 4 deletions migrations/20190115071420-create-reply.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Replies', {
Expand All @@ -25,9 +25,9 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
});
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Replies');
return queryInterface.dropTable('Replies')
}
};
}
Loading