Ejemplo n.º 1
0
    def deleteOne(cls, req, res):
        if not req.user:
            res.status = 403
            return

        bleat = Bleat.findOne(req.params)
        if not bleat:
            return

        if req.user.id != bleat.user:
            res.status = 403
            return

        bleat.erase()
        return True
Ejemplo n.º 2
0
    def createOne(cls, req, res):
        if not req.user:
            res.status = 403
            return

        if not "content" in req.body or len(req.body["content"]) < 1:
            res.status = 400
            return

        req.body["user"] = req.user.id
        req.body["timestamp"] = datetime.utcnow()

        bleat = super(BleatController, cls).createOne(req, res)

        # Find all mentioned users that want notifications
        usersToNotify = {}
        for userId in bleat.mentions:
            user = User.findOne({"id": userId})
            if user.notifyOnMention and not user.isDisabled:
                usersToNotify[user.id] = user

        # Find out who we're replying to and send a notification email if needed
        if bleat.inReplyTo:
            inReplyTo = Bleat.findOne({"id": bleat.inReplyTo})
            inReplyToUser = User.findOne({"id": inReplyTo.user})

            if inReplyToUser.notifyOnReply and not inReplyToUser.isDisabled:
                if inReplyToUser.id in usersToNotify:
                    del usersToNotify[
                        inReplyToUser.
                        id]  # Don't send two emails to a single person

                sendEmail(
                    inReplyToUser.email, "Bitter Bleat Reply Notification",
                    u"{0} has responded to you in their bleat:\n{1}\n\n{2}".
                    format(req.user.name or req.user.username, bleat.content,
                           u"{0}/bleat/{1}".format(req.baseUri, bleat.id)))

        # Send the mentioned notifications
        for user in usersToNotify.values():
            sendEmail(
                user.email, "Bitter Mention Notification",
                u"{0} has mentioned you in their bleat:\n{1}\n\n{2}".format(
                    req.user.name or req.user.username, bleat.content,
                    u"{0}/bleat/{1}".format(req.baseUri, bleat.id)))

        return bleat
Ejemplo n.º 3
0
                bleatId = bleatId.decode("utf8").strip()
                if not bleatId:
                    continue

                if not bleatId in bleats:
                    print "User {0} has unknown bleat: {1}".format(username, bleatId)
                    continue

                if bleats[bleatId]["username"] != username:
                    print "Bleat {0} has inconsistent username: {1} != {2}".format(bleatId, bleats[bleatId]["username"], username)

                bleats[bleatId]["user"] = user.id
                del bleats[bleatId]["username"]

    # Now all users have been created, the listens can be added
    for user in users.values():
        if getattr(user, "listeningTo", ""):
            user.listeningTo = map(lambda username: users[username].id, user.listeningTo.split())
            user.save()

    # Import all the bleats in chronological order
    for bleatId in sorted(bleats.keys(), key = lambda bleat: bleats[bleat]["timestamp"]):
        if not "user" in bleats[bleatId]:
            print "Bleat {0} is orphaned".format(bleatId)
            continue

        if "inReplyTo" in bleats[bleatId]:
            bleats[bleatId]["inReplyTo"] = bleats[bleats[bleatId]["inReplyTo"]].id

        bleats[bleatId] = Bleat.create(bleats[bleatId])