Example #1
0
 public UserMentionEntityModel(UserMentionEntity entity)
     : base(entity)
 {
     Id         = entity.Id;
     Name       = entity.Name;
     ScreenName = entity.ScreenName;
 }
Example #2
0
        private static ContextMenu CreateUserContextMenu(UserMentionEntity entity, IHighlightable item)
        {
            var menu = new ContextMenu();

            menu.Items.Add(new MenuItem
            {
                Header           = Strings.MuteUser,
                CommandParameter = $"@{entity.ScreenName}",
                Command          = GlobalCommands.CreateMuteCommand
            });

            menu.Items.Add(new Separator());

            menu.Items.Add(new MenuItem
            {
                Header           = Strings.Block,
                Command          = item.BlockUserCommand,
                CommandParameter = entity.Id
            });

            menu.Items.Add(new MenuItem
            {
                Header           = Strings.ReportSpam,
                Command          = item.ReportSpamCommand,
                CommandParameter = entity.Id
            });

            return(menu);
        }
Example #3
0
        public void DirectMessage_Constructor_Parses_Json()
        {
            var dmJson = JsonMapper.ToObject(DirectMessagesResponse);

            var dm = new DirectMessage(dmJson);

            Assert.AreEqual(new DateTimeOffset(2012, 7, 22, 18, 38, 09, new TimeSpan(0L)).DateTime, dm.CreatedAt);
            Assert.AreEqual(15411837ul, dm.SenderID);
            Assert.AreEqual("JoeMayo", dm.SenderScreenName);
            Assert.IsNotNull(dm.Sender);
            Assert.AreEqual(16761255ul, dm.RecipientID);
            Assert.AreEqual("Linq2Tweeter", dm.RecipientScreenName);
            Assert.IsNotNull(dm.Recipient);
            Assert.AreEqual(227110300510461952ul, dm.IDResponse);
            Assert.AreEqual("227110300510461952", dm.IDString);
            Assert.AreEqual("Testing entities from @JoeMayo : http://t.co/Q1CQHi2 #entitytest", dm.Text);
            Entities entities = dm.Entities;

            Assert.IsNotNull(entities);
            List <UserMentionEntity> userMentions = entities.UserMentionEntities;

            Assert.IsNotNull(userMentions);
            Assert.IsTrue(userMentions.Any());
            UserMentionEntity usrMention = userMentions.FirstOrDefault();

            Assert.IsNotNull(usrMention);
            Assert.AreEqual("Joe Mayo", usrMention.Name);
        }
Example #4
0
        internal static List <UserMentionEntity> ExtractMentions(string text)
        {
            var mentions = new List <UserMentionEntity>();

            foreach (Match match in Regex.VALID_MENTION_OR_LIST.Matches(text))
            {
                int start = match.Index + 1;
                int end   = start + match.Length - 1;
                if (start == 1)
                {
                    // FIXME: I have no idea why...
                    start = 0;
                }

                UserMentionEntity tag = new UserMentionEntity
                {
                    Start      = start,
                    End        = end,
                    ScreenName = match.Groups[3].Value
                };

                mentions.Add(tag);
            }

            return(mentions);
        }
Example #5
0
        public EntityViewModel(UserMentionEntity userMentionEntity)
        {
            Model = userMentionEntity;

            ExpandedUrl = "@" + userMentionEntity.ScreenName;
            DisplayUrl  = "@" + userMentionEntity.ScreenName;

            Notice = Notice.Instance;
        }
Example #6
0
        /// <summary>
        ///     Generates an inline from a mention entity.
        /// </summary>
        /// <param name="entity">The entity to generate the inline from.</param>
        /// <returns>The generated inline.</returns>
        private static Inline GenerateMention(UserMentionEntity entity)
        {
            Hyperlink link = new Hyperlink();

            link.Inlines.Add(Constants.Twitter.Mention + entity.ScreenName);
            link.SetResourceReference(TextElement.ForegroundProperty, "MentionBrush");
            link.TextDecorations  = null;
            link.Command          = GlobalCommands.OpenProfileCommand;
            link.CommandParameter = entity.Id;
            link.ContextMenu      = CreateUserContextMenu(entity);

            return(link);
        }
        private static void ReplaceMention(TextBlock textBlock, UserMentionEntity mention, ICommand command)
        {
            var hyperlink = new Hyperlink()
            {
                NavigateUri = new Uri("@" + mention.ScreenName, UriKind.Relative),
                Foreground  = new SolidColorBrush(Colors.DodgerBlue)
            };

            hyperlink.RequestNavigate += (sender, e) =>
            {
                command.Execute(mention.ScreenName);
            };
            hyperlink.Inlines.Add(new Run()
            {
                Text = "@" + mention.ScreenName
            });
            textBlock.Inlines.Add(hyperlink);
        }
Example #8
0
        public static IEnumerable <IEntity> ParseUserMentions(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                yield break;
            }
            var matches = Const.UserNameRegex.Matches(text);

            foreach (Match match in matches)
            {
                var entity = new UserMentionEntity
                {
                    ScreenName = match.Groups["name"].Value,
                    Indices    = new int[] { match.Groups["name"].Index - 1, 0 }//remove @
                };
                yield return(new UserMentionEntityModel(entity));
            }
        }
Example #9
0
 public static IEnumerable <IEntity> ParseUserMentions(string text, IList <IUserMentionEntity> mentions)
 {
     foreach (var mention in mentions.GroupBy(m => m.Id).Select(g => g.First()))
     {
         var matches = Regex.Matches(text, string.Format(Const.USERNAMEPATTERN, Regex.Escape(mention.DisplayText)), RegexOptions.IgnoreCase);
         foreach (Match match in matches)
         {
             var entity = new UserMentionEntity
             {
                 Id         = mention.Id,
                 Name       = mention.Name,
                 ScreenName = mention.ScreenName,
                 Indices    = new int[] { match.Index, 0 }
             };
             var model = new UserMentionEntityModel(entity);
             yield return(model);
         }
     }
 }
Example #10
0
        public void ProcessResults_Populates_Entity_Users()
        {
            var searchProc = new SearchRequestProcessor <Search> {
                BaseUrl = "https://api.twitter.com/1.1/search/"
            };

            List <Search> results = searchProc.ProcessResults(SearchJson);

            List <UserMentionEntity> users = results.First().Statuses[0].Entities.UserMentionEntities;

            Assert.IsNotNull(users);
            Assert.AreEqual(1, users.Count);
            UserMentionEntity firstUser = users.First();

            Assert.AreEqual("JoeMayo", firstUser.ScreenName);
            Assert.AreEqual("Joe Mayo", firstUser.Name);
            Assert.AreEqual(15411837ul, firstUser.Id);
            Assert.AreEqual(3, firstUser.Start);
            Assert.AreEqual(11, firstUser.End);
        }
        public async Task NewDirectMessageEventsAsync_WithRecipientID_ReturnsCreatedDM()
        {
            const ulong  RecipientID = 1;
            const string Text        = "Hi";
            var          ctx         = InitializeTwitterContext();

            DirectMessageEvents dmEvt = await ctx.NewDirectMessageEventAsync(RecipientID, Text);

            Assert.IsNotNull(dmEvt);
            DirectMessageEventsValue dmVal = dmEvt.Value;

            Assert.IsNotNull(dmVal);
            DMEvent evt = dmVal.DMEvent;

            Assert.IsNotNull(evt);
            Assert.AreEqual("message_create", evt.Type);
            Assert.AreEqual("917929712638246916", evt.ID);
            Assert.AreEqual("1507686472459", evt.CreatedTimestamp);
            Assert.AreEqual(new DateTime(2017, 10, 11, 01, 47, 52, 459), evt.CreatedAt);
            DirectMessageCreate mc = evt.MessageCreate;

            Assert.IsNotNull(mc);
            Assert.AreEqual("15411837", mc.SenderID);
            DirectMessageTarget tgt = mc.Target;

            Assert.IsNotNull(tgt);
            Assert.AreEqual("16761255", tgt.RecipientID);
            DirectMessageData md = mc.MessageData;

            Assert.IsNotNull(md);
            Assert.AreEqual(
                "DM from @JoeMayo to @Linq2Twitr of $MSFT &amp; $TSLA with #TwitterAPI #chatbot at https://t.co/ikGq2AnAir and https://t.co/nLvwwjz40q on 10/10/2017 6:47:46 PM!'",
                md.Text);
            Entities ent = md.Entities;

            Assert.IsNotNull(ent);
            List <HashTagEntity> hashtags = ent.HashTagEntities;

            Assert.IsNotNull(hashtags);
            Assert.AreEqual(2, hashtags.Count);
            HashTagEntity ht1 = hashtags[0];

            Assert.IsNotNull(ht1);
            Assert.AreEqual("TwitterAPI", ht1.Text);
            int[] ht1i = ht1.Indices;
            Assert.IsNotNull(ht1i);
            Assert.AreEqual(58, ht1i[0]);
            Assert.AreEqual(69, ht1i[1]);
            HashTagEntity ht2 = hashtags[1];

            Assert.IsNotNull(ht2);
            Assert.AreEqual("chatbot", ht2.Text);
            int[] ht2i = ht2.Indices;
            Assert.IsNotNull(ht2i);
            Assert.AreEqual(70, ht2i[0]);
            Assert.AreEqual(78, ht2i[1]);
            List <SymbolEntity> syms = ent.SymbolEntities;

            Assert.IsNotNull(syms);
            Assert.AreEqual(2, syms.Count);
            SymbolEntity sym1 = syms[0];

            Assert.IsNotNull(sym1);
            Assert.AreEqual("MSFT", sym1.Text);
            int[] sym1i = sym1.Indices;
            Assert.IsNotNull(sym1i);
            Assert.AreEqual(35, sym1i[0]);
            Assert.AreEqual(40, sym1i[1]);
            SymbolEntity sym2 = syms[1];

            Assert.IsNotNull(sym2);
            Assert.AreEqual("TSLA", sym2.Text);
            int[] sym2i = sym2.Indices;
            Assert.IsNotNull(sym2i);
            Assert.AreEqual(47, sym2i[0]);
            Assert.AreEqual(52, sym2i[1]);
            List <UserMentionEntity> usrs = ent.UserMentionEntities;

            Assert.IsNotNull(usrs);
            Assert.AreEqual(2, usrs.Count);
            UserMentionEntity um1 = usrs[0];

            Assert.IsNotNull(um1);
            Assert.AreEqual("JoeMayo", um1.ScreenName);
            Assert.AreEqual("Joe Mayo", um1.Name);
            Assert.AreEqual(15411837ul, um1.Id);
            Assert.AreEqual("15411837", um1.IdStr);
            int[] um1i = um1.Indices;
            Assert.IsNotNull(um1i);
            Assert.AreEqual(2, um1i.Length);
            Assert.AreEqual(8, um1i[0]);
            Assert.AreEqual(16, um1i[1]);
            UserMentionEntity um2 = usrs[1];

            Assert.IsNotNull(um2);
            Assert.AreEqual("Linq2Twitr", um2.ScreenName);
            Assert.AreEqual("LINQ to Twitr", um2.Name);
            Assert.AreEqual(16761255ul, um2.Id);
            Assert.AreEqual("16761255", um2.IdStr);
            int[] um2i = um2.Indices;
            Assert.IsNotNull(um2i);
            Assert.AreEqual(2, um2i.Length);
            Assert.AreEqual(20, um2i[0]);
            Assert.AreEqual(31, um2i[1]);
            List <UrlEntity> urls = ent.UrlEntities;

            Assert.IsNotNull(urls);
            Assert.AreEqual(2, urls.Count);
            UrlEntity url1 = urls[0];

            Assert.IsNotNull(url1);
            Assert.AreEqual("https://t.co/ikGq2AnAir", url1.Url);
            Assert.AreEqual("http://bit.ly/2xSJWJk", url1.ExpandedUrl);
            Assert.AreEqual("bit.ly/2xSJWJk", url1.DisplayUrl);
            int[] url1i = url1.Indices;
            Assert.IsNotNull(url1i);
            Assert.AreEqual(2, url1i.Length);
            Assert.AreEqual(82, url1i[0]);
            Assert.AreEqual(105, url1i[1]);
            UrlEntity url2 = urls[1];

            Assert.IsNotNull(url2);
            Assert.AreEqual("https://t.co/nLvwwjz40q", url2.Url);
            Assert.AreEqual("http://amzn.to/2gD09X6", url2.ExpandedUrl);
            Assert.AreEqual("amzn.to/2gD09X6", url2.DisplayUrl);
            int[] url2i = url2.Indices;
            Assert.IsNotNull(url2i);
            Assert.AreEqual(2, url2i.Length);
            Assert.AreEqual(110, url2i[0]);
            Assert.AreEqual(133, url2i[1]);
        }
Example #12
0
 public UserCacheEntry(UserMentionEntity mention)
 {
     UserId   = mention.Id;
     UserName = mention.ScreenName;
 }
Example #13
0
        public void ProcessResults_ForShow_HandlesSingleResult()
        {
            var dmReqProc = new DirectMessageEventsRequestProcessor <DirectMessageEvents>
            {
                BaseUrl = "https://api.twitter.com/1.1/",
                Type    = DirectMessageEventsType.Show
            };

            List <DirectMessageEvents> dms = dmReqProc.ProcessResults(TestQuerySingleResponse);

            Assert.IsNotNull(dms?.SingleOrDefault());

            DirectMessageEvents dmEvt = dms.First();

            Assert.IsNotNull(dmEvt);
            DirectMessageEventsValue dmVal = dmEvt.Value;

            Assert.IsNotNull(dmVal);
            DMEvent evt = dmVal.DMEvent;

            Assert.IsNotNull(evt);
            Assert.AreEqual("message_create", evt.Type);
            Assert.AreEqual("917929712638246916", evt.ID);
            Assert.AreEqual("1507686472459", evt.CreatedTimestamp);
            Assert.AreEqual(new DateTime(2017, 10, 11, 01, 47, 52, 459), evt.CreatedAt);
            DirectMessageCreate mc = evt.MessageCreate;

            Assert.IsNotNull(mc);
            Assert.AreEqual("15411837", mc.SenderID);
            Assert.AreEqual("472356", mc.SourceAppID);
            DirectMessageTarget tgt = mc.Target;

            Assert.IsNotNull(tgt);
            Assert.AreEqual("16761255", tgt.RecipientID);
            DirectMessageData md = mc.MessageData;

            Assert.IsNotNull(md);
            Assert.AreEqual(
                "DM from @JoeMayo to @Linq2Twitr of $MSFT &amp; $TSLA with #TwitterAPI #chatbot at https://t.co/ikGq2AnAir and https://t.co/nLvwwjz40q on 10/10/2017 6:47:46 PM!'",
                md.Text);
            Entities ent = md.Entities;

            Assert.IsNotNull(ent);
            List <HashTagEntity> hashtags = ent.HashTagEntities;

            Assert.IsNotNull(hashtags);
            Assert.AreEqual(2, hashtags.Count);
            HashTagEntity ht1 = hashtags[0];

            Assert.IsNotNull(ht1);
            Assert.AreEqual("TwitterAPI", ht1.Text);
            int[] ht1i = ht1.Indices;
            Assert.IsNotNull(ht1i);
            Assert.AreEqual(58, ht1i[0]);
            Assert.AreEqual(69, ht1i[1]);
            HashTagEntity ht2 = hashtags[1];

            Assert.IsNotNull(ht2);
            Assert.AreEqual("chatbot", ht2.Text);
            int[] ht2i = ht2.Indices;
            Assert.IsNotNull(ht2i);
            Assert.AreEqual(70, ht2i[0]);
            Assert.AreEqual(78, ht2i[1]);
            List <SymbolEntity> syms = ent.SymbolEntities;

            Assert.IsNotNull(syms);
            Assert.AreEqual(2, syms.Count);
            SymbolEntity sym1 = syms[0];

            Assert.IsNotNull(sym1);
            Assert.AreEqual("MSFT", sym1.Text);
            int[] sym1i = sym1.Indices;
            Assert.IsNotNull(sym1i);
            Assert.AreEqual(35, sym1i[0]);
            Assert.AreEqual(40, sym1i[1]);
            SymbolEntity sym2 = syms[1];

            Assert.IsNotNull(sym2);
            Assert.AreEqual("TSLA", sym2.Text);
            int[] sym2i = sym2.Indices;
            Assert.IsNotNull(sym2i);
            Assert.AreEqual(47, sym2i[0]);
            Assert.AreEqual(52, sym2i[1]);
            List <UserMentionEntity> usrs = ent.UserMentionEntities;

            Assert.IsNotNull(usrs);
            Assert.AreEqual(2, usrs.Count);
            UserMentionEntity um1 = usrs[0];

            Assert.IsNotNull(um1);
            Assert.AreEqual("JoeMayo", um1.ScreenName);
            Assert.AreEqual("Joe Mayo", um1.Name);
            Assert.AreEqual(15411837ul, um1.Id);
            Assert.AreEqual("15411837", um1.IdStr);
            int[] um1i = um1.Indices;
            Assert.IsNotNull(um1i);
            Assert.AreEqual(2, um1i.Length);
            Assert.AreEqual(8, um1i[0]);
            Assert.AreEqual(16, um1i[1]);
            UserMentionEntity um2 = usrs[1];

            Assert.IsNotNull(um2);
            Assert.AreEqual("Linq2Twitr", um2.ScreenName);
            Assert.AreEqual("LINQ to Twitr", um2.Name);
            Assert.AreEqual(16761255ul, um2.Id);
            Assert.AreEqual("16761255", um2.IdStr);
            int[] um2i = um2.Indices;
            Assert.IsNotNull(um2i);
            Assert.AreEqual(2, um2i.Length);
            Assert.AreEqual(20, um2i[0]);
            Assert.AreEqual(31, um2i[1]);
            List <UrlEntity> urls = ent.UrlEntities;

            Assert.IsNotNull(urls);
            Assert.AreEqual(2, urls.Count);
            UrlEntity url1 = urls[0];

            Assert.IsNotNull(url1);
            Assert.AreEqual("https://t.co/ikGq2AnAir", url1.Url);
            Assert.AreEqual("http://bit.ly/2xSJWJk", url1.ExpandedUrl);
            Assert.AreEqual("bit.ly/2xSJWJk", url1.DisplayUrl);
            int[] url1i = url1.Indices;
            Assert.IsNotNull(url1i);
            Assert.AreEqual(2, url1i.Length);
            Assert.AreEqual(82, url1i[0]);
            Assert.AreEqual(105, url1i[1]);
            UrlEntity url2 = urls[1];

            Assert.IsNotNull(url2);
            Assert.AreEqual("https://t.co/nLvwwjz40q", url2.Url);
            Assert.AreEqual("http://amzn.to/2gD09X6", url2.ExpandedUrl);
            Assert.AreEqual("amzn.to/2gD09X6", url2.DisplayUrl);
            int[] url2i = url2.Indices;
            Assert.IsNotNull(url2i);
            Assert.AreEqual(2, url2i.Length);
            Assert.AreEqual(110, url2i[0]);
            Assert.AreEqual(133, url2i[1]);
            JsonElement app       = dmVal.Apps;
            JsonElement appDetail = app.GetProperty("472356");

            Assert.AreEqual("472356", appDetail.GetString("id"));
            Assert.AreEqual("LINQ to Tweeter", appDetail.GetString("name"));
            Assert.AreEqual("https://github.com/JoeMayo/LinqToTwitter", appDetail.GetString("url"));
        }