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-18 16:16:48 +02:00
|
|
|
import time
|
|
|
|
|
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
|
2009-05-16 17:05:22 +02:00
|
|
|
from zope.annotation.interfaces import IAnnotations
|
|
|
|
|
|
|
|
from zope.event import notify
|
2009-05-18 16:16:48 +02:00
|
|
|
|
|
|
|
from Acquisition import Explicit
|
|
|
|
from OFS.Traversable import Traversable
|
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
from OFS.event import ObjectWillBeAddedEvent
|
|
|
|
from OFS.event import ObjectWillBeRemovedEvent
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
from zope.app.container.contained import ContainerModifiedEvent
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
from zope.app.container.contained import ObjectAddedEvent
|
|
|
|
from zope.app.container.contained import ObjectRemovedEvent
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
from zope.annotation.interfaces import IAnnotatable
|
2009-05-13 16:54:06 +02:00
|
|
|
|
|
|
|
from BTrees.OIBTree import OIBTree
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2009-05-16 13:21:23 +02:00
|
|
|
try:
|
2009-05-16 17:05:22 +02:00
|
|
|
# These exist in new versions, but not in the one that comes with Zope 2.10.
|
2009-05-16 13:21:23 +02:00
|
|
|
from BTrees.LOBTree import LOBTree
|
2009-05-16 17:05:22 +02:00
|
|
|
from BTrees.LLBTree import LLSet
|
2009-05-16 13:21:23 +02:00
|
|
|
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 plone.app.discussion.interfaces import IConversation, IComment, IReplies
|
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
from Acquisition import aq_base
|
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
ANNO_KEY = 'plone.app.discussion:conversation'
|
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
class Conversation(Traversable, 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()
|
|
|
|
|
|
|
|
# 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):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get the id of
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled(self):
|
2009-05-18 16:16:48 +02:00
|
|
|
# TODO - check __parent__'s settings + global settings
|
2009-05-13 16:54:06 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total_comments(self):
|
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-18 16:16:48 +02:00
|
|
|
try:
|
|
|
|
return self._comments[self._comments.maxKey()].creation_date
|
|
|
|
except (ValueError, KeyError, AttributeError,):
|
|
|
|
return None
|
2009-05-13 16:54:06 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def commentators(self):
|
2009-05-18 16:16:48 +02:00
|
|
|
return self._commentators.keys()
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
def getComments(self, start=0, size=None):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get unthreaded comments
|
|
|
|
"""
|
|
|
|
# TODO - batching
|
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 getThreads(self, start=0, size=None, root=None, depth=None):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get threaded comments
|
|
|
|
"""
|
|
|
|
# TODO - build threads
|
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-18 16:16:48 +02:00
|
|
|
"""Add a new comment. The parent id should have been set already. The
|
|
|
|
comment id may be modified to find a free key. The id used will be
|
|
|
|
returned.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Make sure we don't have a wrapped object
|
|
|
|
|
|
|
|
comment = aq_base(comment)
|
|
|
|
|
|
|
|
id = long(time.time() * 1e6)
|
|
|
|
while id in self._comments:
|
|
|
|
id += 1
|
|
|
|
|
|
|
|
comment.comment_id = id
|
2009-05-16 17:05:22 +02:00
|
|
|
notify(ObjectWillBeAddedEvent(comment, self, id))
|
2009-05-16 12:34:12 +02:00
|
|
|
self._comments[id] = comment
|
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
# for logged in users only
|
|
|
|
commentator = comment.author_username
|
|
|
|
if commentator:
|
|
|
|
if not commentator in self._commentators:
|
|
|
|
self._commentators[commentator] = 0
|
|
|
|
self._commentators[commentator] += 1
|
2009-05-16 12:34:12 +02:00
|
|
|
|
|
|
|
reply_to = comment.in_reply_to
|
2009-05-18 16:16:48 +02:00
|
|
|
if not reply_to:
|
|
|
|
# top level comments are in reply to the faux id 0
|
|
|
|
comment.in_reply_to = reply_to = 0
|
|
|
|
|
2009-05-16 12:34:12 +02:00
|
|
|
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-18 16:16:48 +02:00
|
|
|
|
2009-05-17 20:38:45 +02:00
|
|
|
# Notify that the object is added. The object must here be
|
|
|
|
# acquisition wrapped or the indexing will fail.
|
|
|
|
notify(ObjectAddedEvent(comment.__of__(self), self, id))
|
2009-05-16 17:05:22 +02:00
|
|
|
notify(ContainerModifiedEvent(self))
|
2009-05-16 12:34:12 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# Dict API
|
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self._comments)
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return long(key) in self._comments
|
|
|
|
|
|
|
|
# TODO: Should __getitem__, get, __iter__, values(), items() and iter* return aq-wrapped comments?
|
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
def __getitem__(self, key):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Get an item by its long key
|
|
|
|
"""
|
|
|
|
return self._comments[long(key)]
|
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
def __delitem__(self, key):
|
2009-05-18 16:16:48 +02:00
|
|
|
"""Delete an item by its long key
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = long(key)
|
|
|
|
|
|
|
|
comment = self[key]
|
|
|
|
commentator = comment.author_username
|
|
|
|
|
|
|
|
notify(ObjectWillBeRemovedEvent(comment, self, key))
|
|
|
|
self._comments.remove(key)
|
|
|
|
notify(ObjectRemovedEvent(comment, self, key))
|
|
|
|
|
|
|
|
if commentator and commentator in self._commentators:
|
|
|
|
if self._commentators[commentator] <= 1:
|
|
|
|
del self._commentators[commentator]
|
|
|
|
else:
|
|
|
|
self._commentators[commentator] -= 1
|
|
|
|
|
|
|
|
notify(ContainerModifiedEvent(self))
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._comments)
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
return self._comments.get(long(key), default)
|
2009-05-16 17:05:22 +02:00
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self._comments.keys()
|
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def items(self):
|
|
|
|
return self._comments.items()
|
|
|
|
|
|
|
|
def values(self):
|
|
|
|
return self._comments.values()
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
|
|
return self._comments.iterkeys()
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
return self._comments.itervalues()
|
2009-05-17 20:38:45 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iteritems(self):
|
|
|
|
return self._comments.iteritems()
|
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-16 17:05:22 +02:00
|
|
|
annotions = IAnnotations(content)
|
|
|
|
if not ANNO_KEY in annotions:
|
|
|
|
conversation = Conversation()
|
|
|
|
conversation._parent_uid = content.UID()
|
|
|
|
annotions[ANNO_KEY] = conversation
|
|
|
|
conversation = annotions[ANNO_KEY]
|
|
|
|
return conversation
|
2009-05-13 17:20:02 +02:00
|
|
|
|
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)
|
2009-05-18 16:16:48 +02:00
|
|
|
adapts(Conversation) # relies on implementation details
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
def __init__(self, context):
|
|
|
|
self.conversation = context
|
2009-05-18 16:16:48 +02:00
|
|
|
self.children = self.conversation._children.get(0, LLSet())
|
2009-05-13 17:20:02 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def addComment(self, comment):
|
|
|
|
comment.in_reply_to = None
|
|
|
|
return self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
# Dict API
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.children)
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return long(key) in self.children
|
|
|
|
|
|
|
|
# TODO: Should __getitem__, get, __iter__, values(), items() and iter* return aq-wrapped comments?
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Get an item by its long key
|
|
|
|
"""
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
raise KeyError(key)
|
|
|
|
return self.conversation[key]
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
"""Delete an item by its long key
|
|
|
|
"""
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
raise KeyError(key)
|
|
|
|
del self.conversation[key]
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.children)
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
return default
|
|
|
|
return self.conversation.get(key)
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self.children
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return [(k, self.conversation[k]) for k in self.children]
|
|
|
|
|
|
|
|
def values(self):
|
|
|
|
return [self.conversation[k] for k in self.children]
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
|
|
return iter(self.children)
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
for key in self.children:
|
|
|
|
yield self.conversation[key]
|
|
|
|
|
|
|
|
def iteritems(self):
|
|
|
|
for key in self.children:
|
|
|
|
yield (key, self.conversation[key],)
|
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):
|
2009-05-18 16:16:48 +02:00
|
|
|
self.comment = context
|
|
|
|
self.comment_id = context.comment_id
|
|
|
|
self.children = self.conversation._children.get(0, LLSet())
|
|
|
|
|
|
|
|
def addComment(self, comment):
|
|
|
|
comment.in_reply_to = self.comment_id
|
|
|
|
return self.conversation.addComment(comment)
|
|
|
|
|
|
|
|
# Dict API is inherited
|