2009-05-13 16:54:06 +02:00
|
|
|
"""The conversation and replies adapters
|
|
|
|
|
|
|
|
The conversation is responsible for storing all comments. It provides a
|
|
|
|
dict-like API for accessing comments, where keys are integers and values
|
|
|
|
are IComment objects. It also provides features for finding comments quickly.
|
|
|
|
|
|
|
|
The two IReplies adapters - one for the IConversation and one for IComment -
|
|
|
|
manipulate the same data structures, but provide an API for finding and
|
|
|
|
manipulating the comments directly in reply to a particular comment or at the
|
|
|
|
top level of the conversation.
|
|
|
|
"""
|
|
|
|
|
2009-05-16 11:47:10 +02:00
|
|
|
from persistent import Persistent
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
from zope.interface import implements, implementer
|
|
|
|
from zope.component import adapts, adapter
|
|
|
|
|
|
|
|
from zope.annotation.interfaces import IAnnotatable
|
2009-05-13 16:54:06 +02:00
|
|
|
|
|
|
|
from BTrees.OIBTree import OIBTree
|
|
|
|
from BTrees.IOBTree import IOBTree
|
|
|
|
from BTrees.IIBTree import IIBTree, IISet
|
2009-05-16 13:21:23 +02:00
|
|
|
try:
|
|
|
|
from BTrees.LOBTree import LOBTree
|
|
|
|
from BTrees.LLBTree import LLBTree # TODO: Does this even exist?
|
|
|
|
except ImportError:
|
|
|
|
from BTrees.OOBTree import OOBTree as LOBTree
|
|
|
|
from BTrees.OOBTree import OOSet as LLSet
|
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
|
|
|
|
from Acquisition import Explicit
|
|
|
|
|
|
|
|
from plone.app.discussion.interfaces import IConversation, IComment, IReplies
|
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
class Conversation(Persistent, Explicit):
|
2009-05-13 16:54:06 +02:00
|
|
|
"""A conversation is a container for all comments on a content object.
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
It manages internal data structures for comment threading and efficient
|
|
|
|
comment lookup.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
implements(IConversation)
|
|
|
|
|
|
|
|
def __init__(self, id="++comments++"):
|
|
|
|
self.id = id
|
|
|
|
|
|
|
|
# username -> count of comments; key is removed when count reaches 0
|
|
|
|
self._commentators = OIBTree()
|
|
|
|
self._last_comment_date = None
|
|
|
|
|
|
|
|
# id -> comment - find comment by id
|
2009-05-16 13:21:23 +02:00
|
|
|
self._comments = LOBTree()
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-16 06:46:47 +02:00
|
|
|
# id -> IISet (children) - find all children for a given comment. 0 signifies root.
|
2009-05-16 13:21:23 +02:00
|
|
|
self._children = LOBTree()
|
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def getId(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
# TODO
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total_comments(self):
|
|
|
|
# TODO
|
2009-05-16 12:34:12 +02:00
|
|
|
return len(self._comments)
|
2009-05-13 16:54:06 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def last_comment_date(self):
|
2009-05-16 12:34:12 +02:00
|
|
|
return self._last_comment_date
|
2009-05-13 16:54:06 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def commentators(self):
|
|
|
|
# TODO:
|
|
|
|
return set()
|
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
def getComments(self, start=0, size=None):
|
|
|
|
return self._comments.values()
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
def getThreads(self, start=0, size=None, root=None, depth=None):
|
2009-05-16 13:21:23 +02:00
|
|
|
# TODO:
|
2009-05-16 12:34:12 +02:00
|
|
|
return self._comments.values()
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
def addComment(self, comment):
|
2009-05-16 13:21:23 +02:00
|
|
|
id = comment.comment_id
|
|
|
|
if id in self._comments:
|
|
|
|
id = max(self._comments.keys()) +1
|
2009-05-16 12:34:12 +02:00
|
|
|
self._comments[id] = comment
|
|
|
|
comment.comment_id = id
|
|
|
|
|
|
|
|
commentator = comment.creator
|
|
|
|
if not commentator in self._commentators:
|
|
|
|
self._commentators[commentator] = 0
|
|
|
|
self._commentators[commentator] += 1
|
|
|
|
|
|
|
|
self._last_comment_date = comment.creation_date
|
|
|
|
|
|
|
|
reply_to = comment.in_reply_to
|
|
|
|
if not reply_to in self._children:
|
2009-05-16 13:21:23 +02:00
|
|
|
self._children[reply_to] = LLSet()
|
2009-05-16 12:34:12 +02:00
|
|
|
self._children[reply_to].insert(id)
|
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# Dict API
|
|
|
|
|
|
|
|
# TODO: Update internal data structures when items added or removed
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
@implementer(IConversation)
|
|
|
|
@adapter(IAnnotatable)
|
|
|
|
def conversationAdapterFactory(content):
|
|
|
|
"""Adapter factory to fetch a conversation from annotations
|
|
|
|
"""
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
# TODO
|
|
|
|
return None
|
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
class ConversationReplies(object):
|
2009-05-13 17:20:02 +02:00
|
|
|
"""An IReplies adapter for conversations.
|
|
|
|
|
|
|
|
This makes it easy to work with top-level comments.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
implements(IReplies)
|
|
|
|
adapts(Conversation)
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
def __init__(self, context):
|
|
|
|
self.conversation = context
|
|
|
|
self.root = 0
|
|
|
|
|
|
|
|
# TODO: dict interface - generalise to work with any starting point, so
|
|
|
|
# that the subclassing below works
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
class CommentReplies(ConversationReplies):
|
|
|
|
"""An IReplies adapter for comments.
|
|
|
|
|
|
|
|
This makes it easy to work with replies to specific comments.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
implements(IReplies)
|
2009-05-13 17:20:02 +02:00
|
|
|
adapts(IComment)
|
|
|
|
|
|
|
|
def __init__(self, context):
|
|
|
|
self.conversation = context.__parent__
|
|
|
|
self.root = context.comment_id
|