接收或取消主题回帖提醒的功能分析

在Discuz!之前一系列版中,一直有个功能,叫关注此主题,一有回复,就会通知这些关注此主题的人,以短消息的方式。在Discuz!X系列中,只有主题的作者才有这个功能,接收或取消回复提醒。现在分析一下Discuz!X系列的这个功能,如果站长有兴趣,还可以依照这个思路去自己动手修改一番,未尝不可。

首先我们看看在模版中的代码:

<!--{if $thread['authorid'] == $_G['uid']}-->
        <span class="pipe">|</span>
	    <!--{if $replynotice == 1}-->
	            <a id="replynotice" href="forum.php?mod=misc&action=replynotice&op=ignore&tid=$_G[tid]" status="1" onclick="ajaxmenu(this, 3000, 0, 0, '43', function () {replyNotice();});return false;">{lang ignore_replynotice}</a>
	    <!--{else}-->
	            <a id="replynotice" href="forum.php?mod=misc&action=replynotice&op=receive&tid=$_G[tid]" status="0" onclick="ajaxmenu(this, 3000, 0, 0, '43', function () {replyNotice();});return false;">{lang receive_replynotice}</a>
            <!--{/if}-->
<!--{/if}-->


意思是当主题的作者和当前登陆的uid一致,也就是主题作者浏览时,显示以下内容。$replynotice等于1,则当前状态显示为取消回复通知,反之则显示接收回复通知。这里是通过ajax异步处理的。我们看处理得链接是提交到f orum.php?mod=misc&action=replynotice&op=ignore&tid=$_G[tid],那么,我们就顺着这个地址查看代码,mod等于misc,调用的就是调用source\module\forum\forum_misc.php文件,action等于replynotice,被当作是一个条件来处理,当action等于replynotice,则会去操作数据库,是取消是接收,都在这里处理。

elseif($_G['gp_action'] == 'replynotice') {
	$tid = intval($_G['gp_tid']);
	$status = $_G['gp_op'] == 'ignore' ? 0 : 1;
	if(!empty($tid)) {
		$thread = DB::fetch_first("SELECT authorid, status FROM ".DB::table('forum_thread')." WHERE tid='$tid' AND displayorder>='0'");
		if($thread['authorid'] == $_G['uid']) {
			$thread['status'] = setstatus(6, $status, $thread['status']);
			DB::query("UPDATE ".DB::table('forum_thread')." SET status='$thread[status]' WHERE tid='$tid'", 'UNBUFFERED');
			showmessage('replynotice_success_'.$status);
		}
	}

我们看到$_G[‘gp_action’] == ‘replynotice’时,首先intval一下tid值,然后当前的op值是不是等于ignore,即取消,如果是,则$stauts等于0,否则等于1。然后判断是否主题作者和当前uid一致,如果一致,则对之前获取的$stauts值进行setstatus处理,setstatus即是对数值进行了异或位运算处理,然后就是更新数据库了,对forum_thread表的status字段进行更新。并提示更新成功。

下面当此主题有了新回复的时候,程序又是怎么处理的呢?我们来看看post_newreply.php这个文件,大约97行开始:

if(!empty($_G['uid']) && $_G['uid'] != $post['authorid']) {
		notification_add($post['authorid'], 'pcomment', 'comment_add', array(
			'tid' => $_G['tid'],
			'pid' => $_G['gp_pid'],
			'subject' => $thread['subject'],
			'commentmsg' => cutstr(str_replace(array('[b]', '[/b]', '[/color]'), '', preg_replace("/\[color=([#\w]+?)\]/i", "", stripslashes($comment))), 200)
		));
	}

回复后,程序判断uid是否为空,并且uid和作者是否是同一个人,不是的话,就通知作者。notification_add函数就是通知作者已有了新回复,参数为authorid,回复内容,类型为回复,还有个数组:tid,pid,主题,内容。并替换掉了里面的粗体,颜色等的标签属性。

如果哪位站长有兴趣,可以沿着这个思路,去做些改进,比如任何人都可以对主题进行关注,这样就需要对uid做出判断,并在表中加个字段,专门用来存储uid的相关信息,以便有了新回复的时候进行通知。

Comments

No comments yet. Why don’t you start the discussion?

发表回复