Exemplo n.º 1
0
        public static string GenerateTimeline(List <MessageReadDTO> messages, timelineType type, UserReadDTO user = null, string otherPersonUsername = "")
        {
            bool loggedin = user != null;

            messages.Sort((x, y) => DateTime.Compare(x.pub_date, y.pub_date));
            messages.Reverse();

            StringBuilder sb = new StringBuilder();

            if (type == timelineType.PUBLIC)
            {
                sb.Append("<h2>Public Timeline</h2>");
            }
            else if (type == timelineType.OTHER)
            {
                sb.Append($"<h2>{otherPersonUsername}'s Timeline</h2>");
            }
            else if (type == timelineType.SELF)
            {
                sb.Append($"<h2>My Timeline</h2>");
            }

            if (loggedin && type == timelineType.SELF)
            {
                sb.Append(
                    $@"<div class=""twitbox""><h3>What's on your mind {user.username}?</h3>
        <form action=""/add_message"" method=""post""><p><input type=""text"" name=""text"" size=""60""><input type=""submit"" value=""Share""></p></form></div>"
                    );
            }
            if (messages.Count == 0)
            {
                sb.Append(@"<ul class=""messages"">
          <li><em>There's no messages so far.</em></li>
          </ul>
        ");
            }
            else
            {
                sb.Append(@"<ul class=""messages"">");
                foreach (MessageReadDTO msg in messages)
                {
                    string optionalZero        = msg.pub_date.Month < 10 ? "0" : "";
                    var    reformattedDateTime = "- " + msg.pub_date.Year + "-" + optionalZero + msg.pub_date.Month + "-" + msg.pub_date.Day + " @ " + msg.pub_date.Hour + ":" + msg.pub_date.Minute;
                    // sb.Append($"<p>{msg.author.username} [{msg.pub_date}]: {msg.text}</p>");
                    sb.Append($@"
          <li>

            <p>
              <strong>
                <a href=""/{msg.author.username}"">{msg.author.username}</a>
              </strong>
              {msg.text}
              <small>
                {reformattedDateTime}
              </small>
            </p>
          </li>");
                    // if (loggedin)
                    // {
                    //   sb.Append($"<form method=post action={msg.author.username}/follow><button type=submit>Follow</button></form>");
                    // }
                }
                sb.Append("</ul>");
            }


            return(Layout(title: loggedin ? "Your Timeline" : "Public Timeline", body: sb.ToString(), user: user));
        }
Exemplo n.º 2
0
        public static string GenerateTimeline(List <MessageReadDTO> messages, timelineType type, UserReadDTO user = null, string otherPersonUsername = "")
        {
            bool loggedin = user != null;

            messages.Sort((x, y) => DateTime.Compare(x.pub_date, y.pub_date));
            messages.Reverse();

            StringBuilder sb = new StringBuilder();

            if (type == timelineType.PUBLIC)
            {
                sb.Append("<h2>Public Timeline</h2>");
            }
            else if (type == timelineType.OTHER)
            {
                sb.Append($"<h2>{HtmlEncode(otherPersonUsername)}'s Timeline</h2>");
                if (user != null)
                {
                    if (otherPersonUsername == user.username)
                    {
                        sb.Append(@"<div class=""followstatus"">This is you!</div>");
                    }
                    else
                    {
                        if (/*userFollows(user, otherPersonUsername)*/ user.following.Contains(otherPersonUsername))
                        {
                            sb.Append($@"<div class=""followstatus"">You are currently following this user.

              <form method=post action=""/{HtmlEncode(otherPersonUsername)}/unfollow""  style=""display: inline-block""><button class=nav_a type=submit>Unfollow user</button></form>
              </div>
              ");
                        }
                        else
                        {
                            sb.Append($@"<div class=""followstatus"">You are not yet following this user yet.
              <form method=post action=""/{HtmlEncode(otherPersonUsername)}/follow""  style=""display: inline-block""><button class=nav_a type=submit>Follow user</button></form>
              </div>
              ");
                        }
                    }
                }
            }
            else if (type == timelineType.SELF)
            {
                sb.Append($"<h2>My Timeline</h2>");
            }

            sb = addNotifications(sb);

            if (loggedin && type == timelineType.SELF)
            {
                sb.Append(
                    $@"<div class=""twitbox""><h3>What's on your mind {HtmlEncode(user.username)}?</h3>
        <form action=""/add_message"" method=""post""><p><input type=""text"" name=""text"" size=""60""><input type=""submit"" value=""Share""></p></form></div>"
                    );
            }
            if (messages.Count == 0)
            {
                sb.Append(@"<ul class=""messages"">
          <li><em>There's no messages so far.</em></li>
          </ul>
        ");
            }
            else
            {
                sb.Append(@"<ul class=""messages"">");
                foreach (MessageReadDTO msg in messages)
                {
                    var reformattedDateTime = generateDateTimeString(msg.pub_date);
                    var email = msg.author.email ?? "";

                    var bytes             = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(email.ToLower()));
                    var gravatarEmailHash = new StringBuilder();

                    foreach (var b in bytes)
                    {
                        gravatarEmailHash.Append(b.ToString("x2"));
                    }

                    sb.Append($@"
          <li>
            <img src=""https://www.gravatar.com/avatar/{gravatarEmailHash}?d=identicon&s=48"">
            <p>
              <strong>
                <a href=""/{HtmlEncode(msg.author.username)}"">{HtmlEncode(msg.author.username)}</a>
              </strong>
              {HtmlEncode(msg.text)}
              <small>
                {reformattedDateTime}
              </small>
            </p>
          </li>");
                    // if (loggedin)
                    // {
                    //   sb.Append($"<form method=post action={msg.author.username}/follow><button type=submit>Follow</button></form>");
                    // }
                }
                sb.Append("</ul>");
            }
            string toReturn = Layout(title: loggedin ? "Your Timeline" : "Public Timeline", body: sb.ToString(), user: user);

            clearNotifications();
            return(toReturn);
        }