Beispiel #1
0
        public async Task <IActionResult> DoLongPolling()
        {
            SimpleLongPolling lp = new SimpleLongPolling("sample-channel");
            var message          = await lp.WaitAsync();

            return(new ObjectResult(new { Message = (message != null ? message : "Long polling timeout!") }));
        }
Beispiel #2
0
        public IActionResult CreateDiscussionEntry([FromBody] DiscussionEntry entry)
        {
            if (entry == null)
            {
                _logger.LogError("Invalid object: entry was null");
                return(BadRequest("Invalid client request"));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid DiscussionEntry object sent from client.");
                return(BadRequest("Invalid DiscussionEntry object sent from client."));
            }

            try
            {
                if (entry.NormalGroup.HasValue && !CheckGroupAuthorized(entry.NormalGroup.Value))
                {
                    return(Unauthorized());
                }
                else if (entry.Subgroup.HasValue && !CheckSubGroupAuthorized(entry.Subgroup.Value))
                {
                    return(Unauthorized());
                }


                entry.TimeStamp = DateTime.Now;
                var userMail = AuthControllerExtensions.JwtNameExtractor(Request.Headers["Authorization"]);
                var dbUser   = _repository.User.GetUserByEmail(userMail);
                entry.UserId = dbUser.Id;

                _repository.DiscussionEntry.PostDiscussion(entry);
                _repository.Save();

                if (entry.Subgroup != null)
                {
                    SimpleLongPolling.Publish($"subgroup{entry.Subgroup.Value}", entry.Id);
                    _pushSender.SendSubGroupPush(entry.Subgroup.Value, entry.Id, entry.Text, dbUser.Id);
                }
                else if (entry.NormalGroup != null)
                {
                    SimpleLongPolling.Publish($"group{entry.NormalGroup.Value}", entry.Id);
                    _pushSender.SendGroupPush(entry.NormalGroup.Value, entry.Id, entry.Text, dbUser.Id);
                }

                return(Ok(_repository.DiscussionEntry.GetDiscussionEntryById(entry.Id)));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside CreateDiscussionEntry: {e.Message}");
                return(StatusCode(500, "Something went wrong during creating discussion entry"));
            }
        }
Beispiel #3
0
        public async System.Threading.Tasks.Task <IActionResult> GetNewSubgroupDiscussionEntryAsync(int groupId)
        {
            try
            {
                if (!CheckSubGroupAuthorized(groupId))
                {
                    return(Unauthorized());
                }

                var lp = new SimpleLongPolling($"subgroup{groupId}");
                var id = await lp.WaitAsync();

                var entry = _repository.DiscussionEntry.GetDiscussionEntryById(id);

                return(Ok(entry));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong while GetNewSubgroupDiscussionEntryAsync: {e.Message}");
                return(StatusCode(500, $"Something went wrong while getting GetNewSubgroupDiscussionEntryAsync"));
            }
        }
Beispiel #4
0
 public IActionResult SimulateTrigger(string message = "Sample publish message")
 {
     SimpleLongPolling.Publish("sample-channel", message);
     return(new ObjectResult(new { Message = message, Status = "Published" }));
 }