Skip to content

Commit

Permalink
Removed onmessage mqtt event for sending messages in old chat feature
Browse files Browse the repository at this point in the history
  • Loading branch information
joemarct committed Nov 24, 2024
1 parent 65d7fd6 commit 2a5a87d
Showing 1 changed file with 0 additions and 49 deletions.
49 changes: 0 additions & 49 deletions chat/management/commands/mqtt_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,6 @@ def on_connect(client, userdata, flags, rc):
# reconnect then subscriptions will be renewed.
client.subscribe("chat/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
try:
payload = json.loads(msg.payload)
if 'from' in payload.keys() and 'to' in payload.keys():
LOGGER.info(f"Chat messsage received from {payload['from']} to {payload['to']}!")
try:
conversation_check = Conversation.objects.filter(topic=msg.topic)
if conversation_check.exists():
conversation = conversation_check.last()
conversation.last_messaged = timezone.now()
conversation.save()
LOGGER.info('Conversation last messaged timestamp updated: ' + str(conversation.id))
else:
conversation = Conversation(
from_address=Address.objects.get(address=payload['from']),
to_address=Address.objects.get(address=payload['to']),
topic=msg.topic,
last_messaged=timezone.now()
)
conversation.save()
LOGGER.info('Conversation saved: ' + str(conversation.id))

try:
# Refresh the sender's `last_online` timestamp
sender_identity = ChatIdentity.objects.get(address__address=payload['from'])
sender_identity.last_online = timezone.now()
sender_identity.save()

# Send notification to recipient if not online for the last 5 minutes
recipient_identity = ChatIdentity.objects.get(address__address=payload['to'])
send_notif = False
if recipient_identity.last_online:
time_diff = timezone.now() - recipient_identity.last_online
if time_diff.total_seconds() < (60 * 5):
send_notif = True
else:
send_notif = True
if send_notif:
send_chat_notication.delay(payload['to'])
except ChatIdentity.DoesNotExist:
pass

except Address.DoesNotExist:
pass
except JSONDecodeError:
pass

FIRST_RECONNECT_DELAY = 1
RECONNECT_RATE = 2
MAX_RECONNECT_COUNT = 12
Expand Down Expand Up @@ -103,7 +55,6 @@ def on_disconnect(client, userdata, rc):
client = mqtt.Client(client_id=mqtt_client_id, clean_session=False)

client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect


Expand Down

0 comments on commit 2a5a87d

Please sign in to comment.