YAFLogo

tommy382
  • tommy382
  • 100% (Exalted)
  • YAF Commander Topic Starter
14 years ago
As promised, this is how I implement the hidden bbcode. What this tag does is it hides the content inside from the viewer until the viewer THANK the topic starter. If the viewer is an admin or the topic starter himself, the content is not hidden.

To see a sample topic with this bbcode in action, go here .

Here is the code modification.

    public static string FormatMessage([NotNull] string message, [NotNull] MessageFlags messageFlags, bool targetBlankOverride, DateTime messageLastEdited)
    {
      ...
      // do html damage control
      message = RepairHtml(message, messageFlags.IsHtml);

      //Process hidden tag replacement. - //Tommy
      {
        bool isUserAdmin = YafContext.Current.IsAdmin;

        Match hiddenTagMatch = _rgxHidden.Match(message);
        if (hiddenTagMatch.Success)
        {
          Group hiddenContent = hiddenTagMatch.Groups["hiddencontent"];
          //TODO: Get "shownContent" from configuration so an admin can change w/o recompiling.
          string shownContent = "<div class='Warning'style='width:670px;'>" +
            "<div style='float:right; padding-top:10px;'><img src='http://forum.tkaraoke.com/images/ThankYouPost.png' /></div>" +
            "The content of this page is hidden. After you THANK the poster, refresh the page to see the hidden content. You only need to thank the first post (post #1).<br /><br />" +
            "Phần nội dung đã bị ẩn đi. Sau khi nhấn vào nút THANK để cám ơn người đăng bài này, bạn phải Refresh trang này lại để thấy nội dung. Bạn chỉ cần THANK <span style='text-decoration:underline; font-weight:bold;'>post đầu tiên</span> (post #1 ở trang 1)." +
            "</div>";
          if (isUserAdmin)
          {
            shownContent = hiddenContent.Value;
          }
          else if (YafContext.Current.User != null &&
                   YafContext.Current.User.IsApproved)
          {
            int userId = YafContext.Current.CurrentUserData.UserID;
            int topicId = YafContext.Current.PageTopicID;
            if ((GetTopicStarter(topicId) == userId) ||
               (IsUserThankedTopicStarter(topicId, userId)))
            {
              //Show hiddent content if user is the poster or have thanked the poster.
              shownContent = hiddenContent.Value;
            }
          }

          //Update the hidden content with the updated content.
          message = _rgxHidden.Replace(message, shownContent);
        }
      }
...
    public static int GetTopicStarter(int topicId)  //Tommy
    {
      try
      {
        string query = "SELECT UserID FROM yaf_Topic WHERE TopicID=" + topicId;
        using (var cmd = new System.Data.SqlClient.SqlCommand(query))
        {
          cmd.CommandType = CommandType.Text;
          int userId = (int)YafDBAccess.Current.ExecuteScalar(cmd);
          return userId;
        }
      }
      catch (Exception)
      {
        return -1;
      }
    }

    public static bool IsUserThankedTopicStarter(int topicId, int userId)  //Tommy
    {
      System.Text.StringBuilder querySb =
        new System.Text.StringBuilder();
      querySb.Append("SELECT COUNT(TH.ThanksID) ");
      querySb.Append("FROM yaf_Message AS M INNER JOIN ");
      querySb.Append("yaf_Topic AS T ON M.TopicID = T.TopicID INNER JOIN ");
      querySb.Append("yaf_Thanks AS TH ON M.MessageID = TH.MessageID ");
      querySb.Append("WHERE (T.TopicID = {0}) AND (M.Position = 0) AND (TH.ThanksFromUserID = {1})");
      string query = string.Format(querySb.ToString(),
        topicId, userId);

      using (var cmd = new System.Data.SqlClient.SqlCommand(query))
      {
        cmd.CommandType = CommandType.Text;
        int thankCount = (int)YafDBAccess.Current.ExecuteScalar(cmd);
        return (thankCount > 0);
      }
    }
    /// <summary>
    /// The regular expression to process [hidden]...[/hidden]
    /// </summary>
    private static readonly Regex _rgxHidden =
      new Regex(@"\[hidden\](?<hiddencontent>(.*?))\[/hidden\]", _options |
        RegexOptions.Singleline | RegexOptions.Compiled);  //Tommy
Sponsor
Jaben
  • Jaben
  • 100% (Exalted)
  • YAF Developer
14 years ago
There is a custom BBCode module system for this type of modification. Not sure why you wanted to hard-code this code into YAF.
tommy382
  • tommy382
  • 100% (Exalted)
  • YAF Commander Topic Starter
14 years ago
This code was added in before 1.9.5. Are you saying with 1.9.5 I can add a new bbcode like the above via the admin interface w/o adding any code? How?
tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
14 years ago

This code was added in before 1.9.5. Are you saying with 1.9.5 I can add a new bbcode like the above via the admin interface w/o adding any code? How?

Originally Posted by: tommy382 

The BBCode Extensions are included in YAF for a very long time.

Go to Admin -> Settings -> BB Code Extensions. Take a look at the existing items how its done, in your case you need to use "Use Module:Use server-side module (class) instead of replace regex?". take a look at the SPOILER Tag, and the SpoilerBBCodeModule.cs in the Modules Folder.

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
14 years ago
@tommy382

I am currently testing your mod, i converted it to a bbcode extension module. Also i made to bbcode out of it


[HIDDEN]...[HIDDEN]

and


[HIDDEN=123]...[HIDDEN]

Where you can specify the postid to directly use the current post as thank.

But i found some issues. For Example when you use the Quote on a Hidden Post, the hidden content is exposed. Also on the RSS Feed.

But i already working on it. Maybe i add it directly in yaf ?!

tommy382
  • tommy382
  • 100% (Exalted)
  • YAF Commander Topic Starter
14 years ago
There is another piece of code that return a constant "You can't quote a post with the hidden tag." I'll post that code when I get home. I'm not sure about RSS. Maybe RSS is also using the same "Format" method that the to-be-posted code already handled?

You can test the quote logic here .

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
13 years ago

There is another piece of code that return a constant "You can't quote a post with the hidden tag." I'll post that code when I get home. I'm not sure about RSS. Maybe RSS is also using the same "Format" method that the to-be-posted code already handled?

You can test the quote logic here .

Originally Posted by: tommy382 

I completed my code now, its fully committed to svn. The hidden tag now uses automatically the current message id, you now can use this bbcode in any posting.

the hidden content in a reply and in the rss feed will be completly removed.

bbobb
  • bbobb
  • 100% (Exalted)
  • YAF Developer
13 years ago
It would be interesting to link this to access types too. Hopefully, it works when thanks are disabled.

A useful feature in general usage.

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
13 years ago

It would be interesting to link this to access types too. Hopefully, it works when thanks are disabled.

A useful feature in general usage.

Originally Posted by: bbobb 

No its currently not working when the thanks feature is disabled.

I think the only solution would be if disabled just show the hidden content.

tommy382
  • tommy382
  • 100% (Exalted)
  • YAF Commander Topic Starter
13 years ago
Nice work tha_watcha 👍. Showing the content when THANKS is disabled make sense.

By the way, did you have to put the code to remove the hidden content in RSS and REPLY or both of these call the same "format message" function so taking care of one case will automatically take care of the other? I don't use RSS ever so I have never tested my forum on RSS - if there is a backdoor to see the hidden content w/o thanking via the RSS, I want to patch up that back door.

Another backdoor: UNDO THANK

1. User X thanks Poster Y

2. User X saw the hidden content

3. User X undo thank on Poster Y

I had to manually disable undo thank in the code but would be nice to have an option to "disable undo thank" in the admin interface.

tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
13 years ago

By the way, did you have to put the code to remove the hidden content in RSS and REPLY or both of these call the same "format

Yes i do.

 
// Remove HIDDEN Text
message = this.Get<IFormatMessage>().RemoveHiddenBBCodeContent(message);

Another Backdoor was the printtopic page, i also use the code above there.

Another backdoor: UNDO THANK

1. User X thanks Poster Y

2. User X saw the hidden content

3. User X undo thank on Poster Y

I had to manually disable undo thank in the code but would be nice to have an option to "disable undo thank" in the admin interface.

Not sure if this really a problem if a user removes the thank it shows the hidden image again.

tommy382
  • tommy382
  • 100% (Exalted)
  • YAF Commander Topic Starter
13 years ago

Not sure if this really a problem if a user removes the thank it shows the hidden image again.

Originally Posted by: tha_watcha 

Normally the poster want to know how popular a particular hidden content is. Having "undo thanks" make it possible for someone not on the "thanks list" to view the hidden content.

juju
  • juju
  • 72.2% (Friendly)
  • YAF Lover
13 years ago

I completed my code now, its fully committed to svn. The hidden tag now uses automatically the current message id, you now can use this bbcode in any posting.

the hidden content in a reply and in the rss feed will be completly removed.

Originally Posted by: tha_watcha 

i dowloaded the HideBBCodeModule.cs from the svn to try and implement this. I walked through this and downloaded other related modified files like the LegacyDb.cs. Unfortunately I have an error that I do not know how to fix:

'YAF.Modules.BBCode.HideBBCodeModule' does not contain a definition for 'MessageID' and no extension method 'MessageID' accepting a first argument of type 'YAF.Modules.BBCode.HideBBCodeModule' could be found (are you missing a using directive or an assembly reference?)


protected override void Render(HtmlTextWriter writer)
        {
            var hiddenContent = Parameters["inner"];

            var messageId = this.MessageID;

            if (hiddenContent.IsNotSet())
            {
                return;
            }

Please help

Thank you


hmmmm.... click thanks if my posts are useful
tha_watcha
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 4.0.1 BETA
13 years ago
You need to download all file from svn and recompile all projects, i modified multiple files when i add this bbcode.
juju
  • juju
  • 72.2% (Friendly)
  • YAF Lover
13 years ago
Thank you for the reply, I realized it now as I step into each error. I got it up and running and about to test the hidden post feature. Nice job!
hmmmm.... click thanks if my posts are useful