示例#1
0
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
func (self *Topics) Delete(request *gottp.Request) {
	var args struct {
		Ident string `json:"ident" required:"true"`
	}

	request.ConvertArguments(&args)

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

	err := topics.DeleteTopic(args.Ident)
	if err != nil {
		log.Println(err)
		request.Raise(gottp.HttpError{
			http.StatusInternalServerError,
			"Unable to delete.",
		})
		return
	}

	request.Write(utils.R{
		Data:       nil,
		Message:    "true",
		StatusCode: http.StatusNoContent,
	})
	return
}
示例#3
0
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
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
func (self *UserLocale) Post(request *gottp.Request) {
	userLocale := new(db.UserLocale)
	request.ConvertArguments(userLocale)
	userLocale.PrepareSave()

	if !userLocale.IsValid() {
		request.Raise(gottp.HttpError{http.StatusBadRequest,
			"user, region_id and language_id must be present."})
		return
	}

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

	dblocale, err := user_locale.Get(userLocale.User)

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

	if dblocale != nil {
		request.Raise(gottp.HttpError{http.StatusConflict,
			"User locale information already exists"})
		return
	}

	user_locale.Insert(userLocale)

	request.Write(utils.R{Data: userLocale.Id, Message: "Created",
		StatusCode: http.StatusCreated})
	return
}
示例#6
0
func (self *Topics) Post(request *gottp.Request) {
	newTopic := new(db.AvailableTopic)

	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
}
示例#7
0
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"`
	}

	request.ConvertArguments(&args)

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

	appTopics := 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)
	}

	var iter map[string]available_topics.ChotaTopic
	var err error

	if args.User == "" {
		iter, err = available_topics.GetOrgTopics(args.Organization, appTopics, &channels)
	} else {
		iter, err = available_topics.GetUserTopics(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
	}

	ret := []available_topics.ChotaTopic{}
	for _, topic := range *appTopics {
		ret = append(ret, iter[topic])
	}

	request.Write(ret)
	return
}
示例#8
0
func (self *Gully) Post(request *gottp.Request) {

	inputGully := new(db.Gully)
	request.ConvertArguments(inputGully)
	inputGully.PrepareSave()

	log.Println("Input :", inputGully)

	if !core.IsChannelAvailable(inputGully.Ident) {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Channel is not supported",
		})

		return
	}

	if !inputGully.IsValid(db.INSERT_OPERATION) {
		request.Raise(gottp.HttpError{
			http.StatusBadRequest,
			"Atleast one of the user, org and app_name must be present.",
		})

		return
	}

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

	gly, err := gully.Get(
		inputGully.User,
		inputGully.AppName,
		inputGully.Organization,
		inputGully.Ident,
	)

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

	if gly != nil {
		request.Raise(gottp.HttpError{
			http.StatusConflict,
			"Channel already exists",
		})

		return
	}

	gully.Insert(inputGully)

	log.Println("Saving :", inputGully)

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

	return
}