Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Fixed the webhook commands
Browse files Browse the repository at this point in the history
  • Loading branch information
darkalfx committed Apr 10, 2020
1 parent 0a969c3 commit 0086c2e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Requestrr.WebApi/ClientApp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Requestrr",
"version": "1.0.10",
"version": "1.0.11",
"description": "Requestrr is a server designed to faciliate the request of media through chat applications.",
"main": "index.js",
"author": "Darkalfx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Footer extends React.Component {
<Col xl="6">
<div className="copyright text-center text-xl-left text-muted">
© {new Date().getFullYear()}{" "}
Requestrr (v1.0.10)
Requestrr (v1.0.11)
</div>
</Col>
</Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AuthFooter extends React.Component {
<Col xl="6">
<div className="copyright text-center text-xl-left text-muted">
© {new Date().getFullYear()}{" "}
Requestrr (v1.0.10)
Requestrr (v1.0.11)
</div>
</Col>
</Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,37 @@ public async Task HandleMovieRequestAsync(object[] args)
return;
}

var IsWebHook = this.Context.Message.Source == MessageSource.Webhook;
var movieName = stringArgs[0].ToString();

await DeleteSafeAsync(this.Context.Message);
if (IsWebHook)
{
await ForceDeleteSafeAsync(this.Context.Message);
}
else
{
await DeleteSafeAsync(this.Context.Message);
}

IMovieUserInterface userInterface = this;

if (IsWebHook)
{
userInterface = new WebHookMovieUserInterface(this);
}

IMovieNotificationWorkflow movieNotificationWorkflow = new DisabledMovieNotificationWorkflow(this);
IMovieNotificationWorkflow movieNotificationWorkflow = new DisabledMovieNotificationWorkflow(userInterface);

if (_discordSettings.NotificationMode != NotificationMode.Disabled)
if (_discordSettings.NotificationMode != NotificationMode.Disabled && !IsWebHook)
{
movieNotificationWorkflow = new MovieNotificationWorkflow(_notificationRequestRepository, this, _discordSettings.AutomaticallyNotifyRequesters);
movieNotificationWorkflow = new MovieNotificationWorkflow(_notificationRequestRepository, userInterface, _discordSettings.AutomaticallyNotifyRequesters);
}

var workFlow = new MovieRequestingWorkflow(
new MovieUserRequester(this.Context.Message.Author.Id.ToString(), $"{this.Context.Message.Author.Username}#{this.Context.Message.Author.Discriminator}"),
_movieSearcher,
_movieRequester,
this.Context.Message.Source == MessageSource.Webhook ? (IMovieUserInterface)new WebHookMovieUserInterface(this) : (IMovieUserInterface)this,
userInterface,
movieNotificationWorkflow);

await workFlow.RequestMovieAsync(movieName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,39 @@ public async Task HandleTvShowRequestAsync(object[] args)
return;
}

var IsWebHook = this.Context.Message.Source == MessageSource.Webhook;
var tvShowName = stringArgs[0].ToString();

await DeleteSafeAsync(this.Context.Message);
if (IsWebHook)
{
await ForceDeleteSafeAsync(this.Context.Message);
}
else
{
await DeleteSafeAsync(this.Context.Message);
}

ITvShowUserInterface userInterface = this;

if (IsWebHook)
{
var webhookMessage = tvShowName.Split("//");
userInterface = new WebHookTvShowUserInterface(this, webhookMessage[1]);
tvShowName = webhookMessage[0];
}

ITvShowNotificationWorkflow tvShowNotificationWorkflow = new DisabledTvShowNotificationWorkflow(this);
ITvShowNotificationWorkflow tvShowNotificationWorkflow = new DisabledTvShowNotificationWorkflow(userInterface);

if (_discordSettings.NotificationMode != NotificationMode.Disabled)
if (_discordSettings.NotificationMode != NotificationMode.Disabled && !IsWebHook)
{
tvShowNotificationWorkflow = new TvShowNotificationWorkflow(_notificationsRepository, this, _discordSettings.AutomaticallyNotifyRequesters);
tvShowNotificationWorkflow = new TvShowNotificationWorkflow(_notificationsRepository, userInterface, _discordSettings.AutomaticallyNotifyRequesters);
}

var workFlow = new TvShowRequestingWorkflow(
new TvShowUserRequester(this.Context.Message.Author.Id.ToString(), $"{this.Context.Message.Author.Username}#{this.Context.Message.Author.Discriminator}"),
_tvShowSearcher,
_tvShowRequester,
this.Context.Message.Source == MessageSource.Webhook ? (ITvShowUserInterface)new WebHookTvShowUserInterface(_discord, this.Context, _discordSettingsProvider, this) : (ITvShowUserInterface)this,
userInterface,
tvShowNotificationWorkflow);

await workFlow.RequestTvShowAsync(tvShowName);
Expand Down
25 changes: 13 additions & 12 deletions Requestrr.WebApi/RequestrrBot/Movies/WebHookMovieUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@ public WebHookMovieUserInterface(IMovieUserInterface movieUserInterface)

public Task<bool> AskForNotificationRequestAsync()
{
return _movieUserInterface.AskForNotificationRequestAsync();
return Task.FromResult(false);
}

public Task DisplayMovieDetailsAsync(Movie movie)
{
return _movieUserInterface.DisplayMovieDetailsAsync(movie);
return Task.CompletedTask;
}

public Task DisplayNotificationSuccessAsync(Movie movie)
{
return _movieUserInterface.DisplayNotificationSuccessAsync(movie);
return Task.CompletedTask;
}

public Task DisplayRequestDeniedAsync(Movie movie)
{
return _movieUserInterface.DisplayRequestDeniedAsync(movie);
return Task.CompletedTask;
}

public Task DisplayRequestSuccessAsync(Movie movie)
public async Task DisplayRequestSuccessAsync(Movie movie)
{
return _movieUserInterface.DisplayRequestSuccessAsync(movie);
await _movieUserInterface.DisplayMovieDetailsAsync(movie);
await _movieUserInterface.DisplayRequestSuccessAsync(movie);
}

public Task<bool> GetMovieRequestAsync(Movie movie)
Expand All @@ -54,32 +55,32 @@ public Task<MovieSelection> GetMovieSelectionAsync(IReadOnlyList<Movie> movies)

public Task WarnInvalidMovieSelectionAsync()
{
return _movieUserInterface.WarnInvalidMovieSelectionAsync();
return Task.CompletedTask;
}

public Task WarnMovieAlreadyAvailableAsync()
{
return _movieUserInterface.WarnMovieAlreadyAvailableAsync();
return Task.CompletedTask;
}

public Task WarnMovieAlreadyRequestedAsync()
{
return _movieUserInterface.WarnMovieAlreadyRequestedAsync();
return Task.CompletedTask;
}

public Task WarnMovieUnavailableAndAlreadyHasNotificationAsync()
{
return _movieUserInterface.WarnMovieUnavailableAndAlreadyHasNotificationAsync();
return Task.CompletedTask;
}

public Task WarnNoMovieFoundAsync(string movieName)
{
return _movieUserInterface.WarnNoMovieFoundAsync(movieName);
return Task.CompletedTask;
}

public Task WarnNoMovieFoundByTheMovieDbIdAsync(string theMovieDbIdTextValue)
{
return _movieUserInterface.WarnNoMovieFoundByTheMovieDbIdAsync(theMovieDbIdTextValue);
return Task.CompletedTask;
}
}
}
76 changes: 35 additions & 41 deletions Requestrr.WebApi/RequestrrBot/TvShows/WebHookTvShowUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,88 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using Requestrr.WebApi.RequestrrBot.ChatClients.Discord;

namespace Requestrr.WebApi.RequestrrBot.TvShows
{
public class WebHookTvShowUserInterface : RequestrrModuleBase<SocketCommandContext>, ITvShowUserInterface
public class WebHookTvShowUserInterface : ITvShowUserInterface
{
private readonly string _seasonSpecified;
private readonly ITvShowUserInterface _tvShowUserInterface;
private TvShow _selectedTvShow;

public WebHookTvShowUserInterface(
DiscordSocketClient discord,
SocketCommandContext commandContext,
DiscordSettingsProvider discordSettingsProvider,
ITvShowUserInterface tvShowUserInterface)
: base(discord, commandContext, discordSettingsProvider)
ITvShowUserInterface tvShowUserInterface,
string seasonSpecified)
{
_seasonSpecified = seasonSpecified;
_tvShowUserInterface = tvShowUserInterface;
}

public Task<bool> AskForSeasonNotificationRequestAsync(TvShow tvShow, TvSeason requestedSeason)
{
return _tvShowUserInterface.AskForSeasonNotificationRequestAsync(tvShow, requestedSeason);
return Task.FromResult(false);
}

public Task DisplayNotificationSuccessForSeasonAsync(TvSeason requestedSeason)
{
return _tvShowUserInterface.DisplayNotificationSuccessForSeasonAsync(requestedSeason);
return Task.CompletedTask;
}

public Task DisplayRequestDeniedForSeasonAsync(TvSeason selectedSeason)
{
return _tvShowUserInterface.DisplayRequestDeniedForSeasonAsync(selectedSeason);
return Task.CompletedTask;
}

public Task DisplayRequestSuccessForSeasonAsync(TvSeason selectedSeason)
public async Task DisplayRequestSuccessForSeasonAsync(TvSeason selectedSeason)
{
return _tvShowUserInterface.DisplayRequestSuccessForSeasonAsync(selectedSeason);
await _tvShowUserInterface.DisplayTvShowDetailsAsync(_selectedTvShow);
await _tvShowUserInterface.DisplayRequestSuccessForSeasonAsync(selectedSeason);
}

public Task DisplayTvShowDetailsAsync(TvShow tvShow)
{
return _tvShowUserInterface.DisplayTvShowDetailsAsync(tvShow);
_selectedTvShow = tvShow;
return Task.CompletedTask;
}

public Task<bool> GetTvShowRequestConfirmationAsync(TvSeason season)
{
return Task.FromResult(true);
}

public async Task<TvSeasonsSelection> GetTvShowSeasonSelectionAsync(TvShow tvShow)
public Task<TvSeasonsSelection> GetTvShowSeasonSelectionAsync(TvShow tvShow)
{
var tvShowSeasons = tvShow.Seasons;

var selectionMessage = await NextMessageAsync(Context);
var selectionMessageContent = selectionMessage?.Content?.Trim();

await DeleteSafeAsync(selectionMessage);

if (selectionMessageContent.ToLower().StartsWith("all") && tvShowSeasons.OfType<AllTvSeasons>().Any())
if (_seasonSpecified.ToLower().StartsWith("all") && tvShowSeasons.OfType<AllTvSeasons>().Any())
{
return new TvSeasonsSelection
return Task.FromResult(new TvSeasonsSelection
{
SelectedSeason = tvShowSeasons.OfType<AllTvSeasons>().First(),
};
});
}
else if (selectionMessageContent.ToLower().StartsWith("future") && tvShowSeasons.OfType<FutureTvSeasons>().Any())
else if (_seasonSpecified.ToLower().StartsWith("future") && tvShowSeasons.OfType<FutureTvSeasons>().Any())
{
return new TvSeasonsSelection
return Task.FromResult(new TvSeasonsSelection
{
SelectedSeason = tvShowSeasons.OfType<FutureTvSeasons>().First(),
};
});
}
else if (int.TryParse(selectionMessageContent, out var selectedSeasonNumber) && tvShowSeasons.Any(x => x.SeasonNumber == selectedSeasonNumber))
else if (int.TryParse(_seasonSpecified, out var selectedSeasonNumber) && tvShowSeasons.Any(x => x.SeasonNumber == selectedSeasonNumber))
{
var selectedSeason = tvShowSeasons.Single(x => x.SeasonNumber == selectedSeasonNumber);

return new TvSeasonsSelection
return Task.FromResult(new TvSeasonsSelection
{
SelectedSeason = selectedSeason
};
});
}
else
{
return new TvSeasonsSelection
return Task.FromResult(new TvSeasonsSelection
{
IsCancelled = true
};
});
}
}

Expand All @@ -103,47 +97,47 @@ public Task<TvShowSelection> GetTvShowSelectionAsync(IReadOnlyList<SearchedTvSho

public Task WarnAlreadyNotifiedForSeasonsAsync(TvShow tvShow, TvSeason requestedSeason)
{
return _tvShowUserInterface.WarnAlreadyNotifiedForSeasonsAsync(tvShow, requestedSeason);
return Task.CompletedTask;
}

public Task WarnAlreadySeasonAlreadyRequestedAsync(TvShow tvShow, TvSeason selectedSeason)
{
return _tvShowUserInterface.WarnAlreadySeasonAlreadyRequestedAsync(tvShow, selectedSeason);
return Task.CompletedTask;
}

public Task WarnInvalidSeasonSelectionAsync()
{
return _tvShowUserInterface.WarnInvalidSeasonSelectionAsync();
return Task.CompletedTask;
}

public Task WarnInvalidTvShowSelectionAsync()
{
return _tvShowUserInterface.WarnInvalidTvShowSelectionAsync();
return Task.CompletedTask;
}

public Task WarnNoTvShowFoundAsync(string tvShowName)
{
return _tvShowUserInterface.WarnNoTvShowFoundAsync(tvShowName);
return Task.CompletedTask;
}

public Task WarnNoTvShowFoundByTvDbIdAsync(string tvDbIdTextValue)
{
return _tvShowUserInterface.WarnNoTvShowFoundByTvDbIdAsync(tvDbIdTextValue);
return Task.CompletedTask;
}

public Task WarnSeasonAlreadyAvailableAsync(TvSeason requestedSeason)
{
return _tvShowUserInterface.WarnSeasonAlreadyAvailableAsync(requestedSeason);
return Task.CompletedTask;
}

public Task WarnShowCannotBeRequestedAsync(TvShow tvShow)
{
return _tvShowUserInterface.WarnShowCannotBeRequestedAsync(tvShow);
return Task.CompletedTask;
}

public Task WarnShowHasEndedAsync(TvShow tvShow)
{
return _tvShowUserInterface.WarnShowHasEndedAsync(tvShow);
return Task.CompletedTask;
}
}
}
15 changes: 15 additions & 0 deletions Requestrr.WebApi/RequestrrModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ protected async Task DeleteSafeAsync(IMessage message, RequestOptions options =
}
}

protected async Task ForceDeleteSafeAsync(IMessage message, RequestOptions options = null)
{
try
{
if (message != null && !(message.Channel is IPrivateChannel))
{
await message.DeleteAsync();
}
}
catch
{
//We did our best
}
}

protected Task<IUserMessage> ReplyToUserAsync(string message)
{
return ReplyAsync($"{Context.Message.Author.Mention} " + message);
Expand Down

0 comments on commit 0086c2e

Please sign in to comment.