Exemplo n.º 1
0
        public async Task DoesNotFireWhenFilterIsNotEvaluatedToTrue()
        {
            var result = this.schema.Execute(@"subscription wasub {
                testSub(author : ""other"") {
                    content
                }
            }", null, null, "1", 0);

            OnMessageReceivedEventArgs eventArgs = null;

            this.eventBus.OnMessageReceived += async(OnMessageReceivedEventArgs args) =>
            {
                await Task.Yield();

                eventArgs = args;
            };

            await this.eventBus.Publish(new Message()
            {
                Author = "test", Content = "blabla"
            },
                                        "testingSsubscriptionChannel");

            Assert.IsNull(eventArgs);
        }
Exemplo n.º 2
0
        public async Task CallingSubscriptionFromGraphQLPropagatesSubscriptionToEventBus()
        {
            var result = this.schema.Execute(@"subscription wasub {
                testSub(author : ""test"") {
                    content
                }
            }", null, null, "1", 0);

            OnMessageReceivedEventArgs eventArgs = null;

            this.eventBus.OnMessageReceived += async(OnMessageReceivedEventArgs args) =>
            {
                await Task.Yield();

                eventArgs = args;
            };

            await this.eventBus.Publish(new Message()
            {
                Author = "test", Content = "blabla"
            },
                                        "testingSsubscriptionChannel");

            Assert.IsNotNull(eventArgs);
        }
Exemplo n.º 3
0
        public async Task DoesntSendDataWhenNoSubscriptionMatchesTheFilter()
        {
            await this.eventBus.Subscribe(EventBusSubscription.Create <Message>(
                                              "testChannel",
                                              "someClientId",
                                              0,
                                              null,
                                              new { },
                                              e => e.Author == "Sam",
                                              operation));

            OnMessageReceivedEventArgs eventArgs = null;

            this.eventBus.OnMessageReceived += async(OnMessageReceivedEventArgs args) =>
            {
                await Task.Yield();

                eventArgs = args;
            };

            await this.eventBus.Publish(new Message()
            {
                Author = "Bob", Content = "stuff"
            }, "testChannel");


            Assert.IsNull(eventArgs);
        }
Exemplo n.º 4
0
        public async Task ShouldReceiveDataDefinedBySubscription()
        {
            await this.eventBus.Subscribe(EventBusSubscription.Create <Message>(
                                              "testChannel",
                                              "someClientId",
                                              0,
                                              null,
                                              new { },
                                              e => e.Author == "Bob",
                                              operation));

            OnMessageReceivedEventArgs eventArgs = null;

            this.eventBus.OnMessageReceived += async(OnMessageReceivedEventArgs args) =>
            {
                await Task.Yield();

                eventArgs = args;
            };

            await this.eventBus.Publish(new Message()
            {
                Author = "Bob", Content = "stuff"
            }, "testChannel");


            Assert.AreEqual("testChannel", eventArgs.Channel);
        }
Exemplo n.º 5
0
        private ExecutionManager GetExecutionContext(OnMessageReceivedEventArgs args)
        {
            if (args.Variables == null)
            {
                return(new ExecutionManager(this, args.Document));
            }

            return(new ExecutionManager(this, args.Document, args.Variables));
        }
Exemplo n.º 6
0
        private async Task InvokeSubscriptionMessageReceived(OnMessageReceivedEventArgs args)
        {
            using (var context = this.GetExecutionContext(args))
            {
                foreach (var definition in args.Document.Definitions)
                {
                    context.ResolveDefinition(definition, args.OperationToExecute);
                }

                var data = await context.ComposeResultForQuery(this.SubscriptionType, context.Operation, args.Data);

                await this.OnSubscriptionMessageReceived?.Invoke(args.ClientId, args.SubscriptionId, data);
            }
        }
Exemplo n.º 7
0
 public void InvokeOnMessageReceived(OnMessageReceivedEventArgs args)
 {
     try
     {
         if (Events.OnMessageReceived != null)
         {
             InvokeCancelableModuleEvent(Events.OnMessageReceived, args);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine($"InvokeOnMessageReceived({args.Message}) threw an exception with message: {ex.Message}");
     }
 }
Exemplo n.º 8
0
        public async Task ShouldNotReceiveAnythingIfNoSubscriptionIsDefined()
        {
            OnMessageReceivedEventArgs eventArgs = null;

            this.eventBus.OnMessageReceived += async(OnMessageReceivedEventArgs args) =>
            {
                await Task.Yield();

                eventArgs = args;
            };

            await this.eventBus.Publish(new Message()
            {
                Author = "Bob", Content = "stuff"
            }, "testChannel");


            Assert.IsNull(eventArgs);
        }
Exemplo n.º 9
0
        private async Task ReceiveMessage(SocketMessage message)
        {
            if (OnMessageReceived == null)
            {
                return;
            }
            if (message.Author.Id == _client.CurrentUser.Id)
            {
                return;
            }

            var gm        = GetGambotMessage(message);
            var typing    = gm.Addressed ? message.Channel.EnterTypingState() : null;
            var eventArgs = new OnMessageReceivedEventArgs
            {
                Message = gm,
            };
            await OnMessageReceived.Invoke(this, eventArgs);

            typing?.Dispose();
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            var    path   = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "Modules");
            var    events = new ExampleEvents();
            var    host   = new ExampleEventsHost(path, events);
            string line;
            int    id = 1;

            while ((line = Prompt()) != "exit")
            {
                Console.WriteLine($"Receiving new message '{line}' ... beep boop...");

                var onReceivedEventArgs = new OnMessageReceivedEventArgs(line);
                host.InvokeOnMessageReceived(onReceivedEventArgs);

                if (onReceivedEventArgs.Cancel)
                {
                    Console.WriteLine(" => Message Aborted!");
                    continue;
                }

                // Module could augment the message
                line = onReceivedEventArgs.Message;

                Console.WriteLine($"Message received: {line}");

                // Do something constructive
                var message = new Message {
                    MessageText = line, Id = id++
                };

                var onMessageCreatedEventArgs = new OnMessageCreatedEventArgs(message);
                host.InvokeOnMessageCreated(onMessageCreatedEventArgs);

                // All done, moving on...
            }
        }
Exemplo n.º 11
0
        private async Task ReceiveMessage(SlackMessage message)
        {
            if (OnMessageReceived == null)
            {
                return;
            }
            if (message.User.Id == _connection.Self.Id)
            {
                return;
            }

            var gm = GetGambotMessage(message);

            UpdateMessageHistory(gm);
            if (gm.Addressed)
            {
                await _connection.IndicateTyping(message.ChatHub);
            }
            var eventArgs = new OnMessageReceivedEventArgs
            {
                Message = gm,
            };
            await OnMessageReceived.Invoke(this, eventArgs);
        }
Exemplo n.º 12
0
        public async Task HandleMessage(object sender, OnMessageReceivedEventArgs e)
        {
            var      message  = e.Message;
            Response response = null;

            _log.Trace("Handling commands");
            foreach (var command in _commands)
            {
                try
                {
                    response = await command.Handle(message);

                    if (response != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(ex, $"An error occurred while {command} was handling: {ex.Message}");
                }
            }
            if (response != null)
            {
                _log.Trace("Handled command, responding immediately.");
                await response.Send();

                return;
            }
            _log.Trace("No commands to handle.");

            _log.Trace("Processing listeners");
            await Task.WhenAll(_listeners.Select(async l =>
            {
                try { await l.Listen(message); }
                catch (Exception ex)
                {
                    _log.Error(ex, $"An error occurred while {l} was listening: {ex.Message}");
                }
            }));

            _log.Trace("Listeners have listened.");

            _log.Trace("Processing responders");
            foreach (var responder in _responders)
            {
                try
                {
                    response = await responder.Respond(message);
                }
                catch (Exception ex)
                {
                    _log.Error(ex, $"An error occurred while trying to get a response from {responder}: {ex.Message}");
                }
                if (response != null)
                {
                    _log.Trace("Got a response");
                    break;
                }
            }
            _log.Trace("Responders have responded.");

            if (response == null)
            {
                _log.Trace("No response generated.");
                return;
            }

            _log.Trace("Processing transformers");
            foreach (var transformer in _transformers)
            {
                try
                {
                    response = await transformer.Transform(response);
                }
                catch (Exception ex)
                {
                    _log.Error(ex, $"An error ocurred while attempting to transform the response with {transformer}: {ex.Message}");
                }
            }
            _log.Trace("Transformers have transformed");

            _log.Trace("Sending response");
            try
            {
                await response.Send();
            }
            catch (Exception ex)
            {
                _log.Error(ex, $"An error occurred while attempting to send the response: {ex.Message}");
            }
        }
Exemplo n.º 13
0
 private void AnalyzeMessage(object sender, OnMessageReceivedEventArgs args)
 {
     Analyzer.Analyze(args.Message);
 }