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