From 2fbf7a41142bec2ded60ddc2b55f15c5e78e20c6 Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Sat, 28 Mar 2015 22:30:58 -0700 Subject: [PATCH 1/2] Inital work on Twitter notifications --- src/NzbDrone.Api/ProviderModuleBase.cs | 25 +- src/NzbDrone.Api/REST/RestModule.cs | 4 +- .../Annotations/FieldDefinitionAttribute.cs | 1 + .../Download/DownloadClientBase.cs | 2 + src/NzbDrone.Core/Indexers/IndexerBase.cs | 1 + src/NzbDrone.Core/Metadata/MetadataBase.cs | 2 + .../Notifications/NotificationBase.cs | 3 + .../Notifications/Twitter/Twitter.cs | 81 ++++++ .../Notifications/Twitter/TwitterService.cs | 127 +++++++++ .../Notifications/Twitter/TwitterSettings.cs | 57 ++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 8 + src/NzbDrone.Core/ThingiProvider/IProvider.cs | 1 + .../ThingiProvider/IProviderFactory.cs | 2 + .../ThingiProvider/ProviderFactory.cs | 5 + src/NzbDrone.Core/TinyTwitter.cs | 262 ++++++++++++++++++ src/NzbDrone.Core/packages.config | 2 + src/UI/Content/oauthLand.html | 13 + src/UI/Form/ActionTemplate.hbs | 7 + src/UI/Form/FormBuilder.js | 9 + src/UI/Form/HiddenTemplate.hbs | 9 +- .../Edit/NotificationEditView.js | 20 +- src/UI/Settings/ProviderSettingsModelBase.js | 60 ++++ 22 files changed, 683 insertions(+), 18 deletions(-) create mode 100644 src/NzbDrone.Core/Notifications/Twitter/Twitter.cs create mode 100644 src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs create mode 100644 src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs create mode 100644 src/NzbDrone.Core/TinyTwitter.cs create mode 100644 src/UI/Content/oauthLand.html create mode 100644 src/UI/Form/ActionTemplate.hbs diff --git a/src/NzbDrone.Api/ProviderModuleBase.cs b/src/NzbDrone.Api/ProviderModuleBase.cs index 660bc2b46..0baf8fa99 100644 --- a/src/NzbDrone.Api/ProviderModuleBase.cs +++ b/src/NzbDrone.Api/ProviderModuleBase.cs @@ -11,6 +11,7 @@ using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; using Omu.ValueInjecter; +using Newtonsoft.Json; namespace NzbDrone.Api { @@ -27,7 +28,8 @@ protected ProviderModuleBase(IProviderFactory pr _providerFactory = providerFactory; Get["schema"] = x => GetTemplates(); - Post["test"] = x => Test(ReadResourceFromRequest()); + Post["test"] = x => Test(ReadResourceFromRequest(true)); + Post["connectData/{stage}"] = x => ConnectData(x.stage, ReadResourceFromRequest(true)); GetResourceAll = GetAll; GetResourceById = GetProviderById; @@ -98,7 +100,7 @@ private void UpdateProvider(TProviderResource providerResource) _providerFactory.Update(providerDefinition); } - private TProviderDefinition GetDefinition(TProviderResource providerResource, bool includeWarnings = false) + private TProviderDefinition GetDefinition(TProviderResource providerResource, bool includeWarnings = false, bool validate = true) { var definition = new TProviderDefinition(); @@ -111,8 +113,10 @@ private TProviderDefinition GetDefinition(TProviderResource providerResource, bo var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract); definition.Settings = (IProviderConfig)SchemaBuilder.ReadFormSchema(providerResource.Fields, configContract, preset); - - Validate(definition, includeWarnings); + if (validate) + { + Validate(definition, includeWarnings); + } return definition; } @@ -163,6 +167,19 @@ private Response Test(TProviderResource providerResource) return "{}"; } + + private Response ConnectData(string stage, TProviderResource providerResource) + { + TProviderDefinition providerDefinition = GetDefinition(providerResource, true, false); + + if (!providerDefinition.Enable) return "{}"; + + object data = _providerFactory.ConnectData(providerDefinition, stage, (IDictionary) Request.Query.ToDictionary()); + Response resp = JsonConvert.SerializeObject(data); + resp.ContentType = "application/json"; + return resp; + } + protected virtual void Validate(TProviderDefinition definition, bool includeWarnings) { var validationResult = definition.Settings.Validate(); diff --git a/src/NzbDrone.Api/REST/RestModule.cs b/src/NzbDrone.Api/REST/RestModule.cs index 434dd804f..893d690bd 100644 --- a/src/NzbDrone.Api/REST/RestModule.cs +++ b/src/NzbDrone.Api/REST/RestModule.cs @@ -182,7 +182,7 @@ protected Action UpdateResource } } - protected TResource ReadResourceFromRequest() + protected TResource ReadResourceFromRequest(bool skipValidate = false) { //TODO: handle when request is null var resource = Request.Body.FromJson(); @@ -194,7 +194,7 @@ protected TResource ReadResourceFromRequest() var errors = SharedValidator.Validate(resource).Errors.ToList(); - if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase) && !Request.Url.Path.EndsWith("/test", StringComparison.InvariantCultureIgnoreCase)) + if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase) && !skipValidate && !Request.Url.Path.EndsWith("/test", StringComparison.InvariantCultureIgnoreCase)) { errors.AddRange(PostValidator.Validate(resource).Errors); } diff --git a/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs b/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs index 60a1a8223..53e79d31a 100644 --- a/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs +++ b/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs @@ -29,5 +29,6 @@ public enum FieldType Path, Hidden, Tag + Action } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Download/DownloadClientBase.cs b/src/NzbDrone.Core/Download/DownloadClientBase.cs index 69e93f813..1934a00df 100644 --- a/src/NzbDrone.Core/Download/DownloadClientBase.cs +++ b/src/NzbDrone.Core/Download/DownloadClientBase.cs @@ -50,6 +50,8 @@ public IEnumerable DefaultDefinitions public ProviderDefinition Definition { get; set; } + public object ConnectData(string stage, IDictionary query) { return null; } + protected TSettings Settings { get diff --git a/src/NzbDrone.Core/Indexers/IndexerBase.cs b/src/NzbDrone.Core/Indexers/IndexerBase.cs index ea23275d8..92f923087 100644 --- a/src/NzbDrone.Core/Indexers/IndexerBase.cs +++ b/src/NzbDrone.Core/Indexers/IndexerBase.cs @@ -65,6 +65,7 @@ public virtual IEnumerable DefaultDefinitions } public virtual ProviderDefinition Definition { get; set; } + public object ConnectData(string stage, IDictionary query) { return null; } protected TSettings Settings { diff --git a/src/NzbDrone.Core/Metadata/MetadataBase.cs b/src/NzbDrone.Core/Metadata/MetadataBase.cs index 07bbcbce1..37f5666cf 100644 --- a/src/NzbDrone.Core/Metadata/MetadataBase.cs +++ b/src/NzbDrone.Core/Metadata/MetadataBase.cs @@ -52,6 +52,8 @@ public ValidationResult Test() public abstract List SeasonImages(Series series, Season season); public abstract List EpisodeImages(Series series, EpisodeFile episodeFile); + public object ConnectData(string stage, IDictionary query) { return null; } + protected TSettings Settings { get diff --git a/src/NzbDrone.Core/Notifications/NotificationBase.cs b/src/NzbDrone.Core/Notifications/NotificationBase.cs index fda893ab3..469f6cafa 100644 --- a/src/NzbDrone.Core/Notifications/NotificationBase.cs +++ b/src/NzbDrone.Core/Notifications/NotificationBase.cs @@ -60,5 +60,8 @@ public override string ToString() { return GetType().Name; } + + public virtual object ConnectData(string stage, IDictionary query) { return null; } + } } diff --git a/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs b/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs new file mode 100644 index 000000000..fe81ad80b --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs @@ -0,0 +1,81 @@ +using System.Collections.Generic; +using FluentValidation.Results; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Tv; +using System; +using OAuth; +using System.Net; +using System.IO; + +namespace NzbDrone.Core.Notifications.Twitter +{ + class Twitter : NotificationBase + { + + private readonly ITwitterService _TwitterService; + + public Twitter(ITwitterService TwitterService) + { + _TwitterService = TwitterService; + } + + public override string Link + { + get { return "https://twitter.com/"; } + } + + public override void OnGrab(string message) + { + _TwitterService.SendNotification(message, Settings.AccessToken, Settings.AccessTokenSecret, Settings.ConsumerKey, Settings.ConsumerSecret); + } + + public override void OnDownload(DownloadMessage message) + { + _TwitterService.SendNotification(message.Message, Settings.AccessToken, Settings.AccessTokenSecret, Settings.ConsumerKey, Settings.ConsumerSecret); + } + + public override void AfterRename(Series series) + { + } + + public override object ConnectData(string stage, IDictionary query) + { + if (stage == "step1") + { + return new + { + nextStep = "step2", + action = "openwindow", + url = _TwitterService.GetOAuthRedirect( + Settings.ConsumerKey, + Settings.ConsumerSecret, + "http://localhost:8989/Content/oauthLand.html" /* FIXME - how do I get http host and such */ + ) + }; + } + else if (stage == "step2") + { + return new + { + action = "updatefields", + fields = _TwitterService.GetOAuthToken( + Settings.ConsumerKey, Settings.ConsumerSecret, + query["oauth_token"].ToString(), + query["oauth_verifier"].ToString() + ) + }; + } + return new {}; + } + + public override ValidationResult Test() + { + var failures = new List(); + + failures.AddIfNotNull(_TwitterService.Test(Settings)); + + return new ValidationResult(failures); + } + } + +} diff --git a/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs b/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs new file mode 100644 index 000000000..fadfa29d7 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs @@ -0,0 +1,127 @@ +using FluentValidation.Results; +using NzbDrone.Common.Extensions; +using NLog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using OAuth; +using System.Net; +using System.Collections.Specialized; + +namespace NzbDrone.Core.Notifications.Twitter +{ + public interface ITwitterService + { + void SendNotification(string message, String accessToken, String accessTokenSecret, String consumerKey, String consumerSecret); + ValidationFailure Test(TwitterSettings settings); + string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback); + object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier); + } + + public class TwitterService : ITwitterService + { + private readonly Logger _logger; + + public TwitterService(Logger logger) + { + _logger = logger; + + var logo = typeof(TwitterService).Assembly.GetManifestResourceBytes("NzbDrone.Core.Resources.Logo.64.png"); + } + + private NameValueCollection oauthQuery(OAuthRequest client) + { + // Using HTTP header authorization + string auth = client.GetAuthorizationHeader(); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(client.RequestUrl); + + request.Headers.Add("Authorization", auth); + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + System.Collections.Specialized.NameValueCollection qscoll; + using (var reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"))) + { + string responseText = reader.ReadToEnd(); + return System.Web.HttpUtility.ParseQueryString(responseText); + } + return null; + } + + public object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier) + { + // Creating a new instance with a helper method + OAuthRequest client = OAuthRequest.ForAccessToken( + consumerKey, + consumerSecret, + oauthToken, + "", + oauthVerifier + ); + client.RequestUrl = "https://api.twitter.com/oauth/access_token"; + NameValueCollection qscoll = oauthQuery(client); + + return new + { + AccessToken = qscoll["oauth_token"], + AccessTokenSecret = qscoll["oauth_token_secret"] + }; + } + + public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback) + { + // Creating a new instance with a helper method + OAuthRequest client = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callback); + client.RequestUrl = "https://api.twitter.com/oauth/request_token"; + NameValueCollection qscoll = oauthQuery(client); + + return "https://api.twitter.com/oauth/authorize?oauth_token=" + qscoll["oauth_token"]; + } + + public void SendNotification(string message, String accessToken, String accessTokenSecret, String consumerKey, String consumerSecret) + { + try + { + var oauth = new TinyTwitter.OAuthInfo + { + AccessToken = accessToken, + AccessSecret = accessTokenSecret, + ConsumerKey = consumerKey, + ConsumerSecret = consumerSecret + }; + var twitter = new TinyTwitter.TinyTwitter(oauth); + twitter.UpdateStatus(message); + } + catch (WebException e) + { + using (WebResponse response = e.Response) + { + HttpWebResponse httpResponse = (HttpWebResponse)response; + Console.WriteLine("Error code: {0}", httpResponse.StatusCode); + using (System.IO.Stream data = response.GetResponseStream()) + using (var reader = new System.IO.StreamReader(data)) + { + string text = reader.ReadToEnd(); + Console.WriteLine(text); + } + } + throw e; + } + return; + } + + public ValidationFailure Test(TwitterSettings settings) + { + try + { + string body = "This is a test message from Sonarr @ " + DateTime.Now.ToString(); + SendNotification(body, settings.AccessToken, settings.AccessTokenSecret, settings.ConsumerKey, settings.ConsumerSecret); + } + catch (Exception ex) + { + _logger.ErrorException("Unable to send test message: " + ex.Message, ex); + return new ValidationFailure("Host", "Unable to send test message"); + } + return null; + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs b/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs new file mode 100644 index 000000000..27b086fcd --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs @@ -0,0 +1,57 @@ +using System; +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Notifications.Twitter +{ + public class TwitterSettingsValidator : AbstractValidator + { + public TwitterSettingsValidator() + { + RuleFor(c => c.AccessToken).NotEmpty(); + RuleFor(c => c.AccessTokenSecret).NotEmpty(); + RuleFor(c => c.ConsumerKey).NotEmpty(); + RuleFor(c => c.ConsumerSecret).NotEmpty(); + } + } + + public class TwitterSettings : IProviderConfig + { + private static readonly TwitterSettingsValidator Validator = new TwitterSettingsValidator(); + + public TwitterSettings() + { + ConsumerKey = "3POVsO3KW90LKZXyzPOjQ"; /* FIXME - Key from Couchpotato so needs to be replaced */ + ConsumerSecret = "Qprb94hx9ucXvD4Wvg2Ctsk4PDK7CcQAKgCELXoyIjE"; /* FIXME - Key from Couchpotato so needs to be replaced */ + AuthorizeNotification = "step1"; + } + + [FieldDefinition(0, Label = "Access Token", Advanced = true)] + public String AccessToken { get; set; } + + [FieldDefinition(1, Label = "Access Token Secret", Advanced = true)] + public String AccessTokenSecret { get; set; } + + public String ConsumerKey { get; set; } + public String ConsumerSecret { get; set; } + + [FieldDefinition(4, Label = "Connect to twitter", Type = FieldType.Action)] + public String AuthorizeNotification { get; set; } + + public bool IsValid + { + get + { + return !string.IsNullOrWhiteSpace(AccessToken) && !string.IsNullOrWhiteSpace(AccessTokenSecret) && + !string.IsNullOrWhiteSpace(ConsumerKey) && !string.IsNullOrWhiteSpace(ConsumerSecret); + } + } + + public NzbDroneValidationResult Validate() + { + return new NzbDroneValidationResult(Validator.Validate(this)); + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index b522680b0..ba494028b 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -80,6 +80,9 @@ False ..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll + + ..\packages\OAuth.1.0.3\lib\net40\OAuth.dll + False ..\packages\RestSharp.105.0.1\lib\net4\RestSharp.dll @@ -93,6 +96,7 @@ + @@ -740,6 +744,9 @@ + + + @@ -916,6 +923,7 @@ + diff --git a/src/NzbDrone.Core/ThingiProvider/IProvider.cs b/src/NzbDrone.Core/ThingiProvider/IProvider.cs index a5bc804c8..88ee2881c 100644 --- a/src/NzbDrone.Core/ThingiProvider/IProvider.cs +++ b/src/NzbDrone.Core/ThingiProvider/IProvider.cs @@ -12,5 +12,6 @@ public interface IProvider IEnumerable DefaultDefinitions { get; } ProviderDefinition Definition { get; set; } ValidationResult Test(); + object ConnectData(string stage, IDictionary query); } } \ No newline at end of file diff --git a/src/NzbDrone.Core/ThingiProvider/IProviderFactory.cs b/src/NzbDrone.Core/ThingiProvider/IProviderFactory.cs index 459d25074..d3cc0dbe7 100644 --- a/src/NzbDrone.Core/ThingiProvider/IProviderFactory.cs +++ b/src/NzbDrone.Core/ThingiProvider/IProviderFactory.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using FluentValidation.Results; +using System; namespace NzbDrone.Core.ThingiProvider { @@ -18,5 +19,6 @@ public interface IProviderFactory TProviderDefinition GetProviderCharacteristics(TProvider provider, TProviderDefinition definition); TProvider GetInstance(TProviderDefinition definition); ValidationResult Test(TProviderDefinition definition); + object ConnectData(TProviderDefinition definition, string stage, IDictionary query ); } } \ No newline at end of file diff --git a/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs b/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs index 908d9f840..a9f0b0bc2 100644 --- a/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs +++ b/src/NzbDrone.Core/ThingiProvider/ProviderFactory.cs @@ -81,6 +81,11 @@ public ValidationResult Test(TProviderDefinition definition) return GetInstance(definition).Test(); } + public object ConnectData(TProviderDefinition definition, string stage, IDictionary query) + { + return GetInstance(definition).ConnectData(stage, query); + } + public List GetAvailableProviders() { return Active().Select(GetInstance).ToList(); diff --git a/src/NzbDrone.Core/TinyTwitter.cs b/src/NzbDrone.Core/TinyTwitter.cs new file mode 100644 index 000000000..7c05dd65e --- /dev/null +++ b/src/NzbDrone.Core/TinyTwitter.cs @@ -0,0 +1,262 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using System.Web.Script.Serialization; + +namespace TinyTwitter +{ + public class OAuthInfo + { + public string ConsumerKey { get; set; } + public string ConsumerSecret { get; set; } + public string AccessToken { get; set; } + public string AccessSecret { get; set; } + } + + public class Tweet + { + public long Id { get; set; } + public DateTime CreatedAt { get; set; } + public string UserName { get; set; } + public string ScreenName { get; set; } + public string Text { get; set; } + } + + public class TinyTwitter + { + private readonly OAuthInfo oauth; + + public TinyTwitter(OAuthInfo oauth) + { + this.oauth = oauth; + } + + public void UpdateStatus(string message) + { + new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json") + .AddParameter("status", message) + .Execute(); + } + + public IEnumerable GetHomeTimeline(long? sinceId = null, int? count = 20) + { + return GetTimeline("http://api.twitter.com/1.1/statuses/home_timeline.json", sinceId, count); + } + + public IEnumerable GetMentions(long? sinceId = null, int? count = 20) + { + return GetTimeline("http://api.twitter.com/1.1/statuses/mentions.json", sinceId, count); + } + + public IEnumerable GetUserTimeline(long? sinceId = null, int? count = 20) + { + return GetTimeline("http://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, count); + } + + private IEnumerable GetTimeline(string url, long? sinceId, int? count) + { + var builder = new RequestBuilder(oauth, "GET", url); + + if (sinceId.HasValue) + builder.AddParameter("since_id", sinceId.Value.ToString()); + + if (count.HasValue) + builder.AddParameter("count", count.Value.ToString()); + + using (var response = builder.Execute()) + using (var stream = response.GetResponseStream()) + using (var reader = new StreamReader(stream)) + { + var content = reader.ReadToEnd(); + var serializer = new JavaScriptSerializer(); + + var tweets = (object[])serializer.DeserializeObject(content); + + return tweets.Cast>().Select(tweet => + { + var user = ((Dictionary)tweet["user"]); + var date = DateTime.ParseExact(tweet["created_at"].ToString(), + "ddd MMM dd HH:mm:ss zz00 yyyy", + CultureInfo.InvariantCulture).ToLocalTime(); + return new Tweet + { + Id = (long)tweet["id"], + CreatedAt = + date, + Text = (string)tweet["text"], + UserName = (string)user["name"], + ScreenName = (string)user["screen_name"] + }; + }).ToArray(); + } + } + + #region RequestBuilder + + public class RequestBuilder + { + private const string VERSION = "1.0"; + private const string SIGNATURE_METHOD = "HMAC-SHA1"; + + private readonly OAuthInfo oauth; + private readonly string method; + private readonly IDictionary customParameters; + private readonly string url; + + public RequestBuilder(OAuthInfo oauth, string method, string url) + { + this.oauth = oauth; + this.method = method; + this.url = url; + customParameters = new Dictionary(); + } + + public RequestBuilder AddParameter(string name, string value) + { + customParameters.Add(name, value.EncodeRFC3986()); + return this; + } + + public WebResponse Execute() + { + var timespan = GetTimestamp(); + var nonce = CreateNonce(); + + var parameters = new Dictionary(customParameters); + AddOAuthParameters(parameters, timespan, nonce); + + var signature = GenerateSignature(parameters); + var headerValue = GenerateAuthorizationHeaderValue(parameters, signature); + + var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl()); + request.Method = method; + request.ContentType = "application/x-www-form-urlencoded"; + + request.Headers.Add("Authorization", headerValue); + + WriteRequestBody(request); + + // It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions + // after some requests. Abort the request seems to work. More info: + // http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly + + var response = request.GetResponse(); + request.Abort(); + + return response; + } + + private void WriteRequestBody(HttpWebRequest request) + { + if (method == "GET") + return; + + var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString()); + using (var stream = request.GetRequestStream()) + stream.Write(requestBody, 0, requestBody.Length); + } + + private string GetRequestUrl() + { + if (method != "GET" || customParameters.Count == 0) + return url; + + return string.Format("{0}?{1}", url, GetCustomParametersString()); + } + + private string GetCustomParametersString() + { + return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&"); + } + + private string GenerateAuthorizationHeaderValue(IEnumerable> parameters, string signature) + { + return new StringBuilder("OAuth ") + .Append(parameters.Concat(new KeyValuePair("oauth_signature", signature)) + .Where(x => x.Key.StartsWith("oauth_")) + .Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986())) + .Join(",")) + .ToString(); + } + + private string GenerateSignature(IEnumerable> parameters) + { + var dataToSign = new StringBuilder() + .Append(method).Append("&") + .Append(url.EncodeRFC3986()).Append("&") + .Append(parameters + .OrderBy(x => x.Key) + .Select(x => string.Format("{0}={1}", x.Key, x.Value)) + .Join("&") + .EncodeRFC3986()); + + var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986()); + var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey)); + + var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString())); + return Convert.ToBase64String(signatureBytes); + } + + private void AddOAuthParameters(IDictionary parameters, string timestamp, string nonce) + { + parameters.Add("oauth_version", VERSION); + parameters.Add("oauth_consumer_key", oauth.ConsumerKey); + parameters.Add("oauth_nonce", nonce); + parameters.Add("oauth_signature_method", SIGNATURE_METHOD); + parameters.Add("oauth_timestamp", timestamp); + parameters.Add("oauth_token", oauth.AccessToken); + } + + private static string GetTimestamp() + { + return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); + } + + private static string CreateNonce() + { + return new Random().Next(0x0000000, 0x7fffffff).ToString("X8"); + } + } + + #endregion + } + + public static class TinyTwitterHelperExtensions + { + public static string Join(this IEnumerable items, string separator) + { + return string.Join(separator, items.ToArray()); + } + + public static IEnumerable Concat(this IEnumerable items, T value) + { + return items.Concat(new[] { value }); + } + + public static string EncodeRFC3986(this string value) + { + // From Twitterizer http://www.twitterizer.net/ + + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var encoded = Uri.EscapeDataString(value); + + return Regex + .Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()) + .Replace("(", "%28") + .Replace(")", "%29") + .Replace("$", "%24") + .Replace("!", "%21") + .Replace("*", "%2A") + .Replace("'", "%27") + .Replace("%7E", "~"); + } + } +} diff --git a/src/NzbDrone.Core/packages.config b/src/NzbDrone.Core/packages.config index 229277e3e..50c139e3c 100644 --- a/src/NzbDrone.Core/packages.config +++ b/src/NzbDrone.Core/packages.config @@ -7,7 +7,9 @@ + + \ No newline at end of file diff --git a/src/UI/Content/oauthLand.html b/src/UI/Content/oauthLand.html new file mode 100644 index 000000000..5767c06ff --- /dev/null +++ b/src/UI/Content/oauthLand.html @@ -0,0 +1,13 @@ + + + + + oauth landing page + + + + Shouldn't see this + + \ No newline at end of file diff --git a/src/UI/Form/ActionTemplate.hbs b/src/UI/Form/ActionTemplate.hbs new file mode 100644 index 000000000..73991a2da --- /dev/null +++ b/src/UI/Form/ActionTemplate.hbs @@ -0,0 +1,7 @@ +
+ + +
+ +
+
diff --git a/src/UI/Form/FormBuilder.js b/src/UI/Form/FormBuilder.js index ce40e655b..d68608dea 100644 --- a/src/UI/Form/FormBuilder.js +++ b/src/UI/Form/FormBuilder.js @@ -29,6 +29,10 @@ var _fieldBuilder = function(field) { return _templateRenderer.call(field, 'Form/SelectTemplate'); } + if (field.type === 'hidden') { + return _templateRenderer.call(field, 'Form/HiddenTemplate'); + } + if (field.type === 'path') { return _templateRenderer.call(field, 'Form/PathTemplate'); } @@ -37,6 +41,11 @@ var _fieldBuilder = function(field) { return _templateRenderer.call(field, 'Form/TagTemplate'); } + if (field.type === 'action') { + return _templateRenderer.call(field, 'Form/ActionTemplate'); + } + + return _templateRenderer.call(field, 'Form/TextboxTemplate'); }; diff --git a/src/UI/Form/HiddenTemplate.hbs b/src/UI/Form/HiddenTemplate.hbs index 0de66c464..03933b122 100644 --- a/src/UI/Form/HiddenTemplate.hbs +++ b/src/UI/Form/HiddenTemplate.hbs @@ -1,8 +1 @@ - + \ No newline at end of file diff --git a/src/UI/Settings/Notifications/Edit/NotificationEditView.js b/src/UI/Settings/Notifications/Edit/NotificationEditView.js index c1f6b50d0..839d9311c 100644 --- a/src/UI/Settings/Notifications/Edit/NotificationEditView.js +++ b/src/UI/Settings/Notifications/Edit/NotificationEditView.js @@ -13,8 +13,11 @@ var view = Marionette.ItemView.extend({ template : 'Settings/Notifications/Edit/NotificationEditViewTemplate', ui : { - onDownloadToggle : '.x-on-download', - onUpgradeSection : '.x-on-upgrade', + onDownloadToggle : '.x-on-download', + onUpgradeSection : '.x-on-upgrade', + tags : '.x-tags', + indicator : '.x-indicator', + authorizedNotificationButton : '.AuthorizeNotification' tags : '.x-tags', modalBody : '.modal-body', formTag : '.x-form-tag', @@ -23,7 +26,8 @@ var view = Marionette.ItemView.extend({ events : { 'click .x-back' : '_back', - 'change .x-on-download' : '_onDownloadChanged' + 'change .x-on-download' : '_onDownloadChanged', + 'click .AuthorizeNotification' : '_onAuthorizeNotification' }, _deleteView : DeleteView, @@ -81,7 +85,15 @@ var view = Marionette.ItemView.extend({ } else { this.ui.onUpgradeSection.hide(); } - } + }, + + _onAuthorizeNotification : function(e) { + var self = this; + self.ui.indicator.show(); + this.model.connectData(this.ui.authorizedNotificationButton.data('value')).always(function(newValues) { + self.ui.indicator.hide(); + }); + } }); AsModelBoundView.call(view); diff --git a/src/UI/Settings/ProviderSettingsModelBase.js b/src/UI/Settings/ProviderSettingsModelBase.js index 020cc438c..57864a97f 100644 --- a/src/UI/Settings/ProviderSettingsModelBase.js +++ b/src/UI/Settings/ProviderSettingsModelBase.js @@ -3,6 +3,66 @@ var DeepModel = require('backbone.deepmodel'); var Messenger = require('../Shared/Messenger'); module.exports = DeepModel.extend({ + connectData : function(action) { + var self = this; + + this.trigger('connect:sync'); + + var promise = $.Deferred(); + + var callAction = function(action) { + var params = {}; + params.url = self.collection.url + '/connectData/' + action; + params.contentType = 'application/json'; + params.data = JSON.stringify(self.toJSON()); + params.type = 'POST'; + params.isValidatedCall = true; + + $.ajax(params).fail(promise.reject).success(function(response) { + if (response.action) + { + + if (response.action === "openwindow") + { + var connectResponseWindow = window.open(response.url); + var selfWindow = window; + selfWindow.onCompleteOauth = function(query, callback) { + delete selfWindow.onCompleteOauth; + if (response.nextStep) { callAction(response.nextStep + query); } + else { promise.resolve(response); } + callback(); + }; + return; + } + else if (response.action === "updatefields") + { + Object.keys(response.fields).forEach(function(field) { + self.set(field, response.fields[field]); + self.attributes.fields.forEach(function(fieldDef) { + if (fieldDef.name === field) { fieldDef.value = response.fields[field]; } + }); + }); + } + } + if (response.nextStep) { callAction(response.nextStep); } + else { promise.resolve(response); } + }); + }; + + callAction(action); + + Messenger.monitor({ + promise : promise, + successMessage : 'Connecting for \'{0}\' completed'.format(this.get('name')), + errorMessage : 'Connecting for \'{0}\' failed'.format(this.get('name')) + }); + + promise.fail(function(response) { + self.trigger('connect:failed', response); + }); + + return promise; + }, test : function() { var self = this; From b82e830e8648bb0369e6857167037f16338ec833 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 25 Jun 2015 00:10:40 -0700 Subject: [PATCH 2/2] Cleanup and refactoring of Twitter notifications Closes #301 New: Twitter Notifications --- .../Frontend/Mappers/StaticResourceMapper.cs | 3 +- .../Annotations/FieldDefinitionAttribute.cs | 3 +- .../Notifications/Twitter/Twitter.cs | 50 +- .../Notifications/Twitter/TwitterException.cs | 24 + .../Notifications/Twitter/TwitterService.cs | 127 +++--- .../Notifications/Twitter/TwitterSettings.cs | 36 +- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + src/NzbDrone.Core/TinyTwitter.cs | 431 ++++++++++-------- src/UI/Form/ActionTemplate.hbs | 2 +- .../Edit/NotificationEditView.js | 25 +- .../Edit/NotificationEditViewTemplate.hbs | 2 +- src/UI/Settings/ProviderSettingsModelBase.js | 67 ++- src/UI/{Content/oauthLand.html => oauth.html} | 0 13 files changed, 429 insertions(+), 342 deletions(-) create mode 100644 src/NzbDrone.Core/Notifications/Twitter/TwitterException.cs rename src/UI/{Content/oauthLand.html => oauth.html} (100%) diff --git a/src/NzbDrone.Api/Frontend/Mappers/StaticResourceMapper.cs b/src/NzbDrone.Api/Frontend/Mappers/StaticResourceMapper.cs index f22ff9ce1..61ed14e9b 100644 --- a/src/NzbDrone.Api/Frontend/Mappers/StaticResourceMapper.cs +++ b/src/NzbDrone.Api/Frontend/Mappers/StaticResourceMapper.cs @@ -33,7 +33,8 @@ public override bool CanHandle(string resourceUrl) resourceUrl.EndsWith(".map") || resourceUrl.EndsWith(".css") || (resourceUrl.EndsWith(".ico") && !resourceUrl.Equals("/favicon.ico")) || - resourceUrl.EndsWith(".swf"); + resourceUrl.EndsWith(".swf") || + resourceUrl.EndsWith("oauth.html"); } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs b/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs index 53e79d31a..2d6dc5249 100644 --- a/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs +++ b/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; namespace NzbDrone.Core.Annotations { @@ -28,7 +27,7 @@ public enum FieldType Select, Path, Hidden, - Tag + Tag, Action } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs b/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs index fe81ad80b..a6ef406c6 100644 --- a/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs +++ b/src/NzbDrone.Core/Notifications/Twitter/Twitter.cs @@ -2,21 +2,17 @@ using FluentValidation.Results; using NzbDrone.Common.Extensions; using NzbDrone.Core.Tv; -using System; -using OAuth; -using System.Net; -using System.IO; namespace NzbDrone.Core.Notifications.Twitter { class Twitter : NotificationBase { - private readonly ITwitterService _TwitterService; + private readonly ITwitterService _twitterService; - public Twitter(ITwitterService TwitterService) + public Twitter(ITwitterService twitterService) { - _TwitterService = TwitterService; + _twitterService = twitterService; } public override string Link @@ -26,15 +22,15 @@ public override string Link public override void OnGrab(string message) { - _TwitterService.SendNotification(message, Settings.AccessToken, Settings.AccessTokenSecret, Settings.ConsumerKey, Settings.ConsumerSecret); + _twitterService.SendNotification(message, Settings); } public override void OnDownload(DownloadMessage message) { - _TwitterService.SendNotification(message.Message, Settings.AccessToken, Settings.AccessTokenSecret, Settings.ConsumerKey, Settings.ConsumerSecret); + _twitterService.SendNotification(message.Message, Settings); } - public override void AfterRename(Series series) + public override void OnRename(Series series) { } @@ -45,34 +41,42 @@ public override object ConnectData(string stage, IDictionary que return new { nextStep = "step2", - action = "openwindow", - url = _TwitterService.GetOAuthRedirect( - Settings.ConsumerKey, - Settings.ConsumerSecret, - "http://localhost:8989/Content/oauthLand.html" /* FIXME - how do I get http host and such */ - ) + action = "openWindow", + url = _twitterService.GetOAuthRedirect(query["callbackUrl"].ToString()) }; } else if (stage == "step2") { return new { - action = "updatefields", - fields = _TwitterService.GetOAuthToken( - Settings.ConsumerKey, Settings.ConsumerSecret, - query["oauth_token"].ToString(), - query["oauth_verifier"].ToString() - ) + action = "updateFields", + fields = _twitterService.GetOAuthToken(query["oauth_token"].ToString(), query["oauth_verifier"].ToString()) }; } return new {}; } + public override string Name + { + get + { + return "Twitter"; + } + } + + public override bool SupportsOnRename + { + get + { + return false; + } + } + public override ValidationResult Test() { var failures = new List(); - failures.AddIfNotNull(_TwitterService.Test(Settings)); + failures.AddIfNotNull(_twitterService.Test(Settings)); return new ValidationResult(failures); } diff --git a/src/NzbDrone.Core/Notifications/Twitter/TwitterException.cs b/src/NzbDrone.Core/Notifications/Twitter/TwitterException.cs new file mode 100644 index 000000000..99146fa71 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Twitter/TwitterException.cs @@ -0,0 +1,24 @@ +using System; +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Notifications.Twitter +{ + public class TwitterException : NzbDroneException + { + public TwitterException(string message, params object[] args) : base(message, args) + { + } + + public TwitterException(string message) : base(message) + { + } + + public TwitterException(string message, Exception innerException, params object[] args) : base(message, innerException, args) + { + } + + public TwitterException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs b/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs index fadfa29d7..3675b9da2 100644 --- a/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs +++ b/src/NzbDrone.Core/Notifications/Twitter/TwitterService.cs @@ -1,64 +1,54 @@ using FluentValidation.Results; -using NzbDrone.Common.Extensions; using NLog; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; using OAuth; using System.Net; using System.Collections.Specialized; +using System.IO; +using System.Web; +using NzbDrone.Common.Extensions; +using NzbDrone.Common.Http; namespace NzbDrone.Core.Notifications.Twitter { public interface ITwitterService { - void SendNotification(string message, String accessToken, String accessTokenSecret, String consumerKey, String consumerSecret); + void SendNotification(string message, TwitterSettings settings); ValidationFailure Test(TwitterSettings settings); - string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback); - object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier); + string GetOAuthRedirect(string callbackUrl); + object GetOAuthToken(string oauthToken, string oauthVerifier); } public class TwitterService : ITwitterService { + private readonly IHttpClient _httpClient; private readonly Logger _logger; - public TwitterService(Logger logger) + private static string _consumerKey = "5jSR8a3cp0ToOqSMLMv5GtMQD"; + private static string _consumerSecret = "dxoZjyMq4BLsC8KxyhSOrIndhCzJ0Dik2hrLzqyJcqoGk4Pfsp"; + + public TwitterService(IHttpClient httpClient, Logger logger) { + _httpClient = httpClient; _logger = logger; - - var logo = typeof(TwitterService).Assembly.GetManifestResourceBytes("NzbDrone.Core.Resources.Logo.64.png"); } - private NameValueCollection oauthQuery(OAuthRequest client) + private NameValueCollection OAuthQuery(OAuthRequest oAuthRequest) { - // Using HTTP header authorization - string auth = client.GetAuthorizationHeader(); - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(client.RequestUrl); - + var auth = oAuthRequest.GetAuthorizationHeader(); + var request = new Common.Http.HttpRequest(oAuthRequest.RequestUrl); request.Headers.Add("Authorization", auth); - HttpWebResponse response = (HttpWebResponse)request.GetResponse(); - System.Collections.Specialized.NameValueCollection qscoll; - using (var reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"))) - { - string responseText = reader.ReadToEnd(); - return System.Web.HttpUtility.ParseQueryString(responseText); - } - return null; + var response = _httpClient.Get(request); + + return HttpUtility.ParseQueryString(response.Content); } - public object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier) + public object GetOAuthToken(string oauthToken, string oauthVerifier) { // Creating a new instance with a helper method - OAuthRequest client = OAuthRequest.ForAccessToken( - consumerKey, - consumerSecret, - oauthToken, - "", - oauthVerifier - ); - client.RequestUrl = "https://api.twitter.com/oauth/access_token"; - NameValueCollection qscoll = oauthQuery(client); + var oAuthRequest = OAuthRequest.ForAccessToken(_consumerKey, _consumerSecret, oauthToken, "", oauthVerifier); + oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token"; + var qscoll = OAuthQuery(oAuthRequest); return new { @@ -67,54 +57,77 @@ public object GetOAuthToken(string consumerKey, string consumerSecret, string oa }; } - public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback) + public string GetOAuthRedirect(string callbackUrl) { // Creating a new instance with a helper method - OAuthRequest client = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callback); - client.RequestUrl = "https://api.twitter.com/oauth/request_token"; - NameValueCollection qscoll = oauthQuery(client); + var oAuthRequest = OAuthRequest.ForRequestToken(_consumerKey, _consumerSecret, callbackUrl); + oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token"; + var qscoll = OAuthQuery(oAuthRequest); - return "https://api.twitter.com/oauth/authorize?oauth_token=" + qscoll["oauth_token"]; + return String.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]); } - public void SendNotification(string message, String accessToken, String accessTokenSecret, String consumerKey, String consumerSecret) + public void SendNotification(string message, TwitterSettings settings) { try { - var oauth = new TinyTwitter.OAuthInfo + var oAuth = new TinyTwitter.OAuthInfo { - AccessToken = accessToken, - AccessSecret = accessTokenSecret, - ConsumerKey = consumerKey, - ConsumerSecret = consumerSecret + AccessToken = settings.AccessToken, + AccessSecret = settings.AccessTokenSecret, + ConsumerKey = _consumerKey, + ConsumerSecret = _consumerSecret }; - var twitter = new TinyTwitter.TinyTwitter(oauth); - twitter.UpdateStatus(message); + + var twitter = new TinyTwitter.TinyTwitter(oAuth); + + if (settings.DirectMessage) + { + twitter.DirectMessage(message, settings.Mention); + } + + else + { + if (settings.Mention.IsNotNullOrWhiteSpace()) + { + message += String.Format(" @{0}", settings.Mention); + } + + twitter.UpdateStatus(message); + } } catch (WebException e) { - using (WebResponse response = e.Response) + using (var response = e.Response) { - HttpWebResponse httpResponse = (HttpWebResponse)response; - Console.WriteLine("Error code: {0}", httpResponse.StatusCode); - using (System.IO.Stream data = response.GetResponseStream()) - using (var reader = new System.IO.StreamReader(data)) + var httpResponse = (HttpWebResponse)response; + + using (var responseStream = response.GetResponseStream()) { - string text = reader.ReadToEnd(); - Console.WriteLine(text); + if (responseStream == null) + { + _logger.Trace("Status Code: {0}", httpResponse.StatusCode); + throw new TwitterException("Error received from Twitter: " + httpResponse.StatusCode, _logger , e); + } + + using (var reader = new StreamReader(responseStream)) + { + var responseBody = reader.ReadToEnd(); + _logger.Trace("Reponse: {0} Status Code: {1}", responseBody, httpResponse.StatusCode); + throw new TwitterException("Error received from Twitter: " + responseBody, _logger, e); + } } } - throw e; } - return; } public ValidationFailure Test(TwitterSettings settings) { try { - string body = "This is a test message from Sonarr @ " + DateTime.Now.ToString(); - SendNotification(body, settings.AccessToken, settings.AccessTokenSecret, settings.ConsumerKey, settings.ConsumerSecret); + var body = "Sonarr: Test Message @ " + DateTime.Now; + + SendNotification(body, settings); } catch (Exception ex) { diff --git a/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs b/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs index 27b086fcd..2be9cb409 100644 --- a/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs +++ b/src/NzbDrone.Core/Notifications/Twitter/TwitterSettings.cs @@ -1,5 +1,4 @@ -using System; -using FluentValidation; +using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; @@ -12,8 +11,12 @@ public TwitterSettingsValidator() { RuleFor(c => c.AccessToken).NotEmpty(); RuleFor(c => c.AccessTokenSecret).NotEmpty(); - RuleFor(c => c.ConsumerKey).NotEmpty(); - RuleFor(c => c.ConsumerSecret).NotEmpty(); + //TODO: Validate that it is a valid username (numbers, letters and underscores - I think) + RuleFor(c => c.Mention).NotEmpty().When(c => c.DirectMessage); + + RuleFor(c => c.DirectMessage).Equal(true) + .WithMessage("Using Direct Messaging is recommended, or use a private account.") + .AsWarning(); } } @@ -23,31 +26,24 @@ public class TwitterSettings : IProviderConfig public TwitterSettings() { - ConsumerKey = "3POVsO3KW90LKZXyzPOjQ"; /* FIXME - Key from Couchpotato so needs to be replaced */ - ConsumerSecret = "Qprb94hx9ucXvD4Wvg2Ctsk4PDK7CcQAKgCELXoyIjE"; /* FIXME - Key from Couchpotato so needs to be replaced */ + DirectMessage = true; AuthorizeNotification = "step1"; } [FieldDefinition(0, Label = "Access Token", Advanced = true)] - public String AccessToken { get; set; } + public string AccessToken { get; set; } [FieldDefinition(1, Label = "Access Token Secret", Advanced = true)] - public String AccessTokenSecret { get; set; } + public string AccessTokenSecret { get; set; } - public String ConsumerKey { get; set; } - public String ConsumerSecret { get; set; } + [FieldDefinition(2, Label = "Mention", HelpText = "Mention this user in sent tweets")] + public string Mention { get; set; } + + [FieldDefinition(3, Label = "Direct Message", Type = FieldType.Checkbox, HelpText = "Send a direct message instead of a public message")] + public bool DirectMessage { get; set; } [FieldDefinition(4, Label = "Connect to twitter", Type = FieldType.Action)] - public String AuthorizeNotification { get; set; } - - public bool IsValid - { - get - { - return !string.IsNullOrWhiteSpace(AccessToken) && !string.IsNullOrWhiteSpace(AccessTokenSecret) && - !string.IsNullOrWhiteSpace(ConsumerKey) && !string.IsNullOrWhiteSpace(ConsumerSecret); - } - } + public string AuthorizeNotification { get; set; } public NzbDroneValidationResult Validate() { diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index ba494028b..f10c85961 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -743,6 +743,7 @@ + diff --git a/src/NzbDrone.Core/TinyTwitter.cs b/src/NzbDrone.Core/TinyTwitter.cs index 7c05dd65e..508783b6b 100644 --- a/src/NzbDrone.Core/TinyTwitter.cs +++ b/src/NzbDrone.Core/TinyTwitter.cs @@ -11,252 +11,281 @@ namespace TinyTwitter { - public class OAuthInfo - { - public string ConsumerKey { get; set; } - public string ConsumerSecret { get; set; } - public string AccessToken { get; set; } - public string AccessSecret { get; set; } - } + public class OAuthInfo + { + public string ConsumerKey { get; set; } + public string ConsumerSecret { get; set; } + public string AccessToken { get; set; } + public string AccessSecret { get; set; } + } - public class Tweet - { - public long Id { get; set; } - public DateTime CreatedAt { get; set; } - public string UserName { get; set; } - public string ScreenName { get; set; } - public string Text { get; set; } - } + public class Tweet + { + public long Id { get; set; } + public DateTime CreatedAt { get; set; } + public string UserName { get; set; } + public string ScreenName { get; set; } + public string Text { get; set; } + } - public class TinyTwitter - { - private readonly OAuthInfo oauth; + public class TinyTwitter + { + private readonly OAuthInfo oauth; - public TinyTwitter(OAuthInfo oauth) - { - this.oauth = oauth; - } + public TinyTwitter(OAuthInfo oauth) + { + this.oauth = oauth; + } - public void UpdateStatus(string message) - { - new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json") - .AddParameter("status", message) - .Execute(); - } + public void UpdateStatus(string message) + { + new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json") + .AddParameter("status", message) + .Execute(); + } - public IEnumerable GetHomeTimeline(long? sinceId = null, int? count = 20) - { - return GetTimeline("http://api.twitter.com/1.1/statuses/home_timeline.json", sinceId, count); - } + /** + * + * As of June 26th 2015 Direct Messaging is not part of TinyTwitter. + * I have added it to Sonarr's copy to make our implementation easier + * and added this banner so it's not blindly updated. + * + **/ - public IEnumerable GetMentions(long? sinceId = null, int? count = 20) - { - return GetTimeline("http://api.twitter.com/1.1/statuses/mentions.json", sinceId, count); - } + public void DirectMessage(string message, string screenName) + { + new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json") + .AddParameter("text", message) + .AddParameter("screen_name", screenName) + .Execute(); + } - public IEnumerable GetUserTimeline(long? sinceId = null, int? count = 20) - { - return GetTimeline("http://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, count); - } + public IEnumerable GetHomeTimeline(long? sinceId = null, long? maxId = null, int? count = 20) + { + return GetTimeline("https://api.twitter.com/1.1/statuses/home_timeline.json", sinceId, maxId, count, ""); + } - private IEnumerable GetTimeline(string url, long? sinceId, int? count) - { - var builder = new RequestBuilder(oauth, "GET", url); + public IEnumerable GetMentions(long? sinceId = null, long? maxId = null, int? count = 20) + { + return GetTimeline("https://api.twitter.com/1.1/statuses/mentions.json", sinceId, maxId, count, ""); + } - if (sinceId.HasValue) - builder.AddParameter("since_id", sinceId.Value.ToString()); + public IEnumerable GetUserTimeline(long? sinceId = null, long? maxId = null, int? count = 20, string screenName = "") + { + return GetTimeline("https://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, maxId, count, screenName); + } - if (count.HasValue) - builder.AddParameter("count", count.Value.ToString()); + private IEnumerable GetTimeline(string url, long? sinceId, long? maxId, int? count, string screenName) + { + var builder = new RequestBuilder(oauth, "GET", url); - using (var response = builder.Execute()) - using (var stream = response.GetResponseStream()) - using (var reader = new StreamReader(stream)) - { - var content = reader.ReadToEnd(); - var serializer = new JavaScriptSerializer(); + if (sinceId.HasValue) + builder.AddParameter("since_id", sinceId.Value.ToString()); - var tweets = (object[])serializer.DeserializeObject(content); + if (maxId.HasValue) + builder.AddParameter("max_id", maxId.Value.ToString()); - return tweets.Cast>().Select(tweet => - { - var user = ((Dictionary)tweet["user"]); - var date = DateTime.ParseExact(tweet["created_at"].ToString(), - "ddd MMM dd HH:mm:ss zz00 yyyy", - CultureInfo.InvariantCulture).ToLocalTime(); - return new Tweet - { - Id = (long)tweet["id"], - CreatedAt = - date, - Text = (string)tweet["text"], - UserName = (string)user["name"], - ScreenName = (string)user["screen_name"] - }; - }).ToArray(); - } - } + if (count.HasValue) + builder.AddParameter("count", count.Value.ToString()); - #region RequestBuilder + if (screenName != "") + builder.AddParameter("screen_name", screenName); - public class RequestBuilder - { - private const string VERSION = "1.0"; - private const string SIGNATURE_METHOD = "HMAC-SHA1"; + var responseContent = builder.Execute(); - private readonly OAuthInfo oauth; - private readonly string method; - private readonly IDictionary customParameters; - private readonly string url; + var serializer = new JavaScriptSerializer(); - public RequestBuilder(OAuthInfo oauth, string method, string url) - { - this.oauth = oauth; - this.method = method; - this.url = url; - customParameters = new Dictionary(); - } + var tweets = (object[])serializer.DeserializeObject(responseContent); - public RequestBuilder AddParameter(string name, string value) - { - customParameters.Add(name, value.EncodeRFC3986()); - return this; - } + return tweets.Cast>().Select(tweet => + { + var user = ((Dictionary)tweet["user"]); + var date = DateTime.ParseExact(tweet["created_at"].ToString(), + "ddd MMM dd HH:mm:ss zz00 yyyy", + CultureInfo.InvariantCulture).ToLocalTime(); - public WebResponse Execute() - { - var timespan = GetTimestamp(); - var nonce = CreateNonce(); + return new Tweet + { + Id = (long)tweet["id"], + CreatedAt = date, + Text = (string)tweet["text"], + UserName = (string)user["name"], + ScreenName = (string)user["screen_name"] + }; + }).ToArray(); + } - var parameters = new Dictionary(customParameters); - AddOAuthParameters(parameters, timespan, nonce); + #region RequestBuilder - var signature = GenerateSignature(parameters); - var headerValue = GenerateAuthorizationHeaderValue(parameters, signature); + public class RequestBuilder + { + private const string VERSION = "1.0"; + private const string SIGNATURE_METHOD = "HMAC-SHA1"; - var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl()); - request.Method = method; - request.ContentType = "application/x-www-form-urlencoded"; + private readonly OAuthInfo oauth; + private readonly string method; + private readonly IDictionary customParameters; + private readonly string url; - request.Headers.Add("Authorization", headerValue); + public RequestBuilder(OAuthInfo oauth, string method, string url) + { + this.oauth = oauth; + this.method = method; + this.url = url; + customParameters = new Dictionary(); + } - WriteRequestBody(request); + public RequestBuilder AddParameter(string name, string value) + { + customParameters.Add(name, value.EncodeRFC3986()); + return this; + } - // It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions - // after some requests. Abort the request seems to work. More info: - // http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly + public string Execute() + { + var timespan = GetTimestamp(); + var nonce = CreateNonce(); + + var parameters = new Dictionary(customParameters); + AddOAuthParameters(parameters, timespan, nonce); + + var signature = GenerateSignature(parameters); + var headerValue = GenerateAuthorizationHeaderValue(parameters, signature); + + var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl()); + request.Method = method; + request.ContentType = "application/x-www-form-urlencoded"; + + request.Headers.Add("Authorization", headerValue); + + WriteRequestBody(request); + + // It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions + // after some requests. Abort the request seems to work. More info: + // http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly var response = request.GetResponse(); + + string content; + + using (var stream = response.GetResponseStream()) + { + using (var reader = new StreamReader(stream)) + { + content = reader.ReadToEnd(); + } + } + request.Abort(); - return response; - } + return content; + } - private void WriteRequestBody(HttpWebRequest request) - { - if (method == "GET") - return; + private void WriteRequestBody(HttpWebRequest request) + { + if (method == "GET") + return; - var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString()); - using (var stream = request.GetRequestStream()) - stream.Write(requestBody, 0, requestBody.Length); - } + var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString()); + using (var stream = request.GetRequestStream()) + stream.Write(requestBody, 0, requestBody.Length); + } - private string GetRequestUrl() - { - if (method != "GET" || customParameters.Count == 0) - return url; + private string GetRequestUrl() + { + if (method != "GET" || customParameters.Count == 0) + return url; - return string.Format("{0}?{1}", url, GetCustomParametersString()); - } + return string.Format("{0}?{1}", url, GetCustomParametersString()); + } - private string GetCustomParametersString() - { - return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&"); - } + private string GetCustomParametersString() + { + return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&"); + } - private string GenerateAuthorizationHeaderValue(IEnumerable> parameters, string signature) - { - return new StringBuilder("OAuth ") - .Append(parameters.Concat(new KeyValuePair("oauth_signature", signature)) - .Where(x => x.Key.StartsWith("oauth_")) - .Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986())) - .Join(",")) - .ToString(); - } + private string GenerateAuthorizationHeaderValue(IEnumerable> parameters, string signature) + { + return new StringBuilder("OAuth ") + .Append(parameters.Concat(new KeyValuePair("oauth_signature", signature)) + .Where(x => x.Key.StartsWith("oauth_")) + .Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986())) + .Join(",")) + .ToString(); + } - private string GenerateSignature(IEnumerable> parameters) - { - var dataToSign = new StringBuilder() - .Append(method).Append("&") - .Append(url.EncodeRFC3986()).Append("&") - .Append(parameters - .OrderBy(x => x.Key) - .Select(x => string.Format("{0}={1}", x.Key, x.Value)) - .Join("&") - .EncodeRFC3986()); + private string GenerateSignature(IEnumerable> parameters) + { + var dataToSign = new StringBuilder() + .Append(method).Append("&") + .Append(url.EncodeRFC3986()).Append("&") + .Append(parameters + .OrderBy(x => x.Key) + .Select(x => string.Format("{0}={1}", x.Key, x.Value)) + .Join("&") + .EncodeRFC3986()); - var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986()); - var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey)); + var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986()); + var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey)); - var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString())); - return Convert.ToBase64String(signatureBytes); - } + var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString())); + return Convert.ToBase64String(signatureBytes); + } - private void AddOAuthParameters(IDictionary parameters, string timestamp, string nonce) - { - parameters.Add("oauth_version", VERSION); - parameters.Add("oauth_consumer_key", oauth.ConsumerKey); - parameters.Add("oauth_nonce", nonce); - parameters.Add("oauth_signature_method", SIGNATURE_METHOD); - parameters.Add("oauth_timestamp", timestamp); - parameters.Add("oauth_token", oauth.AccessToken); - } + private void AddOAuthParameters(IDictionary parameters, string timestamp, string nonce) + { + parameters.Add("oauth_version", VERSION); + parameters.Add("oauth_consumer_key", oauth.ConsumerKey); + parameters.Add("oauth_nonce", nonce); + parameters.Add("oauth_signature_method", SIGNATURE_METHOD); + parameters.Add("oauth_timestamp", timestamp); + parameters.Add("oauth_token", oauth.AccessToken); + } - private static string GetTimestamp() - { - return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); - } + private static string GetTimestamp() + { + return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); + } - private static string CreateNonce() - { - return new Random().Next(0x0000000, 0x7fffffff).ToString("X8"); - } - } + private static string CreateNonce() + { + return new Random().Next(0x0000000, 0x7fffffff).ToString("X8"); + } + } - #endregion - } + #endregion + } - public static class TinyTwitterHelperExtensions - { - public static string Join(this IEnumerable items, string separator) - { - return string.Join(separator, items.ToArray()); - } + public static class TinyTwitterHelperExtensions + { + public static string Join(this IEnumerable items, string separator) + { + return string.Join(separator, items.ToArray()); + } - public static IEnumerable Concat(this IEnumerable items, T value) - { - return items.Concat(new[] { value }); - } + public static IEnumerable Concat(this IEnumerable items, T value) + { + return items.Concat(new[] { value }); + } - public static string EncodeRFC3986(this string value) - { - // From Twitterizer http://www.twitterizer.net/ + public static string EncodeRFC3986(this string value) + { + // From Twitterizer http://www.twitterizer.net/ - if (string.IsNullOrEmpty(value)) - return string.Empty; + if (string.IsNullOrEmpty(value)) + return string.Empty; - var encoded = Uri.EscapeDataString(value); + var encoded = Uri.EscapeDataString(value); - return Regex - .Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()) - .Replace("(", "%28") - .Replace(")", "%29") - .Replace("$", "%24") - .Replace("!", "%21") - .Replace("*", "%2A") - .Replace("'", "%27") - .Replace("%7E", "~"); - } - } -} + return Regex + .Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()) + .Replace("(", "%28") + .Replace(")", "%29") + .Replace("$", "%24") + .Replace("!", "%21") + .Replace("*", "%2A") + .Replace("'", "%27") + .Replace("%7E", "~"); + } + } +} \ No newline at end of file diff --git a/src/UI/Form/ActionTemplate.hbs b/src/UI/Form/ActionTemplate.hbs index 73991a2da..27ffcb538 100644 --- a/src/UI/Form/ActionTemplate.hbs +++ b/src/UI/Form/ActionTemplate.hbs @@ -2,6 +2,6 @@
- +
diff --git a/src/UI/Settings/Notifications/Edit/NotificationEditView.js b/src/UI/Settings/Notifications/Edit/NotificationEditView.js index 839d9311c..e6ad3231a 100644 --- a/src/UI/Settings/Notifications/Edit/NotificationEditView.js +++ b/src/UI/Settings/Notifications/Edit/NotificationEditView.js @@ -16,12 +16,10 @@ var view = Marionette.ItemView.extend({ onDownloadToggle : '.x-on-download', onUpgradeSection : '.x-on-upgrade', tags : '.x-tags', - indicator : '.x-indicator', + modalBody : '.x-modal-body', + formTag : '.x-form-tag', + path : '.x-path', authorizedNotificationButton : '.AuthorizeNotification' - tags : '.x-tags', - modalBody : '.modal-body', - formTag : '.x-form-tag', - path : '.x-path' }, events : { @@ -87,13 +85,16 @@ var view = Marionette.ItemView.extend({ } }, - _onAuthorizeNotification : function(e) { - var self = this; - self.ui.indicator.show(); - this.model.connectData(this.ui.authorizedNotificationButton.data('value')).always(function(newValues) { - self.ui.indicator.hide(); - }); - } + _onAuthorizeNotification : function() { + var self = this; + var callbackUrl = window.location.origin + '/oauth.html'; + this.ui.indicator.show(); + var promise = this.model.connectData(this.ui.authorizedNotificationButton.data('value') + '?callbackUrl=' + callbackUrl); + + promise.always(function() { + self.ui.indicator.hide(); + }); + } }); AsModelBoundView.call(view); diff --git a/src/UI/Settings/Notifications/Edit/NotificationEditViewTemplate.hbs b/src/UI/Settings/Notifications/Edit/NotificationEditViewTemplate.hbs index 8833326e9..02196cb75 100644 --- a/src/UI/Settings/Notifications/Edit/NotificationEditViewTemplate.hbs +++ b/src/UI/Settings/Notifications/Edit/NotificationEditViewTemplate.hbs @@ -7,7 +7,7 @@

Add - {{implementationName}}

{{/if}} -