MyBB Community Forums

Full Version: "MarkUnread" Plugin: Problem setting thread dateline
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm trying to write a plugin that allows the user to mark a thread as unread.

Use Case: I sometimes read a thread, don't want to answer right now, and want to leave the thread as "unread".

What already works is that I added a link "mark unread" to the bottom of the thread view (under "Subscribe to this thread") and call my code.

AFAIK MyBB compares the "dateline" value of a thread (= when did the user visit the thread last time) with the "dateline" of the last post to detect if a thread is "new". My idea is to set the "dateline" of the thread to the dateline of the last (newest) post minus some seconds. I thought the thread should then be shown as unread again.

But it does not work. The thread is shown read as before.

Do you have any hint for me? Is there another place in the database where MyBB gets the information if a thread was already read?

I wrote this function to set the dateline. See the attachment for the complete plugin.

function mark_thread_unread($tid, $fid)
{
	global $mybb, $db;

	// Can only do "true" tracking for registered users
	if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'])
	{
		// For registered users, store the information in the database.
		
		// get newest post in thread
		$visibleonly2 = "AND p.visible='1' AND t.visible='1'";
		
		$query = $db->query("
			SELECT p.pid, p.dateline
			FROM ".TABLE_PREFIX."posts p
			LEFT JOIN ".TABLE_PREFIX."threads t ON(p.tid=t.tid)
			WHERE t.fid='".$fid."' AND t.closed NOT LIKE 'moved|%' {$visibleonly2}
			ORDER BY p.dateline DESC
			LIMIT 1
		");

		// get dateline (number like e.g. "1416388892")
		$dateline = $db->fetch_field($query, "dateline") - 100;
		
		switch($db->type)
		{
			case "pgsql":
			case "sqlite":
				$db->replace_query("threadsread", array('tid' => $tid, 'uid' => $mybb->user['uid'], 'dateline' => $dateline), array("tid", "uid"));
				break;
			default:
				$db->write_query("
					REPLACE INTO ".TABLE_PREFIX."threadsread (tid, uid, dateline)
					VALUES('$tid', '{$mybb->user['uid']}', '".$dateline."')
				");
		}
		
		//$threadcache[$tid]['lastread'] = $dateline;
	}
	// Default back to cookie marking
	else
	{
		my_set_array_cookie("threadread", $tid, $dateline, -1);
	}

	$unread_count = fetch_unread_count($fid);
	if($unread_count > 0)
	{
		//mark_forum_unread($fid);
	}
}

I solved it. My DB query was wrong. I have to filter the tid to get the newest post in the thread (and not in the whole forum).
Additionally, the dateline in the "forumsread" table had to be updated.

The correct and working plugin is attached!