Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
173 commits
Select commit Hold shift + click to select a range
2166137
setting handlebars & bootstrap
jamiennn Jun 6, 2023
2426e2f
add customized styles
jamiennn Jun 6, 2023
18f14d8
add sign up page
jamiennn Jun 6, 2023
1278423
add errorHandler & messages partial
jamiennn Jun 6, 2023
beca510
add signup page draft
jamiennn Jun 6, 2023
2b2aae4
Merge pull request #1 from sherryylin0719/views
shilohlin0719 Jun 6, 2023
3d421fc
feat: update user model
Jun 6, 2023
27576e7
Merge remote-tracking branch 'origin/master' into createDB
Jun 6, 2023
e6905fe
feat: create user & tweet db
Jun 6, 2023
678d710
feat: add dotenv
NeroKuraudius Jun 6, 2023
9010875
feat: add localPassport
NeroKuraudius Jun 6, 2023
a01a666
feat: upadate user & tweet db
Jun 6, 2023
bbba05c
feat: create reply db
Jun 6, 2023
9c097be
feat: create Like db
Jun 6, 2023
b757122
feat: add signup function
NeroKuraudius Jun 6, 2023
3350b01
feat: create followships db
Jun 6, 2023
e1cc7be
feat: add signin function
NeroKuraudius Jun 6, 2023
eb3ca08
feat: add logout function
NeroKuraudius Jun 6, 2023
8585851
modify: combine passport & login
NeroKuraudius Jun 7, 2023
8bea6d6
feat: pull origin/master
NeroKuraudius Jun 7, 2023
f2f91df
feat: fixd conflict
NeroKuraudius Jun 7, 2023
baa174d
feature: add navbar partial
jamiennn Jun 7, 2023
6cb037a
modify tweet page
jamiennn Jun 7, 2023
416238f
feat: add users seed file
Jun 7, 2023
508cd5b
Merge pull request #2 from sherryylin0719/createDB
NeroKuraudius Jun 7, 2023
7d87170
feat: adjust auth.js
NeroKuraudius Jun 7, 2023
822984f
feat: signout function
NeroKuraudius Jun 7, 2023
4bc08a3
modify: complete user login & register
NeroKuraudius Jun 7, 2023
28743a1
feat: modify tweets page main and followship section
jamiennn Jun 7, 2023
61459ca
Merge remote-tracking branch 'origin/master' into createDB
Jun 7, 2023
543b6eb
Merge pull request #3 from sherryylin0719/userLogin
jamiennn Jun 7, 2023
8d7df98
feat: add tweets seed file
Jun 7, 2023
0b0d5d8
modify tweet page ui
jamiennn Jun 7, 2023
ef37038
feat: add replies seed file
Jun 7, 2023
b8615d0
feat: modify card-text
jamiennn Jun 7, 2023
3ab8201
modify: like unlike button's href
jamiennn Jun 7, 2023
a6bed25
Merge pull request #5 from sherryylin0719/createDB
NeroKuraudius Jun 7, 2023
0dff1b2
modify modal ui
jamiennn Jun 7, 2023
a0b0f76
feat: add reply card & handlebars helper
jamiennn Jun 7, 2023
326e466
Merge branch 'master' into views
shilohlin0719 Jun 8, 2023
e97e0e4
Merge pull request #6 from sherryylin0719/views
shilohlin0719 Jun 8, 2023
88859a0
add handlebarsHelper back to app.js
Jun 8, 2023
60bd43c
add users route
jamiennn Jun 8, 2023
3b40baf
feat: add admin-controller
NeroKuraudius Jun 8, 2023
5f2a8a5
feat: add admin/signin
NeroKuraudius Jun 8, 2023
ec9002d
feat: add admin login function
NeroKuraudius Jun 8, 2023
636f9f9
feat: add profile area
jamiennn Jun 8, 2023
db681e3
feat: signin error handle
NeroKuraudius Jun 8, 2023
5fa3978
feat: modify user and tweet seeds
Jun 8, 2023
9900a63
modify user tweets page and controller
jamiennn Jun 8, 2023
40e17f2
Merge pull request #7 from sherryylin0719/show_user_profile
shilohlin0719 Jun 9, 2023
1214c76
feat: modify to prevent future conflict
Jun 9, 2023
ab89fbe
feat: pull origin master
NeroKuraudius Jun 9, 2023
11fadb0
feat: add view of admin/tweet
NeroKuraudius Jun 9, 2023
9a25d4d
add user replies page and route
jamiennn Jun 9, 2023
3c76ea3
Merge pull request #8 from sherryylin0719/adminLogin
jamiennn Jun 9, 2023
e4f147a
add user likes page and route
jamiennn Jun 9, 2023
a5d2cec
feat: show all users' tweets
NeroKuraudius Jun 9, 2023
0099921
Merge branch 'user_replies_and_likes'
jamiennn Jun 9, 2023
9d68c5d
Merge branch 'master' into user_replies_and_likes
shilohlin0719 Jun 9, 2023
213963e
Merge pull request #9 from sherryylin0719/user_replies_and_likes
shilohlin0719 Jun 9, 2023
7681af6
feat: add tweetController & getTweets fn
Jun 9, 2023
95d37fa
feat: add admin delete tweet
NeroKuraudius Jun 9, 2023
cb71afa
add user followings page and route
jamiennn Jun 9, 2023
823e2d2
modify controller
jamiennn Jun 9, 2023
b8cb23e
modify: admin users herf
NeroKuraudius Jun 9, 2023
b745758
modify: adjust showing length of tweet description
NeroKuraudius Jun 9, 2023
6b23f6c
Merge branch 'master' into tweetController
NeroKuraudius Jun 9, 2023
a3a682b
Merge pull request #10 from sherryylin0719/tweetController
NeroKuraudius Jun 9, 2023
6569eb7
add user followers page and route
jamiennn Jun 9, 2023
ed1f343
Merge branch 'master' of https://github.com/sherryylin0719/twitter-fu…
jamiennn Jun 9, 2023
8707ad6
Merge branch 'master' into userFollowings
shilohlin0719 Jun 9, 2023
ff3a1e6
Merge pull request #11 from sherryylin0719/userFollowings
shilohlin0719 Jun 9, 2023
794c534
Merge branch 'master' of https://github.com/sherryylin0719/twitter-fu…
jamiennn Jun 9, 2023
c63c790
feat: fix some conflix
jamiennn Jun 9, 2023
7f1ac85
setting heroku'
jamiennn Jun 9, 2023
708a3cb
feat: pull origin master
NeroKuraudius Jun 10, 2023
1bbbe3b
feat: add admin users page
NeroKuraudius Jun 10, 2023
649f7d7
feat: add postTweet function
Jun 10, 2023
bf10fd7
feat: add likeTweet & unlikeTweet function
Jun 10, 2023
105d525
feat: modify user-tweets.handlebars
Jun 10, 2023
779f227
feat: add followUser & unfollowUser functions
Jun 10, 2023
89ca5d8
feat: fix profile-card bug
Jun 10, 2023
7ce4da9
feat: fix main page bug
Jun 10, 2023
b21c4ca
Merge branch 'master' into tweetController
NeroKuraudius Jun 10, 2023
cdf566d
Merge pull request #12 from sherryylin0719/tweetController
NeroKuraudius Jun 10, 2023
9f95e21
Merge branch 'master' of https://github.com/sherryylin0719/twitter-fu…
NeroKuraudius Jun 11, 2023
fd75922
feat: pull origin master
NeroKuraudius Jun 11, 2023
3fea0f7
add user edit modal function
jamiennn Jun 11, 2023
08ab2a7
Merge branch 'master' into usersEdit
jamiennn Jun 11, 2023
6b40bc3
Merge pull request #13 from sherryylin0719/usersEdit
shilohlin0719 Jun 11, 2023
81ed343
feat: adjust admin users list view
NeroKuraudius Jun 11, 2023
84ed74a
feat: add users controller
NeroKuraudius Jun 11, 2023
738ec33
fix some bugs in edit modal
jamiennn Jun 11, 2023
f1b89a1
feat: add getReplies function phase 1
Jun 11, 2023
4eaaa2c
fix some bugs
jamiennn Jun 11, 2023
bf42726
feat: finish getReplies function
Jun 11, 2023
535ddf4
add user setting function
jamiennn Jun 11, 2023
5e75f6d
Merge pull request #14 from sherryylin0719/userSettings
shilohlin0719 Jun 12, 2023
2f5780e
Merge branch 'master' of https://github.com/sherryylin0719/twitter-fu…
NeroKuraudius Jun 12, 2023
a9e98d0
feat: add admin users list
NeroKuraudius Jun 12, 2023
ae046f3
modify: adjust admin users view
NeroKuraudius Jun 12, 2023
13f780a
feat: update reply model
Jun 12, 2023
783b19f
Merge remote-tracking branch 'origin/master' into tweetController
Jun 12, 2023
98fed99
fix logo image bug
jamiennn Jun 12, 2023
0c3967d
modify: adjust auth.js
NeroKuraudius Jun 12, 2023
db5f117
feat: add postReplies, create replies-reply-modal & user-likes-modal
Jun 13, 2023
6b21c10
feat: update user seed avatar
Jun 13, 2023
052c3b6
Merge pull request #15 from sherryylin0719/herokuSetting
shilohlin0719 Jun 13, 2023
1540a1d
modify: adjust admin user likes
NeroKuraudius Jun 13, 2023
559586f
Merge pull request #16 from sherryylin0719/tweetController
NeroKuraudius Jun 13, 2023
6b34d27
feat: pull origin master
NeroKuraudius Jun 13, 2023
9463437
feat: pull origin adminPage
NeroKuraudius Jun 13, 2023
f36385b
feat: git pull master
NeroKuraudius Jun 13, 2023
84cb715
Merge branch 'adminPage' of https://github.com/sherryylin0719/twitter…
NeroKuraudius Jun 13, 2023
46ed807
modify: adjust passport into admin & user version
NeroKuraudius Jun 13, 2023
22c38f0
Merge pull request #17 from sherryylin0719/adminPage
jamiennn Jun 13, 2023
ebb5d98
feat: add admin users sort
NeroKuraudius Jun 14, 2023
704b0e2
feat: add admin users view
NeroKuraudius Jun 14, 2023
93a8559
modify: adjust database connect times in adminGetUsers
NeroKuraudius Jun 14, 2023
c5994de
Merge pull request #18 from sherryylin0719/totalCheck
jamiennn Jun 14, 2023
ac900b1
feat: add README.md
NeroKuraudius Jun 16, 2023
5b03df8
feat: add home page preview image
NeroKuraudius Jun 16, 2023
e6b15f2
modify: update README.md
NeroKuraudius Jun 16, 2023
fdc54bf
feat: add preview image
NeroKuraudius Jun 16, 2023
1213944
feat: add sequelize command
NeroKuraudius Jun 16, 2023
10a2cab
Merge pull request #19 from sherryylin0719/README
jamiennn Jun 16, 2023
21af203
feat: modify profile anchor
Jun 16, 2023
f0210b6
Merge pull request #20 from sherryylin0719/modify/profile
NeroKuraudius Jun 16, 2023
7e57f75
feat: remove user avata r& cover default, refactor tweet and reply model
Jun 16, 2023
e36f090
feat: add tweet & reply words count
Jun 17, 2023
385c6db
feat: improve like/unlike UX
Jun 17, 2023
810c03b
Merge pull request #21 from sherryylin0719/modifyUserSeed
NeroKuraudius Jun 17, 2023
19d214f
modify: adjust admin delete tweet & replies/like
NeroKuraudius Jun 17, 2023
a7ba33a
feat: add admin tweet check whole article
NeroKuraudius Jun 17, 2023
4cb0582
refactor login redirect
Jun 17, 2023
2696575
modify sign up page message's position and back stage card's ui
jamiennn Jun 17, 2023
259c2d7
feat: add admin tweet modal & UX
NeroKuraudius Jun 17, 2023
b7084cc
modify user follow page's follow and unfollow button
jamiennn Jun 17, 2023
ff5aeaf
modify flexbox space
jamiennn Jun 17, 2023
d71ae25
refactor ux
Jun 17, 2023
056d1b8
Merge pull request #23 from sherryylin0719/modifyViewsAndBugs
shilohlin0719 Jun 17, 2023
7e6d528
add size limit to profile image
jamiennn Jun 17, 2023
d9667e6
Merge pull request #22 from sherryylin0719/modifyUserSeed
NeroKuraudius Jun 17, 2023
05ae5ae
Merge pull request #24 from sherryylin0719/modifyViewsAndBugs
shilohlin0719 Jun 17, 2023
9471c04
feat: pull origin master
NeroKuraudius Jun 17, 2023
94278a3
modify: solve conflict
NeroKuraudius Jun 17, 2023
5273f27
Update reply.js
jamiennn Jun 17, 2023
e4094e5
Update signup.handlebars
jamiennn Jun 17, 2023
534d73b
modify: sugnup message position
NeroKuraudius Jun 17, 2023
724694b
modify: signup error message content
NeroKuraudius Jun 17, 2023
8a9a837
Merge pull request #25 from sherryylin0719/backstageAdjust
jamiennn Jun 17, 2023
2957ef8
modify: adjust user avatar default
NeroKuraudius Jun 17, 2023
9a663a7
Merge pull request #26 from sherryylin0719/backstageAdjust
jamiennn Jun 17, 2023
eabd6d1
modify reply modal
jamiennn Jun 17, 2023
9bd5207
Merge branch 'master' into replyModal
jamiennn Jun 17, 2023
899c0bb
Merge pull request #27 from sherryylin0719/replyModal
jamiennn Jun 17, 2023
0d124d3
update seeds
Jun 18, 2023
402b519
Merge pull request #29 from sherryylin0719/SeedBranch
NeroKuraudius Jun 18, 2023
8d404dc
modify seeds
Jun 18, 2023
8d0d812
Merge pull request #30 from sherryylin0719/Seeds
jamiennn Jun 18, 2023
18de754
modify reply css
Jun 18, 2023
5798415
Merge pull request #31 from sherryylin0719/modifyReplyCSS
NeroKuraudius Jun 18, 2023
b8b0e45
fix reply redirect ux
Jun 18, 2023
9c6f9da
Merge pull request #32 from sherryylin0719/fixReplyRedirect
NeroKuraudius Jun 18, 2023
a667685
add user profile card icon: message and notify
jamiennn Jun 18, 2023
68e5a7d
fix reply page nav bar
jamiennn Jun 18, 2023
9e98fa0
Merge pull request #33 from sherryylin0719/userProfileIcon
shilohlin0719 Jun 18, 2023
7cf6468
fix loginUserAvatar bugs
Jun 18, 2023
588a0fc
Merge branch 'master' into fixAvatarBugs
NeroKuraudius Jun 18, 2023
c27df7d
Merge pull request #34 from sherryylin0719/fixAvatarBugs
NeroKuraudius Jun 18, 2023
e29e570
modify height
jamiennn Jun 18, 2023
1a55496
Merge pull request #35 from sherryylin0719/modifyProfileHeight
shilohlin0719 Jun 19, 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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IMGUR_CLIENT_ID=skip
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ typings/
.fusebox/

# DynamoDB Local files
.dynamodb/
.dynamodb/

temp/
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
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# twitter-fullstack-2020
ALPHA Camp | 學期 3 | Simple Twitter | 自動化測試檔 (全端開發組)

![/public/stylesheets/icons/home_page_img.png](https://github.com/sherryylin0719/twitter-fullstack-2020/blob/README/public/stylesheets/icons/home_page_img.png?raw=true)

## 安裝與使用

### ※事前準備
### 進行下列步驟前請先確認已註冊 [Imgur](https://imgur.com/) 帳號並建立自己的Application
### 確認已安裝 [Node.js](https://nodejs.org/zh-tw/download) 與 [MySQL](https://dev.mysql.com/doc/)
### 並在MySQL中建立名為`ac_twitter_workspace`的資料庫


### 1. 下載至本機並安裝套件
開啟cmd並輸入下方指令
```js
git clone 網址
```
繼續在cmd中輸入指令
```js
cd twitter-fullstack-2020
```
進入本機資料夾後接著輸入
```js
npm install
```

### 2. 建立資料表與種子資料
安裝完套件後輸入
```js
npx sequelize db:migrate
```
再接著建立種子資料
```js
npx sequelize db:seed:all
```

### 3. 設定環境變數並啟用
輸入以下指令建立環境變數
```js
touch .env
```
至.env的檔案中將ImgurID 與 Secret存入

回到cmd輸入
```js
npm run start
```
如出現`Example app listening on port 3000!`表示已成功啟動

啟動後點選下方連結進入

[http://localhost:3000](http://localhost:3000)

以預設帳號登入

前台:
帳號 user1
密碼 12345678

後台:
帳號 root
密碼 12345678

或自行註冊一組新帳號登入

### 4. 功能
#### 前台:
1. 左側導覽頁
- 「首頁」可回到首頁(所有推文頁)
- 「個人資料」可進入使用者頁面
- 「設定」可修改使用者帳戶資料
- 「推文」可發布一則新推文
- 「登出」可登出當前使用帳號

2. 首頁
- 可瀏覽所有推文(依時間排序,由新到舊)與推文回覆數、喜歡數、發布時間
- 可發布一則新推文(字數上限140字)
- 可喜歡/收回喜歡單一推文
- 點選單一推文可查看該推文發布時間與回覆串
- 可針對點選推文進行回覆
- 點選使用者頭像可瀏覽該使用者個人頁面

3. 右側推薦跟隨
- 可跟隨/取消跟隨其他使用者
- 點選頭像、名稱或帳號可瀏覽該使用者個人頁面

4. 個人頁面
- 可看到使用者基本資料(姓名、帳號、自我介紹、頭像、封面照、追蹤及跟隨人數等)
- 可編輯基本資料(限使用者本人)
- 最上方可看到該使用者發布推文總數,點選「←」可回到上一頁
- 下方有三個頁籤可切換
- 「推文」可瀏覽該使用者所有推文與回覆數、喜歡數、發布時間
- 「回覆」可查看該使用者所有回覆
- 「喜歡的內容」可查看該使用者喜歡的所有推文

#### 後台:
1. 左側導覽頁
- 「推文清單」可瀏覽所有前台使用者的推文(依時間排序,由新到舊)
- 每則推文顯示前50個字,後台管理者可點選「X」刪除任一推文
- 「使用者列表」可查看所有使用者資料(依推文數排列,由多至少)
- 使用者資料包含名稱、帳號、頭像、封面照、推文數、推文喜歡數、跟隨中與跟隨者
- 「登出」可登出當前管理者帳號
2 changes: 1 addition & 1 deletion _helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function ensureAuthenticated(req) {
return req.isAuthenticated();
return req.isAuthenticated()
}

function getUser(req) {
Expand Down
47 changes: 45 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const express = require('express')
const helpers = require('./_helpers');
const passport = require('./config/passport')
const routes = require('./routes')
const exphbs = require('express-handlebars')
const flash = require('connect-flash')
const methodOverride = require('method-override')
const session = require('express-session')

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

const handlebarsHelper = require('./helpers/handlebars-helper')
// use helpers.getUser(req) to replace req.user
// use helpers.ensureAuthenticated(req) to replace req.isAuthenticated()

app.get('/', (req, res) => res.send('Hello World!'))
// Setting template engine
app.engine('handlebars', exphbs({ defaultLayout: 'main', helpers: handlebarsHelper }))
app.set('view engine', 'handlebars')

app.use(express.static('public'))
app.use(session({ secret: 'SECRET', resave: false, saveUninitialized: false }))
// secret寫死? 加進env?

// setting body-parser
app.use(express.urlencoded({ extended: true }))
app.use(express.json())

// passport setting
app.use(passport.initialize())
app.use(passport.session())

// setting method-override
app.use(methodOverride('_method'))

// Setting middleware
app.use(flash())
app.use(methodOverride('_method'))
app.use((req, res, next) => {
res.locals.danger_msg = req.flash('danger_msg')
res.locals.success_msg = req.flash('success_msg')
res.locals.warning_msg = req.flash('warning_msg')
res.locals.info_msg = req.flash('info_msg')
res.locals.loginUser = helpers.getUser(req)
next()
})

app.use(routes)

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": "CLEARDB_DATABASE_URL"
},
"travis": {
"username": "travis",
Expand Down
61 changes: 61 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcryptjs')
const { User, Tweet } = require('../models')

passport.use('userLogin', new LocalStrategy({
usernameField: 'account',
passwordField: 'password',
passReqToCallback: true
}, async (req, account, password, cb) => {
try {
const user = await User.findOne({ where: { account } })
if (!user) return cb(null, false, req.flash('danger_msg', '帳號或密碼錯誤!'))
if (user.role !== 'user') return cb(null, false, req.flash('danger_msg', '帳號不存在!'))

const passwordCompare = await bcrypt.compare(password, user.password)
if (!passwordCompare) return cb(null, false, req.flash('danger_msg', '帳號或密碼錯誤!'))
return cb(null, user)
} catch (err) {
return cb(err)
}
})
)
passport.use('adminLogin', new LocalStrategy({
usernameField: 'account',
passwordField: 'password',
passReqToCallback: true
}, async (req, account, password, cb) => {
try {
const user = await User.findOne({ where: { account } })
if (!user) return cb(null, false, req.flash('danger_msg', '帳號或密碼錯誤!'))
if (user.role !== 'admin') return cb(null, false, req.flash('danger_msg', '帳號不存在!'))

const passwordCompare = await bcrypt.compare(password, user.password)
if (!passwordCompare) return cb(null, false, req.flash('danger_msg', '帳號或密碼錯誤!'))
return cb(null, user)
} catch (err) {
return cb(err)
}
})
)

passport.serializeUser((user, cb) => {
return cb(null, user.id)
})
passport.deserializeUser(async (id, cb) => {
try {
const user = await User.findByPk(id, {
include: [
{ model: User, as: 'Followers' },
{ model: User, as: 'Followings' },
{ model: Tweet, as: 'LikedTweets' },
]
})
return cb(null, user.toJSON())
} catch (err) {
return cb(err)
}
})

module.exports = passport
75 changes: 75 additions & 0 deletions controllers/admin-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { User, Tweet, Like, Reply } = require('../models')

const adminController = {
// 後台登入
signInPage: (req, res) => {
return res.render('admin/signin')
},
signIn: async (req, res) => {
// req.flash('success_msg', '登入成功')
return res.redirect('/admin/tweets')
},
// 後台頁面
adminGetTweets: async (req, res, next) => {
try {
let tweets = await Tweet.findAll({
order: [['updatedAt', 'DESC']],
include: User
})
tweets = tweets.map(tweet => ({
...tweet.toJSON(),
simpleText: tweet.description.substring(0, 50)
})
)
return res.render('admin/tweets', { tweets })
} catch (e) {
next(e)
}
},
deleteUserTweet: async (req, res, next) => {
const { tid } = req.params
try {
const tweet = await Tweet.findByPk(tid)
const replies = await Reply.findAll({ where: { TweetId: tid } })
const likes = await Like.findAll({ where: { TweetId: tid } })
if (!tweet) throw new Error("This tweet didn't exist!")
await tweet.destroy()
if (replies) await Reply.destroy({ where: { TweetId: tid } })
if (likes) await Like.destroy({ where: { TweetId: tid } })
return res.redirect('/admin/tweets')
} catch (e) {
next(e)
}
},
adminGetUsers: async (req, res, next) => {
try {
const users = await User.findAll({
where: { role: 'user' },
include: [
{ model: User, as: 'Followings' },
{ model: User, as: 'Followers' },
{ model: Tweet, include: [Like] }
]
})
const usersList = users.map(user => {
const { Followings, Followers, Tweets, ...userData } = user.toJSON()
const userLikes = Tweets.map(tweet => { return tweet.Likes.length })
let totalLikes = 0
userLikes.forEach(tweetLikes => totalLikes += tweetLikes)

userData.totalFollowings = Followings.length
userData.totalFollowers = Followers.length
userData.totalTweets = Tweets.length
userData.totalLikes = totalLikes

return userData
})
const sortedUsers = usersList.sort((a, b) => b.totalTweets - a.totalTweets)

return res.render('admin/users', { users: sortedUsers })
} catch (e) {
next(e)
}
}
}
module.exports = adminController
Loading