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-23 06:55:06 +02:00
|
|
|
|
|
|
|
from zope.annotation.interfaces import IAnnotations, IAnnotatable
|
2009-05-16 17:05:22 +02:00
|
|
|
|
|
|
|
from zope.event import notify
|
2009-05-18 16:16:48 +02:00
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
from Acquisition import aq_base
|
2009-05-18 16:16:48 +02:00
|
|
|
from Acquisition import Explicit
|
2009-05-18 16:23:46 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
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
|
|
|
|
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-18 16:23:46 +02:00
|
|
|
from plone.app.discussion.interfaces import IConversation, IReplies
|
|
|
|
from plone.app.discussion.comment import Comment
|
2009-05-13 16:54:06 +02:00
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
ANNOTATION_KEY = 'plone.app.discussion:conversation'
|
2009-05-16 17:05:22 +02:00
|
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
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
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
implements(IConversation)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-23 13:52:57 +02:00
|
|
|
__allow_access_to_unprotected_subobjects__ = True
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-23 13:52:57 +02:00
|
|
|
def __init__(self, id="++conversation++default"):
|
2009-05-13 16:54:06 +02:00
|
|
|
self.id = id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# username -> count of comments; key is removed when count reaches 0
|
|
|
|
self._commentators = OIBTree()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# id -> comment - find comment by id
|
2009-05-23 16:18:35 +02:00
|
|
|
self._comments = LOBTree()
|
|
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
def getId(self):
|
2009-05-23 13:52:57 +02:00
|
|
|
"""Get the id of the conversation. This is used to construct a
|
|
|
|
URL.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
|
|
|
return self.id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
@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
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
@property
|
|
|
|
def total_comments(self):
|
2009-05-16 12:34:12 +02:00
|
|
|
return len(self._comments)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
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-23 16:18:35 +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-23 16:18:35 +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-23 18:10:24 +02:00
|
|
|
return []
|
2009-05-23 16:18:35 +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.
|
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
# Make sure we don't have a wrapped object
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
comment = aq_base(comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
id = long(time.time() * 1e6)
|
|
|
|
while id in self._comments:
|
|
|
|
id += 1
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
comment.__parent__ = self
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
# Record unique users who've commented (for logged in users only)
|
2009-05-18 16:16:48 +02:00
|
|
|
commentator = comment.author_username
|
|
|
|
if commentator:
|
|
|
|
if not commentator in self._commentators:
|
|
|
|
self._commentators[commentator] = 0
|
|
|
|
self._commentators[commentator] += 1
|
2009-05-23 16:18:35 +02:00
|
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
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-23 16:18:35 +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-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
return id
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
# Dict API
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self._comments)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __contains__(self, key):
|
|
|
|
return long(key) in self._comments
|
2009-05-23 16:18:35 +02:00
|
|
|
|
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
|
|
|
|
"""
|
2009-05-18 17:15:36 +02:00
|
|
|
return self._comments[long(key)].__of__(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
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
|
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
key = long(key)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 17:15:36 +02:00
|
|
|
comment = self[key].__of__(self)
|
2009-05-18 16:16:48 +02:00
|
|
|
commentator = comment.author_username
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
notify(ObjectWillBeRemovedEvent(comment, self, key))
|
2009-05-23 16:18:35 +02:00
|
|
|
|
|
|
|
self._comments.pop(key)
|
2009-05-18 16:16:48 +02:00
|
|
|
notify(ObjectRemovedEvent(comment, self, key))
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
if commentator and commentator in self._commentators:
|
|
|
|
if self._commentators[commentator] <= 1:
|
|
|
|
del self._commentators[commentator]
|
|
|
|
else:
|
|
|
|
self._commentators[commentator] -= 1
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
notify(ContainerModifiedEvent(self))
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._comments)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def get(self, key, default=None):
|
2009-05-18 17:15:36 +02:00
|
|
|
comment = self._comments.get(long(key), default)
|
|
|
|
if comment is default:
|
|
|
|
return default
|
|
|
|
return comment.__of__(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-16 17:05:22 +02:00
|
|
|
def keys(self):
|
|
|
|
return self._comments.keys()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def items(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
return [(i[0], i[1].__of__(self),) for i in self._comments.items()]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def values(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
return [v.__of__(self) for v in self._comments.values()]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iterkeys(self):
|
|
|
|
return self._comments.iterkeys()
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def itervalues(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
for v in self._comments.itervalues():
|
|
|
|
yield v.__of__(self)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iteritems(self):
|
2009-05-18 17:15:36 +02:00
|
|
|
for k, v in self._comments.iteritems():
|
|
|
|
yield (k, v.__of__(self),)
|
2009-05-13 17:20:02 +02:00
|
|
|
|
|
|
|
@implementer(IConversation)
|
|
|
|
@adapter(IAnnotatable)
|
|
|
|
def conversationAdapterFactory(content):
|
2009-05-23 13:52:57 +02:00
|
|
|
"""Adapter factory to fetch the default conversation from annotations.
|
|
|
|
Will create the conversation if it does not exist.
|
2009-05-13 17:20:02 +02:00
|
|
|
"""
|
2009-05-16 17:05:22 +02:00
|
|
|
annotions = IAnnotations(content)
|
2009-05-18 16:23:46 +02:00
|
|
|
if not ANNOTATION_KEY in annotions:
|
2009-05-16 17:05:22 +02:00
|
|
|
conversation = Conversation()
|
2009-05-18 16:23:46 +02:00
|
|
|
annotions[ANNOTATION_KEY] = conversation
|
|
|
|
conversation = annotions[ANNOTATION_KEY]
|
2009-05-16 17:05:22 +02:00
|
|
|
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.
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
This makes it easy to work with top-level comments.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
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-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def addComment(self, comment):
|
|
|
|
comment.in_reply_to = None
|
|
|
|
return self.conversation.addComment(comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
# Dict API
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.children)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __contains__(self, key):
|
|
|
|
return long(key) in self.children
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
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]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
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]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.children)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def get(self, key, default=None):
|
|
|
|
key = long(key)
|
|
|
|
if key not in self.children:
|
|
|
|
return default
|
|
|
|
return self.conversation.get(key)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def keys(self):
|
|
|
|
return self.children
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def items(self):
|
|
|
|
return [(k, self.conversation[k]) for k in self.children]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def values(self):
|
|
|
|
return [self.conversation[k] for k in self.children]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def iterkeys(self):
|
|
|
|
return iter(self.children)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def itervalues(self):
|
|
|
|
for key in self.children:
|
|
|
|
yield self.conversation[key]
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
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.
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
This makes it easy to work with replies to specific comments.
|
2009-05-13 16:54:06 +02:00
|
|
|
"""
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-13 16:54:06 +02:00
|
|
|
implements(IReplies)
|
2009-05-18 16:23:46 +02:00
|
|
|
|
|
|
|
# depends on implementation details of conversation
|
|
|
|
# most likely, anyone writing a different type of Conversation will also
|
|
|
|
# have a different type of Comment
|
|
|
|
|
|
|
|
adapts(Comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
|
|
|
|
2009-05-13 17:20:02 +02:00
|
|
|
def __init__(self, context):
|
2009-05-18 16:16:48 +02:00
|
|
|
self.comment = context
|
2009-05-18 16:23:46 +02:00
|
|
|
self.conversation = self.comment.__parent__
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
if self.conversation is None or not hasattr(self.conversation, '_children'):
|
|
|
|
raise TypeError("This adapter doesn't know what to do with the parent conversation")
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
self.comment_id = self.comment.comment_id
|
|
|
|
self.children = self.conversation._children.get(self.comment_id, LLSet())
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:16:48 +02:00
|
|
|
def addComment(self, comment):
|
|
|
|
comment.in_reply_to = self.comment_id
|
|
|
|
return self.conversation.addComment(comment)
|
2009-05-23 16:18:35 +02:00
|
|
|
|
2009-05-18 16:23:46 +02:00
|
|
|
# Dict API is inherited, written in terms of self.conversation and self.children
|