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 }
func CreateMessage(userId string, receiverId string, mr types.MessageRequest) error { var err error err = nil if err = utilsservice.CheckIfMatchExists(userId, receiverId); err == nil { var m types.Message m.SenderId = uuid.Parse(userId) m.ReceiverId = uuid.Parse(receiverId) m.MessageId = uuid.NewRandom() m.IsRead = false m.Text = strings.Replace(mr.Text, "'", "''", -1) if mr.Timestamp != 0 { m.Timestamp = int64(time.Unix(mr.Timestamp, 0).UTC().Unix()) } else { m.Timestamp = int64(time.Now().UTC().Unix()) } if err = messageRepo.CreateMessage(m); err == nil { err = chatservice.UpdateLastMessageChat(userId, receiverId, m.Text) if lastActivity, errA := activityservice.GetUserActivity(receiverId); errA == nil { senderUsername, _ := utilsservice.GetUserUsername(userId) pushMessage := fmt.Sprintf("%s: %s", senderUsername, m.Text) notificationsservice.SendPushNotification(lastActivity.DeviceType, lastActivity.PushToken, pushMessage) } } } return err }
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 }
func Login(l types.LoginRequest) (types.LoginResponse, error) { var userId string var err error var response types.LoginResponse err = nil if userId, err = utilsservice.CheckLoginCredentials(l.Email, l.Password); err == nil { var a types.ActivityRequest a.DeviceId = l.DeviceId a.DeviceType = l.DeviceType a.IsLoggedIn = l.IsLoggedIn a.PushToken = l.PushToken a.Timestamp = l.Timestamp err = activityservice.CreateUserActivity(userId, a) response.UserId = uuid.Parse(userId) response.Username, err = utilsservice.GetUserUsername(userId) response.Timestamp = int64(time.Now().UTC().Unix()) } return response, err }