1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Cleanup and refactoring of Twitter notifications

Closes #301
New: Twitter Notifications
This commit is contained in:
Mark McDowall 2015-06-25 00:10:40 -07:00
parent 2fbf7a4114
commit b82e830e86
13 changed files with 429 additions and 342 deletions

View File

@ -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");
}
}
}

View File

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

View File

@ -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<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
@ -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<string, object> 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<ValidationFailure>();
failures.AddIfNotNull(_TwitterService.Test(Settings));
failures.AddIfNotNull(_twitterService.Test(Settings));
return new ValidationResult(failures);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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<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)
{
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
.AddParameter("text", message)
.AddParameter("screen_name", screenName)
.Execute();
}
public IEnumerable<Tweet> GetUserTimeline(long? sinceId = null, int? count = 20)
{
return GetTimeline("http://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, count);
}
public IEnumerable<Tweet> 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<Tweet> GetTimeline(string url, long? sinceId, int? count)
{
var builder = new RequestBuilder(oauth, "GET", url);
public IEnumerable<Tweet> 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<Tweet> 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<Tweet> 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<Dictionary<string, object>>().Select(tweet =>
{
var user = ((Dictionary<string, object>)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<string, string> 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<string, string>();
}
var tweets = (object[])serializer.DeserializeObject(responseContent);
public RequestBuilder AddParameter(string name, string value)
{
customParameters.Add(name, value.EncodeRFC3986());
return this;
}
return tweets.Cast<Dictionary<string, object>>().Select(tweet =>
{
var user = ((Dictionary<string, object>)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<string, string>(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<string, string> 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<string, string>();
}
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<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();
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<KeyValuePair<string, string>> parameters, string signature)
{
return new StringBuilder("OAuth ")
.Append(parameters.Concat(new KeyValuePair<string, string>("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<KeyValuePair<string, string>> parameters, string signature)
{
return new StringBuilder("OAuth ")
.Append(parameters.Concat(new KeyValuePair<string, string>("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<KeyValuePair<string, string>> 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<KeyValuePair<string, string>> 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<string, string> 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<string, string> 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<T>(this IEnumerable<T> items, string separator)
{
return string.Join(separator, items.ToArray());
}
public static class TinyTwitterHelperExtensions
{
public static string Join<T>(this IEnumerable<T> items, string separator)
{
return string.Join(separator, items.ToArray());
}
public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value)
{
return items.Concat(new[] { value });
}
public static IEnumerable<T> Concat<T>(this IEnumerable<T> 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", "~");
}
}
}

View File

@ -2,6 +2,6 @@
<label class="col-sm-3 control-label"></label>
<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>

View File

@ -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);

View File

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

View File

@ -1,9 +1,10 @@
var $ = require('jquery');
var _ = require('underscore');
var DeepModel = require('backbone.deepmodel');
var Messenger = require('../Shared/Messenger');
module.exports = DeepModel.extend({
connectData : function(action) {
connectData : function(action, initialQueryString) {
var self = this;
this.trigger('connect:sync');
@ -11,45 +12,62 @@ module.exports = DeepModel.extend({
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;
var params = {
url : self.collection.url + '/connectData/' + action,
contentType : 'application/json',
data : JSON.stringify(self.toJSON()),
type : 'POST',
isValidatedCall : true
};
$.ajax(params).fail(promise.reject).success(function(response) {
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;
selfWindow.onCompleteOauth = function(query, callback) {
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();
};
return;
}
else if (response.action === "updatefields")
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]; }
_.each(self.get('fields'), function (value, index) {
var fieldValue = _.find(response.fields, function (field, key) {
return key === value.name;
});
if (fieldValue) {
self.set('fields.' + index + '.value', fieldValue);
}
});
}
}
if (response.nextStep) { callAction(response.nextStep); }
else { promise.resolve(response); }
if (response.nextStep) {
callAction(response.nextStep);
}
else {
promise.resolve(response);
}
});
};
callAction(action);
callAction(action, initialQueryString);
Messenger.monitor({
promise : promise,
@ -63,6 +81,7 @@ module.exports = DeepModel.extend({
return promise;
},
test : function() {
var self = this;