Exemple #1
0
    def getRecentVisionActivity(user):
        """Get list of visions we want to display from the recent activities"""

        # Get list of potential visions
        activities = DataApi.getRecentActivities()

        commentIds = set([a.objectId for a in activities if a.action == Activity.Action.COMMENT_ON_VISION])
        commentModels = DataApi.getVisionCommentsById(commentIds)
        idToComment = dict([(c.id, c) for c in commentModels])

        visionIds = set([a.objectId for a in activities if a.action == Activity.Action.ADD_VISION])
        for c in commentModels:
            visionIds.add(c.visionId)
        visionModels = DataApi.getVisionsById(visionIds, allowRemovedVisions=True)
        idToVision = dict([(v.id, v) for v in visionModels])

        # Now create ordered list without visions that have a duplicate
        # root vision, and without private visions
        rootIds = set()
        orderedVisions = []
        for a in activities:
            vision = None
            if a.action == Activity.Action.ADD_VISION:
                if a.objectId in idToVision:
                    vision = idToVision[a.objectId]
            elif a.action == Activity.Action.COMMENT_ON_VISION:
                if a.objectId in idToComment:
                    comment = idToComment[a.objectId]
                    if comment.visionId in idToVision:
                        vision = idToVision[comment.visionId]

            if (
                vision
                and vision.removed == False
                and vision.privacy == VisionPrivacy.PUBLIC
                and vision.rootId not in rootIds
            ):
                orderedVisions.append(vision)
                rootIds.add(vision.rootId)
        return VisionList(orderedVisions)
Exemple #2
0
    def getUserFeed(user):
        # Get set of all userIds to fetch activities from
        # This includes following ids, and own userId
        follows = user.getFollows()
        userIds = set()
        for f in follows.followList():
            userIds.add(f.userId())
        userIds.add(user.id())

        # Fetch activities
        activities = DataApi.getActivities(userIds)

        # Fetch all objects and create dictonary objects now. Might as well
        # here for now since we don't need anything else yet.
        # TODO: probably will need to change later

        followIds = set([a.objectId for a in activities if a.action == Activity.Action.FOLLOW])
        commentLikeIds = set([a.objectId for a in activities if a.action == Activity.Action.LIKE_VISION_COMMENT])
        visionLikeIds = set([a.objectId for a in activities if a.action == Activity.Action.LIKE_VISION])
        followModels = DataApi.getFollowsById(followIds)
        commentLikeModels = DataApi.getVisionCommentLikesById(commentLikeIds)
        visionLikeModels = DataApi.getVisionLikesById(visionLikeIds)

        idToFollow = dict([(f.id, f) for f in followModels])
        idToCommentLike = dict([(l.id, l) for l in commentLikeModels])
        idToVisionLike = dict([(l.id, l) for l in visionLikeModels])

        commentIds = set([a.objectId for a in activities if a.action == Activity.Action.COMMENT_ON_VISION])
        for l in commentLikeModels:
            commentIds.add(l.visionCommentId)
        commentModels = DataApi.getVisionCommentsById(commentIds)
        idToComment = dict([(c.id, c) for c in commentModels])

        visionIds = set([a.objectId for a in activities if a.action == Activity.Action.ADD_VISION])
        for c in commentModels:
            visionIds.add(c.visionId)
        for l in visionLikeModels:
            visionIds.add(l.visionId)
        unfilteredVisionsModels = DataApi.getVisionsById(visionIds, allowRemovedVisions=True)

        userIds = set([a.subjectId for a in activities if a.action == Activity.Action.JOIN_SITE])
        for c in commentModels:
            userIds.add(c.authorId)
        for l in visionLikeModels:
            userIds.add(l.userId)
        for l in commentLikeModels:
            userIds.add(l.userId)
        for f in followModels:
            userIds.add(f.followerId)
            userIds.add(f.userId)
        userModels = DataApi.getUsersFromIds(userIds)
        idToUser = dict([(u.id, u) for u in userModels])

        # At this point we have all the visions models for the activities
        # We need to remove the visions that we shouldn't allow this user to
        # see
        filteredVisionModels = []
        for vision in unfilteredVisionsModels:
            # vision should not be removed, and be either public, or a
            # private vision you own
            if vision.removed == False and (vision.privacy == VisionPrivacy.PUBLIC or vision.userId == user.id()):
                filteredVisionModels.append(vision)
        visionList = VisionList(filteredVisionModels)

        # Now we have a list of all
        visionObjs = visionList.toDictionary(
            options=[
                Vision.Options.PICTURE,
                Vision.Options.USER,
                Vision.Options.PARENT_USER,
                Vision.Options.COMMENTS,
                Vision.Options.COMMENT_PICTURES,
                Vision.Options.LIKES,
                Vision.Options.COMMENT_LIKES,
            ],
            user=user,
        )
        idToVisionObj = dict([(v[Vision.Key.ID], v) for v in visionObjs])

        # Now build activities!
        feedObjList = []
        visionIdToFeedObj = dict()
        for activity in activities:
            # JOIN SITE
            if activity.action == Activity.Action.JOIN_SITE:
                # Already have enough info to render this activity
                newUser = User(idToUser[activity.subjectId])

                obj = dict()
                obj[Activity.Key.TYPE] = Activity.FeedItem.JOIN
                obj[Activity.Key.USER] = newUser.toDictionary()
                obj[Activity.Key.CREATED] = activity.created.isoformat()

                feedObjList.append(obj)
            # FOLLOW
            elif activity.action == Activity.Action.FOLLOW:
                follow = idToFollow[activity.objectId]
                follower = User(idToUser[follow.followerId])
                following = User(idToUser[follow.userId])

                obj = dict()
                obj[Activity.Key.TYPE] = Activity.FeedItem.FOLLOW
                obj[Activity.Key.USER] = follower.toDictionary()
                obj[Activity.Key.FOLLOWING] = following.toDictionary()
                obj[Activity.Key.CREATED] = activity.created.isoformat()

                feedObjList.append(obj)
            # VISION-RELATED
            elif (
                activity.action == Activity.Action.ADD_VISION
                or activity.action == Activity.Action.LIKE_VISION
                or activity.action == Activity.Action.COMMENT_ON_VISION
                or activity.action == Activity.Action.LIKE_VISION_COMMENT
            ):

                # get vision we are working with
                visionId = None
                if activity.action == Activity.Action.ADD_VISION:
                    visionId = activity.objectId
                elif activity.action == Activity.Action.LIKE_VISION:
                    visionId = idToVisionLike[activity.objectId].visionId
                elif activity.action == Activity.Action.COMMENT_ON_VISION:
                    visionId = idToComment[activity.objectId].visionId
                elif activity.action == Activity.Action.LIKE_VISION_COMMENT:
                    commentLike = idToCommentLike[activity.objectId]
                    visionId = idToComment[commentLike.visionCommentId].visionId

                # if we don't know vision id from here, ignore this activity
                if visionId == None:
                    continue

                # Get the vision object we will be working with
                if visionId in visionIdToFeedObj:
                    obj = visionIdToFeedObj[visionId]
                else:
                    if visionId in idToVisionObj:
                        obj = dict()
                        actionString = Activity.ACTION_STRING[activity.action]
                        obj[Activity.Key.TYPE] = Activity.FeedItem.VISION
                        obj[Activity.Key.VISION] = idToVisionObj[visionId]
                        obj[Activity.Key.RECENT_ACTION] = actionString
                        obj[Activity.Key.CREATED] = activity.created.isoformat()

                        feedObjList.append(obj)
                        visionIdToFeedObj[visionId] = obj
                    else:
                        continue

                # now append whatever we can to feed obj based on activity type
                # Vision-related objects fill in these keys:
                #   VISION - the vision we are working with (with USER in it)
                #            (this should already be filled in)
                #   NEW_VISION - T/F if ADD_VISION activity is seen
                #   LIKER - list of Users that liked vision
                #   COMMENTS - list of comments with AUTHOR
                #   COMMENT_LIKES - list of comment likes info:
                #       USER - user that liked the comment
                #       COMMENT - comment info
                if activity.action == Activity.Action.ADD_VISION:
                    obj[Activity.Key.NEW_VISION] = True
                elif activity.action == Activity.Action.LIKE_VISION:
                    # get liker
                    liker = User(idToUser[activity.subjectId])

                    if Activity.Key.LIKERS in obj:
                        # If a likers list exists, find out if user is already
                        # in the list. Only add if the user isn't
                        existing = [l["id"] for l in obj[Activity.Key.LIKERS]]
                        if liker.id() not in existing:
                            likerObj = liker.toDictionary()
                            obj[Activity.Key.LIKERS].append(likerObj)
                    else:
                        # Else, create the list and enter the liker into it
                        obj[Activity.Key.LIKERS] = [liker.toDictionary()]
                elif activity.action == Activity.Action.COMMENT_ON_VISION:
                    # get comment
                    comment = VisionComment(idToComment[activity.objectId])

                    if Activity.Key.COMMENTS in obj:
                        existing = [c["id"] for c in obj[Activity.Key.COMMENTS]]
                        if comment.id() not in existing:
                            commentObj = comment.toDictionary()
                            author = User(idToUser[comment.authorId()])
                            authorObj = author.toDictionary()
                            commentObj[VisionComment.Key.AUTHOR] = authorObj
                            obj[Activity.Key.COMMENTS].append(commentObj)
                    else:
                        commentObj = comment.toDictionary()
                        author = User(idToUser[comment.authorId()])
                        authorObj = author.toDictionary()
                        commentObj[VisionComment.Key.AUTHOR] = authorObj
                        obj[Activity.Key.COMMENTS] = [commentObj]
                elif activity.action == Activity.Action.LIKE_VISION_COMMENT:
                    # get liker
                    commentLike = idToCommentLike[activity.objectId]
                    liker = User(idToUser[commentLike.userId])

                    if Activity.Key.COMMENT_LIKERS in obj:
                        # If a likers list exists, find out if user is already
                        # in the list. Only add if the user isn't
                        existing = [l["id"] for l in obj[Activity.Key.COMMENT_LIKERS]]
                        if liker.id() not in existing:
                            obj[Activity.Key.COMMENT_LIKERS].append(likerObj)
                    else:
                        # Else, create the list and enter the liker into it
                        likerObj = liker.toDictionary()
                        obj[Activity.Key.COMMENT_LIKERS] = [likerObj]
                else:
                    pass
        return feedObjList

        """