Ejemplo n.º 1
0
        public void AuthCallback(IManosContext ctx)
        {
            if (testInstance) {
                ctx.Response.SetCookie ("apachai:userId", 1.ToString ());
                ctx.Response.SetCookie ("apachai:token", "bar");
                store.SetUserInfos (1, "the_test");
                store.SetExtraUserInfos (1, "http://neteril.org/img/twitter.png", "The Test", "http://neteril.org", "My Foo Bar Life");
                store.SetUserAccessTokens (1, "bar", "bar");
                ctx.Response.Redirect ("/Post");

                return;
            }

            string token = ctx.Request.Data["oauth_token"];
            string tokenVerifier = ctx.Request.Data["oauth_verifier"];

            Log.Info ("Tokens and verifier: {0} and {1}", token, tokenVerifier);

            oauth.AcquireAccessToken (new OAuthToken (token, store.GetTempTokenSecret (token)), tokenVerifier)
                .ContinueWith (resultTask => {
                    var result = resultTask.Result;
                    var userInfos = result.Item2;
                    var tokens = result.Item1;

                    Log.Info ("Access token call return: {0} and {1}", userInfos.ToString (), tokens.ToString ());

                    bool first = false;
                    if ((first = !store.DoWeKnowUser (userInfos.UserId)))
                        store.SetUserInfos (userInfos.UserId, userInfos.UserName);

                    if (first || store.DoesUserNeedInfoUpdate (userInfos.UserId) ) {
                        Task.Factory.StartNew (() => {
                                var twitter = new Twitter (oauth);
                                twitter.Tokens = tokens;

                                var twitterInfos = twitter.GetUserInformations (userInfos.UserId.ToString ());

                                Log.Info ("Got twitter json infos for {0}", userInfos.UserId.ToString ());

                                var retDict = JSON.JsonDecode (twitterInfos) as Dictionary<object, object>;

                                if (retDict == null)
                                    return;

                                var pUrl = retDict["profile_image_url"] as string;
                                pUrl = string.IsNullOrEmpty (pUrl) ? string.Empty : pUrl.Replace ("normal.", "reasonably_small.");
                                var pageUrl = retDict["url"] as string ?? string.Empty;
                                var name = retDict["name"] as string ?? string.Empty;
                                var desc = retDict["description"] as string ?? string.Empty;

                                store.SetExtraUserInfos (userInfos.UserId,
                                                         pUrl,
                                                         name,
                                                         pageUrl,
                                                         desc);
                        });
                    }

                    store.SetUserAccessTokens (userInfos.UserId, tokens.Token, tokens.TokenSecret);

                    ctx.Response.SetCookie ("apachai:userId", userInfos.UserId.ToString (), TimeSpan.FromDays (30));
                    ctx.Response.SetCookie ("apachai:token", tokens.Token, TimeSpan.FromDays (30));

                    ctx.Response.Redirect ("/Post");
              }, ExecuteSync);
        }
Ejemplo n.º 2
0
        void DoPictureTasks(IManosContext ctx, string filename, long uid, string twittertext)
        {
            var finalUrl = baseServerUrl + "/i/" + filename;

            var twitter = new Twitter (oauth);
            twitter.Tokens = testInstance ? null : store.GetUserAccessTokens (uid);

            var task = !testInstance ?
                twitter.SendApachaiTweet (twittertext, finalUrl, filename, baseServerUrl + "/s/") :
                UrlShortener.GetShortenedId ();

            task.ContinueWith ((ret) => {
                Log.Info ("Registered final tweet, {0} | {1} | {2} | {3}", uid, filename, twittertext, ret.Result);
                store.RegisterImageWithTweet (uid,
                                              filename,
                                              string.IsNullOrEmpty (twittertext) ? string.Empty : twittertext,
                                              finalUrl,
                                              ret.Result);
                store.MapShortToLongUrl (ret.Result, filename);
                ctx.Response.Redirect ("/i/" + filename);
            }, ExecuteSync);
        }