public IHttpActionResult JoinBattle(int videoBattleId) { //first check if it's a valid videobattle and the logged in user can actually invite var videoBattle = _videoBattleService.Get(videoBattleId); if (videoBattle == null) { VerboseReporter.ReportError("Video Battle doesn't exist", "join_battle"); return RespondFailure(); } //in any case a sponsor can't join a battle if (_sponsorService.IsSponsor(ApplicationContext.Current.CurrentUser.Id, videoBattleId, BattleType.Video)) { VerboseReporter.ReportError("Unauthorized", "join_battle"); return RespondFailure(); } //only open or signup battle types can be joined directly. it should not be open in status either way if (videoBattle.ParticipationType != BattleParticipationType.InviteOnly && videoBattle.VideoBattleStatus == BattleStatus.Pending) { //get the current customer var customer = ApplicationContext.Current.CurrentUser; //get the participation status var status = _videoBattleParticipantService.GetParticipationStatus(videoBattleId, customer.Id); //get all participants to get count of total, we can't allow if count reaches a max limit for open battles if (videoBattle.ParticipationType == BattleParticipationType.Open) { var participants = _videoBattleParticipantService.GetVideoBattleParticipants(videoBattleId, BattleParticipantStatus.ChallengeAccepted); if (participants.Count == videoBattle.MaximumParticipantCount && videoBattle.MaximumParticipantCount > 0) { //nop, can't join now VerboseReporter.ReportError("No more participants allowed", "join_battle"); return RespondFailure(); } } if (status == BattleParticipantStatus.NotChallenged) { //not challenged so it's a valid request var videoBattleParticipant = new VideoBattleParticipant { ParticipantId = customer.Id, VideoBattleId = videoBattleId, LastUpdated = DateTime.UtcNow, //depending on the type of battle, the challenge is either accepted directly or marked for approval ParticipantStatus = videoBattle.ParticipationType == BattleParticipationType.Open ? BattleParticipantStatus.ChallengeAccepted : BattleParticipantStatus.SignedUp }; //and save it _videoBattleParticipantService.Insert(videoBattleParticipant); //send notification to challenger (the battle host) var challenger = _userService.Get(videoBattle.ChallengerId); var challengee = ApplicationContext.Current.CurrentUser; if (videoBattleParticipant.ParticipantStatus == BattleParticipantStatus.ChallengeAccepted) { //open battle _emailSender.SendVideoBattleJoinNotification(challenger, challengee, videoBattle); } else { _emailSender.SendVideoBattleSignupNotification(challenger, challengee, videoBattle); } //and add user to the followers list _followService.Insert<VideoBattle>(customer.Id, videoBattleId); return RespondSuccess(new { Status = videoBattleParticipant.ParticipantStatus }); } } VerboseReporter.ReportError("No more participants allowed", "join_battle"); return RespondFailure(); }
public IHttpActionResult InviteParticipants(InviteParticipantsModel requestModel) { if (!ModelState.IsValid) { VerboseReporter.ReportError("Missing required parameters or invalid data submitted", "save_video_battle"); return RespondFailure(); } var videoBattleId = requestModel.VideoBattleId; var participantIds = requestModel.ParticipantIds; var emails = requestModel.Emails; //first check if it's a valid videobattle and the logged in user can actually invite var videoBattle = _videoBattleService.Get(videoBattleId); var model = new List<object>(); if (CanInvite(videoBattle)) { if (participantIds != null) { foreach (var pi in participantIds) { //exclude self if (pi == ApplicationContext.Current.CurrentUser.Id) continue; var status = _videoBattleParticipantService.GetParticipationStatus(videoBattleId, pi); //only people who have not been challenged if (status == BattleParticipantStatus.NotChallenged) { var videoBattleParticipant = new VideoBattleParticipant() { ParticipantId = pi, ParticipantStatus = BattleParticipantStatus.Challenged, VideoBattleId = videoBattleId, LastUpdated = DateTime.UtcNow }; _videoBattleParticipantService.Insert(videoBattleParticipant); model.Add(new { Success = true, ParticipantId = pi }); //send email notification to the participant var challengee = _userService.Get(pi); _emailSender.SendSomeoneChallengedYouForABattleNotification(ApplicationContext.Current.CurrentUser, challengee, videoBattle); } else { model.Add(new { Success = false, ParticipantId = pi, Status = status }); } } } if (emails != null) { //and direct email invites foreach (var email in emails) { _emailSender.SendSomeoneChallengedYouForABattleNotification(ApplicationContext.Current.CurrentUser, email, email, videoBattle); model.Add(new { Success = true, Email = email, }); } } return RespondSuccess(model); } VerboseReporter.ReportError("Unauthorized", "invite_participants"); return RespondFailure(); }