« blib/lib/auto/share/dist/Beekeeper/scraper/lib/MyApp/Service/Scraper/Worker.pm
↑ Summary
lib/Beekeeper/Service/Supervisor.pm »
| 1 | package MyApp::Service::Chat::Worker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use MyApp::Service::Base; | ||
| 7 | use base 'MyApp::Service::Base'; | ||
| 8 | |||
| 9 | |||
| 10 | sub on_startup { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | $self->init_persistent_connections; | ||
| 14 | |||
| 15 | $self->accept_remote_calls( | ||
| 16 | 'myapp.chat.message' => 'send_message', | ||
| 17 | 'myapp.chat.pmessage' => 'send_private_message', | ||
| 18 | 'myapp.chat.notice' => 'send_notice', | ||
| 19 | 'myapp.chat.ping' => 'ping', | ||
| 20 | ); | ||
| 21 | |||
| 22 | log_info "Ready"; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub on_shutdown { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | log_info "Stopped"; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub send_message { | ||
| 32 | my ($self, $params) = @_; | ||
| 33 | |||
| 34 | my $msg = $params->{'message'}; | ||
| 35 | my $from = $self->uuid; | ||
| 36 | |||
| 37 | return unless (defined $msg && length $msg); | ||
| 38 | |||
| 39 | # Trim html tags | ||
| 40 | $msg =~ s/<.*?>//sg; | ||
| 41 | |||
| 42 | # Filter profanities | ||
| 43 | $msg =~ s/\b(ass|shit)\b/***/sg; | ||
| 44 | |||
| 45 | # Broadcast to all frontend clients | ||
| 46 | $self->send_notification( | ||
| 47 | method => 'myapp.chat.message', | ||
| 48 | address => 'frontend', | ||
| 49 | params => { from => $from, message => $msg }, | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub send_private_message { | ||
| 54 | my ($self, $params) = @_; | ||
| 55 | |||
| 56 | # For simplicity, this example avoids resolving username <--> uuid | ||
| 57 | my $uuid = $params->{'to_user'}; | ||
| 58 | my $msg = $params->{'message'}; | ||
| 59 | my $from = $self->uuid; | ||
| 60 | |||
| 61 | return unless (defined $msg && length $msg); | ||
| 62 | |||
| 63 | # Push notification to specific user | ||
| 64 | $self->send_notification( | ||
| 65 | method => 'myapp.chat.pmessage', | ||
| 66 | address => "frontend.user-$uuid", | ||
| 67 | params => { from => $from, message => $msg }, | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub send_notice { | ||
| 72 | my ($self, $params) = @_; | ||
| 73 | |||
| 74 | my $uuid = $params->{'to_uuid'}; | ||
| 75 | my $msg = $params->{'message'}; | ||
| 76 | |||
| 77 | $self->send_notification( | ||
| 78 | method => 'myapp.chat.pmessage', | ||
| 79 | address => "frontend.user-$uuid", | ||
| 80 | params => { message => $msg }, | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub ping { | ||
| 85 | my ($self, $params) = @_; | ||
| 86 | |||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; |