Skip to content

Commit

Permalink
chg: [message image] show qrcodes extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrtia committed Oct 4, 2024
1 parent d91e14f commit c8b1c67
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
22 changes: 17 additions & 5 deletions bin/lib/chats_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from lib.objects import ChatSubChannels
from lib.objects import ChatThreads
from lib.objects import Messages
from lib.objects.QrCodes import Qrcode
from lib.objects import UsersAccount
from lib.objects import Usernames
from lib import Language
Expand Down Expand Up @@ -418,7 +419,7 @@ def get_nb_messages_iterator(filters={}):
nb_messages += chat.get_nb_messages()
return nb_messages

def get_chat_object_messages_forward_meta(c_messages):
def get_chat_object_messages_meta(c_messages):
temp_chats = {}
for date in c_messages:
for meta in c_messages[date]:
Expand All @@ -428,6 +429,12 @@ def get_chat_object_messages_forward_meta(c_messages):
temp_chats[meta['forwarded_from']] = chat.get_meta({'icon'})
else:
meta['forwarded_from'] = temp_chats[meta['forwarded_from']]
if meta['qrcodes']:
qrcodes = []
for q in meta['qrcodes']:
qr = Qrcode(q)
qrcodes.append({'id': qr.id, 'content': qr.get_content(), 'tags': qr.get_tags()})
meta['qrcodes'] = qrcodes
return c_messages

def get_user_account_chats_meta(user_id, chats, subchannels):
Expand Down Expand Up @@ -754,7 +761,7 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa
translation_target = None
if messages:
meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page)
meta['messages'] = get_chat_object_messages_forward_meta(meta['messages'])
meta['messages'] = get_chat_object_messages_meta(meta['messages'])
return meta, 200

def api_get_nb_message_by_week(chat_type, chat_instance_uuid, chat_id):
Expand Down Expand Up @@ -800,7 +807,7 @@ def api_get_subchannel(chat_id, chat_instance_uuid, translation_target=None, nb=
if meta.get('username'):
meta['username'] = get_username_meta_from_global_id(meta['username'])
meta['messages'], meta['pagination'], meta['tags_messages'] = subchannel.get_messages(translation_target=translation_target, nb=nb, page=page)
meta['messages'] = get_chat_object_messages_forward_meta(meta['messages'])
meta['messages'] = get_chat_object_messages_meta(meta['messages'])
return meta, 200

def api_get_thread(thread_id, thread_instance_uuid, translation_target=None, nb=-1, page=-1):
Expand All @@ -812,17 +819,22 @@ def api_get_thread(thread_id, thread_instance_uuid, translation_target=None, nb=
# if meta['chat']:
# meta['chat'] = get_chat_meta_from_global_id(meta['chat'])
meta['messages'], meta['pagination'], meta['tags_messages'] = thread.get_messages(translation_target=translation_target, nb=nb, page=page)
meta['messages'] = get_chat_object_messages_forward_meta(meta['messages'])
meta['messages'] = get_chat_object_messages_meta(meta['messages'])
return meta, 200

def api_get_message(message_id, translation_target=None):
message = Messages.Message(message_id)
if not message.exists():
return {"status": "error", "reason": "Unknown uuid"}, 404
meta = message.get_meta({'chat', 'content', 'files-names', 'forwarded_from', 'icon', 'images', 'language', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'translation', 'user-account'}, translation_target=translation_target)
meta = message.get_meta({'chat', 'content', 'files-names', 'forwarded_from', 'icon', 'images', 'language', 'link', 'parent', 'parent_meta', 'qrcodes', 'reactions', 'thread', 'translation', 'user-account'}, translation_target=translation_target)
if 'forwarded_from' in meta:
chat = get_obj_chat_from_global_id(meta['forwarded_from'])
meta['forwarded_from'] = chat.get_meta({'icon'})
qrcodes = []
for q in meta['qrcodes']:
qr = Qrcode(q)
qrcodes.append({'id': qr.id, 'content': qr.get_content(), 'tags': qr.get_tags()})
meta['qrcodes'] = qrcodes
return meta, 200

def api_message_detect_language(message_id):
Expand Down
8 changes: 8 additions & 0 deletions bin/lib/objects/Messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ def get_images(self):
images.append({'id': obj_id, 'ocr': self._get_image_ocr(obj_id)})
return images

def get_qrcodes(self):
qrcodes = []
for c in self.get_correlation('qrcode').get('qrcode', []):
qrcodes.append(c[1:])
return qrcodes

def get_user_account(self, meta=False):
user_account = self.get_correlation('user-account')
if user_account.get('user-account'):
Expand Down Expand Up @@ -300,6 +306,8 @@ def get_meta(self, options=None, timestamp=None, translation_target=''):
meta['thread'] = thread
if 'images' in options:
meta['images'] = self.get_images()
if 'qrcodes' in options:
meta['qrcodes'] = self.get_qrcodes()
if 'files-names' in options:
meta['files-names'] = self.get_files_names()
if 'reactions' in options:
Expand Down
2 changes: 1 addition & 1 deletion bin/lib/objects/abstract_chat_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def get_nb_week_messages(self):
def get_message_meta(self, message, timestamp=None, translation_target='', options=None): # TODO handle file message
message = Messages.Message(message[9:])
if not options:
options = {'content', 'files-names', 'forwarded_from', 'images', 'language', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'translation', 'user-account'}
options = {'content', 'files-names', 'forwarded_from', 'images', 'language', 'link', 'parent', 'parent_meta', 'qrcodes', 'reactions', 'thread', 'translation', 'user-account'}
meta = message.get_meta(options=options, timestamp=timestamp, translation_target=translation_target)
return meta

Expand Down
6 changes: 2 additions & 4 deletions var/www/blueprints/objects_qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def objects_qrcodes_range_json():
return jsonify(QrCodes.Qrcodes().api_get_chart_nb_by_daterange(date_from, date_to))


@objects_qrcode.route("/objects/qrcodes", methods=['GET'])
@objects_qrcode.route("/objects/qrcode", methods=['GET'])
@login_required
@login_read_only
def object_qrcode():
Expand All @@ -90,11 +90,9 @@ def object_qrcode():
return create_json_response(meta[0], meta[1])
else:
meta = meta[0]
languages = Language.get_translation_languages()
return render_template("ShowQrcode.html", meta=meta,
bootstrap_label=bootstrap_label,
ail_tags=Tag.get_modal_add_tags(meta['id'], meta['type'], meta['subtype']),
translation_languages=languages, translation_target=target)
ail_tags=Tag.get_modal_add_tags(meta['id'], meta['type'], meta['subtype']))


# ============= ROUTES ==============
Expand Down
15 changes: 15 additions & 0 deletions var/www/templates/chats_explorer/block_message.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ <h4 class="text-secondary mb-0">
<a class="btn btn-info" target="_blank" href="{{ url_for('objects_ocr.object_ocr', id=message_image['id'])}}"><i class="fas fa-expand"></i> OCR</a>
</span>
{% endif %}
{% if message['qrcodes'] %}
{% for qrcode in message['qrcodes'] %}
<span>
<a class="btn btn-info" target="_blank" href="{{ url_for('correlation.show_correlation', type='qrcode', id=qrcode['id'])}}" style="max-width: 49%">
<i class="fas fa-qrcode fa-lg"></i> {{ qrcode['content'] }}
<div>
{% for tag in qrcode['tags'] %}
<span class="badge badge-{{ bootstrap_label[loop.index0 % 5] }}">{{ tag }}</span>
{% endfor %}
</div>
</a>
</span>

{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% if message['files-names'] %}
Expand Down
3 changes: 2 additions & 1 deletion var/www/templates/objects/qrcode/card_qrcode.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

<div class="card my-1">
<div class="card-header">
<h4 class="text-secondary">{{ meta["id"] }} :</h4>
<small>{{ meta["id"] }} :</small>
<div class="py-1 my-1 bg-white">{{ meta["content"] }}</div>
<ul class="list-group mb-2">
<li class="list-group-item py-0">
<table class="table">
Expand Down

0 comments on commit c8b1c67

Please sign in to comment.