def _deletePresetTag(self, request): orgId = request.getSession(IAuthInfo).organization tagId = utils.getRequestArg(request, 'id') if not tagId: return try: tag = yield db.get(orgId, 'orgTags', super_column=tagId) tag = utils.supercolumnsToDict([tag]) tagName = tag[tagId]['title'] if 'isPreset' in tag[tagId]: yield db.remove(orgId, "orgTags", 'isPreset', tagId) yield db.remove(orgId, 'orgPresetTags', tagName) presetTags = yield db.get_slice(orgId, "orgPresetTags") presetTags = utils.columnsToDict(presetTags, ordered=True).values() if presetTags: tags_ = yield db.get_slice(orgId, "orgTags", presetTags) tags_ = utils.supercolumnsToDict(tags) else: tags_ = {} args = {'tagsList': presetTags, 'tags': tags_} request.write('$("#tag-%s").remove()' % (tagId)) except ttypes.NotFoundException: return
def _revoke(self, request): authinfo = request.getSession(IAuthInfo) myId = authinfo.username myOrgId = authinfo.organization clientId = utils.getRequestArg(request, "id", sanitize=False) client = yield db.get_slice(clientId, "apps") client = utils.supercolumnsToDict(client) if not client: raise errors.InvalidApp(clientId) me = yield db.get_slice(myId, "entities", ["apikeys", "apps"]) me = utils.supercolumnsToDict(me) # Remove the client in case of API Key if client["meta"]["category"] == "apikey": if client["meta"]["author"] != myId: raise errors.AppAccessDenied(clientId) d1 = db.remove(clientId, "apps") d2 = db.remove(myId, "appsByOwner", clientId) d3 = db.remove(myId, "entities", clientId, "apikeys") d4 = db.remove(myOrgId, "appsByOwner", clientId) yield defer.DeferredList([d1, d2, d3, d4]) # Remove the refresh token # XXX: Valid access tokens could still exist else: authorization = me["apps"][clientId] d1 = db.remove(myId, "entities", clientId, "apps") d2 = db.remove(authorization, "oAuthData") yield defer.DeferredList([d1, d2])
def _unlike(self, request, data=None): (appchange, script, args, myId) = yield self._getBasicArgs(request) orgId = args['orgId'] itemId, item = data['id'] item = yield Item.unlike(itemId, item, myId, orgId) if not item: return args["items"] = {itemId: item} args["myLikes"] = {itemId: []} likesCount = int(item["meta"]["likesCount"]) convId = item["meta"].get('parent', itemId) if itemId != convId: t.renderScriptBlock(request, "item.mako", "item_footer", False, "#item-footer-%s" % (itemId), "set", args=[itemId], **args) else: relation = Relation(myId, []) yield relation.initSubscriptionsList() toFetchEntities = set() likes = [] subscriptions = list(relation.subscriptions) if subscriptions: likes = yield db.get_slice(convId, "itemLikes", subscriptions) likes = [x.column.name for x in likes] toFetchEntities = set(likes) feedItems = yield db.get_slice(myId, "feedItems", [convId]) feedItems = utils.supercolumnsToDict(feedItems) isFeed = (utils.getRequestArg(request, "_pg") != "/item") hasComments = False if not isFeed: hasComments = True else: feedItems = yield db.get_slice(myId, "feedItems", [convId]) feedItems = utils.supercolumnsToDict(feedItems) for tuuid in feedItems.get(convId, {}): val = feedItems[convId][tuuid] rtype = val.split(":")[0] if rtype == "C": hasComments = True entities = base.EntitySet(toFetchEntities) if toFetchEntities: yield entities.fetchData() args["entities"] = entities handler = {"onload": "(function(){$$.convs.showHideComponent('%s', 'likes', false)})();" % (convId)} if not likes else None t.renderScriptBlock(request, "item.mako", "conv_footer", False, "#item-footer-%s" % (itemId), "set", args=[itemId, hasComments, likes], **args) t.renderScriptBlock(request, "item.mako", 'conv_likes', False, '#conv-likes-wrapper-%s' % convId, 'set', True, args=[itemId, likesCount, False, likes], handlers=handler, **args)
def _getFileInfo(self, request): """Fetch the meta info on a file that is being requested to be downloaded. Returns the meta info of the file in question. Keyword Arguments: itemId: id of the conversation on which this file is attached. attachmentId: id of the file on the amazon S3 that is to be served. version: version of the file on the amazon S3 that the user is requesting. """ authinfo = request.getSession(IAuthInfo) myId = authinfo.username myOrgId = authinfo.organization itemId = utils.getRequestArg(request, "id", sanitize=False) attachmentId = utils.getRequestArg(request, "fid", sanitize=False) version = utils.getRequestArg(request, "ver", sanitize=False) or '' columns = ["meta", "attachments", "participants"] if not (itemId and attachmentId): raise errors.MissingParams([]) item = yield db.get_slice(itemId, "mConversations", columns) item = utils.supercolumnsToDict(item) if not item: raise errors.InvalidMessage(itemId) if myId not in item.get('participants', {}): raise errors.MessageAccessDenied(itemId) # Check if the attachmentId belong to item if attachmentId not in item['attachments'].keys(): raise errors.InvalidAttachment(itemId, attachmentId, version) fileId, filetype, name = None, 'text/plain', 'file' if version: version = utils.decodeKey(version) try: cols = yield db.get(attachmentId, "attachmentVersions", version) except ttypes.NotFoundException: raise errors.InvalidAttachment(itemId, attachmentId, version) except ttypes.InvalidRequestException: raise errors.InvalidAttachment(itemId, attachmentId, version) cols = utils.columnsToDict([cols]) else: cols = yield db.get_slice(attachmentId, "attachmentVersions", count=1, reverse=True) cols = utils.columnsToDict(cols) version = cols.keys()[0] fileId, name, size, filetype = cols[version].split(':') files = yield db.get_slice(fileId, "files", ["meta"]) files = utils.supercolumnsToDict(files) url = files['meta']['uri'] owner = files["meta"]["owner"] defer.returnValue([owner, url, filetype, size, name])
def _tags(self, request, term): if len(term) < 2: request.write("[]") return orgId = request.getSession(IAuthInfo).organization finish = _getFinishTerm(term) itemId = utils.getRequestArg(request, "itemId") if not itemId: request.write("[]") return toFetchTags = set() d1 = db.get_slice(orgId, "orgTagsByName", start=term, finish=finish, count=10) tags = [] matchedTags = yield d1 matchedTags = [match.column.value for match in matchedTags] if matchedTags: matchedTags = yield db.get_slice(orgId, "orgTags", matchedTags) matchedTags = utils.supercolumnsToDict(matchedTags) for tagId in matchedTags: tags.append({"title": matchedTags[tagId]["title"], "id": tagId}) tags.sort(key=itemgetter("title")) output = [] template = self._singleLineTemplate for tag in tags: data = {"title": tag["title"], "meta": ""} output.append({"value": tag["title"], "label": template % data, "href": "/tags?id=%s" % tag["id"]}) request.write(json.dumps(output))
def deleteFileInfo(myId, orgId, itemId, item, conv=None): if 'parent' in item['meta']: if not conv: conv = yield db.get_slice(item['meta']['parent'], 'items', ['meta']) conv = utils.supercolumnsToDict(conv) convId = item['meta']['parent'] else: conv = item convId = itemId acl = pickle.loads(conv['meta']['acl']) allowedGroups = acl.get('accept', {}).get('groups', []) deniedGroups = acl.get('deny', {}).get('groups', []) groups = [x for x in allowedGroups if x not in deniedGroups] allowedOrgs = acl.get('accept', {}).get('orgs', []) ownerId = conv['meta']['owner'] entityIds = [myId] entityIds.extend(groups) entityIds.extend(allowedOrgs) entityIds_ = yield utils.expandAcl(myId, orgId, conv['meta']['acl'], convId, ownerId, True) entityIds.extend(entityIds_) deferreds = [] for attachmentId in item.get('attachments', {}): col = yield db.get_slice(attachmentId, 'attachmentVersions', count=1) tuuid = col[0].column.name deferreds.append(db.remove(myId, "user_files", tuuid)) #TODO: use batch remove/batch mutate for entityId in entityIds: deferreds.append(db.remove(entityId, "entityFeed_files", tuuid)) if deferreds: yield defer.DeferredList(deferreds)
def _listPresetTags(self, request): (appchange, script, args, myId) = yield self._getBasicArgs(request) orgId = args["orgId"] landing = not self._ajax args['title'] = 'Preset Tags' args['menuId'] = 'tags' args["viewType"] = "tags" if script and landing: t.render(request, "admin.mako", **args) if script and appchange: t.renderScriptBlock(request, "admin.mako", "layout", landing, "#mainbar", "set", **args) presetTags = yield db.get_slice(orgId, "orgPresetTags", count=100) presetTags = utils.columnsToDict(presetTags, ordered=True).values() if presetTags: tags_ = yield db.get_slice(orgId, "orgTags", presetTags) tags_ = utils.supercolumnsToDict(tags_) else: tags_ = {} args['tagsList'] = presetTags args['tags'] = tags_ if script: t.renderScriptBlock(request, "admin.mako", "list_tags", landing, "#content", "set", **args) if not script: t.render(request, "admin.mako", **args)
def reindexItems(): items = {} fetchedItems = yield db.get_range_slice('items', count=10000, reverse=True) for row in fetchedItems: items[row.key] = utils.supercolumnsToDict(row.columns) log.msg("Total items:", len(fetchedItems)) for i, row in enumerate(fetchedItems): itemId = row.key item = items[itemId] log.msg(i+1, itemId) if 'meta' not in item or 'owner' not in item['meta']: continue owner = item['meta']['owner'] try: col = yield db.get(owner, "entities", "org", "basic") ownerOrgId = col.column.value except: log.msg("Error when indexing:", itemId) continue parentId = item['meta'].get('parent', None) if not parentId: yield search.solr.updateItemIndex(itemId, item, ownerOrgId) else: yield search.solr.updateItemIndex(itemId, item, ownerOrgId, conv=items[parentId])
def _getPresence(self, request): authInfo = request.getSession(IAuthInfo) orgId = authInfo.organization myId = authInfo.username data = [] cols = yield db.get_slice(orgId, "presence") cols = utils.supercolumnsToDict(cols) if myId not in cols: myPresence = yield db.get_slice(orgId, "presence", super_column=myId) cols[myId] = utils.columnsToDict(myPresence) presence = {} for userId in cols: presence[userId] = getMostAvailablePresence(cols[userId].values()) if presence[myId] == PresenceStates.OFFLINE: request.write(json.dumps(data)) return userIds = cols.keys() entities = base.EntitySet(userIds) yield entities.fetchData() for entityId in entities.keys(): entity = entities[entityId] _data = {"userId": entityId, "name": entity.basic['name'], "status": presence.get(entityId, PresenceStates.OFFLINE), "title": entity.basic["jobTitle"], "avatar": utils.userAvatar(entityId, entity, 's')} data.append(_data) request.write(json.dumps(data))
def _addPresetTag(self, request): orgId = request.getSession(IAuthInfo).organization tagNames = utils.getRequestArg(request, 'tag') if not tagNames: return invalidTags = [] tagNames = [x.strip().decode('utf-8', 'replace') for x in tagNames.split(',')] for tagName in tagNames: if len(tagName) < 50 and regex.match('^[\w-]*$', tagName): yield tags.ensureTag(request, tagName, orgId, True) else: invalidTags.append(tagName) presetTags = yield db.get_slice(orgId, "orgPresetTags") presetTags = utils.columnsToDict(presetTags, ordered=True).values() tags_ = yield db.get_slice(orgId, "orgTags", presetTags) tags_ = utils.supercolumnsToDict(tags_) args = {'tags': tags_, 'tagsList': presetTags} handlers = {} if invalidTags: if len(invalidTags) == 1: message = " %s is invalid tag." % (invalidTags[0]) else: message = " %s are invalid tags. " % (",".join(invalidTags)) errorMsg = "%s <br/>Tag can contain alpha-numeric characters or hyphen only. It cannot be more than 50 characters" % (message) handlers = {'onload': "$$.alerts.error('%s')" % (errorMsg)} t.renderScriptBlock(request, "admin.mako", "list_tags", False, "#content", "set", True, handlers=handlers, **args)
def delete(itemId, item, myId, orgId): convId = item["meta"].get("parent", itemId) itemOwnerId = item["meta"]["owner"] if itemId == convId: conv = item convOwnerId = itemOwnerId else: conv = yield db.get_slice(convId, "items", ["meta", "tags"]) conv = utils.supercolumnsToDict(conv) if not conv: raise errors.InvalidRequest(_('Conversation does not exist!')) convOwnerId = conv["meta"]["owner"] # TODO: Admin actions. # Do I have permission to delete the comment if (itemOwnerId != myId and convOwnerId != myId): raise errors.PermissionDenied(_("You must either own the comment or the conversation to delete this comment")) deferreds = [] convType = conv["meta"].get('type', 'status') convACL = conv["meta"]["acl"] timestamp = str(int(time.time())) itemUUID = item["meta"]["uuid"] # The conversation is lazy deleted. # If it is the comment being deleted, rollback all feed updates # that were made due to this comment and likes on this comment. d = deleteItem(itemId, myId, orgId, item, conv) deferreds.append(d) yield defer.DeferredList(deferreds) defer.returnValue(conv)
def _renderClientDetails(self, request, clientId): (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax args["detail"] = "apps" if script and landing: t.render(request, "apps.mako", **args) if appchange and script: t.renderScriptBlock(request, "apps.mako", "layout", landing, "#mainbar", "set", **args) client = yield db.get_slice(clientId, "apps") client = utils.supercolumnsToDict(client) if not client: raise errors.InvalidApp(clientId) args.update({"client": client, "clientId": clientId}) if script: self.setTitle(request, client["meta"]["name"]) author = base.Entity(client["meta"]["author"]) yield author.fetchData() args["entities"] = base.EntitySet(author) if script: t.renderScriptBlock(request, "apps.mako", "appDetails", landing, "#apps-contents", "set", **args) else: t.render(request, "apps.mako", **args)
def pushfileinfo(myId, orgId, itemId, item, conv=None): if 'parent' in item['meta']: if not conv: conv = yield db.get_slice(item['meta']['parent'], "items", ["meta"]) conv = utils.supercolumnsToDict(conv) convId = item['meta']['parent'] else: convId = itemId conv = item acl = pickle.loads(conv['meta']['acl']) allowedGroups = acl.get('accept', {}).get('groups', []) deniedGroups = acl.get('deny', {}).get('groups', []) groups = [x for x in allowedGroups if x not in deniedGroups] allowedOrgs = acl.get('accept', {}).get('orgs', []) ownerId = conv['meta']['owner'] entityIds = [myId] entityIds.extend(groups) entityIds.extend(allowedOrgs) entityIds_ = yield utils.expandAcl(myId, orgId, conv['meta']['acl'], convId, ownerId, True) entityIds.extend(entityIds_) for attachmentId in item.get('attachments', {}): name, size, ftype = item['attachments'][attachmentId].split(':') cols = yield db.get_slice(attachmentId, "attachmentVersions", count=1) tuuid = cols[0].column.name value = '%s:%s:%s:%s' % (attachmentId, name, itemId, ownerId) #TODO: use batch remove/batch mutate yield db.insert(myId, "user_files", value, tuuid) for entityId in entityIds: yield db.insert(entityId, "entityFeed_files", value, tuuid)
def fetchData(self, args, convId=None, userId=None, columns=[]): convId = convId or args["convId"] myId = userId or args.get("myId", None) conv = yield db.get_slice(convId, "items", ['options', 'counts'].extend(columns)) conv = utils.supercolumnsToDict(conv, True) conv.update(args.get("items", {}).get(convId, {})) options = conv["options"] if "options" in conv else None if not options: raise errors.InvalidRequest("The poll does not have any options") myVote = yield db.get_slice(myId, "userVotes", [convId]) myVote = myVote[0].column.value if myVote else None startTime = conv['meta'].get('start', None) endTime = conv['meta'].get('end', None) showResults = conv['meta'].get('showResults', 'True') == True if not showResults: # FIX: endTime is String. convert to time if not endTime or time.gmtime() > endTime: showResults = "True" args.setdefault("items", {})[convId] = conv args.setdefault("myVotes", {})[convId] = myVote args.setdefault("showResults", {})[convId] = showResults defer.returnValue(set())
def _listFileVersions(self, request): authinfo = request.getSession(IAuthInfo) myId = authinfo.username myOrgId = authinfo.organization relation = Relation(myId, []) attachmentId = utils.getRequestArg(request, "fid", sanitize=False) itemId, item = yield utils.getValidItemId(request, 'id') if not attachmentId: raise errors.MissingParams() # Check if the attachmentId belong to item if attachmentId not in item.get('attachments', {}).keys(): version = None raise errors.AttachmentAccessDenied(itemId, attachmentId, version) #get the latest file files = [] cols = yield db.get_slice(attachmentId, "attachmentVersions", reverse=True) cols = utils.supercolumnsToDict(cols) for attachmentId in cols: fileId, ftype, name = None, 'text/plain', 'file' for tuuid in cols[attachmentId]: fileId, name, size, ftype = cols[attachmentId][tuuid].split(':') files.append([itemId, attachmentId, name, size, ftype]) ##TODO: use some widget to list the files request.write(json.dumps(files))
def _to_python(self, itemId, state): itemType = self.itemType if self.itemType else 'item' if not itemId or not itemId[0]: raise MissingParam('%s-id' % (itemType), itemId, state) itemId = itemId[0] columns = set(['meta']).update(self.columns) item = yield db.get_slice(itemId, "items", columns) if not item: raise InvalidItem(itemType, itemId, state) item = utils.supercolumnsToDict(item) meta = item["meta"] if self.itemType and meta["type"] != self.itemType: raise InvalidItem(itemType, itemId, state) parentId = meta.get("parent", None) if parentId: parent = yield db.get_slice(parentId, "items", ["meta"]) parent = utils.supercolumnsToDict(parent) else: parent = item acl = parent["meta"]["acl"] owner = parent["meta"]["owner"] #parent is deleted deleted = parent['meta'].get('state', None) == 'deleted' #item is deleted deleted = deleted or meta.get('state', None) == 'deleted' if deleted: raise InvalidItem(itemType, itemId, state) if self.source != 'api': request = state.request authInfo = request.getSession(IAuthInfo) myId = authInfo.username orgId = authInfo.organization isOrgAdmin = authInfo.isAdmin isOrgAdmin = self.checkAdmin and isOrgAdmin relation = Relation(myId, []) yield relation.initGroupsList() if not utils.checkAcl(myId, orgId, isOrgAdmin, relation, parent['meta']): raise ItemAccessDenied(itemType, itemId, state) defer.returnValue((itemId, item))
def delete(self, myId, convId, conv): log.debug("plugin:delete", convId) user_tuids = {} # Get the list of every user who responded to this event res = yield db.get_slice(convId, "eventResponses") attendees = [x.column.name.split(":", 1)[1] for x in res] # Add all the invited people of the item res = yield db.get_slice(convId, "items", ['invitees']) res = utils.supercolumnsToDict(res) attendees.extend(res["invitees"].keys()) invitedPeople = res["invitees"].keys() log.debug("Maps", ["%s:%s"%(uId, convId) for \ uId in attendees]) # Get the Org and GroupIds if any. convMeta = conv["meta"] groupIds = convMeta["target"].split(",") if "target" in convMeta else [] attendees.extend(groupIds+[convMeta["org"]]) log.debug("Attendees", attendees) # Get the timeuuids that were inserted for this user res = yield db.multiget_slice(["%s:%s"%(uId, convId) for \ uId in attendees], "userAgendaMap") res = utils.multiColumnsToDict(res) for k, v in res.iteritems(): uid = k.split(":", 1)[0] tuids = v.keys() if tuids: user_tuids[uid] = tuids log.debug("userAgenda Removal", user_tuids) # Delete their entries in the user's list of event entries for attendee in user_tuids: yield db.batch_remove({'userAgenda': [attendee]}, names=user_tuids[attendee]) # Delete invitation entries for invited people invited_tuids = dict([[x, user_tuids[x]] for x in invitedPeople]) log.debug("userAgenda Invitation Removal", invited_tuids) for attendee in invited_tuids: yield db.batch_remove({'userAgenda': ['%s:%s' %(attendee, 'I')]}, names=invited_tuids[attendee]) log.debug("eventResponses Removal", convId) # Delete the event's entry in eventResponses yield db.remove(convId, "eventResponses") log.debug("userAgendaMap Removal", user_tuids) # Delete their entries in userAgendaMap for attendee in user_tuids: yield db.batch_remove({'userAgendaMap': ["%s:%s"%(attendee, convId)]}, names=user_tuids[attendee])
def fetchData(self, columns=None): if columns == None: columns = ['basic'] if columns == []: data = yield db.get_slice(self.id, "entities") else: data = yield db.get_slice(self.id, "entities", columns) data = utils.supercolumnsToDict(data) self._data = data
def reindexProfileContent(): rows = yield db.get_range_slice('entities', count=1000) for row in rows: entityId = row.key log.msg(entityId) entity = Entity(entityId, utils.supercolumnsToDict(row.columns)) if entity.basic.get('type', '') == 'user': orgId = entity.basic.get('org', '') if orgId: yield search.solr.updatePeopleIndex(entityId, entity, orgId)
def _addMembers(self, request): """This method add a new user to this conversation. Keyword Arguments: newMembers: A list of members who will be added to this conversation. convId: The id of the conversation to which these new members will be added as participants. CF Changes: mConversations """ myId = request.getSession(IAuthInfo).username orgId = request.getSession(IAuthInfo).organization newMembers, body, subject, convId = self._parseComposerArgs(request) if not (convId and newMembers): raise errors.MissingParams(['Recipient']) conv = yield db.get_slice(convId, "mConversations") conv = utils.supercolumnsToDict(conv) subject = conv['meta'].get('subject', None) participants = set(conv.get('participants', {}).keys()) if not conv: raise errors.InvalidMessage(convId) if myId not in participants: raise errors.MessageAccessDenied(convId) #cols = yield db.multiget_slice(newMembers, "entities", ['basic']) #people = utils.multiSuperColumnsToDict(cols) people = base.EntitySet(newMembers) yield people.fetchData() newMembers = set([userId for userId in people.keys() \ if people[userId].basic and \ people[userId].basic["org"] == orgId]) newMembers = newMembers - participants mailNotificants = participants - set([myId]) toFetchEntities = mailNotificants.union([myId, orgId]).union(newMembers) entities = base.EntitySet(toFetchEntities) yield entities.fetchData() data = {"entities": entities} data["orgId"] = orgId data["convId"] = convId data["subject"] = subject data["_fromName"] = entities[myId].basic['name'] if newMembers: data["message"] = conv["meta"]["snippet"] newMembers = dict([(userId, '') for userId in newMembers]) yield db.batch_insert(convId, "mConversations", {'participants': newMembers}) yield self._deliverMessage(convId, newMembers, conv['meta']['uuid'], conv['meta']['owner']) yield notifications.notify(newMembers, ":NM", myId, **data) if mailNotificants and newMembers: data["addedMembers"] = newMembers yield notifications.notify(mailNotificants, ":MA", myId, **data)
def updateData(): convIds = set() rows = yield db.get_range_slice('item_files', count=1000) for row in rows: convId = row.key convIds.add(convId) attachments = utils.supercolumnsToDict(row.columns) for attachmentId in attachments: for timeuuid in attachments[attachmentId]: encodedTimeUUID, aid, name, size, ftype = attachments[attachmentId][timeuuid].split(':') yield db.insert(attachmentId, "attachmentVersions", "%s:%s:%s:%s" %(aid, name, size, ftype), timeuuid) rows = yield db.get_range_slice('items', count=10000) for row in rows: itemId = row.key item = utils.supercolumnsToDict(row.columns) attachments = {} for attachmentId in item.get('attachments', {}): if len(item['attachments'][attachmentId].split(':')) == 4: x,name, size, ftype = item['attachments'][attachmentId].split(':') attachments[attachmentId] = "%s:%s:%s" %(name, size, ftype) if attachments: yield db.remove(itemId, 'items', super_column='attachments') yield db.batch_insert(itemId, "items", {"attachments": attachments}) rows = yield db.get_range_slice('mConversations', count=10000) for row in rows: messageId = row.key message = utils.supercolumnsToDict(row.columns) attachments = {} print messageId for attachmentId in message.get('attachments', {}): if len(message['attachments'][attachmentId].split(':')) == 4: x,name, size, ftype = message['attachments'][attachmentId].split(':') attachments[attachmentId] = "%s:%s:%s" %(name, size, ftype) if attachments: yield db.remove(messageId, 'mConversations', super_column='attachments') yield db.batch_insert(messageId, "mConversations", {"attachments": attachments})
def _to_python(self, tagId, state): if not tagId: raise MissingParam('Tag-id', tagId, state) if self.source != 'api': request = state.request orgId = request.getSession(IAuthInfo).organization tag = yield db.get_slice(orgId, "orgTags", [tagId]) if not tag: raise InvalidTag(tagId) tag = utils.supercolumnsToDict(tag) defer.returnValue((tagId, tag))
def _removeFromPending(group, user): "" yield db.remove(group.id, "pendingConnections", "GI:%s" % (user.id)) yield db.remove(user.id, "pendingConnections", "GO:%s" % (group.id)) #also remove any group invites yield db.remove(user.id, "pendingConnections", "GI:%s" % (group.id)) cols = yield db.get_slice(group.id, "latest", ['groups']) cols = utils.supercolumnsToDict(cols) for tuuid, key in cols.get('groups', {}).items(): if key == user.id: yield db.remove(group.id, "latest", tuuid, 'groups')
def _moveConversation(self, request, convIds, toFolder): """Move a conversation or conversations from one folder to another. Keyword Arguments: convIds: List of conversation ids which need to be moved. toFolder: The final destination of the above conversations CF Changes: mConvFolders mUnreadConversations mAllConversations mDeletedConversations """ myId = request.getSession(IAuthInfo).username convs = yield db.multiget_slice(convIds, "mConversations") convs = utils.multiSuperColumnsToDict(convs) for convId in convs: conv = convs.get(convId, {}) if not conv: raise errors.InvalidMessage(convId) if myId not in conv.get('participants', {}): raise errors.MessageAccessDenied(convId) timeUUID = conv['meta']['uuid'] val = "%s:%s" % ('u' if toFolder == 'unread' else 'r', convId) cols = yield db.get_slice(convId, 'mConvFolders', [myId]) cols = utils.supercolumnsToDict(cols) for folder in cols[myId]: cf = self._folders[folder] if folder in self._folders else folder if toFolder != 'unread': if folder != 'mUnreadConversations': col = yield db.get(myId, cf, timeUUID) val = col.column.value yield db.remove(myId, cf, timeUUID) yield db.remove(convId, "mConvFolders", cf, myId) else: yield db.insert(myId, cf, "u:%s" % (convId), timeUUID) if toFolder == 'unread': val = "u:%s" % (convId) yield db.insert(convId, 'mConvFolders', '', 'mUnreadConversations', myId) yield db.insert(myId, 'mUnreadConversations', val, timeUUID) else: folder = self._folders[toFolder] yield db.insert(myId, folder, val, timeUUID) yield db.insert(convId, 'mConvFolders', '', folder, myId)
def getReason(self, convId, requesters, userId): conv = yield db.get_slice(convId, "items", ["meta"]) conv = utils.supercolumnsToDict(conv) cols = yield db.multiget_slice(requesters, "entities", ["basic"]) entities = utils.multiSuperColumnsToDict(cols) foo = requesters[0] if conv["meta"]["subType"] == "connection": reasonStr = '%s accepted your friend request' \ %(utils.userName(foo, entities[foo])) elif conv["meta"]["subType"] == "pendingConnection": reasonStr = "%s sent a friend request." \ "Click <a href='/profile?id=%s'>here </a> to respond" \ %(utils.userName(foo, entities[foo]), foo) defer.returnValue(reasonStr)
def _members(self, request): """Allow a participant of a conversation to add or remove another user to the conversation. Keyword arguments: action: Either one of [add, remove] convId: The conversation to which this user wants to either add or remove another user. """ (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax action = utils.getRequestArg(request, "action") convId = utils.getRequestArg(request, 'parent') or None if not convId: raise errors.MissingParams([_('Conversation Id')]) if action not in ('add', 'remove'): raise errors.InvalidRequest() if action == "add": yield self._addMembers(request) elif action == "remove": yield self._removeMembers(request) cols = yield db.get_slice(convId, "mConversations") conv = utils.supercolumnsToDict(cols) participants = set(conv['participants']) people = base.EntitySet(participants) yield people.fetchData() args.update({"people": people}) args.update({"conv": conv}) args.update({"id": convId}) args.update({"view": "message"}) if script: onload = """ $('#conversation_add_member').autocomplete({ source: '/auto/users', minLength: 2, select: function( event, ui ) { $('#conversation_recipients').attr('value', ui.item.uid) } }); """ t.renderScriptBlock(request, "message.mako", "right", landing, ".right-contents", "set", True, handlers={"onload": onload}, **args)
def deleteAvatarItem(entity, isLogo=False): entity = yield db.get_slice(entity, "entities", ["basic"]) entity = utils.supercolumnsToDict(entity) itemId = None imgFmt = None col = None if not entity: defer.returnValue(None) if isLogo: col = entity["basic"].get("logo", None) else: col = entity["basic"].get("avatar", None) if col: imgFmt, itemId = col.split(":") if itemId: yield db.remove(itemId, "items")
def _delete(self, request): authinfo = request.getSession(IAuthInfo) myId = authinfo.username myOrgId = authinfo.organization clientId = utils.getRequestArg(request, "id", sanitize=False) client = yield db.get_slice(clientId, "apps") client = utils.supercolumnsToDict(client) if not client: raise errors.InvalidApp(clientId) if client["meta"]["author"] != myId: raise errors.AppAccessDenied(clientId) yield db.remove(clientId, "apps") yield db.remove(myId, "appsByOwner", clientId) yield db.remove(myOrgId, "appsByOwner", clientId)
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 _secret(self, request): myId = request.getSession(IAuthInfo).username clientId = utils.getRequestArg(request, "id", sanitize=False) client = yield db.get_slice(clientId, "apps") client = utils.supercolumnsToDict(client) if not client: raise errors.InvalidApp(clientId) if client["meta"]["author"] != myId: raise errors.AppAccessDenied(clientId) clientSecret = utils.getRandomKey() yield db.insert(clientId, "apps", utils.hashpass(clientSecret), "secret", "meta") args = {"clientId": clientId, "client": client["meta"], "info": "New application secret was generated"} args["client"]["secret"] = clientSecret t.renderScriptBlock(request, "apps.mako", "registrationResults", False, "#apps-contents", "set", **args)