diff --git a/backend/controller/channel/channel.js b/backend/controller/channel/channel.js index 37e42fd9..712f6f6b 100644 --- a/backend/controller/channel/channel.js +++ b/backend/controller/channel/channel.js @@ -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, @@ -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 +} + diff --git a/backend/controller/channel/index.js b/backend/controller/channel/index.js index 2ad08459..a14f0c96 100644 --- a/backend/controller/channel/index.js +++ b/backend/controller/channel/index.js @@ -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 diff --git a/backend/controller/index.js b/backend/controller/index.js index 1e0ce416..5c6e880e 100644 --- a/backend/controller/index.js +++ b/backend/controller/index.js @@ -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 diff --git a/backend/controller/search/index.js b/backend/controller/search/index.js new file mode 100644 index 00000000..dff388ce --- /dev/null +++ b/backend/controller/search/index.js @@ -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 diff --git a/backend/controller/search/search.js b/backend/controller/search/search.js new file mode 100644 index 00000000..e3bde492 --- /dev/null +++ b/backend/controller/search/search.js @@ -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, +} diff --git a/backend/model/UserHistory.js b/backend/model/UserHistory.js index 309fd6c9..43811387 100644 --- a/backend/model/UserHistory.js +++ b/backend/model/UserHistory.js @@ -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, @@ -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 } diff --git a/backend/model/WorkspaceUserInfo.js b/backend/model/WorkspaceUserInfo.js index b63a9b15..2aa49bd3 100644 --- a/backend/model/WorkspaceUserInfo.js +++ b/backend/model/WorkspaceUserInfo.js @@ -40,7 +40,7 @@ const workspaceUserInfoSchema = mongoose.Schema( type: Schema.Types.ObjectId, ref: 'User', }, - workSpaceId: { + workspaceId: { type: Schema.Types.ObjectId, ref: 'Workspace', },