def merge_comments(self, original_id, children_ids):
"""Given an original document and a set of exact duplicates, copies
all comments and messages on the duplicates to the original.
"""
import re
threads = self.get_threads(children_ids + [original_id])
original_section_ids = re.findall(r" id='([a-zA-Z0-9]{11})'",
threads[original_id]["html"])
for thread_id in children_ids:
thread = threads[thread_id]
child_section_ids = re.findall(r" id='([a-zA-Z0-9]{11})'",
thread["html"])
parent_map = dict(zip(child_section_ids, original_section_ids))
messages = self.get_messages(thread_id)
for message in reversed(messages):
kwargs = {}
if "parts" in message:
kwargs["parts"] = json.dumps(message["parts"])
else:
kwargs["content"] = message["text"]
if "annotation" in message:
section_id = None
if "highlight_section_ids" in message["annotation"]:
section_id = message["annotation"][
"highlight_section_ids"][0]
else:
anno_loc = thread["html"].find(
'<annotation id="%s"' % message["annotation"]["id"])
loc = thread["html"].rfind("id=", 0, anno_loc)
if anno_loc >= 0 and loc >= 0:
section_id = thread["html"][loc+4:loc+15]
if section_id and section_id in parent_map:
kwargs["section_id"] = parent_map[section_id]
if "files" in message:
attachments = []
for blob_info in message["files"]:
blob = self.get_blob(thread_id, blob_info["hash"])
new_blob = self.put_blob(
original_id, blob, name=blob_info["name"])
attachments.append(new_blob["id"])
if attachments:
kwargs["attachments"] = ",".join(attachments)
self.new_message(original_id, **kwargs)
评论列表
文章目录