Skip to content

Commit

Permalink
part of async-labs#23
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed May 1, 2019
1 parent d603087 commit 6a6cc28
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"dependencies": {
"@types/mongoose": "5.3.23",
"@types/node": "11.11.6",
"@types/socket.io": "^2.1.2",
"aws-sdk": "2.428.0",
"compression": "1.7.4",
"connect-mongo": "2.0.3",
Expand Down Expand Up @@ -57,6 +56,7 @@
"@types/handlebars": "4.1.0",
"@types/lodash": "4.14.123",
"@types/passport": "1.0.0",
"@types/socket.io": "^2.1.2",
"@types/stripe": "6.25.6",
"husky": "1.3.1",
"jest": "24.5.0",
Expand Down
2 changes: 2 additions & 0 deletions api/server/api/team-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ router.post('/posts/edit', async (req, res, next) => {

postEdited({ socketId, post: updatedPost });

// console.log(socketId);

res.json({ done: 1 });
} catch (err) {
next(err);
Expand Down
2 changes: 1 addition & 1 deletion api/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ auth({ server, ROOT_URL });
api(server);

const http = new httpModule.Server(server);
realtime({ http, origin: PRODUCTION_URL_APP, sessionMiddleware });
realtime({ http, origin: dev ? 'http://localhost:3000' : PRODUCTION_URL_APP, sessionMiddleware });

server.get('/uploaded-file', async (req, res) => {
if (!req.user) {
Expand Down
11 changes: 11 additions & 0 deletions api/server/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function getSocket(socketId?: string) {
}

function setup({ http, origin, sessionMiddleware }) {

if (io === null) {
io = socketio(http, {
origins: `${origin}:443`,
Expand Down Expand Up @@ -51,6 +52,16 @@ function setup({ http, origin, sessionMiddleware }) {

socket.join(`user-${userId}`);

socket.on('joinTeam', teamId => {
logger.debug(` joinTeam ${teamId}`);
socket.join(`team-${teamId}`);
});

socket.on('leaveTeam', teamId => {
logger.debug(`** leaveTeam ${teamId}`);
socket.leave(`team-${teamId}`);
});

socket.on('joinDiscussion', discussionId => {
logger.debug(` joinDiscussion ${discussionId}`);
socket.join(`discussion-${discussionId}`);
Expand Down
1 change: 0 additions & 1 deletion app/lib/store/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class Discussion {
}

public async addPost(content: string): Promise<Post> {

console.log(this.store.socket);
console.log(this.store.socket.id);

Expand Down
8 changes: 3 additions & 5 deletions app/lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ class Store {
for (const team of this.teams) {
if (team.slug === slug) {
found = true;
team.loadInitialMembers().catch(err => console.log(err));
this.currentTeam = team;
team.joinSocketRoom();
this.loadCurrentTeamData();
break;
}
}
Expand Down Expand Up @@ -183,7 +184,7 @@ class Store {
this.teams.remove(team);
}

private setCurrentUser(user, isLoadTeam: boolean, selectedTeamSlug: string) {
private async setCurrentUser(user, isLoadTeam: boolean, selectedTeamSlug: string) {
if (user) {
this.currentUser = new User({ store: this, ...user });

Expand Down Expand Up @@ -242,9 +243,6 @@ decorate(Store, {
isLoggingIn: observable,

changeCurrentUrl: action,
setCurrentUser: action,
changeUserState: action,
setTeams: action,
addTeam: action,
loadTeams: action,
setCurrentTeam: action,
Expand Down
1 change: 0 additions & 1 deletion app/lib/store/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class Post {
}

public async edit(data) {
console.log(this.store.socket);
console.log(this.store.socket.id);

try {
Expand Down
23 changes: 8 additions & 15 deletions app/lib/store/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,33 +362,26 @@ class Team {

public leaveSocketRoom() {
if (this.store.socket) {
this.store.socket.off('discussionEvent', this.handleDiscussionRealtimeEvent);
console.log('leaving socket team room', this.name);
this.store.socket.emit('leaveTeam', this._id);

this.discussion.leaveSocketRoom();
}
}

public joinSocketRoom() {
if (this.store.socket) {
this.store.socket.on('discussionEvent', this.handleDiscussionRealtimeEvent);
console.log('joining socket team room', this.name);
this.store.socket.emit('joinTeam', this._id);

this.discussion.joinSocketRoom();
}
}

public changeLocalCache(data) {
this.name = data.name;
this.memberIds.replace(data.memberIds || []);
}

private handleDiscussionRealtimeEvent = data => {
console.log('discussion realtime event', data);
const { action: actionName } = data;

if (actionName === 'added') {
this.addDiscussionToLocalCache(data.discussion);
} else if (actionName === 'edited') {
this.editDiscussionFromLocalCache(data.discussion);
} else if (actionName === 'deleted') {
this.removeDiscussionFromLocalCache(data.id);
}
};
}

decorate(Team, {
Expand Down
5 changes: 5 additions & 0 deletions app/pages/discussion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class DiscussionComp extends React.Component<Props> {
this.props.store.socket.on('reconnect', this.handleSocketReconnect);

this.changeDiscussion();

const discussion = this.getDiscussion(this.props.discussionSlug);
if (discussion) {
discussion.joinSocketRoom();
}
}

public componentDidUpdate(prevProps: Props) {
Expand Down

0 comments on commit 6a6cc28

Please sign in to comment.