Commit 1d1d33a7 authored by Alp Deniz Ogut's avatar Alp Deniz Ogut
Browse files

Merge branch 'fix_me' into dev

parents 8071a28d 7a45f7c5
Pipeline #10890 passed with stage
in 2 minutes and 24 seconds
Showing with 36 additions and 18 deletions
+36 -18
......@@ -16,7 +16,7 @@ from pymemri.webserver.qr_endpoints import (
qr_code_dict,
)
from pymemri.webserver.qr_endpoints import router as qr_router
from wamd.utils import isGroupJid, jidNormalize
from wamd.utils import isGroupJid, jidNormalize, mediaTypeFromMime, decryptMedia
from . import helpers
from .constants import (
......@@ -27,6 +27,8 @@ from .constants import (
)
from .multidevice import MultiDevice
from .schema import Message, MessageChannel
from base64 import b64decode
MAX_NUM_CHANNELS = 25
MAX_CHANNEL_SIZE = 50
......@@ -37,8 +39,7 @@ class WhatsappPlugin(PluginBase):
service = SERVICE_NAME
me_id = None
running = True
running = True
num_auth_tries = 1
contacts = {}
push_names = {}
......@@ -120,8 +121,8 @@ class WhatsappPlugin(PluginBase):
self.set_run_status("authenticated")
self.add_progress(0.05)
credentials = json.dumps(conn.authState.toJson())
self.me_id = jidNormalize(conn.authState.me['jid'])
self.store_credentials(credentials)
jid = jidNormalize(conn.authState.me['jid'])
self.store_credentials(jid, credentials)
self.stop_qr_server()
else:
if self.num_auth_tries < 0:
......@@ -144,7 +145,7 @@ class WhatsappPlugin(PluginBase):
for p in pushnames:
self.push_names[p.id] = p.pushname
if p.id == self.me_id:
if p.id == self.me.externalId:
self.me.displayName = p.pushname
self.add_to_queue(updates=[self.me])
self.add_progress(0.05)
......@@ -159,6 +160,7 @@ class WhatsappPlugin(PluginBase):
def inbox_callback(self, message):
items = []
edges = []
files = []
if message['id'] not in self.existing_message_ids + list(self.messages):
logger.debug(f"Received a new message: {message}")
jid = message['to'] if message['fromMe'] else message['from']
......@@ -169,10 +171,8 @@ class WhatsappPlugin(PluginBase):
if self.should_be_ignored(jid):
return
content = message['conversation'] or message['text']
# TODO: for non standard messages, change this
if content == "" or content is None:
return
content = message['conversation'] or message['caption'] or message['text']
# Build message
msg_time = int(message['messageTimestamp'])
msg = Message(service=SERVICE_NAME, externalId=message['id'], content=content,
......@@ -214,7 +214,25 @@ class WhatsappPlugin(PluginBase):
edges.append(Edge(msg, self.contacts[message['participant']], 'sender'))
edges.append(Edge(msg, self.me, 'receiver'))
self.add_to_queue(items=items, updates=None, edges=edges)
# handle media files
if 'mimetype' in message and message['mimetype']:
print("Saving media file")
try:
mediaType = mediaTypeFromMime(message['mimetype'])
if mediaType == "image":
image_bytes = requests.get(message['url']).content
key = b64decode(message['mediaKey'])
image_bytes = decryptMedia(image_bytes, key, mediaType)
photo = Photo.from_bytes(image_bytes)
edges += photo.get_edges("file")
items += [photo, photo.file[0]]
edges.append(Edge(msg, photo, 'photo'))
files.append(photo.data)
except Exception as e:
print("Exception while saving media", e)
self.add_to_queue(items=items, updates=None, edges=edges, files=files)
# Gets called whenever a profile picture request is responded
# Picture replies are accounted for plugin's progress after bootstrap data
......@@ -271,7 +289,7 @@ class WhatsappPlugin(PluginBase):
logger.info(f"Received {START_REQUESTING_AFTER} events {self.completed_events}")
self.setup_daemon_mode()
# add me user's profile picture to queue
self.pictures[self.me_id] = datetime.now().timestamp()
self.pictures[self.me.externalId] = datetime.now().timestamp()
self.write()
def close_callback(self, reason, conn):
......@@ -301,7 +319,7 @@ class WhatsappPlugin(PluginBase):
items = []
updates = []
# there is no self note
if jid == self.me_id:
if jid == self.me.externalId:
return
if jid in list(self.chats) + self.existing_channel_ids:
......@@ -382,16 +400,16 @@ class WhatsappPlugin(PluginBase):
self.contacts[self.me.externalId] = self.me
return self.me
def store_credentials(self, credentials):
try: # use existing account if exists
def store_credentials(self, jid, credentials):
try: # use existing account if exists
self.me = helpers.get_me(self.client)
self.me.externalId = jid
except:
self.me = Account(service=SERVICE_NAME, displayName=None,
isMe=True, externalId=self.me_id)
self.me = Account(service=SERVICE_NAME, displayName=None, isMe=True, externalId=jid)
self.me.secret = credentials
self.client.update_item(self.me)
self.contacts[self.me_id] = self.me
self.contacts[jid] = self.me
qr_code_dict.set(AUTHENTICATED, True)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment