Move buffer message sorting to the backlog event class.

This commit is contained in:
poljar (Damir Jelić) 2018-02-25 17:00:16 +01:00
parent 0aa773ef2f
commit a59d72cd15
2 changed files with 70 additions and 70 deletions

View file

@ -19,10 +19,11 @@ from builtins import str
import time
from functools import partial
from operator import itemgetter
from matrix.globals import W, OPTIONS
from matrix.globals import W
from matrix.utils import (color_for_tags, tags_for_message, sanitize_id,
sanitize_token, sanitize_text)
sanitize_token, sanitize_text, tags_from_line_data)
from matrix.rooms import (matrix_create_room_buffer, RoomInfo, RoomMessageEvent,
RoomRedactedMessageEvent)
@ -242,6 +243,70 @@ class MatrixBacklogEvent(MatrixEvent):
return RoomMessageEvent.from_dict(event_dict)
@staticmethod
def buffer_sort_messages(buff):
lines = []
own_lines = W.hdata_pointer(W.hdata_get('buffer'), buff, 'own_lines')
if own_lines:
hdata_line = W.hdata_get('line')
hdata_line_data = W.hdata_get('line_data')
line = W.hdata_pointer(
W.hdata_get('lines'), own_lines, 'first_line')
while line:
data = W.hdata_pointer(hdata_line, line, 'data')
line_data = {}
if data:
date = W.hdata_time(hdata_line_data, data, 'date')
print_date = W.hdata_time(hdata_line_data, data,
'date_printed')
tags = tags_from_line_data(data)
prefix = W.hdata_string(hdata_line_data, data, 'prefix')
message = W.hdata_string(hdata_line_data, data, 'message')
line_data = {
'date': date,
'date_printed': print_date,
'tags_array': ','.join(tags),
'prefix': prefix,
'message': message
}
lines.append(line_data)
line = W.hdata_move(hdata_line, line, 1)
sorted_lines = sorted(lines, key=itemgetter('date'))
lines = []
# We need to convert the dates to a string for hdata_update(), this
# will reverse the list at the same time
while sorted_lines:
line = sorted_lines.pop()
new_line = {k: str(v) for k, v in line.items()}
lines.append(new_line)
MatrixBacklogEvent.update_buffer_lines(lines, own_lines)
@staticmethod
def update_buffer_lines(new_lines, own_lines):
hdata_line = W.hdata_get('line')
hdata_line_data = W.hdata_get('line_data')
line = W.hdata_pointer(W.hdata_get('lines'), own_lines, 'first_line')
while line:
data = W.hdata_pointer(hdata_line, line, 'data')
if data:
W.hdata_update(hdata_line_data, data, new_lines.pop())
line = W.hdata_move(hdata_line, line, 1)
@classmethod
def from_dict(cls, server, room_id, parsed_dict):
try:
@ -266,13 +331,14 @@ class MatrixBacklogEvent(MatrixEvent):
def execute(self):
room = self.server.rooms[self.room_id]
buf = self.server.buffers[self.room_id]
buff = self.server.buffers[self.room_id]
tags = tags_for_message("backlog")
for event in self.events:
event.execute(self.server, room, buf, list(tags))
event.execute(self.server, room, buff, list(tags))
room.prev_batch = self.end_token
MatrixBacklogEvent.buffer_sort_messages(buff)
room.backlog_pending = False
W.bar_item_update("buffer_modes")

View file

@ -21,8 +21,6 @@ import time
import pprint
import datetime
from operator import itemgetter
from matrix.globals import W
from matrix.api import MessageType
@ -33,69 +31,6 @@ from matrix.utils import (server_buffer_prnt, tags_from_line_data, prnt_debug,
from matrix.plugin_options import DebugType
def matrix_sort_old_messages(server, room_id):
lines = []
buf = server.buffers[room_id]
own_lines = W.hdata_pointer(W.hdata_get('buffer'), buf, 'own_lines')
if own_lines:
hdata_line = W.hdata_get('line')
hdata_line_data = W.hdata_get('line_data')
line = W.hdata_pointer(W.hdata_get('lines'), own_lines, 'first_line')
while line:
data = W.hdata_pointer(hdata_line, line, 'data')
line_data = {}
if data:
date = W.hdata_time(hdata_line_data, data, 'date')
print_date = W.hdata_time(hdata_line_data, data, 'date_printed')
tags = tags_from_line_data(data)
prefix = W.hdata_string(hdata_line_data, data, 'prefix')
message = W.hdata_string(hdata_line_data, data, 'message')
line_data = {
'date': date,
'date_printed': print_date,
'tags_array': ','.join(tags),
'prefix': prefix,
'message': message
}
lines.append(line_data)
line = W.hdata_move(hdata_line, line, 1)
sorted_lines = sorted(lines, key=itemgetter('date'))
lines = []
# We need to convert the dates to a string for hdata_update(), this
# will reverse the list at the same time
while sorted_lines:
line = sorted_lines.pop()
new_line = {k: str(v) for k, v in line.items()}
lines.append(new_line)
matrix_update_buffer_lines(lines, own_lines)
def matrix_update_buffer_lines(new_lines, own_lines):
hdata_line = W.hdata_get('line')
hdata_line_data = W.hdata_get('line_data')
line = W.hdata_pointer(W.hdata_get('lines'), own_lines, 'first_line')
while line:
data = W.hdata_pointer(hdata_line, line, 'data')
if data:
W.hdata_update(hdata_line_data, data, new_lines.pop())
line = W.hdata_move(hdata_line, line, 1)
def matrix_handle_message(
server, # type: MatrixServer
message, # type: MatrixMessage
@ -135,7 +70,6 @@ def matrix_handle_message(
elif message_type == MessageType.ROOM_MSG:
event = message.event
event.execute()
matrix_sort_old_messages(server, message.room_id)
elif message_type is MessageType.SYNC:
event = message.event