Skip to content

Commit

Permalink
removed public/private Discussion, instead TL chooses members
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Jun 29, 2018
1 parent fa9b83f commit 351dc6c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 165 deletions.
21 changes: 2 additions & 19 deletions api/server/api/team-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,15 @@ router.get('/topics/list', async (req, res) => {
}
});

router.get('/topics/private-topic', async (req, res) => {
try {
const topic = await Topic.getPrivateTopic({
userId: req.user.id,
teamId: req.query.teamId,
topicSlug: req.query.topicSlug,
});

res.json({ topic });
} catch (err) {
logger.error(err);
res.json({ error: err.post || err.toString() });
}
});

router.post('/discussions/add', async (req, res) => {
try {
const { name, topicId, memberIds = [], isPrivate = false } = req.body;
const { name, topicId, memberIds = [] } = req.body;

const discussion = await Discussion.add({
userId: req.user.id,
name,
topicId,
memberIds,
isPrivate,
});

res.json({ discussion });
Expand All @@ -173,14 +157,13 @@ router.post('/discussions/add', async (req, res) => {

router.post('/discussions/edit', async (req, res) => {
try {
const { name, id, memberIds = [], isPrivate = false } = req.body;
const { name, id, memberIds = [] } = req.body;

const { topicId } = await Discussion.edit({
userId: req.user.id,
name,
id,
memberIds,
isPrivate,
});

res.json({ done: 1 });
Expand Down
21 changes: 5 additions & 16 deletions api/server/models/Discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ const mongoSchema = new mongoose.Schema({
required: true,
},
memberIds: [String],
isPrivate: {
type: Boolean,
default: false,
},
createdAt: {
type: Date,
required: true,
Expand All @@ -49,7 +45,6 @@ interface IDiscussionDocument extends mongoose.Document {
name: string;
slug: string;
memberIds: string[];
isPrivate: boolean;
createdAt: Date;
lastActivityDate: Date;
}
Expand All @@ -67,27 +62,23 @@ interface IDiscussionModel extends mongoose.Model<IDiscussionDocument> {
name,
userId,
topicId,
isPrivate,
memberIds,
}: {
name: string;
userId: string;
topicId: string;
isPrivate: boolean;
memberIds: string[];
}): Promise<IDiscussionDocument>;

edit({
userId,
id,
name,
isPrivate,
memberIds,
}: {
userId: string;
id: string;
name: string;
isPrivate: boolean;
memberIds: string[];
}): Promise<{ topicId: string }>;

Expand Down Expand Up @@ -129,7 +120,7 @@ class DiscussionClass extends mongoose.Model {
static async getList({ userId, topicId }) {
await this.checkPermission({ userId, topicId });

const filter: any = { topicId, $or: [{ isPrivate: false }, { memberIds: userId }] };
const filter: any = { topicId, memberIds: userId };

const discussions: any[] = await this.find(filter)
.sort({ lastActivityDate: -1 })
Expand All @@ -138,7 +129,7 @@ class DiscussionClass extends mongoose.Model {
return { discussions };
}

static async add({ name, userId, topicId, isPrivate = false, memberIds = [] }) {
static async add({ name, userId, topicId, memberIds = [] }) {
if (!name) {
throw new Error('Bad data');
}
Expand All @@ -152,13 +143,12 @@ class DiscussionClass extends mongoose.Model {
topicId,
name,
slug,
isPrivate,
memberIds: (isPrivate && uniq([userId, ...memberIds])) || [],
memberIds: uniq([userId, ...memberIds]),
createdAt: new Date(),
});
}

static async edit({ userId, id, name, isPrivate = false, memberIds = [] }) {
static async edit({ userId, id, name, memberIds = [] }) {
if (!id) {
throw new Error('Bad data');
}
Expand All @@ -181,8 +171,7 @@ class DiscussionClass extends mongoose.Model {
{ _id: id },
{
name,
isPrivate,
memberIds: (isPrivate && uniq([userId, ...memberIds])) || [],
memberIds: uniq([userId, ...memberIds]),
},
);

Expand Down
9 changes: 3 additions & 6 deletions api/server/models/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Topic from './Topic';
import Team from './Team';
import Discussion from './Discussion';
import { deleteFiles } from '../aws-s3';
// import logger from '../logs';

function deletePostFiles(posts: IPostDocument[]) {
const imgRegEx = /\<img.+data-src=[\"|\'](.+?)[\"|\']/g;
Expand Down Expand Up @@ -167,14 +166,14 @@ class PostClass extends mongoose.Model {
}

const discussion = await Discussion.findById(discussionId)
.select('topicId memberIds isPrivate slug')
.select('topicId memberIds slug')
.lean();

if (!discussion) {
throw new Error('Discussion not found');
}

if (discussion.isPrivate && discussion.memberIds.indexOf(userId) === -1) {
if (discussion.memberIds.indexOf(userId) === -1) {
throw new Error('Permission denied');
}

Expand Down Expand Up @@ -222,9 +221,7 @@ class PostClass extends mongoose.Model {

Discussion.updateOne({ _id: discussion._id }, { lastActivityDate: new Date() }).exec();

const memberIds: string[] = discussion.isPrivate
? discussion.memberIds.filter(id => id !== userId)
: team.memberIds.filter(id => id !== userId);
const memberIds: string[] = discussion.memberIds.filter(id => id !== userId);

return post;
}
Expand Down
80 changes: 22 additions & 58 deletions app/components/discussions/CreateDiscussionForm.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import React from 'react';
import Button from '@material-ui/core/Button';
import Radio from '@material-ui/core/Radio';
import RadioGroup from '@material-ui/core/RadioGroup';
import FormLabel from '@material-ui/core/FormLabel';
import FormControl from '@material-ui/core/FormControl';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Drawer from '@material-ui/core/Drawer';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import { inject } from 'mobx-react';
import Router from 'next/router';

import NProgress from 'nprogress';

import MemberChooser from '../users/MemberChooser';
import PostEditor from '../posts/PostEditor';
import notify from '../../lib/notifier';
import { Store } from '../../lib/store';
import AutoComplete from '../common/AutoComplete';
import PostEditor from '../posts/PostEditor';

const styles = {
paper: {
Expand All @@ -35,7 +29,6 @@ interface Props {
interface State {
name: string;
memberIds: string[];
privacy: string;
disabled: boolean;
content: string;
}
Expand All @@ -45,21 +38,16 @@ class CreateDiscussionForm extends React.Component<Props, State> {
name: '',
content: '',
memberIds: [],
privacy: 'public',
disabled: false,
};

handleClose = () => {
this.setState({ name: '', content: '', memberIds: [], privacy: 'public', disabled: false });
this.setState({ name: '', content: '', memberIds: [], disabled: false });
this.props.onClose();
};

handlePrivacyChange = event => {
this.setState({ privacy: event.target.value });
};

handleAutoCompleteChange = selectedItems => {
this.setState({ memberIds: selectedItems.map(i => i.value) });
handleMemberChange = memberIds => {
this.setState({ memberIds });
};

onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
Expand All @@ -78,8 +66,7 @@ class CreateDiscussionForm extends React.Component<Props, State> {
return;
}

const { name, memberIds, privacy, content } = this.state;
const isPrivate = privacy === 'private';
const { name, memberIds, content } = this.state;
if (!name) {
notify('Name is required');
return;
Expand All @@ -96,14 +83,13 @@ class CreateDiscussionForm extends React.Component<Props, State> {

const discussion = await currentTopic.addDiscussion({
name,
memberIds: isPrivate ? memberIds : [],
isPrivate,
memberIds,
});

await discussion.addPost(content);

this.setState({ name: '', memberIds: [], privacy: 'public' });
notify('You successfully created Discussion.');
this.setState({ name: '', memberIds: [] });
notify('You successfully created Discussion');

Router.push(
`/discussions/detail?teamSlug=${currentTeam.slug}&topicSlug=${
Expand All @@ -115,33 +101,26 @@ class CreateDiscussionForm extends React.Component<Props, State> {
console.log(error);
notify(error);
} finally {
this.props.onClose();
NProgress.done();
this.setState({ disabled: false });
NProgress.done();
this.props.onClose();
}
};

renderAutoComplete() {
renderMemberChooser() {
const { store } = this.props;
const { currentUser } = store;
const memberIds: string[] = [];

const suggestions = Array.from(store.currentTeam.members.values())
.filter(user => user._id !== currentUser._id)
.map(user => ({
label: user.displayName,
value: user._id,
}));

const selectedItems = suggestions.filter(s => memberIds.indexOf(s.value) !== -1);
const members = Array.from(store.currentTeam.members.values()).filter(
user => user._id !== currentUser._id,
);

return (
<AutoComplete
label="Type name of Team Member"
helperText="These members will see all posts and be notified about unread posts in this discussion."
onChange={this.handleAutoCompleteChange}
suggestions={suggestions}
selectedItems={selectedItems}
<MemberChooser
helperText="These members will see all posts and be notified about unread posts in this Discussion."
onChange={this.handleMemberChange}
members={members}
selectedMemberIds={this.state.memberIds}
/>
);
}
Expand Down Expand Up @@ -177,29 +156,14 @@ class CreateDiscussionForm extends React.Component<Props, State> {
this.setState({ name: event.target.value });
}}
/>
<p />
<br />
<FormControl component="fieldset">
<FormLabel component="legend">Privacy setting:</FormLabel>
<RadioGroup
aria-label="privacy"
name="privacy"
value={this.state.privacy}
onChange={this.handlePrivacyChange}
>
<FormControlLabel value="public" control={<Radio />} label="Public" />
<FormControlLabel value="private" control={<Radio />} label="Private" />
</RadioGroup>
</FormControl>
</div>
<p />
{this.renderMemberChooser()}
<br />
<PostEditor
content={this.state.content}
onChanged={content => this.setState({ content })}
/>
<p />

{this.state.privacy === 'private' ? this.renderAutoComplete() : null}
<br />
<div style={{ float: 'right' }}>
<Button type="submit" variant="raised" color="primary" disabled={this.state.disabled}>
Expand Down
Loading

0 comments on commit 351dc6c

Please sign in to comment.