Ejemplo n.º 1
0
func CreateMatch(mr types.MatchRequest) error {
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(mr.FirstUserId); err == nil {
		if err = utilsservice.CheckIfUserExists(mr.SecondUserId); err == nil {
			var firstUsername string
			var secondUsername string

			if firstUsername, err = utilsservice.GetUserUsername(mr.FirstUserId); err == nil {
				if secondUsername, err = utilsservice.GetUserUsername(mr.SecondUserId); err == nil {
					var m types.Match

					m.UserId = uuid.Parse(mr.FirstUserId)
					m.MatchedUserId = uuid.Parse(mr.SecondUserId)
					m.MatchedUsername = secondUsername
					m.Timestamp = int64(time.Now().UTC().Unix())

					//create first match
					if err = chatRepo.CreateMatch(m); err == nil {
						m.UserId = uuid.Parse(mr.SecondUserId)
						m.MatchedUserId = uuid.Parse(mr.FirstUserId)
						m.MatchedUsername = firstUsername
						m.Timestamp = int64(time.Now().UTC().Unix())

						//create second match
						if err = chatRepo.CreateMatch(m); err == nil {
							var cr types.ChatRequest

							//create chat
							cr.FirstUserId = mr.FirstUserId
							cr.SecondUserId = mr.SecondUserId
							err = CreateChat(cr)

							//send push message to first user
							if lastActivity, errA := activityservice.GetUserActivity(mr.FirstUserId); errA == nil {
								notificationsservice.SendPushNotification(lastActivity.DeviceType, lastActivity.PushToken, "you've been matched!")
							}

							//send push message to second user
							if lastActivity, errA := activityservice.GetUserActivity(mr.SecondUserId); errA == nil {
								notificationsservice.SendPushNotification(lastActivity.DeviceType, lastActivity.PushToken, "you've been matched!")
							}
						}
					}
				}
			}
		}
	}

	return err
}
Ejemplo n.º 2
0
func GetUserLastMessageByReceiverId(userId string, receiverId string) (types.Message, error) {
	var response types.Message
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		if err = utilsservice.CheckIfUserExists(userId); err == nil {
			response, err = messageRepo.GetUserLastMessageByReceiverId(userId, receiverId)
		}
	}

	return response, err
}
Ejemplo n.º 3
0
func CreateAccelerations(userId string, accelerations types.AccelerationsRequest) error {
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		for _, ar := range accelerations {
			var a types.Acceleration
			a.UserId = uuid.Parse(userId)
			a.X = ar.X
			a.Y = ar.Y
			a.Z = ar.Z

			if ar.Timestamp != 0 {
				a.Timestamp = int64(time.Unix(ar.Timestamp, 0).UTC().Unix())
			} else {
				a.Timestamp = int64(time.Now().UTC().Unix())
			}

			err = accelerationRepo.CreateAcceleration(a)

			if err != nil {
				break
			}
		}

		//increment walking progress for test users
		if err == nil {
			if user, errU := userservice.GetUser(userId); errU == nil {
				if userExtraInfo, errU := userservice.GetUserExtraInfo(userId); errU == nil {
					if userExtraInfo.WalkingProgress < 100 {
						if user.IsTest {
							var userExtraInfoRequest types.UserExtraInfoRequest

							if userExtraInfo.WalkingProgress >= 95 {
								userExtraInfoRequest.WalkingProgress = 100
							} else {
								userExtraInfoRequest.WalkingProgress = userExtraInfo.WalkingProgress + 3
							}

							if errU := userservice.UpdateUserExtraInfo(userId, userExtraInfoRequest); errU != nil {
								log.Printf(fmt.Sprintf("AccelerationService.CreateAccelerations() - Error: %v", errU.Error()))
							}
						} else {
							if accelerations != nil && len(accelerations) > 1 {
								kafkaMessage := fmt.Sprintf("%v,%v,%v", userId, accelerations[0].Timestamp, accelerations[len(accelerations)-1].Timestamp)
								kafkaservice.ProduceMessage(kafkaMessage)
							}
						}
					}
				} else {
					log.Printf(fmt.Sprintf("AccelerationService.CreateAccelerations() - Error: %v", errU.Error()))
				}
			} else {
				log.Printf(fmt.Sprintf("AccelerationService.CreateAccelerations() - Error: %v", errU.Error()))
			}
		}
	}

	return err
}
Ejemplo n.º 4
0
func GetUserMatch(userId string) (types.Match, error) {
	var response types.Match
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		response, err = chatRepo.GetUserMatch(userId)
	}

	return response, err
}
Ejemplo n.º 5
0
func GetUserActivity(userId string) (types.Activity, error) {
	var response types.Activity
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		response, err = activityRepo.GetUserActivity(userId)
	}

	return response, err
}
Ejemplo n.º 6
0
func GetAccelerations(userId string) (types.Accelerations, error) {
	var accelerations types.Accelerations
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		accelerations, err = accelerationRepo.GetAccelerations(userId)
	}

	return accelerations, err
}
Ejemplo n.º 7
0
func CreateChat(cr types.ChatRequest) error {
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(cr.FirstUserId); err == nil {
		if err = utilsservice.CheckIfUserExists(cr.SecondUserId); err == nil {
			var firstUsername string
			var secondUsername string

			if firstUsername, err = utilsservice.GetUserUsername(cr.FirstUserId); err == nil {
				if secondUsername, err = utilsservice.GetUserUsername(cr.SecondUserId); err == nil {
					var c types.Chat

					c.SenderId = uuid.Parse(cr.FirstUserId)
					c.ReceiverId = uuid.Parse(cr.SecondUserId)
					c.ReceiverUsername = secondUsername
					c.IsChatActive = true
					c.IsChatBlockedBySender = false
					c.IsChatBlockedByReceiver = false
					c.LastMessage = ""
					c.Timestamp = int64(time.Now().UTC().Unix())

					if err = chatRepo.CreateChat(c); err == nil {
						c.SenderId = uuid.Parse(cr.SecondUserId)
						c.ReceiverId = uuid.Parse(cr.FirstUserId)
						c.ReceiverUsername = firstUsername
						c.IsChatActive = true
						c.IsChatBlockedBySender = false
						c.IsChatBlockedByReceiver = false
						c.LastMessage = ""
						c.Timestamp = int64(time.Now().UTC().Unix())

						err = chatRepo.CreateChat(c)
					}
				}
			}
		}
	}

	return err
}
Ejemplo n.º 8
0
func GetUserActiveChats(userId string) (types.Chats, error) {
	var response types.Chats
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		CreateUserPerfectMatch(userId)
		response, err = chatRepo.GetUserActiveChats(userId)
	}

	return response, err
}
Ejemplo n.º 9
0
func ReportUser(userId string, urr types.UserReportRequest) error {
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		var ur types.UserReport

		ur.ReportedUserId = uuid.Parse(userId)
		ur.ReporterUserId = urr.ReporterUserId
		ur.Reason = urr.Reason
		ur.Comment = urr.Comment
		ur.Timestamp = int64(time.Now().UTC().Unix())

		err = userRepo.ReportUser(ur)
	}

	return err
}
Ejemplo n.º 10
0
func UpdateUserExtraInfo(userId string, uer types.UserExtraInfoRequest) error {
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		if uer.WalkingProgress >= 0 && uer.WalkingProgress <= 100 {
			var ue types.UserExtraInfo

			ue.UserId = uuid.Parse(userId)
			ue.WalkingProgress = uer.WalkingProgress
			ue.Timestamp = int64(time.Now().UTC().Unix())

			err = userRepo.UpdateUserExtraInfo(ue)
		} else {
			err = errors.New("400")
		}
	}

	return err
}
Ejemplo n.º 11
0
func CreateUserActivity(userId string, ar types.ActivityRequest) error {
	var err error
	err = nil

	if err = utilsservice.CheckIfUserExists(userId); err == nil {
		var a types.Activity

		a.DeviceId = ar.DeviceId
		a.DeviceType = ar.DeviceType
		a.IsLoggedIn = ar.IsLoggedIn
		a.UserId = uuid.Parse(userId)
		a.PushToken = ar.PushToken

		if ar.Timestamp != 0 {
			a.Timestamp = int64(time.Unix(ar.Timestamp, 0).UTC().Unix())
		} else {
			a.Timestamp = int64(time.Now().UTC().Unix())
		}

		err = activityRepo.CreateUserActivity(a)
	}

	return err
}