Beispiel #1
0
        public TwitterUser GetUser(string username)
        {
            if (!_userCache.TryGetValue(username, out TwitterUser user))
            {
                user = _twitterService.GetUser(username);
                if (user != null)
                {
                    _userCache.Set(username, user, _cacheEntryOptions);
                }
            }

            return(user);
        }
Beispiel #2
0
        public IActionResult Index(string id)
        {
            _logger.LogTrace("User Index: {Id}", id);

            id = id.Trim(new[] { ' ', '@' }).ToLowerInvariant();

            // Ensure valid username
            // https://help.twitter.com/en/managing-your-account/twitter-username-rules
            TwitterUser user = null;

            if (!string.IsNullOrWhiteSpace(id) && UserRegexes.TwitterAccount.IsMatch(id) && id.Length <= 15)
            {
                user = _twitterUserService.GetUser(id);
            }

            var acceptHeaders = Request.Headers["Accept"];

            if (acceptHeaders.Any())
            {
                var r = acceptHeaders.First();
                if (r.Contains("application/activity+json"))
                {
                    if (user == null)
                    {
                        return(NotFound());
                    }
                    var apUser     = _userService.GetUser(user);
                    var jsonApUser = JsonConvert.SerializeObject(apUser);
                    return(Content(jsonApUser, "application/activity+json; charset=utf-8"));
                }
            }

            if (user == null)
            {
                return(View("UserNotFound"));
            }

            var displayableUser = new DisplayTwitterUser
            {
                Name            = user.Name,
                Description     = user.Description,
                Acct            = user.Acct.ToLowerInvariant(),
                Url             = user.Url,
                ProfileImageUrl = user.ProfileImageUrl,
                Protected       = user.Protected,

                InstanceHandle = $"@{user.Acct.ToLowerInvariant()}@{_instanceSettings.Domain}"
            };

            return(View(displayableUser));
        }
Beispiel #3
0
        public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
        {
            TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;

            var user = _twitterUserService.GetUser(username);

            if (user.Protected)
            {
                return(new ExtractedTweet[0]);
            }

            var tweets = new List <ITweet>();

            try
            {
                if (fromTweetId == -1)
                {
                    var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
                    _statisticsHandler.CalledTimelineApi();
                    if (timeline != null)
                    {
                        tweets.AddRange(timeline);
                    }
                }
                else
                {
                    var timelineRequestParameters = new UserTimelineParameters
                    {
                        SinceId = fromTweetId,
                        MaximumNumberOfTweetsToRetrieve = nberTweets
                    };
                    var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
                    _statisticsHandler.CalledTimelineApi();
                    if (timeline != null)
                    {
                        tweets.AddRange(timeline);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error retrieving timeline from {Username}, from {TweetId}", username, fromTweetId);
            }

            return(tweets.Select(_tweetExtractor.Extract).ToArray());
        }
        public IActionResult Index(string id)
        {
            id = id.Trim(new[] { ' ', '@' }).ToLowerInvariant();
            var user = _twitterUserService.GetUser(id);

            var acceptHeaders = Request.Headers["Accept"];

            if (acceptHeaders.Any())
            {
                var r = acceptHeaders.First();
                if (r.Contains("application/activity+json"))
                {
                    if (user == null)
                    {
                        return(NotFound());
                    }
                    var apUser     = _userService.GetUser(user);
                    var jsonApUser = JsonConvert.SerializeObject(apUser);
                    return(Content(jsonApUser, "application/activity+json; charset=utf-8"));
                }
            }

            if (user == null)
            {
                return(View("UserNotFound"));
            }

            var displayableUser = new DisplayTwitterUser
            {
                Name            = user.Name,
                Description     = user.Description,
                Acct            = user.Acct.ToLowerInvariant(),
                Url             = user.Url,
                ProfileImageUrl = user.ProfileImageUrl,
                Protected       = user.Protected,

                InstanceHandle = $"@{user.Acct.ToLowerInvariant()}@{_instanceSettings.Domain}"
            };

            return(View(displayableUser));
        }
Beispiel #5
0
        public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
        {
            var tweets = new List <ITweet>();

            _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
            ExceptionHandler.SwallowWebExceptions           = false;
            TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;

            var user = _twitterUserService.GetUser(username);

            if (user == null || user.Protected)
            {
                return(new ExtractedTweet[0]);
            }

            if (fromTweetId == -1)
            {
                var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
                _statisticsHandler.CalledTimelineApi();
                if (timeline != null)
                {
                    tweets.AddRange(timeline);
                }
            }
            else
            {
                var timelineRequestParameters = new UserTimelineParameters
                {
                    SinceId = fromTweetId,
                    MaximumNumberOfTweetsToRetrieve = nberTweets
                };
                var timeline = Timeline.GetUserTimeline(user.Id, timelineRequestParameters);
                _statisticsHandler.CalledTimelineApi();
                if (timeline != null)
                {
                    tweets.AddRange(timeline);
                }
            }

            return(tweets.Select(_tweetExtractor.Extract).ToArray());
        }
        public IActionResult Webfinger(string resource = null)
        {
            var acct = resource.Split("acct:")[1].Trim();

            string name   = null;
            string domain = null;

            var splitAcct = acct.Split('@', StringSplitOptions.RemoveEmptyEntries);

            var atCount = acct.Count(x => x == '@');

            if (atCount == 1 && acct.StartsWith('@'))
            {
                name = splitAcct[1];
            }
            else if (atCount == 1 || atCount == 2)
            {
                name   = splitAcct[0];
                domain = splitAcct[1];
            }
            else
            {
                return(BadRequest());
            }

            // Ensure lowercase
            name = name.ToLowerInvariant();

            if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain)
            {
                return(NotFound());
            }

            var user = _twitterUserService.GetUser(name);

            if (user == null)
            {
                return(NotFound());
            }

            var actorUrl = UrlFactory.GetActorUrl(_settings.Domain, name);

            var result = new WebFingerResult()
            {
                subject = $"acct:{name}@{_settings.Domain}",
                aliases = new[]
                {
                    actorUrl
                },
                links = new List <WebFingerLink>
                {
                    new WebFingerLink()
                    {
                        rel  = "http://webfinger.net/rel/profile-page",
                        type = "text/html",
                        href = actorUrl
                    },
                    new WebFingerLink()
                    {
                        rel  = "self",
                        type = "application/activity+json",
                        href = actorUrl
                    }
                }
            };

            return(new JsonResult(result));
        }