Cleanup and refactoring of Twitter notifications

Closes #301
New: Twitter Notifications
pull/644/head
Mark McDowall 9 years ago
parent 2fbf7a4114
commit b82e830e86

@ -33,7 +33,8 @@ namespace NzbDrone.Api.Frontend.Mappers
resourceUrl.EndsWith(".map") || resourceUrl.EndsWith(".map") ||
resourceUrl.EndsWith(".css") || resourceUrl.EndsWith(".css") ||
(resourceUrl.EndsWith(".ico") && !resourceUrl.Equals("/favicon.ico")) || (resourceUrl.EndsWith(".ico") && !resourceUrl.Equals("/favicon.ico")) ||
resourceUrl.EndsWith(".swf"); resourceUrl.EndsWith(".swf") ||
resourceUrl.EndsWith("oauth.html");
} }
} }
} }

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
namespace NzbDrone.Core.Annotations namespace NzbDrone.Core.Annotations
{ {
@ -28,7 +27,7 @@ namespace NzbDrone.Core.Annotations
Select, Select,
Path, Path,
Hidden, Hidden,
Tag Tag,
Action Action
} }
} }

@ -2,21 +2,17 @@
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using System;
using OAuth;
using System.Net;
using System.IO;
namespace NzbDrone.Core.Notifications.Twitter namespace NzbDrone.Core.Notifications.Twitter
{ {
class Twitter : NotificationBase<TwitterSettings> class Twitter : NotificationBase<TwitterSettings>
{ {
private readonly ITwitterService _TwitterService; private readonly ITwitterService _twitterService;
public Twitter(ITwitterService TwitterService) public Twitter(ITwitterService twitterService)
{ {
_TwitterService = TwitterService; _twitterService = twitterService;
} }
public override string Link public override string Link
@ -26,15 +22,15 @@ namespace NzbDrone.Core.Notifications.Twitter
public override void OnGrab(string message) 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) 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 @@ namespace NzbDrone.Core.Notifications.Twitter
return new return new
{ {
nextStep = "step2", nextStep = "step2",
action = "openwindow", action = "openWindow",
url = _TwitterService.GetOAuthRedirect( url = _twitterService.GetOAuthRedirect(query["callbackUrl"].ToString())
Settings.ConsumerKey,
Settings.ConsumerSecret,
"http://localhost:8989/Content/oauthLand.html" /* FIXME - how do I get http host and such */
)
}; };
} }
else if (stage == "step2") else if (stage == "step2")
{ {
return new return new
{ {
action = "updatefields", action = "updateFields",
fields = _TwitterService.GetOAuthToken( fields = _twitterService.GetOAuthToken(query["oauth_token"].ToString(), query["oauth_verifier"].ToString())
Settings.ConsumerKey, Settings.ConsumerSecret,
query["oauth_token"].ToString(),
query["oauth_verifier"].ToString()
)
}; };
} }
return new {}; return new {};
} }
public override string Name
{
get
{
return "Twitter";
}
}
public override bool SupportsOnRename
{
get
{
return false;
}
}
public override ValidationResult Test() public override ValidationResult Test()
{ {
var failures = new List<ValidationFailure>(); var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_TwitterService.Test(Settings)); failures.AddIfNotNull(_twitterService.Test(Settings));
return new ValidationResult(failures); return new ValidationResult(failures);
} }

@ -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)
{
}
}
}

@ -1,64 +1,54 @@
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NLog; using NLog;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OAuth; using OAuth;
using System.Net; using System.Net;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO;
using System.Web;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
namespace NzbDrone.Core.Notifications.Twitter namespace NzbDrone.Core.Notifications.Twitter
{ {
public interface ITwitterService 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); ValidationFailure Test(TwitterSettings settings);
string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback); string GetOAuthRedirect(string callbackUrl);
object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier); object GetOAuthToken(string oauthToken, string oauthVerifier);
} }
public class TwitterService : ITwitterService public class TwitterService : ITwitterService
{ {
private readonly IHttpClient _httpClient;
private readonly Logger _logger; 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; _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 var auth = oAuthRequest.GetAuthorizationHeader();
string auth = client.GetAuthorizationHeader(); var request = new Common.Http.HttpRequest(oAuthRequest.RequestUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(client.RequestUrl);
request.Headers.Add("Authorization", auth); request.Headers.Add("Authorization", auth);
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var response = _httpClient.Get(request);
System.Collections.Specialized.NameValueCollection qscoll;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"))) return HttpUtility.ParseQueryString(response.Content);
{
string responseText = reader.ReadToEnd();
return System.Web.HttpUtility.ParseQueryString(responseText);
}
return null;
} }
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 // Creating a new instance with a helper method
OAuthRequest client = OAuthRequest.ForAccessToken( var oAuthRequest = OAuthRequest.ForAccessToken(_consumerKey, _consumerSecret, oauthToken, "", oauthVerifier);
consumerKey, oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
consumerSecret, var qscoll = OAuthQuery(oAuthRequest);
oauthToken,
"",
oauthVerifier
);
client.RequestUrl = "https://api.twitter.com/oauth/access_token";
NameValueCollection qscoll = oauthQuery(client);
return new return new
{ {
@ -67,54 +57,77 @@ namespace NzbDrone.Core.Notifications.Twitter
}; };
} }
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback) public string GetOAuthRedirect(string callbackUrl)
{ {
// Creating a new instance with a helper method // Creating a new instance with a helper method
OAuthRequest client = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callback); var oAuthRequest = OAuthRequest.ForRequestToken(_consumerKey, _consumerSecret, callbackUrl);
client.RequestUrl = "https://api.twitter.com/oauth/request_token"; oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
NameValueCollection qscoll = oauthQuery(client); 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 try
{ {
var oauth = new TinyTwitter.OAuthInfo var oAuth = new TinyTwitter.OAuthInfo
{ {
AccessToken = accessToken, AccessToken = settings.AccessToken,
AccessSecret = accessTokenSecret, AccessSecret = settings.AccessTokenSecret,
ConsumerKey = consumerKey, ConsumerKey = _consumerKey,
ConsumerSecret = consumerSecret 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) catch (WebException e)
{ {
using (WebResponse response = e.Response) using (var response = e.Response)
{ {
HttpWebResponse httpResponse = (HttpWebResponse)response; var httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (System.IO.Stream data = response.GetResponseStream()) using (var responseStream = response.GetResponseStream())
using (var reader = new System.IO.StreamReader(data))
{ {
string text = reader.ReadToEnd(); if (responseStream == null)
Console.WriteLine(text); {
_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) public ValidationFailure Test(TwitterSettings settings)
{ {
try try
{ {
string body = "This is a test message from Sonarr @ " + DateTime.Now.ToString(); var body = "Sonarr: Test Message @ " + DateTime.Now;
SendNotification(body, settings.AccessToken, settings.AccessTokenSecret, settings.ConsumerKey, settings.ConsumerSecret);
SendNotification(body, settings);
} }
catch (Exception ex) catch (Exception ex)
{ {

@ -1,5 +1,4 @@
using System; using FluentValidation;
using FluentValidation;
using NzbDrone.Core.Annotations; using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
@ -12,8 +11,12 @@ namespace NzbDrone.Core.Notifications.Twitter
{ {
RuleFor(c => c.AccessToken).NotEmpty(); RuleFor(c => c.AccessToken).NotEmpty();
RuleFor(c => c.AccessTokenSecret).NotEmpty(); RuleFor(c => c.AccessTokenSecret).NotEmpty();
RuleFor(c => c.ConsumerKey).NotEmpty(); //TODO: Validate that it is a valid username (numbers, letters and underscores - I think)
RuleFor(c => c.ConsumerSecret).NotEmpty(); 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 @@ namespace NzbDrone.Core.Notifications.Twitter
public TwitterSettings() public TwitterSettings()
{ {
ConsumerKey = "3POVsO3KW90LKZXyzPOjQ"; /* FIXME - Key from Couchpotato so needs to be replaced */ DirectMessage = true;
ConsumerSecret = "Qprb94hx9ucXvD4Wvg2Ctsk4PDK7CcQAKgCELXoyIjE"; /* FIXME - Key from Couchpotato so needs to be replaced */
AuthorizeNotification = "step1"; AuthorizeNotification = "step1";
} }
[FieldDefinition(0, Label = "Access Token", Advanced = true)] [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)] [FieldDefinition(1, Label = "Access Token Secret", Advanced = true)]
public String AccessTokenSecret { get; set; } public string AccessTokenSecret { get; set; }
public String ConsumerKey { get; set; } [FieldDefinition(2, Label = "Mention", HelpText = "Mention this user in sent tweets")]
public String ConsumerSecret { get; set; } public string Mention { get; set; }
[FieldDefinition(4, Label = "Connect to twitter", Type = FieldType.Action)] [FieldDefinition(3, Label = "Direct Message", Type = FieldType.Checkbox, HelpText = "Send a direct message instead of a public message")]
public String AuthorizeNotification { get; set; } public bool DirectMessage { get; set; }
public bool IsValid [FieldDefinition(4, Label = "Connect to twitter", Type = FieldType.Action)]
{ public string AuthorizeNotification { get; set; }
get
{
return !string.IsNullOrWhiteSpace(AccessToken) && !string.IsNullOrWhiteSpace(AccessTokenSecret) &&
!string.IsNullOrWhiteSpace(ConsumerKey) && !string.IsNullOrWhiteSpace(ConsumerSecret);
}
}
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()
{ {

@ -743,6 +743,7 @@
<Compile Include="Notifications\Synology\SynologyIndexer.cs" /> <Compile Include="Notifications\Synology\SynologyIndexer.cs" />
<Compile Include="Notifications\Synology\SynologyIndexerProxy.cs" /> <Compile Include="Notifications\Synology\SynologyIndexerProxy.cs" />
<Compile Include="Notifications\Synology\SynologyIndexerSettings.cs" /> <Compile Include="Notifications\Synology\SynologyIndexerSettings.cs" />
<Compile Include="Notifications\Twitter\TwitterException.cs" />
<Compile Include="Organizer\NamingConfigRepository.cs" /> <Compile Include="Organizer\NamingConfigRepository.cs" />
<Compile Include="Notifications\Twitter\Twitter.cs" /> <Compile Include="Notifications\Twitter\Twitter.cs" />
<Compile Include="Notifications\Twitter\TwitterService.cs" /> <Compile Include="Notifications\Twitter\TwitterService.cs" />

@ -11,252 +11,281 @@ using System.Web.Script.Serialization;
namespace TinyTwitter namespace TinyTwitter
{ {
public class OAuthInfo public class OAuthInfo
{ {
public string ConsumerKey { get; set; } public string ConsumerKey { get; set; }
public string ConsumerSecret { get; set; } public string ConsumerSecret { get; set; }
public string AccessToken { get; set; } public string AccessToken { get; set; }
public string AccessSecret { get; set; } public string AccessSecret { get; set; }
} }
public class Tweet public class Tweet
{ {
public long Id { get; set; } public long Id { get; set; }
public DateTime CreatedAt { get; set; } public DateTime CreatedAt { get; set; }
public string UserName { get; set; } public string UserName { get; set; }
public string ScreenName { get; set; } public string ScreenName { get; set; }
public string Text { get; set; } public string Text { get; set; }
} }
public class TinyTwitter public class TinyTwitter
{ {
private readonly OAuthInfo oauth; private readonly OAuthInfo oauth;
public TinyTwitter(OAuthInfo oauth) public TinyTwitter(OAuthInfo oauth)
{ {
this.oauth = oauth; this.oauth = oauth;
} }
public void UpdateStatus(string message) public void UpdateStatus(string message)
{ {
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json") new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
.AddParameter("status", message) .AddParameter("status", message)
.Execute(); .Execute();
} }
public IEnumerable<Tweet> 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<Tweet> 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)
{
public IEnumerable<Tweet> GetUserTimeline(long? sinceId = null, int? count = 20) new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
{ .AddParameter("text", message)
return GetTimeline("http://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, count); .AddParameter("screen_name", screenName)
} .Execute();
}
private IEnumerable<Tweet> GetTimeline(string url, long? sinceId, int? count)
{ public IEnumerable<Tweet> GetHomeTimeline(long? sinceId = null, long? maxId = null, int? count = 20)
var builder = new RequestBuilder(oauth, "GET", url); {
return GetTimeline("https://api.twitter.com/1.1/statuses/home_timeline.json", sinceId, maxId, count, "");
if (sinceId.HasValue) }
builder.AddParameter("since_id", sinceId.Value.ToString());
public IEnumerable<Tweet> GetMentions(long? sinceId = null, long? maxId = null, int? count = 20)
if (count.HasValue) {
builder.AddParameter("count", count.Value.ToString()); return GetTimeline("https://api.twitter.com/1.1/statuses/mentions.json", sinceId, maxId, count, "");
}
using (var response = builder.Execute())
using (var stream = response.GetResponseStream()) public IEnumerable<Tweet> GetUserTimeline(long? sinceId = null, long? maxId = null, int? count = 20, string screenName = "")
using (var reader = new StreamReader(stream)) {
{ return GetTimeline("https://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, maxId, count, screenName);
var content = reader.ReadToEnd(); }
var serializer = new JavaScriptSerializer();
private IEnumerable<Tweet> GetTimeline(string url, long? sinceId, long? maxId, int? count, string screenName)
var tweets = (object[])serializer.DeserializeObject(content); {
var builder = new RequestBuilder(oauth, "GET", url);
return tweets.Cast<Dictionary<string, object>>().Select(tweet =>
{ if (sinceId.HasValue)
var user = ((Dictionary<string, object>)tweet["user"]); builder.AddParameter("since_id", sinceId.Value.ToString());
var date = DateTime.ParseExact(tweet["created_at"].ToString(),
"ddd MMM dd HH:mm:ss zz00 yyyy", if (maxId.HasValue)
CultureInfo.InvariantCulture).ToLocalTime(); builder.AddParameter("max_id", maxId.Value.ToString());
return new Tweet
{ if (count.HasValue)
Id = (long)tweet["id"], builder.AddParameter("count", count.Value.ToString());
CreatedAt =
date, if (screenName != "")
Text = (string)tweet["text"], builder.AddParameter("screen_name", screenName);
UserName = (string)user["name"],
ScreenName = (string)user["screen_name"] var responseContent = builder.Execute();
};
}).ToArray(); var serializer = new JavaScriptSerializer();
}
} var tweets = (object[])serializer.DeserializeObject(responseContent);
#region RequestBuilder return tweets.Cast<Dictionary<string, object>>().Select(tweet =>
{
public class RequestBuilder var user = ((Dictionary<string, object>)tweet["user"]);
{ var date = DateTime.ParseExact(tweet["created_at"].ToString(),
private const string VERSION = "1.0"; "ddd MMM dd HH:mm:ss zz00 yyyy",
private const string SIGNATURE_METHOD = "HMAC-SHA1"; CultureInfo.InvariantCulture).ToLocalTime();
private readonly OAuthInfo oauth; return new Tweet
private readonly string method; {
private readonly IDictionary<string, string> customParameters; Id = (long)tweet["id"],
private readonly string url; CreatedAt = date,
Text = (string)tweet["text"],
public RequestBuilder(OAuthInfo oauth, string method, string url) UserName = (string)user["name"],
{ ScreenName = (string)user["screen_name"]
this.oauth = oauth; };
this.method = method; }).ToArray();
this.url = url; }
customParameters = new Dictionary<string, string>();
} #region RequestBuilder
public RequestBuilder AddParameter(string name, string value) public class RequestBuilder
{ {
customParameters.Add(name, value.EncodeRFC3986()); private const string VERSION = "1.0";
return this; private const string SIGNATURE_METHOD = "HMAC-SHA1";
}
private readonly OAuthInfo oauth;
public WebResponse Execute() private readonly string method;
{ private readonly IDictionary<string, string> customParameters;
var timespan = GetTimestamp(); private readonly string url;
var nonce = CreateNonce();
public RequestBuilder(OAuthInfo oauth, string method, string url)
var parameters = new Dictionary<string, string>(customParameters); {
AddOAuthParameters(parameters, timespan, nonce); this.oauth = oauth;
this.method = method;
var signature = GenerateSignature(parameters); this.url = url;
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature); customParameters = new Dictionary<string, string>();
}
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
request.Method = method; public RequestBuilder AddParameter(string name, string value)
request.ContentType = "application/x-www-form-urlencoded"; {
customParameters.Add(name, value.EncodeRFC3986());
request.Headers.Add("Authorization", headerValue); return this;
}
WriteRequestBody(request);
public string Execute()
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions {
// after some requests. Abort the request seems to work. More info: var timespan = GetTimestamp();
// http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly var nonce = CreateNonce();
var parameters = new Dictionary<string, string>(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(); var response = request.GetResponse();
string content;
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
}
request.Abort(); request.Abort();
return response; return content;
} }
private void WriteRequestBody(HttpWebRequest request) private void WriteRequestBody(HttpWebRequest request)
{ {
if (method == "GET") if (method == "GET")
return; return;
var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString()); var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString());
using (var stream = request.GetRequestStream()) using (var stream = request.GetRequestStream())
stream.Write(requestBody, 0, requestBody.Length); stream.Write(requestBody, 0, requestBody.Length);
} }
private string GetRequestUrl() private string GetRequestUrl()
{ {
if (method != "GET" || customParameters.Count == 0) if (method != "GET" || customParameters.Count == 0)
return url; return url;
return string.Format("{0}?{1}", url, GetCustomParametersString()); return string.Format("{0}?{1}", url, GetCustomParametersString());
} }
private string GetCustomParametersString() private string GetCustomParametersString()
{ {
return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&"); return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
} }
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature) private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
{ {
return new StringBuilder("OAuth ") return new StringBuilder("OAuth ")
.Append(parameters.Concat(new KeyValuePair<string, string>("oauth_signature", signature)) .Append(parameters.Concat(new KeyValuePair<string, string>("oauth_signature", signature))
.Where(x => x.Key.StartsWith("oauth_")) .Where(x => x.Key.StartsWith("oauth_"))
.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986())) .Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986()))
.Join(",")) .Join(","))
.ToString(); .ToString();
} }
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters) private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
{ {
var dataToSign = new StringBuilder() var dataToSign = new StringBuilder()
.Append(method).Append("&") .Append(method).Append("&")
.Append(url.EncodeRFC3986()).Append("&") .Append(url.EncodeRFC3986()).Append("&")
.Append(parameters .Append(parameters
.OrderBy(x => x.Key) .OrderBy(x => x.Key)
.Select(x => string.Format("{0}={1}", x.Key, x.Value)) .Select(x => string.Format("{0}={1}", x.Key, x.Value))
.Join("&") .Join("&")
.EncodeRFC3986()); .EncodeRFC3986());
var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986()); var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986());
var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey)); var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey));
var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString())); var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString()));
return Convert.ToBase64String(signatureBytes); return Convert.ToBase64String(signatureBytes);
} }
private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce) private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce)
{ {
parameters.Add("oauth_version", VERSION); parameters.Add("oauth_version", VERSION);
parameters.Add("oauth_consumer_key", oauth.ConsumerKey); parameters.Add("oauth_consumer_key", oauth.ConsumerKey);
parameters.Add("oauth_nonce", nonce); parameters.Add("oauth_nonce", nonce);
parameters.Add("oauth_signature_method", SIGNATURE_METHOD); parameters.Add("oauth_signature_method", SIGNATURE_METHOD);
parameters.Add("oauth_timestamp", timestamp); parameters.Add("oauth_timestamp", timestamp);
parameters.Add("oauth_token", oauth.AccessToken); parameters.Add("oauth_token", oauth.AccessToken);
} }
private static string GetTimestamp() private static string GetTimestamp()
{ {
return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
} }
private static string CreateNonce() private static string CreateNonce()
{ {
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8"); return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
} }
} }
#endregion #endregion
} }
public static class TinyTwitterHelperExtensions public static class TinyTwitterHelperExtensions
{ {
public static string Join<T>(this IEnumerable<T> items, string separator) public static string Join<T>(this IEnumerable<T> items, string separator)
{ {
return string.Join(separator, items.ToArray()); return string.Join(separator, items.ToArray());
} }
public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value) public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value)
{ {
return items.Concat(new[] { value }); return items.Concat(new[] { value });
} }
public static string EncodeRFC3986(this string value) public static string EncodeRFC3986(this string value)
{ {
// From Twitterizer http://www.twitterizer.net/ // From Twitterizer http://www.twitterizer.net/
if (string.IsNullOrEmpty(value)) if (string.IsNullOrEmpty(value))
return string.Empty; return string.Empty;
var encoded = Uri.EscapeDataString(value); var encoded = Uri.EscapeDataString(value);
return Regex return Regex
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper()) .Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
.Replace("(", "%28") .Replace("(", "%28")
.Replace(")", "%29") .Replace(")", "%29")
.Replace("$", "%24") .Replace("$", "%24")
.Replace("!", "%21") .Replace("!", "%21")
.Replace("*", "%2A") .Replace("*", "%2A")
.Replace("'", "%27") .Replace("'", "%27")
.Replace("%7E", "~"); .Replace("%7E", "~");
} }
} }
} }

@ -2,6 +2,6 @@
<label class="col-sm-3 control-label"></label> <label class="col-sm-3 control-label"></label>
<div class="col-sm-5"> <div class="col-sm-5">
<button class="form-control x-path {{name}}" data-value="{{value}}">{{label}}</button> <button class="form-control {{name}}" data-value="{{value}}">{{label}}</button>
</div> </div>
</div> </div>

@ -16,12 +16,10 @@ var view = Marionette.ItemView.extend({
onDownloadToggle : '.x-on-download', onDownloadToggle : '.x-on-download',
onUpgradeSection : '.x-on-upgrade', onUpgradeSection : '.x-on-upgrade',
tags : '.x-tags', tags : '.x-tags',
indicator : '.x-indicator', modalBody : '.x-modal-body',
formTag : '.x-form-tag',
path : '.x-path',
authorizedNotificationButton : '.AuthorizeNotification' authorizedNotificationButton : '.AuthorizeNotification'
tags : '.x-tags',
modalBody : '.modal-body',
formTag : '.x-form-tag',
path : '.x-path'
}, },
events : { events : {
@ -87,13 +85,16 @@ var view = Marionette.ItemView.extend({
} }
}, },
_onAuthorizeNotification : function(e) { _onAuthorizeNotification : function() {
var self = this; var self = this;
self.ui.indicator.show(); var callbackUrl = window.location.origin + '/oauth.html';
this.model.connectData(this.ui.authorizedNotificationButton.data('value')).always(function(newValues) { this.ui.indicator.show();
self.ui.indicator.hide(); var promise = this.model.connectData(this.ui.authorizedNotificationButton.data('value') + '?callbackUrl=' + callbackUrl);
});
} promise.always(function() {
self.ui.indicator.hide();
});
}
}); });
AsModelBoundView.call(view); AsModelBoundView.call(view);

@ -7,7 +7,7 @@
<h3>Add - {{implementationName}}</h3> <h3>Add - {{implementationName}}</h3>
{{/if}} {{/if}}
</div> </div>
<div class="modal-body notification-modal"> <div class="modal-body notification-modal x-modal">
<div class="form-horizontal"> <div class="form-horizontal">
<div class="form-group"> <div class="form-group">
<label class="col-sm-3 control-label">Name</label> <label class="col-sm-3 control-label">Name</label>

@ -1,55 +1,73 @@
var $ = require('jquery'); var $ = require('jquery');
var _ = require('underscore');
var DeepModel = require('backbone.deepmodel'); var DeepModel = require('backbone.deepmodel');
var Messenger = require('../Shared/Messenger'); var Messenger = require('../Shared/Messenger');
module.exports = DeepModel.extend({ module.exports = DeepModel.extend({
connectData : function(action) { connectData : function(action, initialQueryString) {
var self = this; var self = this;
this.trigger('connect:sync'); this.trigger('connect:sync');
var promise = $.Deferred(); var promise = $.Deferred();
var callAction = function(action) { var callAction = function(action) {
var params = {}; var params = {
params.url = self.collection.url + '/connectData/' + action; url : self.collection.url + '/connectData/' + action,
params.contentType = 'application/json'; contentType : 'application/json',
params.data = JSON.stringify(self.toJSON()); data : JSON.stringify(self.toJSON()),
params.type = 'POST'; type : 'POST',
params.isValidatedCall = true; isValidatedCall : true
};
$.ajax(params).fail(promise.reject).success(function(response) {
if (response.action) var ajaxPromise = $.ajax(params);
ajaxPromise.fail(promise.reject);
ajaxPromise.success(function(response) {
if (response.action)
{ {
if (response.action === 'openWindow')
if (response.action === "openwindow")
{ {
var connectResponseWindow = window.open(response.url); window.open(response.url);
var selfWindow = window; var selfWindow = window;
selfWindow.onCompleteOauth = function(query, callback) { selfWindow.onCompleteOauth = function(query, callback) {
delete selfWindow.onCompleteOauth; delete selfWindow.onCompleteOauth;
if (response.nextStep) { callAction(response.nextStep + query); }
else { promise.resolve(response); } if (response.nextStep) {
callAction(response.nextStep + query);
}
else {
promise.resolve(response);
}
callback(); callback();
}; };
return; return;
} }
else if (response.action === "updatefields") else if (response.action === 'updateFields')
{ {
Object.keys(response.fields).forEach(function(field) { _.each(self.get('fields'), function (value, index) {
self.set(field, response.fields[field]); var fieldValue = _.find(response.fields, function (field, key) {
self.attributes.fields.forEach(function(fieldDef) { return key === value.name;
if (fieldDef.name === field) { fieldDef.value = response.fields[field]; }
}); });
if (fieldValue) {
self.set('fields.' + index + '.value', fieldValue);
}
}); });
} }
} }
if (response.nextStep) { callAction(response.nextStep); } if (response.nextStep) {
else { promise.resolve(response); } callAction(response.nextStep);
}
else {
promise.resolve(response);
}
}); });
}; };
callAction(action); callAction(action, initialQueryString);
Messenger.monitor({ Messenger.monitor({
promise : promise, promise : promise,
@ -63,6 +81,7 @@ module.exports = DeepModel.extend({
return promise; return promise;
}, },
test : function() { test : function() {
var self = this; var self = this;

Loading…
Cancel
Save