From 08f73ad923dd6fabe25ba3c9e835ce2123274422 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Sat, 4 Jun 2022 14:26:53 -0700 Subject: [PATCH] Fix autoscroll, allow plaintext chat, forbid collab cancelation on wrong status --- api/logics.py | 2 + chat/consumers.py | 3 + frontend/src/components/Chat.js | 176 ----------------------- frontend/src/components/EncryptedChat.js | 50 +++++-- 4 files changed, 45 insertions(+), 186 deletions(-) delete mode 100644 frontend/src/components/Chat.js diff --git a/api/logics.py b/api/logics.py index 02a5e5de..1e2d2808 100644 --- a/api/logics.py +++ b/api/logics.py @@ -786,6 +786,8 @@ class Logics: @classmethod def collaborative_cancel(cls, order): + if not order.status in [Order.Status.WFI, Order.Status.CHA]: + return cls.return_bond(order.maker_bond) cls.return_bond(order.taker_bond) cls.return_escrow(order) diff --git a/chat/consumers.py b/chat/consumers.py index e5472a1d..67720f46 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -227,6 +227,7 @@ class ChatRoomConsumer(AsyncWebsocketConsumer): "peer_connected": peer_connected, }, ) + # Unencrypted messages are not stored, just echoed. else: await self.channel_layer.group_send( self.room_group_name, @@ -243,6 +244,8 @@ class ChatRoomConsumer(AsyncWebsocketConsumer): nick = event["nick"] peer_connected = event["peer_connected"] + print(message) + await self.send(text_data=json.dumps({ "message": message, "user_nick": nick, diff --git a/frontend/src/components/Chat.js b/frontend/src/components/Chat.js deleted file mode 100644 index 6d54e3a5..00000000 --- a/frontend/src/components/Chat.js +++ /dev/null @@ -1,176 +0,0 @@ -import React, { Component } from 'react'; -import { withTranslation, Trans} from "react-i18next"; -import {Button, Link, Badge, TextField, Grid, Container, Card, CardHeader, Paper, Avatar, FormHelperText, Typography} from "@mui/material"; -import ReconnectingWebSocket from 'reconnecting-websocket'; - -class Chat extends Component { - // Deprecated chat component - // Will still be used for ~1 week, until users change to robots with PGP keys - - constructor(props) { - super(props); - } - - state = { - messages: [], - value:'', - connected: false, - peer_connected: false, - }; - - rws = new ReconnectingWebSocket('ws://' + window.location.host + '/ws/chat/' + this.props.orderId + '/'); - - componentDidMount() { - this.rws.addEventListener('open', () => { - console.log('Connected!'); - this.setState({connected: true}); - this.rws.send(JSON.stringify({ - type: "message", - message: 'just-connected', - nick: this.props.ur_nick, - })); - }); - - this.rws.addEventListener('message', (message) => { - - const dataFromServer = JSON.parse(message.data); - console.log('Got reply!', dataFromServer.type); - - if (dataFromServer){ - if (dataFromServer.message != 'just-connected' & dataFromServer.message != 'peer-disconnected'){ - this.setState((state) => - ({ - messages: [...state.messages, - { - msg: dataFromServer.message, - userNick: dataFromServer.user_nick, - }], - }) - ) - } - this.setState({peer_connected: dataFromServer.peer_connected}) - } - }); - - this.rws.addEventListener('close', () => { - console.log('Socket is closed. Reconnect will be attempted'); - this.setState({connected: false}); - }); - - this.rws.addEventListener('error', () => { - console.error('Socket encountered error: Closing socket'); - }); - } - - componentDidUpdate() { - this.scrollToBottom(); - } - - scrollToBottom = () => { - this.messagesEnd.scrollIntoView({ behavior: "smooth" }); - } - - onButtonClicked = (e) => { - if(this.state.value!=''){ - this.rws.send(JSON.stringify({ - type: "message", - message: this.state.value, - nick: this.props.ur_nick, - })); - this.setState({value: ""}); - } - e.preventDefault(); - } - - render() { - const { t } = this.props; - return ( - - - - - - - {t("You")+": "}{this.state.connected ? t("connected"): t("disconnected")} - - - - - - - - {t("Peer")+": "}{this.state.peer_connected ? t("connected"): t("disconnected")} - - - - - - - {this.state.messages.map((message, index) => -
  • - - {/* If message sender is not our nick, gray color, if it is our nick, green color */} - {message.userNick == this.props.ur_nick ? - - - - } - style={{backgroundColor: '#eeeeee'}} - title={message.userNick} - subheader={message.msg} - subheaderTypographyProps={{sx: {wordWrap: "break-word", width: '200px', color: '#444444'}}} - /> - : - - - - } - style={{backgroundColor: '#fafafa'}} - title={message.userNick} - subheader={message.msg} - subheaderTypographyProps={{sx: {wordWrap: "break-word", width: '200px', color: '#444444'}}} - />} - -
  • )} -
    { this.messagesEnd = el; }}>
    -
    -
    - - - { - this.setState({ value: e.target.value }); - this.value = this.state.value; - }} - sx={{width: 244}} - /> - - - - - -
    - - {t("The chat has no memory: if you leave, messages are lost.")} {t("Learn easy PGP encryption.")} - -
    - ) - } -} - -export default withTranslation()(Chat); diff --git a/frontend/src/components/EncryptedChat.js b/frontend/src/components/EncryptedChat.js index ca9ab66a..057ea480 100644 --- a/frontend/src/components/EncryptedChat.js +++ b/frontend/src/components/EncryptedChat.js @@ -110,7 +110,26 @@ class Chat extends Component { }), }) )); - } + + } else + + // We allow plaintext communication. The user must write # to start + // If we receive an plaintext message + if (dataFromServer.message.substring(0,1) == "#"){ + console.log("Got plaintext message", dataFromServer.message) + this.setState((state) => + ({ + scrollNow: true, + messages: [...state.messages, + { + index: this.state.latestIndex + 0.001, + encryptedMessage: dataFromServer.message, + plainTextMessage: dataFromServer.message, + validSignature: false, + userNick: dataFromServer.user_nick, + time: (new Date).toString(), + }]})); + } } }); @@ -125,6 +144,8 @@ class Chat extends Component { } componentDidUpdate() { + + // Only fire the scroll when the reason for Update is a new message if (this.state.scrollNow){ this.scrollToBottom(); this.setState({scrollNow:false}) @@ -136,17 +157,26 @@ class Chat extends Component { } onButtonClicked = (e) => { - if(this.state.value!=''){ - this.setState({waitingEcho:true, lastSent:this.state.value}); + if(this.state.value.substring(0,1)=='#'){ + this.rws.send(JSON.stringify({ + type: "message", + message: this.state.value, + nick: this.props.ur_nick, + })); + this.setState({value: ""}); + } + + else if(this.state.value!=''){ + this.setState({value: "", waitingEcho: true, lastSent:this.state.value}) encryptMessage(this.state.value, this.state.own_pub_key, this.state.peer_pub_key, this.state.own_enc_priv_key, this.state.token) .then((encryptedMessage) => - console.log("Sending Encrypted MESSAGE "+encryptedMessage) & + console.log("Sending Encrypted MESSAGE", encryptedMessage) & this.rws.send(JSON.stringify({ type: "message", message: encryptedMessage.split('\n').join('\\'), nick: this.props.ur_nick, }) - ) & this.setState({value: "", waitingEcho: false}) + ) ); } e.preventDefault(); @@ -179,8 +209,8 @@ class Chat extends Component { style={{backgroundColor: props.cardColor}} title={ -
    -
    +
    +
    {props.message.userNick} {props.message.validSignature ? @@ -240,8 +270,8 @@ class Chat extends Component { -
    - +
    + {this.state.messages.map((message, index) =>
  • {message.userNick == this.props.ur_nick ? @@ -265,7 +295,7 @@ class Chat extends Component { this.setState({ value: e.target.value }); this.value = this.state.value; }} - sx={{width: 232}} + sx={{width: 219}} />