示例#1
0
文件: bounce.go 项目: josjevv/khabar
func (self *MandrillBounce) Post(request *gottp.Request) {
	type mandrillEvent struct {
		Events []struct {
			Type    string `json:"type" required:"true"`
			Message struct {
				Email string `json:"email" required:"true"`
			} `json:"msg" required:"true"`
		} `json:"mandrill_events" required:"true"`
	}

	var eventTypes = map[string]bool{HARD_BOUNCE: true, SOFT_BOUNCE: true, SPAM: true}

	args := new(mandrillEvent)

	request.ConvertArguments(&args)

	if !utils.ValidateAndRaiseError(request, args) {
		log.Println("Invalid Request", request.GetArguments())
		return
	}

	for _, event := range args.Events {
		if !eventTypes[event.Type] {
			continue
		}
		DisableBounceEmail(event.Message.Email, request)
	}

	request.Write(utils.R{
		StatusCode: http.StatusOK,
		Data:       nil,
	})
}
示例#2
0
文件: topics.go 项目: josjevv/khabar
func (self *Topics) Get(request *gottp.Request) {
	var args struct {
		AppName      string `json:"app_name" required:"true"`
		Organization string `json:"org"`
		User         string `json:"user"`
	}
	var iter map[string]available_topics.ChotaTopic
	var err error

	request.ConvertArguments(&args)

	if !utils.ValidateAndRaiseError(request, args) {
		log.Println("Validation Failed")
		return
	}

	appTopics, err := available_topics.GetAppTopics(args.AppName, args.Organization)

	if args.Organization == "" {
		request.Write(appTopics)
		return
	}

	channels := []string{}
	for ident, _ := range core.ChannelMap {
		channels = append(channels, ident)
	}

	if args.User == "" {
		iter, err = available_topics.GetOrgPreferences(args.Organization, appTopics, &channels)
	} else {
		iter, err = available_topics.GetUserPreferences(args.User, args.Organization, appTopics, &channels)
	}

	if err != nil {
		if err != mgo.ErrNotFound {
			log.Println(err)
			request.Raise(gottp.HttpError{
				http.StatusInternalServerError,
				"Unable to fetch data, Please try again later.",
			})

		} else {
			request.Raise(gottp.HttpError{
				http.StatusNotFound,
				"Not Found.",
			})
		}

		return
	}

	request.Write(iter)
	return
}
示例#3
0
文件: sent.go 项目: josjevv/khabar
// Send a notification to the user depending on preferences
func (self *Notifications) Post(request *gottp.Request) {
	pending_item := new(db.PendingItem)
	request.ConvertArguments(pending_item)

	if request.GetArgument("topic") == nil {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Please provide topic for notification.",
		})

		return
	}

	if pending_item.CreatedBy == pending_item.User {
		MSG := "Receiver is the same as the notification creator. Skipping."

		log.Println(MSG)
		request.Raise(gottp.HttpError{http.StatusOK, MSG})
		return
	}

	if pending.Throttled(pending_item) {
		MSG := "Repeated Notifications are Blocked. Skipping."

		log.Println(MSG)
		request.Raise(gottp.HttpError{http.StatusBadRequest, MSG})
		return
	}

	pending_item.Topic = request.GetArgument("topic").(string)
	pending_item.IsRead = false

	pending_item.PrepareSave()

	if !utils.ValidateAndRaiseError(request, pending_item) {
		return
	}

	if !pending_item.IsValid() {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Context is required for sending notification.",
		})

		return
	}

	core.SendNotification(pending_item)
	request.Write(utils.R{StatusCode: http.StatusNoContent,
		Data: nil, Message: "true"})
	return
}
示例#4
0
文件: bounce.go 项目: josjevv/khabar
func (self *SnsBounce) Post(request *gottp.Request) {

	args := new(snsNotice)

	request.ConvertArguments(&args)

	if !utils.ValidateAndRaiseError(request, args) {
		log.Println("Invalid Request", request.GetArguments())
		return
	}

	msg := bounceMessage{}
	gottpUtils.Decoder([]byte(args.Message), &msg)

	errs := gottpUtils.Validate(&msg)
	if len(*errs) > 0 {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			ConcatenateErrors(errs),
		})

		return
	}

	if msg.Type != BounceNotification {
		log.Println("Invalid Bounce Request", request.GetArguments())

		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Invalid Bounce Request",
		})

		return
	}

	for _, entry := range msg.Bounce.Recipients {
		DisableBounceEmail(entry.Email, request)
	}

	request.Write(utils.R{
		StatusCode: http.StatusOK,
		Data:       nil,
	})

}
示例#5
0
文件: topics.go 项目: josjevv/khabar
func (self *Topics) Post(request *gottp.Request) {
	newTopic := new(db.AvailableTopic)
	newTopic.Channels = []string{"email", "web", "push"}

	request.ConvertArguments(newTopic)

	newTopic.PrepareSave()

	if !utils.ValidateAndRaiseError(request, newTopic) {
		log.Println("Validation Failed")
		return
	}

	if _, err := available_topics.Get(newTopic.Ident); err == nil {
		request.Raise(gottp.HttpError{
			http.StatusConflict,
			"Topic already exists"})
		return
	} else {
		if err != mgo.ErrNotFound {
			log.Println(err)
			request.Raise(gottp.HttpError{
				http.StatusInternalServerError,
				"Unable to fetch data, Please try again later.",
			})
			return
		}
	}

	available_topics.Insert(newTopic)

	request.Write(utils.R{
		StatusCode: http.StatusCreated,
		Data:       newTopic.Id,
		Message:    "Created",
	})
	return
}