From 2fbf7a41142bec2ded60ddc2b55f15c5e78e20c6 Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Sat, 28 Mar 2015 22:30:58 -0700 Subject: [PATCH] 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.Common.Reflection; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; using Omu.ValueInjecter; +using Newtonsoft.Json; namespace NzbDrone.Api { @@ -27,7 +28,8 @@ namespace NzbDrone.Api _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 @@ namespace NzbDrone.Api _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 @@ namespace NzbDrone.Api 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 @@ namespace NzbDrone.Api 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 @@ namespace NzbDrone.Api.REST } } - protected TResource ReadResourceFromRequest() + protected TResource ReadResourceFromRequest(bool skipValidate = false) { //TODO: handle when request is null var resource = Request.Body.FromJson(); @@ -194,7 +194,7 @@ namespace NzbDrone.Api.REST 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 @@ namespace NzbDrone.Core.Annotations 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 @@ namespace NzbDrone.Core.Download 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 @@ namespace NzbDrone.Core.Indexers } 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 @@ namespace NzbDrone.Core.Metadata 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 @@ namespace NzbDrone.Core.Notifications { 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 @@ namespace NzbDrone.Core.ThingiProvider 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 @@ namespace NzbDrone.Core.ThingiProvider 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 @@ namespace NzbDrone.Core.ThingiProvider 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;