Skip to content

Commit

Permalink
chg: [chats] add discord threads, Forum channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrtia committed Dec 4, 2023
1 parent 93ef541 commit 941838a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 13 deletions.
11 changes: 8 additions & 3 deletions bin/importer/feeders/abstract_chats_feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ def process_chat(self, new_objs, obj, date, timestamp, reply_id=None):

return chat

# def process_subchannels(self):
# pass

def process_subchannel(self, obj, date, timestamp, reply_id=None): # TODO CREATE DATE
meta = self.json_data['meta']['chat']['subchannel']
Expand Down Expand Up @@ -216,12 +214,17 @@ def process_thread(self, obj, obj_chat, date, timestamp, reply_id=None):
p_subchannel_id = meta['parent'].get('subchannel')
p_message_id = meta['parent'].get('message')

# print(thread_id, p_chat_id, p_subchannel_id, p_message_id)

if p_chat_id == self.get_chat_id() and p_subchannel_id == self.get_subchannel_id():
thread = ChatThreads.create(thread_id, self.get_chat_instance_uuid(), p_chat_id, p_subchannel_id, p_message_id, obj_chat)
thread.add(date, obj)
thread.add_message(obj.get_global_id(), self.get_message_id(), timestamp, reply_id=reply_id)
# TODO OTHERS CORRELATIONS TO ADD

if meta.get('name'):
thread.set_name(meta['name'])

return thread

# TODO
Expand Down Expand Up @@ -308,8 +311,10 @@ def process_meta(self): # TODO CHECK MANDATORY FIELDS

else:
chat_id = self.get_chat_id()
thread_id = self.get_thread_id()
channel_id = self.get_subchannel_id()
message_id = self.get_message_id()
message_id = Messages.create_obj_id(self.get_chat_instance_uuid(), chat_id, message_id, timestamp)
message_id = Messages.create_obj_id(self.get_chat_instance_uuid(), chat_id, message_id, timestamp, channel_id=channel_id, thread_id=thread_id)
message = Messages.Message(message_id)
# create empty message if message don't exists
if not message.exists():
Expand Down
12 changes: 10 additions & 2 deletions bin/lib/chats_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ def get_chat_meta_from_global_id(chat_global_id):
chat = Chats.Chat(chat_id, instance_uuid)
return chat.get_meta()

def get_threads_metas(threads):
metas = []
for thread in threads:
metas.append(ChatThreads.ChatThread(thread['id'], thread['subtype']).get_meta(options={'name', 'nb_messages'}))
return metas

def get_username_meta_from_global_id(username_global_id):
_, instance_uuid, username_id = username_global_id.split(':', 2)
username = Usernames.Username(username_id, instance_uuid)
Expand All @@ -305,7 +311,7 @@ def api_get_chat(chat_id, chat_instance_uuid):
chat = Chats.Chat(chat_id, chat_instance_uuid)
if not chat.exists():
return {"status": "error", "reason": "Unknown chat"}, 404
meta = chat.get_meta({'created_at', 'icon', 'info', 'subchannels', 'username'})
meta = chat.get_meta({'created_at', 'icon', 'info', 'subchannels', 'threads', 'username'})
if meta['username']:
meta['username'] = get_username_meta_from_global_id(meta['username'])
if meta['subchannels']:
Expand All @@ -326,9 +332,11 @@ def api_get_subchannel(chat_id, chat_instance_uuid):
subchannel = ChatSubChannels.ChatSubChannel(chat_id, chat_instance_uuid)
if not subchannel.exists():
return {"status": "error", "reason": "Unknown subchannel"}, 404
meta = subchannel.get_meta({'chat', 'created_at', 'icon', 'nb_messages'})
meta = subchannel.get_meta({'chat', 'created_at', 'icon', 'nb_messages', 'threads'})
if meta['chat']:
meta['chat'] = get_chat_meta_from_global_id(meta['chat'])
if meta.get('threads'):
meta['threads'] = get_threads_metas(meta['threads'])
if meta.get('username'):
meta['username'] = get_username_meta_from_global_id(meta['username'])
meta['messages'], meta['tags_messages'] = subchannel.get_messages()
Expand Down
7 changes: 5 additions & 2 deletions bin/lib/objects/ChatSubChannels.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ def get_meta(self, options=set()):
meta['chat'] = self.get_chat()
if 'img' in options:
meta['img'] = self.get_img()
if 'nb_messages':
if 'nb_messages' in options:
meta['nb_messages'] = self.get_nb_messages()
if 'created_at':
if 'created_at' in options:
meta['created_at'] = self.get_created_at(date=True)
if 'threads' in options:
meta['threads'] = self.get_threads()
print(meta['threads'])
return meta

def get_misp_object(self):
Expand Down
2 changes: 2 additions & 0 deletions bin/lib/objects/ChatThreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def get_meta(self, options=set()):
meta['id'] = self.id
meta['subtype'] = self.subtype
meta['tags'] = self.get_tags(r_list=True)
if 'name':
meta['name'] = self.get_name()
if 'nb_messages':
meta['nb_messages'] = self.get_nb_messages()
# created_at ???
Expand Down
3 changes: 3 additions & 0 deletions bin/lib/objects/Chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def get_meta(self, options=set()):
meta['nb_subchannels'] = self.get_nb_subchannels()
if 'created_at':
meta['created_at'] = self.get_created_at(date=True)
if 'threads' in options:
meta['threads'] = self.get_threads()
print(meta['threads'])
return meta

def get_misp_object(self):
Expand Down
7 changes: 4 additions & 3 deletions bin/lib/objects/abstract_chat_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def get_nb_subchannels(self):

def get_threads(self):
threads = []
for obj_global_id in self.get_childrens():
obj_type, _ = obj_global_id.split(':', 1)
for child in self.get_childrens():
obj_type, obj_subtype, obj_id = child.split(':', 2)
if obj_type == 'chat-thread':
threads.append(obj_global_id)
threads.append({'type': obj_type, 'subtype': obj_subtype, 'id': obj_id})
return threads

def get_created_at(self, date=False):
Expand Down Expand Up @@ -242,6 +242,7 @@ def add_message(self, obj_global_id, message_id, timestamp, reply_id=None):
for mess_id in self.get_cached_message_reply(message_id):
self.add_obj_children(obj_global_id, mess_id)

# def get_deleted_messages(self, message_id):


# get_messages_meta ????
Expand Down
45 changes: 44 additions & 1 deletion var/www/templates/chats_explorer/SubChannelMessages.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,43 @@ <h3 class="text-secondary">{% if subchannel['chat']['name'] %}{{ subchannel['cha
</div>
</div>


{% if subchannel['threads'] %}
<table id="tablethreads" class="table">
<thead class="bg-dark text-white">
<tr>
<th>Name</th>
<th>Created at</th>
<th>First seen</th>
<th>Last seen</th>
<th>Nb Messages</th>
</tr>
</thead>
<tbody>

{% for thread in subchannel['threads'] %}
<tr>
<td>
<a href="{{ url_for('chats_explorer.objects_thread_messages')}}?uuid={{ thread['subtype'] }}&id={{ thread['id'] }}">{{ thread['name'] }} <i class="far fa-comment"></i> {{ thread['nb_messages'] }} Messages</a>
</td>
<td>{{ thread["created_at"] }}</td>
<td>
{% if thread['first_seen'] %}
{{ thread['first_seen'][0:4] }}-{{ thread['first_seen'][4:6] }}-{{ thread['first_seen'][6:8] }}
{% endif %}
</td>
<td>
{% if thread['last_seen'] %}
{{ thread['last_seen'][0:4] }}-{{ thread['last_seen'][4:6] }}-{{ thread['last_seen'][6:8] }}
{% endif %}
</td>
<td>{{ thread['nb_messages'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}

{% for tag in subchannel['tags_messages'] %}
<span class="badge badge-{{ bootstrap_label[loop.index0 % 5] }}">{{ tag }} <span class="badge badge-light">{{ subchannel['tags_messages'][tag] }}</span></span>
{% endfor %}
Expand Down Expand Up @@ -182,7 +219,13 @@ <h3 class="text-secondary">{% if subchannel['chat']['name'] %}{{ subchannel['cha
$(document).ready(function(){
$("#page-Decoded").addClass("active");
$("#nav_chat").addClass("active");

{% if subchannel['threads'] %}
$('#tablethreads').DataTable({
"aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]],
"iDisplayLength": 10,
"order": [[ 3, "desc" ]]
});
{% endif %}
});

function toggle_sidebar(){
Expand Down
4 changes: 2 additions & 2 deletions var/www/templates/chats_explorer/ThreadMessages.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Parent</th>
<th>First seen</th>
<th>Last seen</th>
Expand All @@ -72,7 +72,7 @@
<tbody>
<tr>
<td>
{{ meta['id'] }}
{{ meta['name'] }}
</td>
<td>
{% if meta['first_seen'] %}
Expand Down

0 comments on commit 941838a

Please sign in to comment.