Skip to content

Commit

Permalink
Merge pull request #131 from boostcamp-2020/dev
Browse files Browse the repository at this point in the history
Dev -> master : 신규 유저 random채널로 이동
  • Loading branch information
rnjshippo authored Dec 16, 2020
2 parents 1d5be62 + 33b5175 commit 4698a34
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion server/src/lib/passport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from '@/config';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import passport from 'passport';
import { userModel } from '@/models';
import { channelModel, userModel } from '@/models';
import { User } from '@/types';

export default () => {
Expand Down Expand Up @@ -32,6 +32,7 @@ export default () => {
}

const [{ insertId }] = await userModel.addOAuthUser({ email, displayName });
await channelModel.setUserChannel({ userId: insertId });
done(undefined, { id: insertId, displayName, email });
return;
} catch (err) {
Expand Down
7 changes: 0 additions & 7 deletions server/src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ export const bindSocketServer = (server: http.Server): void => {
const namespace = io.of('/');

namespace.on(CONNECT, (socket: Socket) => {
console.log('메인 채널 연결됨 socketID : ', socket.id);
// io.to(socket.id).emit(MESSAGE, { socketId: socket.id });

socket.on(MESSAGE, async (data: SocketEvent) => {
if (isThreadEvent(data)) {
const { type, subType, room, thread } = data;
Expand Down Expand Up @@ -381,14 +378,10 @@ export const bindSocketServer = (server: http.Server): void => {
});

socket.on(ENTER_ROOM, (data: RoomEvent) => {
console.log('enter');
console.dir(data, { depth: null });
socket.join(data.room);
});

socket.on(LEAVE_ROOM, (data: RoomEvent) => {
console.log('leave');
console.dir(data, { depth: null });
socket.leave(data.room);
});

Expand Down
25 changes: 25 additions & 0 deletions server/src/models/channels.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,29 @@ export const channelModel: Model = {
const sql = `UPDATE user_channel SET unread = ? WHERE user_id = ? AND channel_id = ?;`;
return pool.execute(sql, [unread, userId, channelId]);
},
async setUserChannel({
userId,
channelId = 1,
}: {
userId: number;
channelId?: number;
}): Promise<{ err?: Error }> {
const conn = await pool.getConnection();
await conn.beginTransaction();
try {
const sql1 = `INSERT INTO user_channel (user_id, channel_id) VALUES (?, ?);`;
const sql2 = `UPDATE channel SET member_count = member_count + 1 WHERE id = ?;`;

conn.execute(sql1, [userId, channelId]);
conn.execute(sql2, [channelId]);
conn.commit();
return {};
} catch (err) {
console.error(err);
conn.rollback();
return { err };
} finally {
conn.release();
}
},
};
5 changes: 3 additions & 2 deletions server/src/routes/api/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
decrypt,
sendEmail,
} from '@/utils/utils';
import { userModel } from '@/models';
import { channelModel, userModel } from '@/models';
import config from '@/config';
import { TIME, TOKEN_TYPE, ERROR_MESSAGE } from '@/utils/constants';
import isEmail from 'validator/lib/isEmail';
Expand Down Expand Up @@ -71,7 +71,8 @@ export const signup = async (req: Request, res: Response, next: NextFunction): P
if (verifyRequestData([email, pw, displayName])) {
try {
const hashPw = await bcrypt.hash(pw, 10);
await userModel.addUser({ email, pw: hashPw, displayName });
const [{ insertId }] = await userModel.addUser({ email, pw: hashPw, displayName });
await channelModel.setUserChannel({ userId: insertId });

res.status(200).end();
return;
Expand Down
3 changes: 2 additions & 1 deletion server/src/routes/api/oauth/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ERROR_MESSAGE, TIME } from '@/utils/constants';
import { User } from '@/types';
import axios from 'axios';
import { verifyRequestData } from '@/utils/utils';
import { userModel } from '@/models';
import { channelModel, userModel } from '@/models';

/**
* GET /api/oauth/google
Expand Down Expand Up @@ -67,6 +67,7 @@ export const googleSignup = async (
userInfo = { email: user.email, displayName: user.displayName, id: user.id };
} else {
const [{ insertId }] = await userModel.addOAuthUser({ email, displayName: name });
await channelModel.setUserChannel({ userId: insertId });
userInfo = { email, displayName: name, id: insertId };
}

Expand Down

0 comments on commit 4698a34

Please sign in to comment.