<commit_before>part of cart;

class CartPost extends Object with PinSerializable {

  String uuid;
  String driveId; // id of google drive

  String title;
  String link;
  int created;
  int updated;
  String author;

  String category; // uuid
  List<String> tags; // [uuid, uuid, ...]
  HashMap<String, CartPostAttachment> attachments;

  CartPost.fromJson(HashMap<String, dynamic> json) {
    if (json.containsKey('uuid')) {
      uuid = json['uuid'];
    }
    if (json.containsKey('driveId')) {
      driveId = json['driveId'];
    }
    if (json.containsKey('title')) {
      title = json['title'];
    }
    if (json.containsKey('link')) {
      link = json['link'];
    }
    if (json.containsKey('created')) {
      created = json['created'];
    }
    if (json.containsKey('updated')) {
      updated = json['updated'];
    }
    if (json.containsKey('author')) {
      author = json['author'];
    }
    if (json.containsKey('category')) {
      category = json['category'];
    }
    if (json.containsKey('tags')) {
      tags = json['tags'];
    }
    if (json.containsKey('attachments')) {
      attachments = json['attachments'];
    }
  }

  void addTagUuid(String uuid) {
    tags.add(uuid);
  }

  void removeTagUuid(String uuid) {
    tags.remove(uuid);
  }

  void addNewAttachment(String uuid, String name, {int timestamp: null}) {
    if (timestamp == null) {
      timestamp = PinTime.getTime();
    }
    var attachment = new CartPostAttachment.fromJson({
        "uuid": uuid,
        "title": name,
        "created": timestamp,
        "updated": timestamp
    });
    addAttachment(attachment);
  }

  void addAttachment(CartPostAttachment attachment) {
    attachments.addAll({ attachment.uuid: attachment });
  }

  void updateAttachment(CartPostAttachment attachment) {
    if (attachments.containsKey(attachment.uuid)) {
      attachments[attachment.uuid] = attachment;
    }
  }

  void removeAttachment(CartPostAttachment attachment) {
    attachments.remove(attachment.uuid);
  }

  CartPostAttachment findAttachment(String uuid) {
    if (attachments.containsKey(uuid)) {
      return attachments[uuid];
    } else {
      return null;
    }
  }

  void checkPostTagsChange(CartTagList tagList, CartPostHeader header) {
    var tagsToBeRemoved = new List<String>();
    var tagsToBeAdded = new List<String>();

    tags.forEach((String tagUuid) {
      var tagFound = false;
      header.tags.forEach((HashMap<String, String> tagData) {
        if (tagData['uuid'] == tagUuid) {
          tagFound = true;
        }
      });
      if (!tagFound) {
        // old tag no longer exists in the post
        tagsToBeRemoved.add(tagUuid);
      }
    });
    header.tags.forEach((HashMap<String, String> tagData) {
      var tagFound = false;
      tags.forEach((String tagUuid) {
        if (tagData['uuid'] == tagUuid) {
          tagFound = true;
        }
      });
      if (!tagFound) {
        // new tag does not exist in the post
        tagsToBeAdded.add(tagData['uuid']);
      }
    });
    tagsToBeRemoved.forEach((String tagUuid) {
      removeTagUuid(tagUuid);
      tagList.removePostFromTag(tagUuid, uuid);
    });
    tagsToBeAdded.forEach((String tagUuid) {
      addTagUuid(tagUuid);
      tagList.addPostIntoTag(tagUuid, uuid);
      List tagDatas = header.tags.where((HashMap<String, String> tagData) {
        if (tagData['uuid'] == tagUuid) {
          return true;
        } else {
          return false;
        }
      });
      String tagName = tagDatas.removeAt(0)['name'];
      tagList.addNewTag(tagUuid, tagName);
    });
  }

  HashMap<String, Future> checkPostAttachmentsChange(CartPostHeader header) {
    var attsToBeRemoved = new List<String>();
    var attsToBeAdded = new List<String>();
    var processFutures = new HashMap<String, Future>();

    attachments.forEach((String attUuid, CartPostAttachment attachment) {
      var attFound = false;
      header.attachments.forEach((HashMap<String, String> attData) {
        if (attData['uuid'] == attUuid) {
          attFound = true;
        }
      });
      if (!attFound) {
        // old attachment no longer exists in the post
        attsToBeRemoved.add(attUuid);
      }
    });
    header.attachments.forEach((HashMap<String, String> attData) {
      var attFound = false;
      attachments.forEach((String attUuid, CartPostAttachment attachment) {
        if (attData['uuid'] == attUuid) {
          attFound = true;
        }
      });
      if (!attFound) {
        // new attachment does not exist in the post
        attsToBeAdded.add(attData['uuid']);
      }
    });
    attsToBeRemoved.forEach((String attUuid) {
      CartPostAttachment attachment = findAttachment(attUuid);
      removeAttachment(attachment);
      CartSystem.instance.drive.drive_delete(attachment.driveId); // delete it without listening to the results
    });
    attsToBeAdded.forEach((String attUuid) {
      String postBaseDir = LibPath.join(CartConst.WWW_POST_PATH, uuid);
      List attDatas = header.attachments.where((HashMap<String, String> attData) {
        if (attData['uuid'] == attUuid) {
          return true;
        } else {
          return false;
        }
      });
      String attName = attDatas.removeAt(0)['name'];<commit_msg>Use trash instead of delete directly.<commit_after>part of cart;

class CartPost extends Object with PinSerializable {

  String uuid;
  String driveId; // id of google drive

  String title;
  String link;
  int created;
  int updated;
  String author;

  String category; // uuid
  List<String> tags; // [uuid, uuid, ...]
  HashMap<String, CartPostAttachment> attachments;

  CartPost.fromJson(HashMap<String, dynamic> json) {
    if (json.containsKey('uuid')) {
      uuid = json['uuid'];
    }
    if (json.containsKey('driveId')) {
      driveId = json['driveId'];
    }
    if (json.containsKey('title')) {
      title = json['title'];
    }
    if (json.containsKey('link')) {
      link = json['link'];
    }
    if (json.containsKey('created')) {
      created = json['created'];
    }
    if (json.containsKey('updated')) {
      updated = json['updated'];
    }
    if (json.containsKey('author')) {
      author = json['author'];
    }
    if (json.containsKey('category')) {
      category = json['category'];
    }
    if (json.containsKey('tags')) {
      tags = json['tags'];
    }
    if (json.containsKey('attachments')) {
      attachments = json['attachments'];
    }
  }

  void addTagUuid(String uuid) {
    tags.add(uuid);
  }

  void removeTagUuid(String uuid) {
    tags.remove(uuid);
  }

  void addNewAttachment(String uuid, String name, {int timestamp: null}) {
    if (timestamp == null) {
      timestamp = PinTime.getTime();
    }
    var attachment = new CartPostAttachment.fromJson({
        "uuid": uuid,
        "title": name,
        "created": timestamp,
        "updated": timestamp
    });
    addAttachment(attachment);
  }

  void addAttachment(CartPostAttachment attachment) {
    attachments.addAll({ attachment.uuid: attachment });
  }

  void updateAttachment(CartPostAttachment attachment) {
    if (attachments.containsKey(attachment.uuid)) {
      attachments[attachment.uuid] = attachment;
    }
  }

  void removeAttachment(CartPostAttachment attachment) {
    attachments.remove(attachment.uuid);
  }

  CartPostAttachment findAttachment(String uuid) {
    if (attachments.containsKey(uuid)) {
      return attachments[uuid];
    } else {
      return null;
    }
  }

  void checkPostTagsChange(CartTagList tagList, CartPostHeader header) {
    var tagsToBeRemoved = new List<String>();
    var tagsToBeAdded = new List<String>();

    tags.forEach((String tagUuid) {
      var tagFound = false;
      header.tags.forEach((HashMap<String, String> tagData) {
        if (tagData['uuid'] == tagUuid) {
          tagFound = true;
        }
      });
      if (!tagFound) {
        // old tag no longer exists in the post
        tagsToBeRemoved.add(tagUuid);
      }
    });
    header.tags.forEach((HashMap<String, String> tagData) {
      var tagFound = false;
      tags.forEach((String tagUuid) {
        if (tagData['uuid'] == tagUuid) {
          tagFound = true;
        }
      });
      if (!tagFound) {
        // new tag does not exist in the post
        tagsToBeAdded.add(tagData['uuid']);
      }
    });
    tagsToBeRemoved.forEach((String tagUuid) {
      removeTagUuid(tagUuid);
      tagList.removePostFromTag(tagUuid, uuid);
    });
    tagsToBeAdded.forEach((String tagUuid) {
      addTagUuid(tagUuid);
      tagList.addPostIntoTag(tagUuid, uuid);
      List tagDatas = header.tags.where((HashMap<String, String> tagData) {
        if (tagData['uuid'] == tagUuid) {
          return true;
        } else {
          return false;
        }
      });
      String tagName = tagDatas.removeAt(0)['name'];
      tagList.addNewTag(tagUuid, tagName);
    });
  }

  HashMap<String, Future> checkPostAttachmentsChange(CartPostHeader header) {
    var attsToBeRemoved = new List<String>();
    var attsToBeAdded = new List<String>();
    var processFutures = new HashMap<String, Future>();

    attachments.forEach((String attUuid, CartPostAttachment attachment) {
      var attFound = false;
      header.attachments.forEach((HashMap<String, String> attData) {
        if (attData['uuid'] == attUuid) {
          attFound = true;
        }
      });
      if (!attFound) {
        // old attachment no longer exists in the post
        attsToBeRemoved.add(attUuid);
      }
    });
    header.attachments.forEach((HashMap<String, String> attData) {
      var attFound = false;
      attachments.forEach((String attUuid, CartPostAttachment attachment) {
        if (attData['uuid'] == attUuid) {
          attFound = true;
        }
      });
      if (!attFound) {
        // new attachment does not exist in the post
        attsToBeAdded.add(attData['uuid']);
      }
    });
    attsToBeRemoved.forEach((String attUuid) {
      CartPostAttachment attachment = findAttachment(attUuid);
      removeAttachment(attachment);
      CartSystem.instance.drive.drive_trash(attachment.driveId); // delete it without listening to the results
    });
    attsToBeAdded.forEach((String attUuid) {
      String postBaseDir = LibPath.join(CartConst.WWW_POST_PATH, uuid);
      List attDatas = header.attachments.where((HashMap<String, String> attData) {
        if (attData['uuid'] == attUuid) {
          return true;
        } else {
          return false;
        }
      });
      String attName = attDatas.removeAt(0)['name'];
{'fraction_non_alphanumeric': 0.12979994311178533, 'fraction_numerical': 0.00037925476438797765, 'mean_word_length': 2.6739811912225706, 'pattern_counts': {'":': 8, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '492148', 'n_tokens_mistral': 3171, 'n_tokens_neox': 3202, 'n_words': 674}