Skip to content

Commit

Permalink
Merge pull request #62 from boostcamp-2020/feat/channel-list
Browse files Browse the repository at this point in the history
Feat/channel api
  • Loading branch information
Changyu-Ryou committed Nov 26, 2020
2 parents 6d025ba + b8f17c5 commit acaeca9
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 11 deletions.
127 changes: 126 additions & 1 deletion backend/controller/channel/channel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,124 @@
import { asyncWrapper } from '../../util'
import service from '../../service/channel'

const { WorkspaceUserInfo } = require('../../model/WorkspaceUserInfo')
const { Channel } = require('../../model/Channel')
const { ChannelConfig } = require('../../model/ChannelConfig')
const { Chat } = require('../../model/Chat')

const getChannelList = async (req, res, next) => {
try {
const workspaceUserInfoId = req.query.workspaceUserInfoId

const channelConfig = await ChannelConfig.find(
{
workspaceUserInfoId: workspaceUserInfoId,
},
{
_id: 0,
channelId: 1,
readChatId: 1,
isMute: 1,
notification: 1,
sectionId: 1,
},
).lean()

const channel = await Channel.find(
{
_id: { $in: channelConfig.map(el => el.channelId) },
},
{ _id: 1, title: 1, channelType: 1 },
).lean()

const result = channel.map(el => {
const [currentConfig] = channelConfig.filter(
val => val.channelId.toString() === el._id.toString(),
)
return { ...currentConfig, ...el }
})

res.status(200).json({ success: true, result })
} catch (err) {
next(err)
}
}

const getChannelHeaderInfo = async (req, res, next) => {
try {
const channelId = req.params.channelId

const pinned = await Chat.find({ pinned: true, channel: channelId })
const channelConfig = await ChannelConfig.find({
channelId: channelId,
}).lean()
const workspaceUserInfo = await WorkspaceUserInfo.find({
_id: { $in: channelConfig.map(el => el.workspaceUserInfoId) },
}).lean()

const channel = await Channel.findOne(
{
_id: channelId,
},
{ title: 1, topic: 1, channelType: 1 },
).lean()
const extraData = {
pinnedCount: pinned.length,
memberNum: workspaceUserInfo.length,
member: workspaceUserInfo,
}

let result = { ...channel, ...extraData }

res.status(200).json({ success: true, result })
} catch (err) {
next(err)
}
}

const inviteUser = (req, res, next) => {
try {
const workspaceUserInfoId = req.body.workspaceUserInfoId
const channelId = req.body.channelId

workspaceUserInfoId.forEach(el => {
const channelConfig = ChannelConfig({
workspaceUserInfoId: el,
channelId,
isMute: false,
notification: 0,
sectionId: null,
})
channelConfig.save()
})

res.status(200).json({ success: true })
} catch (err) {
next(err)
}
}

const muteChannel = async (req, res, next) => {
try {
const workspaceUserInfoId = req.body.workspaceUserInfoId
const channelId = req.body.channelId
const isMute = req.body.isMute

await ChannelConfig.updateOne(
{
workspaceUserInfoId,
channelId,
},
{ isMute: isMute },
)

res.status(200).json({ success: true })
} catch (err) {
next(err)
}
}


const createChannel = asyncWrapper(async (req, res) => {
const { code, success, data } = await service.createChannel({
...req.body,
Expand All @@ -9,4 +127,11 @@ const createChannel = asyncWrapper(async (req, res) => {
return res.status(code).json({ success, data })
})

module.exports = { createChannel }
module.exports = {
getChannelList,
getChannelHeaderInfo,
inviteUser,
muteChannel,
createChannel
}

18 changes: 15 additions & 3 deletions backend/controller/channel/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import express from 'express'
import controller from './channel'

const express = require('express')
const router = express.Router()
const controller = require('./channel')

/* GET /api/channle get channel list */
router.get('/', controller.getChannelList)


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

/* GET /api/channle/{channelId}/info get channel header info */
router.get('/:channelId/info', controller.getChannelHeaderInfo)

/* POST /api/channle/invite invite user to channel */
router.post('/invite', controller.inviteUser)

/* PATCH /api/channle/mute mute channel */
router.patch('/mute', controller.muteChannel)

module.exports = router
8 changes: 5 additions & 3 deletions backend/controller/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import express from 'express'

import channelCotroller from './channel'
import searchCotroller from './search'
import userController from './user'
import channelController from './channel'

const router = express.Router()

router.use('/channel', channelController)

router.use('/channel', channelCotroller)
router.use('/search', searchCotroller)
router.use('/user', userController)

module.exports = router
8 changes: 8 additions & 0 deletions backend/controller/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express')
const router = express.Router()
const searchController = require('./search')

/* POST /api/search/uesr search user */
router.post('/user', searchController.searchUser)

module.exports = router
36 changes: 36 additions & 0 deletions backend/controller/search/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { ChannelConfig } = require('../../model/ChannelConfig')
const { WorkspaceUserInfo } = require('../../model/WorkspaceUserInfo')

const searchUser = async (req, res, next) => {
try {
const keyword = req.body.keyword
const channelId = req.body.channelId
const workspaceId = req.body.workspaceId

const userInfo = await WorkspaceUserInfo.find(
{
$or: [
{ fullName: new RegExp(keyword, 'i') },
{ displayName: new RegExp(keyword, 'i') },
],
workspaceId: workspaceId,
},
{ displayName: 1, profileUrl: 1, isActive: 1 },
).lean()
const existMember = await ChannelConfig.find(
{
workspaceUserInfoId: { $in: userInfo.map(el => el._id) },
channelId: channelId,
},
{ _id: 0, workspaceUserInfoId: 1 },
).lean()

res.status(200).json({ success: true, result: { userInfo, existMember } })
} catch (err) {
next(err)
}
}

module.exports = {
searchUser,
}
6 changes: 3 additions & 3 deletions backend/model/UserHistory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const fileSchema = mongoose.Schema(
const userHistorySchema = mongoose.Schema(
{
userId: {
type: Schema.Types.ObjectId,
Expand All @@ -18,5 +18,5 @@ const fileSchema = mongoose.Schema(
},
{ timestamps: true },
)
const File = mongoose.model('File', fileSchema)
module.exports = { File }
const UserHistory = mongoose.model('UserHistroy', userHistorySchema)
module.exports = { UserHistory }
2 changes: 1 addition & 1 deletion backend/model/WorkspaceUserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const workspaceUserInfoSchema = mongoose.Schema(
type: Schema.Types.ObjectId,
ref: 'User',
},
workSpaceId: {
workspaceId: {
type: Schema.Types.ObjectId,
ref: 'Workspace',
},
Expand Down

0 comments on commit acaeca9

Please sign in to comment.