Skip to content

Commit acaeca9

Browse files
authored
Merge pull request #62 from boostcamp-2020/feat/channel-list
Feat/channel api
2 parents 6d025ba + b8f17c5 commit acaeca9

File tree

7 files changed

+194
-11
lines changed

7 files changed

+194
-11
lines changed
Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,124 @@
11
import { asyncWrapper } from '../../util'
22
import service from '../../service/channel'
33

4+
const { WorkspaceUserInfo } = require('../../model/WorkspaceUserInfo')
5+
const { Channel } = require('../../model/Channel')
6+
const { ChannelConfig } = require('../../model/ChannelConfig')
7+
const { Chat } = require('../../model/Chat')
8+
9+
const getChannelList = async (req, res, next) => {
10+
try {
11+
const workspaceUserInfoId = req.query.workspaceUserInfoId
12+
13+
const channelConfig = await ChannelConfig.find(
14+
{
15+
workspaceUserInfoId: workspaceUserInfoId,
16+
},
17+
{
18+
_id: 0,
19+
channelId: 1,
20+
readChatId: 1,
21+
isMute: 1,
22+
notification: 1,
23+
sectionId: 1,
24+
},
25+
).lean()
26+
27+
const channel = await Channel.find(
28+
{
29+
_id: { $in: channelConfig.map(el => el.channelId) },
30+
},
31+
{ _id: 1, title: 1, channelType: 1 },
32+
).lean()
33+
34+
const result = channel.map(el => {
35+
const [currentConfig] = channelConfig.filter(
36+
val => val.channelId.toString() === el._id.toString(),
37+
)
38+
return { ...currentConfig, ...el }
39+
})
40+
41+
res.status(200).json({ success: true, result })
42+
} catch (err) {
43+
next(err)
44+
}
45+
}
46+
47+
const getChannelHeaderInfo = async (req, res, next) => {
48+
try {
49+
const channelId = req.params.channelId
50+
51+
const pinned = await Chat.find({ pinned: true, channel: channelId })
52+
const channelConfig = await ChannelConfig.find({
53+
channelId: channelId,
54+
}).lean()
55+
const workspaceUserInfo = await WorkspaceUserInfo.find({
56+
_id: { $in: channelConfig.map(el => el.workspaceUserInfoId) },
57+
}).lean()
58+
59+
const channel = await Channel.findOne(
60+
{
61+
_id: channelId,
62+
},
63+
{ title: 1, topic: 1, channelType: 1 },
64+
).lean()
65+
const extraData = {
66+
pinnedCount: pinned.length,
67+
memberNum: workspaceUserInfo.length,
68+
member: workspaceUserInfo,
69+
}
70+
71+
let result = { ...channel, ...extraData }
72+
73+
res.status(200).json({ success: true, result })
74+
} catch (err) {
75+
next(err)
76+
}
77+
}
78+
79+
const inviteUser = (req, res, next) => {
80+
try {
81+
const workspaceUserInfoId = req.body.workspaceUserInfoId
82+
const channelId = req.body.channelId
83+
84+
workspaceUserInfoId.forEach(el => {
85+
const channelConfig = ChannelConfig({
86+
workspaceUserInfoId: el,
87+
channelId,
88+
isMute: false,
89+
notification: 0,
90+
sectionId: null,
91+
})
92+
channelConfig.save()
93+
})
94+
95+
res.status(200).json({ success: true })
96+
} catch (err) {
97+
next(err)
98+
}
99+
}
100+
101+
const muteChannel = async (req, res, next) => {
102+
try {
103+
const workspaceUserInfoId = req.body.workspaceUserInfoId
104+
const channelId = req.body.channelId
105+
const isMute = req.body.isMute
106+
107+
await ChannelConfig.updateOne(
108+
{
109+
workspaceUserInfoId,
110+
channelId,
111+
},
112+
{ isMute: isMute },
113+
)
114+
115+
res.status(200).json({ success: true })
116+
} catch (err) {
117+
next(err)
118+
}
119+
}
120+
121+
4122
const createChannel = asyncWrapper(async (req, res) => {
5123
const { code, success, data } = await service.createChannel({
6124
...req.body,
@@ -9,4 +127,11 @@ const createChannel = asyncWrapper(async (req, res) => {
9127
return res.status(code).json({ success, data })
10128
})
11129

12-
module.exports = { createChannel }
130+
module.exports = {
131+
getChannelList,
132+
getChannelHeaderInfo,
133+
inviteUser,
134+
muteChannel,
135+
createChannel
136+
}
137+
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
import express from 'express'
2-
import controller from './channel'
3-
1+
const express = require('express')
42
const router = express.Router()
3+
const controller = require('./channel')
4+
5+
/* GET /api/channle get channel list */
6+
router.get('/', controller.getChannelList)
7+
58

69
router.post('/', controller.createChannel)
710

11+
/* GET /api/channle/{channelId}/info get channel header info */
12+
router.get('/:channelId/info', controller.getChannelHeaderInfo)
13+
14+
/* POST /api/channle/invite invite user to channel */
15+
router.post('/invite', controller.inviteUser)
16+
17+
/* PATCH /api/channle/mute mute channel */
18+
router.patch('/mute', controller.muteChannel)
19+
820
module.exports = router

β€Žbackend/controller/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import express from 'express'
2+
3+
import channelCotroller from './channel'
4+
import searchCotroller from './search'
25
import userController from './user'
3-
import channelController from './channel'
46

57
const router = express.Router()
68

7-
router.use('/channel', channelController)
8-
9+
router.use('/channel', channelCotroller)
10+
router.use('/search', searchCotroller)
911
router.use('/user', userController)
1012

1113
module.exports = router

β€Žbackend/controller/search/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const express = require('express')
2+
const router = express.Router()
3+
const searchController = require('./search')
4+
5+
/* POST /api/search/uesr search user */
6+
router.post('/user', searchController.searchUser)
7+
8+
module.exports = router
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { ChannelConfig } = require('../../model/ChannelConfig')
2+
const { WorkspaceUserInfo } = require('../../model/WorkspaceUserInfo')
3+
4+
const searchUser = async (req, res, next) => {
5+
try {
6+
const keyword = req.body.keyword
7+
const channelId = req.body.channelId
8+
const workspaceId = req.body.workspaceId
9+
10+
const userInfo = await WorkspaceUserInfo.find(
11+
{
12+
$or: [
13+
{ fullName: new RegExp(keyword, 'i') },
14+
{ displayName: new RegExp(keyword, 'i') },
15+
],
16+
workspaceId: workspaceId,
17+
},
18+
{ displayName: 1, profileUrl: 1, isActive: 1 },
19+
).lean()
20+
const existMember = await ChannelConfig.find(
21+
{
22+
workspaceUserInfoId: { $in: userInfo.map(el => el._id) },
23+
channelId: channelId,
24+
},
25+
{ _id: 0, workspaceUserInfoId: 1 },
26+
).lean()
27+
28+
res.status(200).json({ success: true, result: { userInfo, existMember } })
29+
} catch (err) {
30+
next(err)
31+
}
32+
}
33+
34+
module.exports = {
35+
searchUser,
36+
}

β€Žbackend/model/UserHistory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const mongoose = require('mongoose')
22
const Schema = mongoose.Schema
33

4-
const fileSchema = mongoose.Schema(
4+
const userHistorySchema = mongoose.Schema(
55
{
66
userId: {
77
type: Schema.Types.ObjectId,
@@ -18,5 +18,5 @@ const fileSchema = mongoose.Schema(
1818
},
1919
{ timestamps: true },
2020
)
21-
const File = mongoose.model('File', fileSchema)
22-
module.exports = { File }
21+
const UserHistory = mongoose.model('UserHistroy', userHistorySchema)
22+
module.exports = { UserHistory }

β€Žbackend/model/WorkspaceUserInfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const workspaceUserInfoSchema = mongoose.Schema(
4040
type: Schema.Types.ObjectId,
4141
ref: 'User',
4242
},
43-
workSpaceId: {
43+
workspaceId: {
4444
type: Schema.Types.ObjectId,
4545
ref: 'Workspace',
4646
},

0 commit comments

Comments
Β (0)