Skip to content

Commit

Permalink
Add turtle chat component
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit f60870f
Author: Reckless_Satoshi <[email protected]>
Date:   Thu Nov 10 10:30:42 2022 -0800

    Fix internal error when entering chat

commit f1eeb49
Author: Reckless_Satoshi <[email protected]>
Date:   Tue Nov 8 10:08:22 2022 -0800

    Fix final serializer

commit d0c08ba
Author: Reckless_Satoshi <[email protected]>
Date:   Tue Nov 8 09:44:57 2022 -0800

    Chat API changes

commit a66bf64
Author: Reckless_Satoshi <[email protected]>
Date:   Tue Nov 8 09:28:29 2022 -0800

    Fix param on post

commit 60b18d1
Author: Reckless_Satoshi <[email protected]>
Date:   Tue Nov 8 08:56:25 2022 -0800

    Fix serializer

commit 11212d3
Author: KoalaSat <[email protected]>
Date:   Sun Nov 6 21:07:18 2022 +0100

    CR 2

commit c82790c
Author: KoalaSat <[email protected]>
Date:   Sun Nov 6 20:09:18 2022 +0100

    Fix

commit 605a3b6
Author: KoalaSat <[email protected]>
Date:   Sun Nov 6 14:44:42 2022 +0100

    CR

commit 09776e9
Author: KoalaSat <[email protected]>
Date:   Wed Nov 2 18:12:29 2022 +0100

    translations

commit 432e4d2
Author: KoalaSat <[email protected]>
Date:   Wed Nov 2 17:39:02 2022 +0100

    Switch and better UX

commit df6e476
Author: KoalaSat <[email protected]>
Date:   Tue Nov 1 18:20:01 2022 +0100

    Unused code

commit 5b8d6b4
Author: Reckless_Satoshi <[email protected]>
Date:   Mon Oct 31 09:20:20 2022 -0700

    Add Chat Turtle Mode
  • Loading branch information
Reckless-Satoshi committed Nov 11, 2022
1 parent 160c24c commit 3446fc3
Show file tree
Hide file tree
Showing 14 changed files with 994 additions and 543 deletions.
11 changes: 7 additions & 4 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
get_robosats_commit,
get_robosats_version,
)
from chat.views import ChatView
from chat.models import Message
from control.models import AccountingDay, BalanceLog

from .nick_generator.nick_generator import NickGenerator
Expand Down Expand Up @@ -405,9 +405,12 @@ def get(self, request, format=None):
else:
data["asked_for_cancel"] = False

offset = request.GET.get("offset", None)
if offset:
data["chat"] = ChatView.get(None, request).data
# Add index of last chat message. To be used by client on Chat endpoint to fetch latest messages
messages = Message.objects.filter(order=order)
if len(messages) == 0:
data["chat_last_index"] = 0
else:
data["chat_last_index"] = messages.latest().index

# 9) If status is 'DIS' and all HTLCS are in LOCKED
elif order.status == Order.Status.DIS:
Expand Down
9 changes: 8 additions & 1 deletion chat/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Meta:
class PostMessageSerializer(serializers.ModelSerializer):
class Meta:
model = Message
fields = ("PGP_message", "order", "offset")
fields = ("PGP_message", "order_id", "offset")
depth = 0

offset = serializers.IntegerField(
Expand All @@ -28,3 +28,10 @@ class Meta:
min_value=0,
help_text="Offset for message index to get as response",
)

order_id = serializers.IntegerField(
allow_null=False,
required=True,
min_value=0,
help_text="Order ID of chatroom",
)
13 changes: 8 additions & 5 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ def get(self, request, format=None):
chatroom.maker_connected = True
chatroom.save()
peer_connected = chatroom.taker_connected
peer_public_key = order.taker.profile.public_key
elif chatroom.taker == request.user:
chatroom.maker_connected = order.maker_last_seen > (
timezone.now() - timedelta(minutes=1)
)
chatroom.taker_connected = True
chatroom.save()
peer_connected = chatroom.maker_connected
peer_public_key = order.maker.profile.public_key

messages = []
for message in queryset:
d = ChatSerializer(message).data
print(d)
# Re-serialize so the response is identical to the consumer message
data = {
"index": d["index"],
Expand All @@ -79,7 +80,11 @@ def get(self, request, format=None):
}
messages.append(data)

response = {"peer_connected": peer_connected, "messages": messages}
response = {
"peer_connected": peer_connected,
"messages": messages,
"peer_pubkey": peer_public_key,
}

return Response(response, status.HTTP_200_OK)

Expand All @@ -94,8 +99,7 @@ def post(self, request, format=None):
context = {"bad_request": "Invalid serializer"}
return Response(context, status=status.HTTP_400_BAD_REQUEST)

print(request)
order_id = serializer.data.get("order", None)
order_id = serializer.data.get("order_id")

if order_id is None:
return Response(
Expand Down Expand Up @@ -172,7 +176,6 @@ def post(self, request, format=None):
messages = []
for message in queryset:
d = ChatSerializer(message).data
print(d)
# Re-serialize so the response is identical to the consumer message
data = {
"index": d["index"],
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ services:
environment:
TOR_PROXY_IP: 127.0.0.1
TOR_PROXY_PORT: 9050
ROBOSATS_ONION: robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion
ROBOSATS_ONION: robotestagw3dcxmd66r4rgksb4nmmr43fh77bzn2ia2eucduyeafnyd.onion
network_mode: service:tor
volumes:
- ./frontend/static:/usr/src/robosats/static
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { useState } from 'react';
import { Grid, useTheme, Tooltip, Button } from '@mui/material';
import { ExportIcon } from '../../../Icons';
import KeyIcon from '@mui/icons-material/Key';
import { useTranslation } from 'react-i18next';
import { saveAsJson } from '../../../../utils';

interface Props {
orderId: number;
setAudit: (audit: boolean) => void;
audit: boolean;
createJsonFile: () => object;
}

const ChatBottom: React.FC<Props> = ({ orderId, setAudit, audit, createJsonFile }) => {
const { t } = useTranslation();
const theme = useTheme();

return (
<>
<Grid item xs={6}>
<Tooltip
placement='bottom'
enterTouchDelay={0}
enterDelay={500}
enterNextDelay={2000}
title={t('Verify your privacy')}
>
<Button size='small' color='primary' variant='outlined' onClick={() => setAudit(!audit)}>
<KeyIcon />
{t('Audit PGP')}{' '}
</Button>
</Tooltip>
</Grid>

<Grid item xs={6}>
<Tooltip
placement='bottom'
enterTouchDelay={0}
enterDelay={500}
enterNextDelay={2000}
title={t('Save full log as a JSON file (messages and credentials)')}
>
<Button
size='small'
color='primary'
variant='outlined'
onClick={() => saveAsJson('complete_log_chat_' + orderId + '.json', createJsonFile())}
>
<div style={{ width: '1.4em', height: '1.4em' }}>
<ExportIcon sx={{ width: '0.8em', height: '0.8em' }} />
</div>{' '}
{t('Export')}{' '}
</Button>
</Tooltip>
</Grid>
</>
);
};

export default ChatBottom;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { Grid, Paper, Typography, useTheme } from '@mui/material';
import { useTranslation } from 'react-i18next';

interface Props {
connected: boolean;
peerConnected: boolean;
}

const ChatHeader: React.FC<Props> = ({ connected, peerConnected }) => {
const { t } = useTranslation();
const theme = useTheme();
const connectedColor = theme.palette.mode === 'light' ? '#b5e3b7' : '#153717';
const connectedTextColor = theme.palette.getContrastText(connectedColor);

return (
<Grid container spacing={0.5}>
<Grid item xs={0.3} />
<Grid item xs={5.5}>
<Paper elevation={1} sx={connected ? { backgroundColor: connectedColor } : {}}>
<Typography variant='caption' sx={{ color: connectedTextColor }}>
{t('You') + ': '}
{connected ? t('connected') : t('disconnected')}
</Typography>
</Paper>
</Grid>
<Grid item xs={0.4} />
<Grid item xs={5.5}>
<Paper elevation={1} sx={peerConnected ? { backgroundColor: connectedColor } : {}}>
<Typography variant='caption' sx={{ color: connectedTextColor }}>
{t('Peer') + ': '}
{peerConnected ? t('connected') : t('disconnected')}
</Typography>
</Paper>
</Grid>
<Grid item xs={0.3} />
</Grid>
);
};

export default ChatHeader;
Loading

0 comments on commit 3446fc3

Please sign in to comment.