def mark_read(self, content: Content, read_datetime: datetime = None, do_flush: bool = True, recursive: bool = True) -> Content: assert self._user assert content # The algorithm is: # 1. define the read datetime # 2. update all revisions related to current Content # 3. do the same for all child revisions # (ie parent_id is content_id of current content) if not read_datetime: read_datetime = datetime.datetime.now() viewed_revisions = self._session.query(ContentRevisionRO) \ .filter(ContentRevisionRO.content_id==content.content_id).all() for revision in viewed_revisions: revision.read_by[self._user] = read_datetime if recursive: # mark read : # - all children # - parent stuff (if you mark a comment as read, # then you have seen the parent) # - parent comments for child in content.get_valid_children(): self.mark_read(child, read_datetime=read_datetime, do_flush=False) if ContentType.Comment == content.type: self.mark_read(content.parent, read_datetime=read_datetime, do_flush=False, recursive=False) for comment in content.parent.get_comments(): if comment != content: self.mark_read(comment, read_datetime=read_datetime, do_flush=False, recursive=False) if do_flush: self.flush() return content
def mark_unread(self, content: Content, do_flush=True) -> Content: assert self._user assert content revisions = self._session.query(ContentRevisionRO) \ .filter(ContentRevisionRO.content_id==content.content_id).all() for revision in revisions: del revision.read_by[self._user] for child in content.get_valid_children(): self.mark_unread(child, do_flush=False) if do_flush: self.flush() return content