extractMessageThread function

ThreadEntry? extractMessageThread(
  1. Bs4Element threadListItem
)

Implementation

ThreadEntry? extractMessageThread(Bs4Element threadListItem) {
  // extract the topic and the content
  String? topic;
  String? content;
  Bs4Element? topicElement =
      threadListItem.find('*', class_: 'message-thread-message-header');
  Bs4Element? contentElement =
      threadListItem.find('*', class_: 'message-thread-message-content');

  // find files, if there are any
  // remove the attachments text, but keep it for later
  Bs4Element? fileRow = contentElement?.descendants
      .where((element) => element.className.contains("message-attachements"))
      .firstOrNull
      ?.extract();

  List<Bs4Element> fileEntries = fileRow?.findAll('a') ?? [];
  List<File> files = [];
  for (var fileLink in fileEntries) {
    files.add(
        File(href: fileLink.getAttrValue("href") ?? "", name: fileLink.text));
  }
  topic = topicElement?.text.trim();
  content = contentElement?.innerHtml.trim();

  // extract info about the message and the sender
  Bs4Element? infoElement =
      threadListItem.find('*', class_: 'message-thread-message-sender');
  Bs4Element? senderElement = infoElement?.find('span');
  String? senderId = senderElement?.getAttrValue(lectioContextKey);
  String? senderName = senderElement?.text;
  MetaDataEntry? sender;
  if (senderId != null && senderName != null) {
    sender = MetaDataEntry(id: senderId, name: senderName);
  }
  String? timestampInfo = infoElement?.text
      .replaceFirst(sender?.name ?? "", "")
      .replaceFirst(',', '')
      .trim();
  DateTime? at;
  if (timestampInfo != null) {
    at = dateThreadFormat.parse(timestampInfo);
  }

  if (content != null && topic != null && at != null && sender != null) {
    return ThreadEntry(at, sender, content, topic, files);
  }
  return null;
}