diff --git a/api/models/order.py b/api/models/order.py index 06fd71aa..95f54a70 100644 --- a/api/models/order.py +++ b/api/models/order.py @@ -157,8 +157,6 @@ class Order(models.Model): default=None, blank=True, ) # unique = True, a taker can only take one order - maker_last_seen = models.DateTimeField(null=True, default=None, blank=True) - taker_last_seen = models.DateTimeField(null=True, default=None, blank=True) # When collaborative cancel is needed and one partner has cancelled. maker_asked_cancel = models.BooleanField(default=False, null=False) diff --git a/api/views.py b/api/views.py index b135cdf7..8efd854d 100644 --- a/api/views.py +++ b/api/views.py @@ -256,20 +256,12 @@ class OrderView(viewsets.ViewSet): ) # WRITE Update last_seen for maker and taker. - # Note down that the taker/maker was here recently, so counterpart knows if the user is paying attention. data["maker_nick"] = str(order.maker) - if order.maker == request.user: - order.maker_last_seen = timezone.now() - order.save() - if order.taker == request.user: - order.taker_last_seen = timezone.now() - order.save() # Add activity status of participants based on last_seen - if order.taker_last_seen is not None: - data["taker_status"] = Logics.user_activity_status(order.taker_last_seen) - if order.maker_last_seen is not None: - data["maker_status"] = Logics.user_activity_status(order.maker_last_seen) + data["maker_status"] = Logics.user_activity_status(order.maker.last_login) + if order.taker is not None: + data["taker_status"] = Logics.user_activity_status(order.taker.last_login) # 3.b) Non participants can view details (but only if PUB) if not data["is_participant"] and order.status == Order.Status.PUB: @@ -904,7 +896,7 @@ class BookView(ListAPIView): data["satoshis_now"] = Logics.satoshis_now(order) # Compute current premium for those orders that are explicitly priced. data["price"], data["premium"] = Logics.price_and_premium_now(order) - data["maker_status"] = Logics.user_activity_status(order.maker_last_seen) + data["maker_status"] = Logics.user_activity_status(order.maker.last_login) for key in ( "status", "taker", diff --git a/chat/views.py b/chat/views.py index 1debfc7f..10ba9735 100644 --- a/chat/views.py +++ b/chat/views.py @@ -72,7 +72,7 @@ class ChatView(viewsets.ViewSet): # Poor idea: is_peer_connected() mockup. Update connection status based on last time a GET request was sent if chatroom.maker == request.user: - chatroom.taker_connected = order.taker_last_seen > ( + chatroom.taker_connected = order.taker.last_login > ( timezone.now() - timedelta(minutes=1) ) chatroom.maker_connected = True @@ -80,7 +80,7 @@ class ChatView(viewsets.ViewSet): peer_connected = chatroom.taker_connected peer_public_key = order.taker.robot.public_key elif chatroom.taker == request.user: - chatroom.maker_connected = order.maker_last_seen > ( + chatroom.maker_connected = order.maker.last_login > ( timezone.now() - timedelta(minutes=1) ) chatroom.taker_connected = True diff --git a/frontend/src/components/OrderDetails/index.tsx b/frontend/src/components/OrderDetails/index.tsx index a8601263..6ed1d863 100644 --- a/frontend/src/components/OrderDetails/index.tsx +++ b/frontend/src/components/OrderDetails/index.tsx @@ -308,7 +308,7 @@ const OrderDetails = ({ @@ -318,7 +318,7 @@ const OrderDetails = ({ diff --git a/frontend/src/components/TradeBox/EncryptedChat/EncryptedSocketChat/index.tsx b/frontend/src/components/TradeBox/EncryptedChat/EncryptedSocketChat/index.tsx index 35e6008d..7a9782c3 100644 --- a/frontend/src/components/TradeBox/EncryptedChat/EncryptedSocketChat/index.tsx +++ b/frontend/src/components/TradeBox/EncryptedChat/EncryptedSocketChat/index.tsx @@ -76,6 +76,7 @@ const EncryptedSocketChat: React.FC = ({ // On component unmount close reconnecting-websockets return () => { connection?.close(); + setConnection(undefined); }; }, []); diff --git a/frontend/src/components/TradeBox/EncryptedChat/EncryptedTurtleChat/index.tsx b/frontend/src/components/TradeBox/EncryptedChat/EncryptedTurtleChat/index.tsx index 28a79146..aeadce81 100644 --- a/frontend/src/components/TradeBox/EncryptedChat/EncryptedTurtleChat/index.tsx +++ b/frontend/src/components/TradeBox/EncryptedChat/EncryptedTurtleChat/index.tsx @@ -207,7 +207,7 @@ const EncryptedTurtleChat: React.FC = ({ order_id: orderId, offset: lastIndex, }, - robot.tokenSHA256, + { tokenSHA256: robot.tokenSHA256 }, ) .then((response) => { if (response != null) { diff --git a/robosats/middleware.py b/robosats/middleware.py index 249e3021..ec5caae9 100644 --- a/robosats/middleware.py +++ b/robosats/middleware.py @@ -4,7 +4,7 @@ from pathlib import Path from channels.db import database_sync_to_async from channels.middleware import BaseMiddleware from django.conf import settings -from django.contrib.auth.models import AnonymousUser, User +from django.contrib.auth.models import AnonymousUser, User, update_last_login from django.db import IntegrityError from rest_framework.authtoken.models import Token from rest_framework.exceptions import AuthenticationFailed @@ -64,7 +64,8 @@ class RobotTokenSHA256AuthenticationMiddleWare: # Check if it is an existing robot. try: - Token.objects.get(key=token_sha256_b91) + token = Token.objects.get(key=token_sha256_b91) + update_last_login(None, token.user) except Token.DoesNotExist: # If we get here the user does not have a robot on this coordinator