Merge pull request #644 from Sonarr/twitter-notifications
Twitter notifications
This commit is contained in:
commit
7f23d25fcf
|
@ -33,7 +33,8 @@ namespace NzbDrone.Api.Frontend.Mappers
|
|||
resourceUrl.EndsWith(".map") ||
|
||||
resourceUrl.EndsWith(".css") ||
|
||||
(resourceUrl.EndsWith(".ico") && !resourceUrl.Equals("/favicon.ico")) ||
|
||||
resourceUrl.EndsWith(".swf");
|
||||
resourceUrl.EndsWith(".swf") ||
|
||||
resourceUrl.EndsWith("oauth.html");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<string, object>) 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();
|
||||
|
|
|
@ -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<TResource>();
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.Annotations
|
||||
{
|
||||
|
@ -28,6 +27,7 @@ namespace NzbDrone.Core.Annotations
|
|||
Select,
|
||||
Path,
|
||||
Hidden,
|
||||
Tag
|
||||
Tag,
|
||||
Action
|
||||
}
|
||||
}
|
|
@ -50,6 +50,8 @@ namespace NzbDrone.Core.Download
|
|||
|
||||
public ProviderDefinition Definition { get; set; }
|
||||
|
||||
public object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
protected TSettings Settings
|
||||
{
|
||||
get
|
||||
|
|
|
@ -65,6 +65,7 @@ namespace NzbDrone.Core.Indexers
|
|||
}
|
||||
|
||||
public virtual ProviderDefinition Definition { get; set; }
|
||||
public object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
protected TSettings Settings
|
||||
{
|
||||
|
|
|
@ -52,6 +52,8 @@ namespace NzbDrone.Core.Metadata
|
|||
public abstract List<ImageFileResult> SeasonImages(Series series, Season season);
|
||||
public abstract List<ImageFileResult> EpisodeImages(Series series, EpisodeFile episodeFile);
|
||||
|
||||
public object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
protected TSettings Settings
|
||||
{
|
||||
get
|
||||
|
|
|
@ -60,5 +60,8 @@ namespace NzbDrone.Core.Notifications
|
|||
{
|
||||
return GetType().Name;
|
||||
}
|
||||
|
||||
public virtual object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
using System.Collections.Generic;
|
||||
using FluentValidation.Results;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Twitter
|
||||
{
|
||||
class Twitter : NotificationBase<TwitterSettings>
|
||||
{
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override void OnDownload(DownloadMessage message)
|
||||
{
|
||||
_twitterService.SendNotification(message.Message, Settings);
|
||||
}
|
||||
|
||||
public override void OnRename(Series series)
|
||||
{
|
||||
}
|
||||
|
||||
public override object ConnectData(string stage, IDictionary<string, object> query)
|
||||
{
|
||||
if (stage == "step1")
|
||||
{
|
||||
return new
|
||||
{
|
||||
nextStep = "step2",
|
||||
action = "openWindow",
|
||||
url = _twitterService.GetOAuthRedirect(query["callbackUrl"].ToString())
|
||||
};
|
||||
}
|
||||
else if (stage == "step2")
|
||||
{
|
||||
return new
|
||||
{
|
||||
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));
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using System;
|
||||
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, TwitterSettings settings);
|
||||
ValidationFailure Test(TwitterSettings settings);
|
||||
string GetOAuthRedirect(string callbackUrl);
|
||||
object GetOAuthToken(string oauthToken, string oauthVerifier);
|
||||
}
|
||||
|
||||
public class TwitterService : ITwitterService
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
private static string _consumerKey = "5jSR8a3cp0ToOqSMLMv5GtMQD";
|
||||
private static string _consumerSecret = "dxoZjyMq4BLsC8KxyhSOrIndhCzJ0Dik2hrLzqyJcqoGk4Pfsp";
|
||||
|
||||
public TwitterService(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private NameValueCollection OAuthQuery(OAuthRequest oAuthRequest)
|
||||
{
|
||||
var auth = oAuthRequest.GetAuthorizationHeader();
|
||||
var request = new Common.Http.HttpRequest(oAuthRequest.RequestUrl);
|
||||
request.Headers.Add("Authorization", auth);
|
||||
var response = _httpClient.Get(request);
|
||||
|
||||
return HttpUtility.ParseQueryString(response.Content);
|
||||
}
|
||||
|
||||
public object GetOAuthToken(string oauthToken, string oauthVerifier)
|
||||
{
|
||||
// Creating a new instance with a helper method
|
||||
var oAuthRequest = OAuthRequest.ForAccessToken(_consumerKey, _consumerSecret, oauthToken, "", oauthVerifier);
|
||||
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
|
||||
var qscoll = OAuthQuery(oAuthRequest);
|
||||
|
||||
return new
|
||||
{
|
||||
AccessToken = qscoll["oauth_token"],
|
||||
AccessTokenSecret = qscoll["oauth_token_secret"]
|
||||
};
|
||||
}
|
||||
|
||||
public string GetOAuthRedirect(string callbackUrl)
|
||||
{
|
||||
// Creating a new instance with a helper method
|
||||
var oAuthRequest = OAuthRequest.ForRequestToken(_consumerKey, _consumerSecret, callbackUrl);
|
||||
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
|
||||
var qscoll = OAuthQuery(oAuthRequest);
|
||||
|
||||
return String.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]);
|
||||
}
|
||||
|
||||
public void SendNotification(string message, TwitterSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var oAuth = new TinyTwitter.OAuthInfo
|
||||
{
|
||||
AccessToken = settings.AccessToken,
|
||||
AccessSecret = settings.AccessTokenSecret,
|
||||
ConsumerKey = _consumerKey,
|
||||
ConsumerSecret = _consumerSecret
|
||||
};
|
||||
|
||||
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 (var response = e.Response)
|
||||
{
|
||||
var httpResponse = (HttpWebResponse)response;
|
||||
|
||||
using (var responseStream = response.GetResponseStream())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ValidationFailure Test(TwitterSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var body = "Sonarr: Test Message @ " + DateTime.Now;
|
||||
|
||||
SendNotification(body, settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
|
||||
return new ValidationFailure("Host", "Unable to send test message");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
using FluentValidation;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.ThingiProvider;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Twitter
|
||||
{
|
||||
public class TwitterSettingsValidator : AbstractValidator<TwitterSettings>
|
||||
{
|
||||
public TwitterSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.AccessToken).NotEmpty();
|
||||
RuleFor(c => c.AccessTokenSecret).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();
|
||||
}
|
||||
}
|
||||
|
||||
public class TwitterSettings : IProviderConfig
|
||||
{
|
||||
private static readonly TwitterSettingsValidator Validator = new TwitterSettingsValidator();
|
||||
|
||||
public TwitterSettings()
|
||||
{
|
||||
DirectMessage = true;
|
||||
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; }
|
||||
|
||||
[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 NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -80,6 +80,9 @@
|
|||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OAuth">
|
||||
<HintPath>..\packages\OAuth.1.0.3\lib\net40\OAuth.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp, Version=105.0.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\RestSharp.105.0.1\lib\net4\RestSharp.dll</HintPath>
|
||||
|
@ -93,6 +96,7 @@
|
|||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
|
@ -739,7 +743,11 @@
|
|||
<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" />
|
||||
<Compile Include="Notifications\Twitter\TwitterSettings.cs" />
|
||||
<Compile Include="Profiles\Delay\DelayProfile.cs" />
|
||||
<Compile Include="Profiles\Delay\DelayProfileService.cs" />
|
||||
<Compile Include="Profiles\Delay\DelayProfileTagInUseValidator.cs" />
|
||||
|
@ -916,6 +924,7 @@
|
|||
<Compile Include="ThingiProvider\ProviderFactory.cs" />
|
||||
<Compile Include="ThingiProvider\ProviderMessage.cs" />
|
||||
<Compile Include="ThingiProvider\ProviderRepository.cs" />
|
||||
<Compile Include="TinyTwitter.cs" />
|
||||
<Compile Include="Tv\Actor.cs" />
|
||||
<Compile Include="Tv\AddSeriesOptions.cs" />
|
||||
<Compile Include="Tv\Commands\MoveSeriesCommand.cs" />
|
||||
|
|
|
@ -12,5 +12,6 @@ namespace NzbDrone.Core.ThingiProvider
|
|||
IEnumerable<ProviderDefinition> DefaultDefinitions { get; }
|
||||
ProviderDefinition Definition { get; set; }
|
||||
ValidationResult Test();
|
||||
object ConnectData(string stage, IDictionary<string, object> query);
|
||||
}
|
||||
}
|
|
@ -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<string, object> query );
|
||||
}
|
||||
}
|
|
@ -81,6 +81,11 @@ namespace NzbDrone.Core.ThingiProvider
|
|||
return GetInstance(definition).Test();
|
||||
}
|
||||
|
||||
public object ConnectData(TProviderDefinition definition, string stage, IDictionary<string, object> query)
|
||||
{
|
||||
return GetInstance(definition).ConnectData(stage, query);
|
||||
}
|
||||
|
||||
public List<TProvider> GetAvailableProviders()
|
||||
{
|
||||
return Active().Select(GetInstance).ToList();
|
||||
|
|
|
@ -0,0 +1,291 @@
|
|||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 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 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> 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, "");
|
||||
}
|
||||
|
||||
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, "");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private IEnumerable<Tweet> GetTimeline(string url, long? sinceId, long? maxId, int? count, string screenName)
|
||||
{
|
||||
var builder = new RequestBuilder(oauth, "GET", url);
|
||||
|
||||
if (sinceId.HasValue)
|
||||
builder.AddParameter("since_id", sinceId.Value.ToString());
|
||||
|
||||
if (maxId.HasValue)
|
||||
builder.AddParameter("max_id", maxId.Value.ToString());
|
||||
|
||||
if (count.HasValue)
|
||||
builder.AddParameter("count", count.Value.ToString());
|
||||
|
||||
if (screenName != "")
|
||||
builder.AddParameter("screen_name", screenName);
|
||||
|
||||
var responseContent = builder.Execute();
|
||||
|
||||
var serializer = new JavaScriptSerializer();
|
||||
|
||||
var tweets = (object[])serializer.DeserializeObject(responseContent);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
#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<string, string> 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<string, string>();
|
||||
}
|
||||
|
||||
public RequestBuilder AddParameter(string name, string value)
|
||||
{
|
||||
customParameters.Add(name, value.EncodeRFC3986());
|
||||
return this;
|
||||
}
|
||||
|
||||
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 content;
|
||||
}
|
||||
|
||||
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<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());
|
||||
|
||||
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<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 CreateNonce()
|
||||
{
|
||||
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
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 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", "~");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,9 @@
|
|||
<package id="ImageResizer" version="3.4.3" targetFramework="net40" />
|
||||
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net40" />
|
||||
<package id="NLog" version="2.1.0" targetFramework="net40" />
|
||||
<package id="OAuth" version="1.0.3" targetFramework="net40" />
|
||||
<package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" />
|
||||
<package id="RestSharp" version="105.0.1" targetFramework="net40" />
|
||||
<package id="ValueInjecter" version="2.3.3" targetFramework="net40" />
|
||||
<package id="TinyTwitter" version="1.1.1" targetFramework="net40" />
|
||||
</packages>
|
|
@ -0,0 +1,7 @@
|
|||
<div class="form-group {{#if advanced}}advanced-setting{{/if}}">
|
||||
<label class="col-sm-3 control-label"></label>
|
||||
|
||||
<div class="col-sm-5">
|
||||
<button class="form-control {{name}}" data-value="{{value}}">{{label}}</button>
|
||||
</div>
|
||||
</div>
|
|
@ -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');
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +1 @@
|
|||
<div class="form-group hidden">
|
||||
<label class="col-sm-3 control-label">{{label}}</label>
|
||||
|
||||
<div class="col-sm-5">
|
||||
<input type="text" name="fields.{{order}}.value" validation-name="{{name}}" spellcheck="false" class="form-control"/>
|
||||
</div>
|
||||
{{> FormHelpPartial}}
|
||||
</div>
|
||||
<input type="hidden" name="fields.{{order}}.value" validation-name="{{name}}" spellcheck="false"/>
|
|
@ -13,17 +13,19 @@ var view = Marionette.ItemView.extend({
|
|||
template : 'Settings/Notifications/Edit/NotificationEditViewTemplate',
|
||||
|
||||
ui : {
|
||||
onDownloadToggle : '.x-on-download',
|
||||
onUpgradeSection : '.x-on-upgrade',
|
||||
tags : '.x-tags',
|
||||
modalBody : '.modal-body',
|
||||
formTag : '.x-form-tag',
|
||||
path : '.x-path'
|
||||
onDownloadToggle : '.x-on-download',
|
||||
onUpgradeSection : '.x-on-upgrade',
|
||||
tags : '.x-tags',
|
||||
modalBody : '.x-modal-body',
|
||||
formTag : '.x-form-tag',
|
||||
path : '.x-path',
|
||||
authorizedNotificationButton : '.AuthorizeNotification'
|
||||
},
|
||||
|
||||
events : {
|
||||
'click .x-back' : '_back',
|
||||
'change .x-on-download' : '_onDownloadChanged'
|
||||
'change .x-on-download' : '_onDownloadChanged',
|
||||
'click .AuthorizeNotification' : '_onAuthorizeNotification'
|
||||
},
|
||||
|
||||
_deleteView : DeleteView,
|
||||
|
@ -81,6 +83,17 @@ var view = Marionette.ItemView.extend({
|
|||
} else {
|
||||
this.ui.onUpgradeSection.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();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1,8 +1,87 @@
|
|||
var $ = require('jquery');
|
||||
var _ = require('underscore');
|
||||
var DeepModel = require('backbone.deepmodel');
|
||||
var Messenger = require('../Shared/Messenger');
|
||||
|
||||
module.exports = DeepModel.extend({
|
||||
connectData : function(action, initialQueryString) {
|
||||
var self = this;
|
||||
|
||||
this.trigger('connect:sync');
|
||||
|
||||
var promise = $.Deferred();
|
||||
|
||||
var callAction = function(action) {
|
||||
var params = {
|
||||
url : self.collection.url + '/connectData/' + action,
|
||||
contentType : 'application/json',
|
||||
data : JSON.stringify(self.toJSON()),
|
||||
type : 'POST',
|
||||
isValidatedCall : true
|
||||
};
|
||||
|
||||
var ajaxPromise = $.ajax(params);
|
||||
ajaxPromise.fail(promise.reject);
|
||||
|
||||
ajaxPromise.success(function(response) {
|
||||
if (response.action)
|
||||
{
|
||||
if (response.action === 'openWindow')
|
||||
{
|
||||
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')
|
||||
{
|
||||
_.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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
callAction(action, initialQueryString);
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>oauth landing page</title>
|
||||
<script><!--
|
||||
window.opener.onCompleteOauth(window.location.search, function() { window.close(); });
|
||||
--></script>
|
||||
</head>
|
||||
<body>
|
||||
Shouldn't see this
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue