def pushToFeed(userKey, timeuuid, itemKey, parentKey, responseType, itemType, convOwner=None, commentOwner=None, tagId='', entities=None, promote=True): # Caveat: assume itemKey as parentKey if parentKey is None parentKey = itemKey if not parentKey else parentKey convOwner = userKey if not convOwner else convOwner commentOwner = userKey if not commentOwner else commentOwner # Get this conversation to the top of the feed only if promote is set if promote: yield db.insert(userKey, "feed", parentKey, timeuuid) if plugins.has_key(itemType) and plugins[itemType].hasIndex: yield db.insert(userKey, "feed_"+itemType, parentKey, timeuuid) yield updateFeedResponses(userKey, parentKey, itemKey, timeuuid, itemType, responseType, convOwner, commentOwner, tagId, entities, promote)
def updateFeedResponses(userKey, parentKey, itemKey, timeuuid, itemType, responseType, convOwner, commentOwner, tagId, entities, promote): if not entities: entities = [commentOwner] else: entities.extend([commentOwner]) entities = ",".join(entities) feedItemValue = ":".join([responseType, commentOwner, itemKey, entities, tagId]) tmp, oldest, latest = {}, None, None cols = yield db.get_slice(userKey, "feedItems", super_column=parentKey, reverse=True) cols = utils.columnsToDict(cols, ordered=True) feedKeys = [] userFeedItems = [] userFeedItemsByType = {} for tuuid, val in cols.items(): # Bailout if we already know about this update. if tuuid == timeuuid: defer.returnValue(None) rtype = val.split(':')[0] if rtype not in ('!', 'I'): tmp.setdefault(rtype, []).append(tuuid) if val.split(':')[1] == userKey: userFeedItems.append(tuuid) userFeedItemsByType.setdefault(rtype, []).append(tuuid) oldest = tuuid feedKeys.append(tuuid) # Remove older entries of this conversation from the feed # only if a new one was added before this function was called. if promote and feedKeys: yield db.batch_remove({'feed': [userKey]}, names=feedKeys) totalItems = len(cols) noOfItems = len(tmp.get(responseType, [])) if noOfItems == MAXFEEDITEMSBYTYPE: if (len(userFeedItemsByType.get(responseType, {})) == MAXFEEDITEMSBYTYPE and not promote)\ or (tmp[responseType][noOfItems-1] not in userFeedItemsByType.get(responseType, {}) \ and len(userFeedItemsByType.get(responseType, {})) == MAXFEEDITEMSBYTYPE-1 and not promote): oldest = userFeedItemsByType[responseType][noOfItems-2] else: oldest = tmp[responseType][noOfItems-1] if ((len(userFeedItems)== MAXFEEDITEMS-1 and not promote) or \ (oldest not in userFeedItems and len(userFeedItems) == MAXFEEDITEMS-2 and not promote)): oldest = userFeedItems[-2] if noOfItems == MAXFEEDITEMSBYTYPE or totalItems == MAXFEEDITEMS: yield db.remove(userKey, "feedItems", oldest, parentKey) if plugins.has_key(itemType) and plugins[itemType].hasIndex: yield db.remove(userKey, "feed_"+itemType, oldest) if totalItems == 0 and responseType != 'I': value = ":".join(["!", convOwner, parentKey, ""]) tuuid = uuid.uuid1().bytes yield db.batch_insert(userKey, "feedItems", {parentKey:{tuuid:value}}) yield db.batch_insert(userKey, "feedItems", {parentKey:{timeuuid: feedItemValue}})
def deleteUserFeed(userId, itemType, tuuid): yield db.remove(userId, "userItems", tuuid) if plugins.has_key(itemType) and plugins[itemType].hasIndex: yield db.remove(userId, "userItems_"+itemType, tuuid)