MyBB Community Forums

Full Version: Help with a Notifications plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm coding a Notifications plugin for MyBB 1.x, but I've run into a rather large snag. The framework is all down. There is a functioning notifications page, plugin hooks, installation, etc. etc. I have started to add the hooks that will send the actual notifications to users. How can I make sure that a user will only be notified one time of replies to a thread until they view that notification or visit the thread itself? This is my current table structure:
	$db->query("CREATE TABLE " . TABLE_PREFIX . "notifications(
	`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
	`html` VARCHAR(1000),
	`to_id` INT,
	`from_id` INT,
	`unix_time` INT,
	`read` INT,
	`tid` INT
	);");
I wrote some really subpar code held together with spit and prayers, but it ended up not working anyways... you'll see what I mean by "subpar" when I post it here so you can maybe see what I'm trying to do.
function notifications_notify_newreply() {
	global $tid,$thread,$db,$mybb;
	// code stolen from misc.php?action=whoposted
	$query = $db->query("SELECT *
	FROM ".TABLE_PREFIX."posts p
	LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
	WHERE p.tid='".$tid."' AND p.visible = '1'
	GROUP BY u.uid, p.username, u.uid, u.username, u.usergroup, u.displaygroup");
	// we have to do a lot of bullshit here to make sure there aren't any double notifications.
	// oh, the sacrifices i make for you, mybb... if anybody knows a better way to do this, please
	// let me know because the way i am doing it now is stupid unclean :(
	if ($db->num_rows($query)>1) {
		// let's get the UIDs that have posted into an easy to reference array index
		while ($poster = $db->fetch_array($query)) {
			$temp[] = $poster['uid'];
			$full[] = $poster;
		}
		// now we should see if there are any existing unread notifications for the thread for any of those users...
		$selectquery = 'SELECT `to_id` FROM '.TABLE_PREFIX.'notifications WHERE (';
		$selectquery .= '`to_id`=' . implode(' OR `to_id`=',$temp);
		$selectquery .= ') AND `read`=0 AND `tid`=' . $tid . ';';
		$selectquery = $db->query($selectquery);
		// and once again let's put this data in an easy to use array
		while ($noti = $db->fetch_array($selectquery)) {
			$alreadynotified[$noti['to_id']] = TRUE;
		}
		// see? unclean. i'm even confused about what i just wrote, but i'm pretty sure it works
		// time to build the insert query for all the valid notifications...
		$insertquery = 'INSERT INTO ' . TABLE_PREFIX . 'notifications (`to_id`,`from_id`,`html`,`read`,`unix_time`,`tid`) VALUES ';
		while ($poster = $full) {
			if (!$alreadynotified[$poster['u.uid']]) {
				$insertquery .= '(' . $poster['u.uid'] . ',' . $mybb->user['uid'] . ',\'';
				$insertquery .= build_profile_link($mybb->user['username'],$mybb->user['uid'],'_blank');
				$insertquery .= ' posted a reply in <a href="' . get_thread_link($tid,NULL,'newpost') . '">' . $db->escape_string($thread['subject']) . '</a>\',0,' . time() . ',' . $tid . '),';
			}
		}
		// remove the trailing comma from the query
		$insertquery = rtrim($insertquery,',');
		// time to run the query, cross your fingers
		$db->query($insertquery);
	}
}
So does anybody know how I could accomplish what I am trying to do?