def _vote(self, request): convId, conv = yield utils.getValidItemId(request, 'id', 'poll', ['options']) vote = utils.getRequestArg(request, 'option') if not vote or vote not in conv.get("options", {}): raise errors.MissingParams(["Option"]) optionCounts = {} myId = request.getSession(IAuthInfo).username prevVote = yield db.get_slice(myId, "userVotes", [convId]) prevVote = prevVote[0].column.value if prevVote else False if prevVote == vote: yield self._results(request) return if prevVote: yield db.remove(convId, "votes", myId, prevVote) prevOptionCount = yield db.get_count(convId, "votes", prevVote) optionCounts[prevVote] = str(prevOptionCount) yield db.insert(myId, "userVotes", vote, convId) yield db.insert(convId, "votes", '', myId, vote) voteCount = yield db.get_count(convId, "votes", vote) optionCounts[vote] = str(voteCount) yield db.batch_insert(convId, "items", {"counts": optionCounts}) yield self._results(request)
def _renderMore(self, request, data=None): myId = request.getSession(IAuthInfo).username group = data['id'] start = data['start'] itemType = data['type'] me = base.Entity(myId) me_d = me.fetchData() args = {'itemType': itemType, 'groupId': group.id, "me": me} isMember = yield db.get_count(group.id, "groupMembers", start=myId, finish=myId) if isMember: feedItems = yield Feed.get(request.getSession(IAuthInfo), feedId=group.id, start=start, itemType=itemType) args.update(feedItems) else: args["conversations"] = [] args["entities"] = {} yield me_d args["isMember"] = isMember args["entities"].update(group) args['entities'].update(me) onload = "(function(obj){$$.convs.load(obj);})(this);" t.renderScriptBlock(request, "group-feed.mako", "feed", False, "#next-load-wrapper", "replace", True, handlers={"onload": onload}, **args)
def _unfollow(self, request): authInfo = request.getSession(IAuthInfo) myId = authInfo.username orgId = authInfo.organization tagId, tag = yield utils.getValidTagId(request, "id") count = int(tag[tagId].get('followersCount', 0)) if count % 5 == 3: count = yield db.get_count(tagId, "tagFollowers") count = count - 1 if count > 0 else count yield db.remove(tagId, 'tagFollowers', myId) yield db.insert(orgId, "orgTags", str(count), "followersCount", tagId) tag[tagId]['followersCount'] = count args = {'tags': tag} args['tagsFollowing'] = [] fromListTags = (utils.getRequestArg(request, '_pg') == '/tags/list') if fromListTags: t.renderScriptBlock(request, "tags.mako", "_displayTag", False, "#tag-%s" % tagId, "replace", args=[tagId], **args) else: t.renderScriptBlock(request, 'tags.mako', "tag_actions", False, "#tag-actions-%s" % (tagId), "set", args=[tagId, False, False])
def untag(itemId, item, tagId, tag, myId, orgId): if "parent" in item: raise errors.InvalidRequest(_("Tags cannot be applied or removed from comments")) if tagId not in item.get("tags", {}): raise errors.InvalidRequest(_("No such tag on the item")) # No such tag on item d1 = db.remove(itemId, "items", tagId, "tags") d2 = db.remove(tagId, "tagItems", item["meta"]["uuid"]) d3 = db.get_slice(tagId, "tagFollowers") try: itemsCountCol = yield db.get(orgId, "orgTags", "itemsCount", tagId) tagItemsCount = int(itemsCountCol.column.value) - 1 if tagItemsCount % 10 == 7: tagItemsCount = yield db.get_count(tagId, "tagItems") tagItemsCount = tagItemsCount - 1 db.insert(orgId, "orgTags", "%s" % tagItemsCount, "itemsCount", tagId) except ttypes.NotFoundException: pass result = yield defer.DeferredList([d1, d2, d3]) followers = utils.columnsToDict(result[2][1]).keys() feedUpdateVal = "T:%s:%s::%s" % (myId, itemId, tagId) yield Feed.unpush(myId, orgId, itemId, item, feedUpdateVal, followers + [myId])
def tag(itemId, item, tagName, myId, orgId): if "parent" in item["meta"]: raise errors.InvalidRequest(_("Tag cannot be applied on a comment")) (tagId, tag) = yield tags._ensureTag(tagName, myId, orgId) if tagId in item.get("tags", {}): raise errors.InvalidRequest(_("Tag already exists on the choosen item")) d1 = db.insert(itemId, "items", myId, tagId, "tags") d2 = db.insert(tagId, "tagItems", itemId, item["meta"]["uuid"]) d3 = db.get_slice(tagId, "tagFollowers") tagItemsCount = int(tag.get("itemsCount", "0")) + 1 if tagItemsCount % 10 == 7: tagItemsCount = yield db.get_count(tagId, "tagItems") tagItemsCount += 1 db.insert(orgId, "orgTags", "%s" % tagItemsCount, "itemsCount", tagId) result = yield defer.DeferredList([d1, d2, d3]) followers = utils.columnsToDict(result[2][1]).keys() if followers: timeUUID = uuid.uuid1().bytes feedUpdateVal = "T:%s:%s::%s" % (myId, itemId, tagId) yield Feed.push(myId, orgId, itemId, item, timeUUID, feedUpdateVal, feeds=followers) defer.returnValue((tagId, tag))
def _render(self, request, viewType, start): (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax orgId = args["orgId"] args["entities"] = {} args["menuId"] = "people" if script and landing: t.render(request, "people.mako", **args) if script and appchange: t.renderScriptBlock(request, "people.mako", "layout", landing, "#mainbar", "set", **args) d = None if viewType == "all": d = getPeople(myId, orgId, orgId, start=start, fetchBlocked=False) elif viewType == "invitations": d = _getInvitationsSent(myId, start=start) else: raise errors.InvalidRequest(_("Unknown view type")) sentInvitationsCount = yield db.get_count(myId, "invitationsSent") if viewType == 'all': users, relations, userIds,\ blockedUsers, nextPageStart, prevPageStart = yield d # First result tuple contains the list of user objects. args["entities"] = users args["relations"] = relations args["people"] = userIds elif viewType == 'invitations': emailIds, prevPageStart, nextPageStart = yield d args['emailIds'] = emailIds # display the invitations tab only when there are invitations sent or # when user explicitly checks for viewType "invitations" showInvitationsTab = sentInvitationsCount > 0 or viewType == 'invitations' args["nextPageStart"] = nextPageStart args["prevPageStart"] = prevPageStart args["viewType"] = viewType args['showInvitationsTab'] = showInvitationsTab if script: t.renderScriptBlock(request, "people.mako", "viewOptions", landing, "#people-view", "set", args=[viewType], showInvitationsTab=showInvitationsTab) t.renderScriptBlock(request, "people.mako", "listPeople", landing, "#users-wrapper", "set", **args) t.renderScriptBlock(request, "people.mako", "paging", landing, "#people-paging", "set", **args) if not script: t.render(request, "people.mako", **args)
def _cleanupMissingComments(convId, missingIds, itemResponses): missingKeys = [] for response in itemResponses: userKey, responseKey = response.column.value.split(':') if responseKey in missingIds: missingKeys.append(response.column.name) d1 = db.batch_remove({'itemResponses': [convId]}, names=missingKeys) d1.addCallback(lambda x: db.get_count(convId, "itemResponses")) d1.addCallback(lambda x: db.insert(convId, 'items', \ str(x), 'responseCount', 'meta')) return d1
def createUser(emailId, displayName, jobTitle, timezone, passwd): localpart, domain = emailId.split("@") existingUser = yield db.get_count(emailId, "userAuth") if not existingUser: orgId = yield getOrgKey(domain) if not orgId: orgId = utils.getUniqueKey() domains = {domain:''} basic = {"name":domain, "type":"org"} yield db.batch_insert(orgId, "entities", {"basic":basic,"domains":domains}) yield db.insert(domain, "domainOrgMap", '', orgId) userId = yield utils.addUser(emailId, displayName, passwd, orgId, jobTitle, timezone) else: raise Exception("User already exists for " + emailId)
def unlike(itemId, item, myId, orgId): # Make sure that I liked this item try: cols = yield db.get(itemId, "itemLikes", myId) likeTimeUUID = cols.column.value except ttypes.NotFoundException: defer.returnValue(None) convId = item["meta"].get("parent", itemId) if convId != itemId: conv = yield db.get(convId, "items", super_column="meta") conv = utils.supercolumnsToDict([conv]) else: conv = item convOwnerId = conv["meta"]["owner"] convType = conv["meta"]["type"] convACL = conv["meta"]["acl"] # 1. remove the user from likes list. yield db.remove(itemId, "itemLikes", myId) # Update the likes count likesCount = int(item["meta"].get("likesCount", "1")) if likesCount % 5 == 0: likesCount = yield db.get_count(itemId, "itemLikes") item['meta']['likesCount'] = likesCount - 1 yield db.insert(itemId, "items", str(likesCount - 1), "likesCount", "meta") responseType = 'L' # 2. Don't remove the user from followers list # (use can also become follower by responding to item, # so user can't be removed from followers list) # Ignore if i am owner of the item if myId != item["meta"]["owner"]: # 3. delete from user's feed, feedItems, feed_* likeUpdateVal = "L:%s:%s:%s" % (myId, itemId, item['meta']['owner']) yield Feed.unpush(myId, orgId, convId, conv, likeUpdateVal) # FIX: if user updates more than one item at exactly same time, # one of the updates will overwrite the other. Fix it. yield db.remove(myId, "userItems", likeTimeUUID) if convType in plugins and plugins[convType].hasIndex: yield db.remove(myId, "userItems_" + convType, likeTimeUUID) defer.returnValue(item)
def _feed(self, request, data=None): (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax group = data['id'] start = data['start'] itemType = data['type'] #if user dont belong to this group show "Join Group" message isMember = yield db.get_count(group.id, "groupMembers", start=myId, finish=myId) isFollower = yield db.get_count(group.id, "followers", start=myId, finish=myId) columns = ["GI:%s" % (group.id), "GO:%s" % (group.id)] cols = yield db.get_slice(myId, "pendingConnections", columns) pendingConnections = utils.columnsToDict(cols) args["menuId"] = "groups" args["groupId"] = group.id args["isMember"] = isMember args['itemType'] = itemType args["entities"] = base.EntitySet(group) ##XXX: following should not be static args["pendingConnections"] = pendingConnections args["myGroups"] = [group.id] if isMember else [] args["groupFollowers"] = {group.id: [myId]} if isFollower else {group.id: []} if script and landing: t.render(request, "group-feed.mako", **args) elif script and appchange: t.renderScriptBlock(request, "group-feed.mako", "layout", landing, "#mainbar", "set", **args) if script: name = group.basic['name'] onload = "$$.acl.switchACL('sharebar-acl', 'group', '%s', '%s');" % (group.id, name.replace("'", "\\'")) onload += "$$.files.init('sharebar-attach');" onload += "$('#sharebar-acl-button').attr('disabled', 'disabled');" if not isMember: onload += "$('#sharebar-attach-fileshare').attr('disabled', 'disabled');" onload += "$('#sharebar-attach-file-input').attr('disabled', 'disabled');" onload += "$('#sharebar-submit').attr('disabled', 'disabled');" onload += "$('#group-share-block').addClass('disabled');" t.renderScriptBlock(request, "group-feed.mako", "summary", landing, "#group-summary", "set", **args) t.renderScriptBlock(request, "feed.mako", "share_block", landing, "#group-share-block", "set", handlers={"onload": onload}, **args) yield self._renderShareBlock(request, "status") if isMember: feedItems = yield Feed.get(request.getSession(IAuthInfo), feedId=group.id, start=start, itemType=itemType) args.update(feedItems) else: args["conversations"] = [] entities = base.EntitySet(group.admins.keys()) yield entities.fetchData() for entityId in entities.keys(): if entityId not in args['entities']: args['entities'][entityId] = entities[entityId] #group info fetched by feed may not have required info. overwrite it. args['entities'].update(group) if script: onload = "(function(obj){$$.convs.load(obj);})(this);" t.renderScriptBlock(request, "group-feed.mako", "feed", landing, "#user-feed", "set", True, handlers={"onload": onload}, **args) t.renderScriptBlock(request, "feed.mako", "feedFilterBar", landing, "#feed-filter-bar", "set", True, args=[itemType], **args) if isMember: onload = """ $('#group_add_invitee').autocomplete({ source: '/auto/users', minLength: 2, select: function( event, ui ) { $('#group_invitee').attr('value', ui.item.uid) } }); $('#feed-side-block-container').empty(); """ t.renderScriptBlock(request, "group-feed.mako", "groupLinks", landing, "#group-links", "set", handlers={"onload": onload}, **args) t.renderScriptBlock(request, "group-feed.mako", "groupAdmins", landing, "#group-admins", "set", True, **args) if isMember: for pluginType in plugins: plugin = plugins[pluginType] if hasattr(plugin, 'renderFeedSideBlock'): yield plugins["event"].renderFeedSideBlock(request, landing, group.id, args) else: t.render(request, "group-feed.mako", **args)
def _addUser(self, request): emailId = utils.getRequestArg(request, 'email') existingUser = db.get_count(emailId, "userAuth") localpart, domain = emailId.split("@") displayName = utils.getRequestArg(request, 'name') jobTitle = utils.getRequestArg(request, 'jobTitle') timezone = utils.getRequestArg(request, 'timezone') passwd = utils.getRequestArg(request, 'password', sanitize=False) pwdrepeat = utils.getRequestArg(request, 'pwdrepeat', sanitize=False) if not displayName or not jobTitle or not timezone or not passwd: raise errors.MissingParams([_("All fields are required to create the user")]) if passwd != pwdrepeat: raise PasswordsNoMatch() args = {'emailId': emailId, 'view': 'invite'} existingUser = yield existingUser if not existingUser: authinfo = yield defer.maybeDeferred(request.getSession, IAuthInfo) orgId = yield getOrgId(domain) if not orgId: orgId = utils.getUniqueKey() domains = {domain: ''} basic = {"name": domain, "type": "org"} yield db.batch_insert(orgId, "entities", {"basic": basic, "domains": domains}) yield db.insert(domain, "domainOrgMap", '', orgId) userId = yield utils.addUser(emailId, displayName, passwd, orgId, jobTitle, timezone) authinfo.username = userId authinfo.organization = orgId authinfo.isAdmin = False yield request._saveSessionToDB() cols = yield db.get_slice(domain, "invitations", [emailId]) cols = utils.supercolumnsToDict(cols) userIds = cols.get(emailId, {}).values() if userIds: db.batch_remove({'invitationsSent': userIds}, names=[emailId]) yield db.remove(domain, "invitations", super_column=emailId) t.render(request, "signup.mako", **args) # Notify all invitees about this user. token = utils.getRequestArg(request, "token") acceptedInvitationSender = cols.get(emailId, {}).get(token) otherInvitees = [x for x in userIds if x not in (acceptedInvitationSender, emailId)] entities = base.EntitySet(userIds + [orgId, userId]) yield entities.fetchData() data = {"entities": entities, 'orgId': orgId} yield notifications.notify([acceptedInvitationSender], ":IA", userId, **data) yield notifications.notify(otherInvitees, ":NU", userId, **data) else: raise InvalidRegistration("A user with this e-mail already exists! Already registered?")
def like(itemId, item, myId, orgId, me=None): try: cols = yield db.get(itemId, "itemLikes", myId) defer.returnValue(None) # Ignore the request if we already liked it. except ttypes.NotFoundException: pass if not me: me = base.Entity(myId) yield me.fetchData() itemOwnerId = item["meta"]["owner"] extraEntities = [itemOwnerId] convId = item["meta"].get("parent", itemId) if convId != itemId: #get parent's meta data conv = yield db.get(convId, "items", super_column="meta") conv = utils.supercolumnsToDict([conv]) commentText = item["meta"]["comment"] richText = item["meta"].get("richText", "False") == "True" commentSnippet = utils.toSnippet(commentText, 35, richText) else: conv = item commentSnippet = "" convOwnerId = conv["meta"]["owner"] convType = conv["meta"]["type"] convACL = conv["meta"]["acl"] # Update the likes count likesCount = int(item["meta"].get("likesCount", "0")) if likesCount % 5 == 3: likesCount = yield db.get_count(itemId, "itemLikes") item["meta"]["likesCount"] = str(likesCount + 1) yield db.insert(itemId, "items", item['meta']['likesCount'], "likesCount", "meta") timeUUID = uuid.uuid1().bytes # 1. add user to Likes list yield db.insert(itemId, "itemLikes", timeUUID, me.id) # Ignore adding to other's feeds if I am owner of the item if me.id != item["meta"]["owner"]: responseType = "L" # 2. add user to the followers list of parent item yield db.insert(convId, "items", "", me.id, "followers") # 3. update user's feed, feedItems, feed_* userItemValue = ":".join([responseType, itemId, convId, convType, convOwnerId, commentSnippet]) yield db.insert(me.id, "userItems", userItemValue, timeUUID) if convType in plugins and plugins[convType].hasIndex: yield db.insert(me.id, "userItems_" + convType, userItemValue, timeUUID) # 4. update feeds feedItemVal = "%s:%s:%s:%s" % (responseType, myId, itemId, ','.join(extraEntities)) yield Feed.push(myId, orgId, convId, conv, timeUUID, feedItemVal) if itemId != convId: if me.id != itemOwnerId: yield _notify("LC", convId, timeUUID, convType=convType, convOwnerId=convOwnerId, myId=me.id, itemOwnerId=itemOwnerId, me=me) else: if me.id != convOwnerId: yield _notify("L", convId, timeUUID, convType=convType, convOwnerId=convOwnerId, myId=me.id, me=me) defer.returnValue(item)
def postFeedback(self, request): """creates a feedback item with feedback, feedback-{mood} tags. Push item to feeback, feedback-{mood} tag followers. Note: Item is owned by feedback-domain/synovel.com, not user sending the feedback. Only users of feedback-domain/synovel.com can access the item. """ comment = utils.getRequestArg(request, 'comment') mood = utils.getRequestArg(request, 'mood') if not mood or not comment: raise errors.MissingParams([_("Feedback")]) authInfo = request.getSession(IAuthInfo) myId = authInfo.username orgId = authInfo.organization tagName = 'feedback' moodTagName = 'feedback-' + mood feedbackDomain = config.get('Feedback', 'Domain') or 'synovel.com' cols = yield db.get_slice(feedbackDomain, 'domainOrgMap') if not cols: raise errors.ConfigurationError("feedbackDomain is invalid!") # Only one org exists per domain synovelOrgId = cols[0].column.name tagId, tag = yield tags.ensureTag(request, tagName, synovelOrgId) moodTagId, moodTag = yield tags.ensureTag(request, moodTagName, synovelOrgId) # Anyone in synovel can receive feedback. acl = {'accept': {'orgs': [synovelOrgId]}} acl = json.dumps(acl) synovelOrg = base.Entity(synovelOrgId) yield synovelOrg.fetchData() # createNewItem expects an entity object with has org in basic info. # organizations wont have 'org' set. synovelOrg.basic['org'] = synovelOrgId item = yield utils.createNewItem(request, 'feedback', synovelOrg, acl, subType=mood) item['meta']['org'] = synovelOrgId item['meta']['userId'] = myId item['meta']['userOrgId'] = orgId item['meta']['comment'] = comment item['tags'] = {tagId: synovelOrgId, moodTagId: synovelOrgId} itemId = utils.getUniqueKey() tagItemCount = int(tag['itemsCount']) moodTagItemCount = int(moodTag['itemsCount']) if tagItemCount % 10 == 7: tagItemCount = yield db.get_count(tagId, "tagItems") if moodTagItemCount % 10 == 7: moodTagItemCount = yield db.get_count(moodTagId, "tagItems") tagItemCount += 1 moodTagItemCount += 1 # Finally save the feedback yield db.batch_insert(itemId, "items", item) yield db.insert(tagId, "tagItems", itemId, item["meta"]["uuid"]) yield db.insert(moodTagId, "tagItems", itemId, item["meta"]["uuid"]) yield db.insert(synovelOrgId, "orgTags", str(tagItemCount), "itemsCount", tagId) yield db.insert(synovelOrgId, "orgTags", str(moodTagItemCount), "itemsCount", moodTagId) cols = yield db.multiget_slice([tagId, moodTagId], "tagFollowers") followers = utils.multiColumnsToDict(cols) followers = set(followers[tagId].keys() + followers[moodTagId].keys()) value = {"feed": {item['meta']['uuid']: itemId}} muts = dict([(x, value) for x in followers]) if muts: yield db.batch_mutate(muts)
def _comment(convId, conv, comment, snippet, myId, orgId, richText, reviewed, fids=None): convType = conv["meta"].get("type", "status") # 1. Create the new item timeUUID = uuid.uuid1().bytes meta = {"owner": myId, "parent": convId, "comment": comment, "timestamp": str(int(time.time())), "org": orgId, "uuid": timeUUID, "richText": str(richText)} item = {'meta':meta} followers = {myId: ''} itemId = utils.getUniqueKey() if snippet: meta['snippet'] = snippet # 1.5. Check if the comment matches any of the keywords entities = base.EntitySet([myId, orgId]) yield entities.fetchData(['basic', 'admins']) orgAdmins = entities[orgId].admins.keys() if orgAdmins: matchedKeywords = yield utils.watchForKeywords(orgId, comment) if matchedKeywords: if reviewed: # Add item to list of items that matched this keyword # and notify the administrators about it. for keyword in matchedKeywords: yield db.insert(orgId + ":" + keyword, "keywordItems", itemId + ":" + convId, timeUUID) yield notifications.notify(orgAdmins, ':KW:' + keyword, myId, entities=entities) else: # This item contains a few keywords that are being monitored # by the admin and cannot be posted unless reviewOK is set. defer.returnValue((None, convId, {convId: conv}, matchedKeywords)) # 1.9. Actually store the item if fids: attachments = yield utils._upload_files(myId, fids) if attachments: item['attachments'] = {} for attachmentId in attachments: fileId, name, size, ftype = attachments[attachmentId] item["attachments"][attachmentId] = "%s:%s:%s" % (name, size, ftype) yield db.batch_insert(itemId, "items", item) yield files.pushfileinfo(myId, orgId, itemId, item, conv) # 2. Update response count and add myself to the followers of conv convOwnerId = conv["meta"]["owner"] convType = conv["meta"]["type"] responseCount = int(conv["meta"].get("responseCount", "0")) if responseCount % 5 == 3: responseCount = yield db.get_count(convId, "itemResponses") responseCount += 1 conv['meta']['responseCount'] = responseCount convUpdates = {"responseCount": str(responseCount)} yield db.batch_insert(convId, "items", {"meta": convUpdates, "followers": followers}) # 3. Add item as response to parent yield db.insert(convId, "itemResponses", "%s:%s" % (myId, itemId), timeUUID) # 4. Update userItems and userItems_* responseType = "Q" if convType == "question" else 'C' commentSnippet = utils.toSnippet(comment, 35, richText) userItemValue = ":".join([responseType, itemId, convId, convType, convOwnerId, commentSnippet]) yield db.insert(myId, "userItems", userItemValue, timeUUID) if convType in plugins and plugins[convType].hasIndex: yield db.insert(myId, "userItems_" + convType, userItemValue, timeUUID) # 5. Update my feed. feedItemVal = "%s:%s:%s" % (responseType, myId, itemId) yield Feed.push(myId, orgId, convId, conv, timeUUID, feedItemVal) yield _notify("C", convId, timeUUID, convType=convType, convOwnerId=convOwnerId, myId=myId, me=entities[myId], comment=comment, richText=richText) search.solr.updateItemIndex(itemId, {'meta': meta}, orgId, conv=conv) items = {itemId: item, convId: conv} defer.returnValue((itemId, convId, items, None))
def existingUser(emailId): count = yield db.get_count(emailId, "userAuth") if count: defer.returnValue(True) defer.returnValue(False)
def _listConversations(self, request): """Renders a time sorted list of coversations in a particular view. Keyword Arguments: filerType: The folder view which is to rendered. One of ['unread', 'all', 'archive', 'trash']. start: The base64 encoded timeUUID of the starting conversation id of the page that needs to be rendered. """ (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax filterType = utils.getRequestArg(request, 'type') folder = self._folders[filterType] if filterType in self._folders else\ self._folders['inbox'] start = utils.getRequestArg(request, "start") or '' start = utils.decodeKey(start) if script and landing: t.render(request, "message.mako", **args) if appchange and script: t.renderScriptBlock(request, "message.mako", "layout", landing, "#mainbar", "set", **args) unread = [] convs = [] users = set() count = 10 fetchCount = count + 1 nextPageStart = '' prevPageStart = '' cols = yield db.get_slice(myId, folder, reverse=True, start=start, count=fetchCount) for col in cols: x, convId = col.column.value.split(':') convs.append(convId) if x == 'u': unread.append(convId) if len(cols) == fetchCount: nextPageStart = utils.encodeKey(col.column.name) convs = convs[:count] ###XXX: try to avoid extra fetch cols = yield db.get_slice(myId, folder, count=fetchCount, start=start) if cols and len(cols) > 1 and start: prevPageStart = utils.encodeKey(cols[-1].column.name) cols = yield db.multiget_slice(convs, 'mConversations') conversations = utils.multiSuperColumnsToDict(cols) m = {} for convId in conversations: if not conversations[convId]: continue participants = conversations[convId]['participants'].keys() users.update(participants) conversations[convId]['people'] = participants conversations[convId]['read'] = str(int(convId not in unread)) messageCount = yield db.get_count(convId, "mConvMessages") conversations[convId]['count'] = messageCount m[convId] = conversations[convId] users = base.EntitySet(users) yield users.fetchData() args.update({"view": "messages"}) args.update({"messages": m}) args.update({"people": users}) args.update({"mids": convs}) args.update({"menuId": "messages"}) args.update({"filterType": filterType or "all"}) args['nextPageStart'] = nextPageStart args['prevPageStart'] = prevPageStart if script: onload = """ $$.menu.selectItem('%s'); $('#mainbar .contents').removeClass("has-right"); """ % args["menuId"] t.renderScriptBlock(request, "message.mako", "render_conversations", landing, ".center-contents", "set", True, handlers={"onload": onload}, **args) yield utils.render_LatestCounts(request, landing) else: t.render(request, "message.mako", **args)
def deleteItem(itemId, userId, orgId, item=None, conv=None,): if not item: item = yield db.get_slice(itemId, "items", ["meta", "tags"]) item = utils.supercolumnsToDict(item) meta = item["meta"] convId = meta.get("parent", itemId) itemUUID = meta["uuid"] itemOwnerId = meta["owner"] timestamp = str(int(time.time())) d = db.insert(itemId, "items", "deleted", "state", "meta") deferreds = [d] if not conv: conv = yield db.get_slice(convId, "items", ['meta', 'tags']) conv = utils.supercolumnsToDict(conv) plugin = plugins[conv['meta']['type']] if convId == itemId: # Delete from tagItems. for tagId in item.get("tags", {}): d = db.remove(tagId, "tagItems", itemUUID) deferreds.append(d) # Actually, delete the conversation. d1 = db.insert(itemOwnerId, 'deletedConvs', timestamp, itemId) d1.addCallback(lambda x: search.solr.delete(itemId)) d1.addCallback(lambda x: files.deleteFileInfo(userId, orgId, itemId, item)) d2 = plugin.delete(userId, convId, conv) deferreds.extend([d1, d2]) itemResponses = yield db.get_slice(convId, "itemResponses") itemResponses = utils.columnsToDict(itemResponses) items = yield db.multiget_slice(itemResponses.values(), "items", ["meta", "attachments"]) items = utils.multiSuperColumnsToDict(items) for responseId in items: deferreds.append(files.deleteFileInfo(userId, orgId, responseId, items[responseId], conv)) else: convType = conv["meta"]["type"] convOwnerId = conv["meta"]["owner"] convACL = conv["meta"]["acl"] d1 = db.insert(convId, 'deletedConvs', timestamp, itemId) d2 = db.remove(convId, 'itemResponses', itemUUID) d2.addCallback(lambda x: db.get_count(convId, "itemResponses")) d2.addCallback(lambda x: db.insert(convId, 'items', \ str(x), 'responseCount', 'meta')) d2.addCallback(lambda x: search.solr.delete(itemId)) d3 = files.deleteFileInfo(userId, orgId, itemId, item, conv) deferreds.extend([d1, d2, d3]) # Rollback changes to feeds caused by this comment d = db.get_slice(itemId, "itemLikes") def removeLikeFromFeeds(result): likes = utils.columnsToDict(result) removeLikeDeferreds = [] for actorId, likeUUID in likes.items(): likeUpdateVal = "L:%s:%s:%s" % (actorId, itemId, itemOwnerId) d1 = feed.deleteUserFeed(actorId, convType, likeUUID) d2 = Feed.unpush(actorId, orgId, convId, conv, likeUpdateVal) removeLikeDeferreds.extend([d1, d2]) return defer.DeferredList(removeLikeDeferreds) d.addCallback(removeLikeFromFeeds) deferreds.append(d) # Delete comment from comment owner's userItems d = feed.deleteUserFeed(itemOwnerId, convType, itemUUID) deferreds.append(d) # Rollback updates done to comment owner's follower's feeds. responseUpdateVal = "Q:%s:%s" if convType == "question" else "C:%s:%s" responseUpdateVal = responseUpdateVal % (itemOwnerId, itemId) d = Feed.unpush(itemOwnerId, orgId, convId, conv, responseUpdateVal) deferreds.append(d) yield defer.DeferredList(deferreds)