Beispiel #1
0
 private void GivenPegValidity(string comment, bool isPegValid)
 {
     _pegHelper.IsPegValid(comment, Arg.Any <int?>(), Arg.Any <string[]>(), Arg.Any <string[]>())
     .Returns(isPegValid);
     _pegHelper
     .IsPegValid(Arg.Any <string>(), Arg.Any <int?>(), Arg.Any <string[]>(), Arg.Any <string[]>())
     .Returns(isPegValid);
 }
Beispiel #2
0
        public async Task <Message> Respond(Message message)
        {
            try
            {
                _logger.LogDebug("Validating peg message");
                _pegRequestValidator.ValidatePegRequest(message);
            }
            catch (PegValidationException e)
            {
                _logger.LogWarning("Peg message is invalid: {@error}", e);
                return(new Message
                {
                    Text = e.Message
                });
            }

            _logger.LogDebug("Getting user information for id {userid}, username {username}", message.Sender.UserId,
                             message.Sender.Username);
            var sender = await _pockyUserRepository.AddOrUpdateUserAsync(message.Sender.UserId, message.Sender.Username);

            var comment = string.Join(string.Empty, message.MessageParts.Skip(3).Select(x => x.Text)).Trim();

            var requireKeywords = _configRepository.GetGeneralConfig("requireValues");
            var keywords        = _configRepository.GetStringConfig("keyword").ToArray();
            var penaltyKeywords = _configRepository.GetStringConfig("penaltyKeyword").ToArray();
            var linkedKeywords  = _configRepository.GetStringConfig("linkedKeyword").Select(x => new LinkedKeyword(x));

            keywords = keywords.Concat(linkedKeywords.Select(x => x.LinkedWord)).ToArray();

            var isPegValid   = _pegHelper.IsPegValid(comment, requireKeywords, keywords, penaltyKeywords);
            var numPegsGiven = sender.PegsGiven?.Count(x =>
                                                       _pegHelper.IsPegValid(x.Comment, requireKeywords, keywords, penaltyKeywords)) ?? 0;

            if (!sender.HasRole(Role.Unmetered) &&
                (isPegValid && numPegsGiven >= _configRepository.GetGeneralConfig("limit")))
            {
                _logger.LogDebug("User {userId} has reached their peg limit", sender.UserId);
                return(new Message
                {
                    Text = "Sorry, but you have already spent all of your pegs for this fortnight."
                });
            }

            var receiverId = message.MessageParts[2].UserId;
            var receiver   = await _chatHelper.People.GetPersonAsync(receiverId).ConfigureAwait(false);

            var dbReceiver = await _pockyUserRepository.AddOrUpdateUserAsync(receiver.UserId, receiver.Username);

            _logger.LogInformation(
                "Giving peg with sender {senderId}, receiver {receiverId}, validity {isPegValid}, comment {comment}",
                sender.UserId, receiver.UserId, isPegValid, comment);
            await _pegGiver.GivePeg(comment, sender, dbReceiver, isPegValid?numPegsGiven + 1 : numPegsGiven).ConfigureAwait(false);

            return(null);
        }
Beispiel #3
0
 private void GivenPegValidity(List <Tuple <string, bool> > pegValidity)
 {
     foreach (var peg in pegValidity)
     {
         _pegHelper.IsPegValid(peg.Item1, Arg.Any <int>(), Arg.Any <string[]>(), Arg.Any <string[]>())
         .Returns(peg.Item2);
     }
 }
Beispiel #4
0
        private Dictionary <bool, List <Persistence.Models.Peg> > GroupPegsByValidity(PockyUser pockyUser)
        {
            var requireKeywords = _configRepository.GetGeneralConfig("requireValues");
            var keywords        = _configRepository.GetStringConfig("keyword").ToArray();
            var penaltyKeywords = _configRepository.GetStringConfig("penaltyKeyword").ToArray();

            var groupedPegs = pockyUser.PegsGiven.GroupBy(x =>
                                                          _pegHelper.IsPegValid(x.Comment, requireKeywords, keywords, penaltyKeywords))
                              .ToDictionary(x => x.Key, x => x.ToList());

            return(groupedPegs);
        }
Beispiel #5
0
 private List <PegDetails> GetValidPegs(List <Peg> pegs, int?requireKeywords, string[] keywords,
                                        string[] penaltyKeywords, LinkedKeyword[] linkedKeywords, string receiverLocation)
 {
     return(pegs.Where(y => _pegHelper.IsPegValid(y.Comment, requireKeywords, keywords.Concat(linkedKeywords.Select(x => x.LinkedWord)).ToArray(), penaltyKeywords))
            .Select(y => new PegDetails
     {
         SenderName = y.Sender.Username,
         Weight = _pegHelper.GetPegWeighting(y.Sender.Location?.Location, receiverLocation),
         Comment = y.Comment,
         Keywords = GetPegKeywords(y.Comment, keywords, linkedKeywords),
         SenderLocation = y.Sender.Location?.Location
     })
            .ToList());
 }
Beispiel #6
0
 private void GivenPegValidity(string penaltyKeyword)
 {
     _pegHelper.IsPegValid(Arg.Any <string>(), Arg.Any <int?>(), Arg.Any <string[]>(), Arg.Any <string[]>()).Returns(
         x => !((string)x[0]).Contains(penaltyKeyword));
 }