Exemplo n.º 1
0
func (place *Place) acceptPlace(request *restful.Request, response *restful.Response) {

	if place.getGeolocation() == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "We were unable to geolocate your plate")
		response.WriteHeaderAndEntity(500, errors)
		return
	}

	place.ValidatedAt = time.Now()
	place.Validated = true

	_, err := r.Table("places").Get(place.Id).Update(place).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}

	go func() {
		notif := notification.SetPlaceAccepted()
		notif.UserId = place.UserId
		notif.UserIdFrom = place.user.Id
		notif.IdThing = place.Id
		notif.Name = place.Name
		notif.IdLink = place.Id
		notif.CreateNotification()
	}()

	response.WriteHeaderAndEntity(http.StatusOK, place)
	helpers.PrintLog(request, response, place.user.Name)
}
Exemplo n.º 2
0
func (place *Place) handleImage(request *restful.Request, response *restful.Response) {
	errors := api.Error{}

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")

	ext := filepath.Ext(hdr.Filename)
	if helpers.CheckFileExtension(ext) == false {
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(406, errors)
		return
	}
	if strings.Compare(place.Image, "") != 0 {
		helpers.RemoveFile("./assets/Images/" + place.Image)
	}
	place.Image = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+place.Image, mpf)

	_, err := r.Table("places").Get(place.Id).Update(place).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}
	response.WriteHeaderAndEntity(http.StatusOK, place)

	helpers.PrintLog(request, response, place.user.Name)
}
Exemplo n.º 3
0
func (place *Place) checkDate(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if place.Type != "event" {
		chain.ProcessFilter(request, response)
		return
	}
	if place.Type == "event" && place.Recurring == true {
		chain.ProcessFilter(request, response)
		return
	}

	errors := api.Error{}
	inOneMonth := time.Now().AddDate(0, 1, 0)
	lastOneMonth := time.Now().AddDate(0, -1, 0)

	if place.StartAt.After(place.EndAt) {
		errors.ListErrors = append(errors.ListErrors, "start date of an event can't be less than end date")
	} else if place.StartAt.After(inOneMonth) {
		errors.ListErrors = append(errors.ListErrors, "start date can not be in more than a month in the future")
	} else if place.EndAt.Before(lastOneMonth) {
		errors.ListErrors = append(errors.ListErrors, "")
	}
	if len(errors.ListErrors) > 0 {
		response.WriteHeaderAndEntity(http.StatusUnauthorized, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 4
0
func (user *User) checkBodyCreate(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	private := UserCreate{}
	errors := api.Error{}

	err := request.ReadEntity(&private)
	if err != nil {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		helpers.PrintLog(request, response, user.Name)
		return
	}

	user.Email = strings.TrimSpace(private.Email)
	user.Name = helpers.RemoveManySpaces(helpers.MakeFirstUpperCase(strings.ToLower(private.Name)))

	if user.isTwoPasswordAreIdentical(private.Password, private.Password2) == false {
		errors.ListErrors = append(errors.ListErrors, "Two passwords aren't identical")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, user.Name)
		return
	}
	if len(private.Password) < 6 {
		errors.ListErrors = append(errors.ListErrors, "Your password must contain at least 6 characters")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, user.Name)
		return
	}
	user.Password = private.Password
	chain.ProcessFilter(request, response)
}
Exemplo n.º 5
0
func (place *Place) createPlace(request *restful.Request, response *restful.Response) {
	place.Lat = 0
	place.Lng = 0
	place.PostNb = 0
	place.ValidatedAt = *new(time.Time)
	place.Validated = false
	place.CreateAt = time.Now()
	place.UserId = place.user.Id
	place.Id = *new(string)

	request.Request.ParseMultipartForm(32 << 20)
	mpf, hdr, _ := request.Request.FormFile("image")
	ext := filepath.Ext(hdr.Filename)

	if helpers.CheckFileExtension(ext) == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "Only .jpg, .jpeg or .png are authorized.")
		response.WriteHeaderAndEntity(406, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	place.Image = helpers.GenerateHash(50) + ext
	go helpers.PutFile("assets/Images/"+place.Image, mpf)

	resp, err := r.Table("places").Insert(place).RunWrite(api.Sess)
	if err != nil {
		response.WriteHeaderAndEntity(http.StatusConflict, err.Error())
		return
	}
	place.Id = resp.GeneratedKeys[0]
	response.WriteHeaderAndEntity(http.StatusCreated, place)

	helpers.PrintLog(request, response, place.user.Name)
}
Exemplo n.º 6
0
func (place *Place) checkType(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	errors := api.Error{}
	if place.Type != "event" && place.Type != "place" {
		errors.ListErrors = append(errors.ListErrors, "Type doesn't exist")
		response.WriteHeaderAndEntity(http.StatusUnauthorized, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 7
0
func (place *Place) CheckIfPlaceIsValidated(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if place.Validated == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This spotted isn't validated, you cannot do this action on it.")
		response.WriteHeaderAndEntity(http.StatusUnauthorized, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 8
0
func (match *Match) checkIfUserIsMatchRecipient(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if match.user.Id != match.UserIdY {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "you must be match's recipient to accept it")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 9
0
func (post *Post) CheckIfPostIsValidated(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if post.Validated == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This post hasn't been accepted, you cannot have this action on it.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 10
0
func (like *Like) checkIfLikeBelongsUser(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if like.UserId != like.user.Id {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This like is not yours")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, like.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 11
0
func (match *Match) checkIfMatchIsAccepted(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if match.Validated == true {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "this match is already validated")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 12
0
func (talk *Talk) checkUserIsInTalk(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if talk.user.Id != talk.UserIdX && talk.user.Id != talk.UserIdY {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This talk doesn't exist.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, talk.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 13
0
func (place *Place) checkIfNameExist(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if place.getPlaceByName() == true {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "A place or an event related to this name already exists")
		response.WriteHeaderAndEntity(http.StatusUnauthorized, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 14
0
func (post *Post) CheckAndGetPostById(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	post.Id = request.PathParameter("post-id")
	if post.GetPostById() == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This post doesn't exist.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 15
0
func (post *Post) CheckIfAPostIsPendingInPlace(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if post.countUsersPendingPostsInPlace() == 1 && post.user.Group < 100 {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "An event request to add a post in this spotted is already pending.")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 16
0
func (post *Post) CheckCountPostInPending(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if post.countUsersPendingPosts() == 5 && post.user.Group < 100 {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "You has already 5 posts in pending.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 17
0
func (talk *Talk) CheckAndGetTalkById(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	talk.Id = request.PathParameter("talk-id")
	if talk.getTalkById() == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This talk doesn't exist.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, talk.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 18
0
func (match *Match) checkIfUserIdIsntPostUserId(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if match.user.Id == match.post.UserId {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "You cannot send match request to yourself")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 19
0
func (place *Place) checkIfPlaceIsPending(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if place.Validated == true {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This is validated spotted you don't have action on it.")
		response.WriteHeaderAndEntity(http.StatusUnauthorized, errors)
		helpers.PrintLog(request, response, place.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 20
0
func (user *User) checkName(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	errors := api.Error{}

	if helpers.IsAConformName(user.Name) == false {
		errors.ListErrors = append(errors.ListErrors, "Your name hasn't a good format")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 21
0
func (like *Like) checkAndGetLikeById(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	like.Id = request.PathParameter("like-id")
	if like.getLikeById() == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This like doesn't exist.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, like.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 22
0
func (post *Post) checkContent(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	errors := api.Error{}

	if len(post.Content) < 40 || len(post.Content) > 1500 {
		errors.ListErrors = append(errors.ListErrors, "Your ad should contain between 40 and 1500 characters")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 23
0
func (match *Match) checkCountMatchInLastDay(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if match.countMatchInLast24Hours() == 5 {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "You can only send 5 requests for match per day.")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 24
0
func (user *User) CheckUserIsAdmin(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if user.Group < 100 {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "You are not allowed to perform this action.")
		response.WriteHeaderAndEntity(http.StatusUnauthorized, errors)
		helpers.PrintLog(request, response, user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 25
0
func (match *Match) checkIfMatchExistWithThisPost(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if match.getMatchByUserIdAndPostId() == true {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "You have already sent a request to match to the author of this post")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 26
0
func (message *Message) CheckUserIsInTalk(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if message.user.Id == message.talk.UserIdX || message.user.Id == message.talk.UserIdY {
		chain.ProcessFilter(request, response)
	} else {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This talk doesn't exist.")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, message.user.Name)
		return
	}
}
Exemplo n.º 27
0
func (session *Session) checkAndGetUserByEmail(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	errors := api.Error{}

	if session.user.GetUserByEmail() == false {
		errors.ListErrors = append(errors.ListErrors, "Email doesn't exist")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, session.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 28
0
func (match *Match) checkIfUserIsInMatch(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {

	if match.user.Id != match.UserIdX && match.user.Id != match.UserIdY {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "you're not be able to remove this match")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 29
0
func (match *Match) checkAndGetMatchById(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	match.Id = request.PathParameter("match-id")

	if match.getMatchById() == false {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "This match doesn't exist.")
		response.WriteHeaderAndEntity(http.StatusNotAcceptable, errors)
		helpers.PrintLog(request, response, match.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}
Exemplo n.º 30
0
func (post *Post) checkIfUserIsAdminOrOwner(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if post.UserId == post.user.Id || post.user.Group == 100 {
		chain.ProcessFilter(request, response)
		return
	} else {
		errors := api.Error{}
		errors.ListErrors = append(errors.ListErrors, "You don't have permission to do this action")
		response.WriteHeaderAndEntity(http.StatusNotFound, errors)
		helpers.PrintLog(request, response, post.user.Name)
		return
	}
	chain.ProcessFilter(request, response)
}