diff --git a/src/actions/index.js b/src/actions/index.js index db75e99..62222bd 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -483,39 +483,29 @@ export function receivePixelUpdate( }; } +export function loginUser( + me: Object, +): Action { + console.log('login', me); + return { + type: 'LOGIN', + ...me, + }; +} + export function receiveMe( me: Object, ): Action { - const { - name, - messages, - mailreg, - totalPixels, - dailyTotalPixels, - ranking, - dailyRanking, - minecraftname, - blockDm, - canvases, - channels, - blocked, - userlvl, - } = me; return { type: 'RECEIVE_ME', - name: (name) || null, - messages: (messages) || [], - mailreg: (mailreg) || false, - totalPixels, - dailyTotalPixels, - ranking, - dailyRanking, - minecraftname, - blockDm: !!blockDm, - canvases, - channels, - blocked, - userlvl, + ...me, + }; +} + +export function logoutUser( +): Action { + return { + type: 'LOGOUT', }; } @@ -819,11 +809,9 @@ export function startDm(query): PromiseAction { 'OK', )); } else { - const cid = res[0]; - if (cid) { - dispatch(addChatChannel(res)); - dispatch(setChatChannel(cid)); - } + const cid = Object.keys(res)[0]; + dispatch(addChatChannel(res)); + dispatch(setChatChannel(cid)); } dispatch(setApiFetching(false)); }; diff --git a/src/actions/types.js b/src/actions/types.js index 44acfea..7c90f72 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -94,6 +94,23 @@ export type Action = blocked: Array, userlvl: number, } + | { type: 'LOGIN', + name: string, + waitSeconds: number, + messages: Array, + mailreg: boolean, + totalPixels: number, + dailyTotalPixels: number, + ranking: number, + dailyRanking: number, + minecraftname: string, + blockDm: boolean, + canvases: Object, + channels: Object, + blocked: Array, + userlvl: number, + } + | { type: 'LOGOUT' } | { type: 'RECEIVE_STATS', totalRanking: Object, totalDailyRanking: Object } | { type: 'SET_NAME', name: string } | { type: 'SET_MINECRAFT_NAME', minecraftname: string } diff --git a/src/components/DeleteAccount.jsx b/src/components/DeleteAccount.jsx index f7cc54e..263e5d8 100644 --- a/src/components/DeleteAccount.jsx +++ b/src/components/DeleteAccount.jsx @@ -4,8 +4,11 @@ */ import React from 'react'; +import { connect } from 'react-redux'; import { validatePassword, parseAPIresponse } from '../utils/validation'; +import { logoutUser } from '../actions'; + function validate(password) { const errors = []; @@ -64,8 +67,8 @@ class DeleteAccount extends React.Component { }); return; } - const { setName } = this.props; - setName(null); + const { logout } = this.props; + logout(); } render() { @@ -94,4 +97,12 @@ class DeleteAccount extends React.Component { } } -export default DeleteAccount; +function mapDispatchToProps(dispatch) { + return { + async logout() { + dispatch(logoutUser()); + }, + }; +} + +export default connect(null, mapDispatchToProps)(DeleteAccount); diff --git a/src/components/LogInForm.jsx b/src/components/LogInForm.jsx index 2569897..128c30a 100644 --- a/src/components/LogInForm.jsx +++ b/src/components/LogInForm.jsx @@ -3,9 +3,11 @@ * @flow */ import React from 'react'; +import { connect } from 'react-redux'; import { validateEMail, validateName, validatePassword, parseAPIresponse, } from '../utils/validation'; +import { loginUser } from '../actions'; function validate(nameoremail, password) { @@ -60,7 +62,7 @@ class LogInForm extends React.Component { e.preventDefault(); const { nameoremail, password, submitting } = this.state; - const { me: setMe } = this.props; + const { login } = this.props; if (submitting) return; const errors = validate(nameoremail, password); @@ -80,7 +82,7 @@ class LogInForm extends React.Component { }); return; } - setMe(me); + login(me); } render() { @@ -116,4 +118,12 @@ class LogInForm extends React.Component { } } -export default LogInForm; +function mapDispatchToProps(dispatch) { + return { + login(me) { + dispatch(loginUser(me)); + }, + }; +} + +export default connect(null, mapDispatchToProps)(LogInForm); diff --git a/src/components/SignUpForm.jsx b/src/components/SignUpForm.jsx index 9945c49..d95c6d2 100644 --- a/src/components/SignUpForm.jsx +++ b/src/components/SignUpForm.jsx @@ -9,7 +9,7 @@ import { validateEMail, validateName, validatePassword, parseAPIresponse, } from '../utils/validation'; -import { showUserAreaModal, receiveMe } from '../actions'; +import { showUserAreaModal, loginUser } from '../actions'; function validate(name, email, password, confirmPassword) { @@ -94,8 +94,8 @@ class SignUpForm extends React.Component { }); return; } - const { doMe, userarea } = this.props; - doMe(me); + const { login, userarea } = this.props; + login(me); userarea(); } @@ -163,8 +163,8 @@ class SignUpForm extends React.Component { function mapDispatchToProps(dispatch) { return { - doMe(me) { - dispatch(receiveMe(me)); + login(me) { + dispatch(loginUser(me)); }, userarea() { dispatch(showUserAreaModal()); diff --git a/src/components/UserArea.jsx b/src/components/UserArea.jsx index 30f11d9..cfc9ee5 100644 --- a/src/components/UserArea.jsx +++ b/src/components/UserArea.jsx @@ -14,6 +14,7 @@ import ChangeName from './ChangeName'; import ChangeMail from './ChangeMail'; import DeleteAccount from './DeleteAccount'; import SocialSettings from './SocialSettings'; +import { logoutUser } from '../actions'; import { numberToString } from '../core/utils'; @@ -172,7 +173,6 @@ class UserArea extends React.Component { {(deleteAccountExtended) && ( { this.setState({ deleteAccountExtended: false }); }} /> )} @@ -226,4 +226,18 @@ function mapStateToProps(state: State) { return { name, mailreg, stats }; } -export default connect(mapStateToProps)(UserArea); +function mapDispatchToProps(dispatch) { + return { + async logout() { + const response = await fetch( + './api/auth/logout', + { credentials: 'include' }, + ); + if (response.ok) { + dispatch(logoutUser()); + } + }, + }; +} + +export default connect(mapStateToProps, mapDispatchToProps)(UserArea); diff --git a/src/components/UserAreaModal.jsx b/src/components/UserAreaModal.jsx index 3668d1a..2582005 100644 --- a/src/components/UserAreaModal.jsx +++ b/src/components/UserAreaModal.jsx @@ -10,7 +10,7 @@ import type { State } from '../reducers'; import { - showRegisterModal, showForgotPasswordModal, setName, setMailreg, receiveMe, + showRegisterModal, showForgotPasswordModal, setName, setMailreg, } from '../actions'; import LogInForm from './LogInForm'; import Tabs from './Tabs'; @@ -87,8 +87,6 @@ const UserAreaModal = ({ name, register, forgotPassword, - doMe, - logout, setUserName, setUserMailreg, userlvl, @@ -99,14 +97,12 @@ const UserAreaModal = ({ ) : (
@@ -142,25 +138,12 @@ function mapDispatchToProps(dispatch) { forgotPassword() { dispatch(showForgotPasswordModal()); }, - doMe(me) { - dispatch(receiveMe(me)); - }, setUserName(name) { dispatch(setName(name)); }, setUserMailreg(mailreg) { dispatch(setMailreg(mailreg)); }, - async logout() { - const response = await fetch( - './api/auth/logout', - { credentials: 'include' }, - ); - if (response.ok) { - const resp = await response.json(); - dispatch(receiveMe(resp.me)); - } - }, }; } diff --git a/src/core/constants.js b/src/core/constants.js index ed2f858..29ba4fc 100644 --- a/src/core/constants.js +++ b/src/core/constants.js @@ -71,16 +71,6 @@ export const TILE_SIZE = 256; // how much to scale for a new tiled zoomlevel export const TILE_ZOOM_LEVEL = 4; -// TODO get rid of those or use it myself -export const social = { - facebook: 'https://www.facebook.com/pixelplanetfun/', - reddit: 'https://reddit.com/r/PixelPlanetFun', - twitter: 'https://twitter.com/pixelplanetfun', - discord: 'https://pixelplanet.fun/discord', - telegram: 'https://telegram.me/pixelplanetfun', - youtube: 'https://www.youtube.com/c/PixelPlanetFun', -}; - export const COOKIE_SESSION_NAME = 'pixelplanet.session'; export const SECOND = 1000; diff --git a/src/core/utils.js b/src/core/utils.js index 3a1c6ca..3dcbb56 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -137,7 +137,6 @@ export function getCellInsideChunk( canvasSize: number, pixel: Cell, ): Cell { - // TODO assert is positive! return pixel.map((x) => mod(x + canvasSize / 2, TILE_SIZE)); } diff --git a/src/data/models/User.js b/src/data/models/User.js index f38c1c9..c81a0e1 100644 --- a/src/data/models/User.js +++ b/src/data/models/User.js @@ -198,7 +198,18 @@ class User { if (this.regUser == null) { return { name: null, + mailVerified: false, + mcVerified: false, + minecraftname: null, + blockDm: false, + totalPixels: 0, + dailyTotalPixels: 0, + ranking: null, + dailyRanking: null, + mailreg: false, + userlvl: 0, channels: this.channels, + blocked: this.blocked, }; } const { regUser } = this; diff --git a/src/reducers/chat.js b/src/reducers/chat.js index 5953878..1b61365 100644 --- a/src/reducers/chat.js +++ b/src/reducers/chat.js @@ -41,7 +41,8 @@ export default function chat( action: Action, ): ChatState { switch (action.type) { - case 'RECEIVE_ME': { + case 'RECEIVE_ME': + case 'LOGIN': { return { ...state, channels: action.channels, @@ -49,6 +50,26 @@ export default function chat( }; } + case 'LOGOUT': { + const channels = { ...state.channels }; + const messages = { ...state.messages }; + const keys = Object.keys(channels); + for (let i = 0; i < messages.length; i += 1) { + const cid = keys[i]; + if (channels[cid][1] === 0) { + delete messages[cid]; + delete channels[cid]; + } + } + return { + ...state, + inputMessage: '', + channels, + blocked: [], + messages, + }; + } + case 'BLOCK_USER': { const { userId, userName } = action; const blocked = [ @@ -88,6 +109,7 @@ export default function chat( case 'ADD_CHAT_CHANNEL': { const { channel } = action; + console.log('adding channel', channel); return { ...state, channels: { diff --git a/src/reducers/gui.js b/src/reducers/gui.js index 993d756..f9814c5 100644 --- a/src/reducers/gui.js +++ b/src/reducers/gui.js @@ -145,7 +145,8 @@ export default function gui( }; } - case 'RECEIVE_ME': { + case 'RECEIVE_ME': + case 'LOGIN': { const { channels } = action; const cids = Object.keys(channels); const chatRead = { ...state.chatRead }; diff --git a/src/reducers/modal.js b/src/reducers/modal.js index 331d8dc..5085d7a 100644 --- a/src/reducers/modal.js +++ b/src/reducers/modal.js @@ -49,12 +49,10 @@ export default function modal( }; } - case 'RECEIVE_ME': { - const { name } = action; - const chatOpen = (name) ? state.chatOpen : false; + case 'LOGOUT': { return { ...state, - chatOpen, + chatOpen: false, }; } diff --git a/src/reducers/user.js b/src/reducers/user.js index cb9ccf1..7993fb5 100644 --- a/src/reducers/user.js +++ b/src/reducers/user.js @@ -139,7 +139,8 @@ export default function user( }; } - case 'RECEIVE_ME': { + case 'RECEIVE_ME': + case 'LOGIN': { const { name, mailreg, @@ -169,6 +170,19 @@ export default function user( }; } + case 'LOGOUT': { + return { + ...state, + name: null, + messages: [], + mailreg: false, + minecraftname: null, + blockDm: false, + userlvl: 0, + nameRegExp: null, + }; + } + case 'RECEIVE_STATS': { const { totalRanking, totalDailyRanking } = action; return { diff --git a/src/routes/api/auth/logout.js b/src/routes/api/auth/logout.js index 737cbae..44b37f2 100644 --- a/src/routes/api/auth/logout.js +++ b/src/routes/api/auth/logout.js @@ -4,9 +4,6 @@ */ import type { Request, Response } from 'express'; -import getMe from '../../../core/me'; -import chatProvider from '../../../core/ChatProvider'; - export default async (req: Request, res: Response) => { const { user } = req; if (!user) { @@ -17,16 +14,9 @@ export default async (req: Request, res: Response) => { return; } - const me = await getMe(user); req.logout(); res.status(200); res.json({ success: true, - me: { - name: null, - waitSeconds: me.waitSeconds, - canvases: me.canvases, - channels: chatProvider.defaultChannels, - }, }); }; diff --git a/src/routes/api/block.js b/src/routes/api/block.js index 8c4ed87..263ad4b 100644 --- a/src/routes/api/block.js +++ b/src/routes/api/block.js @@ -98,7 +98,6 @@ async function block(req: Request, res: Response) { dmu2id = userId; } - // TODO test if this removes association too const channel = await Channel.findOne({ where: { type: 1, diff --git a/src/socket/ProtocolClient.js b/src/socket/ProtocolClient.js index 27defa5..5b2f62d 100644 --- a/src/socket/ProtocolClient.js +++ b/src/socket/ProtocolClient.js @@ -23,7 +23,6 @@ const chunks = []; class ProtocolClient extends EventEmitter { url: string; ws: WebSocket; - name: string; canvasId: number; channelId: number; timeConnected: number; @@ -37,7 +36,6 @@ class ProtocolClient extends EventEmitter { this.isConnecting = false; this.isConnected = false; this.ws = null; - this.name = null; this.canvasId = '0'; this.channelId = 0; this.msgQueue = []; @@ -96,14 +94,6 @@ class ProtocolClient extends EventEmitter { this.ws.close(); } - setName(name) { - if (this.isConnected && this.name !== name) { - console.log('Name change requieres WebSocket restart'); - this.name = name; - this.reconnect(); - } - } - setCanvas(canvasId) { /* canvasId can be string or integer, thanks to * JSON not allowing integer keys @@ -181,13 +171,11 @@ class ProtocolClient extends EventEmitter { // signal const [signal, args] = data; this.emit(signal, args); + break; } default: // nothing } - } else { - // string = name - this.name = data; } } diff --git a/src/socket/SocketServer.js b/src/socket/SocketServer.js index 8b2db88..8a23334 100644 --- a/src/socket/SocketServer.js +++ b/src/socket/SocketServer.js @@ -88,10 +88,6 @@ class SocketServer extends WebSocketEvents { ws.rateLimiter = new RateLimiter(20, 15, true); cheapDetector(user.ip); - if (ws.name) { - ws.send(`"${ws.name}"`); - } - ws.send(OnlineCounter.dehydrate({ online: this.wss.clients.size || 0, })); @@ -337,16 +333,17 @@ class SocketServer extends WebSocketEvents { */ const dmUserId = chatProvider.checkIfDm(user, channelId); if (dmUserId) { + console.log('is dm'); const dmWs = this.findWsByUserId(dmUserId); - if (dmWs) { - const { user: dmUser } = dmWs; - if (!dmUser || !dmUser.userHasChannelAccess(channelId)) { - ChatProvider.addUserToChannel( - dmUserId, - channelId, - [ws.name, 1, Date.now(), user.id], - ); - } + if (!dmWs + || !chatProvider.userHasChannelAccess(dmWs.user, channelId) + ) { + console.log('adding channel') + ChatProvider.addUserToChannel( + dmUserId, + channelId, + [ws.name, 1, Date.now(), user.id], + ); } } diff --git a/src/store/protocolClientHook.js b/src/store/protocolClientHook.js index c8629d5..456661d 100644 --- a/src/store/protocolClientHook.js +++ b/src/store/protocolClientHook.js @@ -18,19 +18,10 @@ export default (store) => (next) => (action) => { break; } - /* - * TODO - * make LOGIN / LOGOUT Actions instead of comparing name changes - */ - case 'RECEIVE_ME': { - const { name } = action; - ProtocolClient.setName(name); - break; - } - - case 'SET_NAME': { - const { name } = action; - ProtocolClient.setName(name); + case 'SET_NAME': + case 'LOGIN:': + case 'LOGOUT': { + ProtocolClient.reconnect(); break; }