Discuz!大师网

搜索
查看: 1679|回复: 0

Discuz!开发之发帖回帖@会员功能代码解析

[复制链接]
发表于 2017-5-2 18:50:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
定义文件:\source\class\extend\extend_thread_allowat.php
相关类定义如下:
  1. class extend_thread_allowat extends extend_thread_base {

  2.         public $atlist;
  3.         public $allowat;

  4.         public function before_newthread($parameters) {

  5.                 if($this->group['allowat']) {
  6.                         $this->atlist = $atlist_tmp = array();
  7.                         preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
  8.                         $atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
  9.                         if(!empty($atlist_tmp)) {
  10.                                 if(!$this->setting['at_anyone']) {
  11.                                         foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
  12.                                                 $this->atlist[$row['followuid']] = $row['fusername'];
  13.                                         }
  14.                                         if(count($this->atlist) < $this->group['allowat']) {
  15.                                                 $query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
  16.                                                 foreach($query as $row) {
  17.                                                         $this->atlist[$row['fuid']] = $row['fusername'];
  18.                                                 }
  19.                                         }
  20.                                 } else {
  21.                                         foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
  22.                                                 $this->atlist[$row['uid']] = $row['username'];
  23.                                         }
  24.                                 }
  25.                         }
  26.                         if($this->atlist) {
  27.                                 foreach($this->atlist as $atuid => $atusername) {
  28.                                         $atsearch[] = "/@".str_replace('/', '\/', preg_quote($atusername))." /i";
  29.                                         $atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
  30.                                 }
  31.                                 $this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
  32.                                 $this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);
  33.                         }
  34.                 }
  35.         }

  36.         public function after_newthread() {
  37.                 if($this->group['allowat'] && $this->atlist) {
  38.                         foreach($this->atlist as $atuid => $atusername) {
  39.                                 notification_add($atuid, 'at', 'at_message', array('from_id' => $this->tid, 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->tid, 'subject' => $this->param['subject'], 'pid' => $this->pid, 'message' => messagecutstr($this->param['message'], 150)));
  40.                         }
  41.                         set_atlist_cookie(array_keys($this->atlist));
  42.                 }
  43.         }

  44.         public function before_newreply($parameters) {
  45.                 if($this->group['allowat']) {
  46.                         $atlist_tmp = $ateduids = array();
  47.                         preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
  48.                         $atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
  49.                         $atnum = $maxselect = 0;
  50.                         foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {
  51.                                 $atnum ++;
  52.                                 $ateduids[$row[uid]] = $row['uid'];
  53.                         }
  54.                         $maxselect = $this->group['allowat'] - $atnum;
  55.                         if($maxselect > 0 && !empty($atlist_tmp)) {
  56.                                 $at_anyone = $this->setting['at_anyone'];
  57.                                 if(empty($at_anyone)) {
  58.                                         foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
  59.                                                 if(!in_array($row['followuid'], $ateduids)) {
  60.                                                         $this->atlist[$row[followuid]] = $row['fusername'];
  61.                                                 }
  62.                                                 if(count($this->atlist) == $maxselect) {
  63.                                                         break;
  64.                                                 }
  65.                                         }
  66.                                         if(count($this->atlist) < $maxselect) {
  67.                                                 $query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
  68.                                                 foreach($query as $row) {
  69.                                                         if(!in_array($row['followuid'], $ateduids)) {
  70.                                                                 $this->atlist[$row[fuid]] = $row['fusername'];
  71.                                                         }
  72.                                                 }
  73.                                         }
  74.                                 } else {
  75.                                         foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
  76.                                                 if(!in_array($row['uid'], $ateduids)) {
  77.                                                         $this->atlist[$row[uid]] = $row['username'];
  78.                                                 }
  79.                                                 if(count($this->atlist) == $maxselect) {
  80.                                                         break;
  81.                                                 }
  82.                                         }
  83.                                 }
  84.                         }
  85.                         if($this->atlist) {
  86.                                 foreach($this->atlist as $atuid => $atusername) {
  87.                                         $atsearch[] = "/@$atusername /i";
  88.                                         $atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
  89.                                 }
  90.                                 $this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
  91.                                 $this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);
  92.                         }
  93.                 }
  94.         }

  95.         public function after_newreply() {
  96.                 if($this->group['allowat'] && $this->atlist) {
  97.                         foreach($this->atlist as $atuid => $atusername) {
  98.                                 notification_add($atuid, 'at', 'at_message', array('from_id' => $this->thread['tid'], 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->thread['tid'], 'subject' => $this->thread['subject'], 'pid' => $this->pid, 'message' => messagecutstr($this->param['message'], 150)));
  99.                         }
  100.                         set_atlist_cookie(array_keys($this->atlist));
  101.                 }
  102.         }

  103.         public function before_editpost($parameters) {
  104.                 if($this->group['allowat']) {
  105.                         $this->atlist = $atlist_tmp = $ateduids = array();
  106.                         $atnum = $maxselect = 0;
  107.                         foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {
  108.                                 $atnum ++;
  109.                                 $ateduids[$row[uid]] = $row['uid'];
  110.                         }
  111.                         $maxselect = $this->group['allowat'] - $atnum;
  112.                         preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
  113.                         $atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
  114.                         if($maxselect > 0 && !empty($atlist_tmp)) {
  115.                                 if(empty($this->setting['at_anyone'])) {
  116.                                         foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
  117.                                                 if(!in_array($row['followuid'], $ateduids)) {
  118.                                                         $this->atlist[$row[followuid]] = $row['fusername'];
  119.                                                 }
  120.                                                 if(count($this->atlist) == $maxselect) {
  121.                                                         break;
  122.                                                 }
  123.                                         }
  124.                                         if(count($this->atlist) < $maxselect) {
  125.                                                 $query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
  126.                                                 foreach($query as $row) {
  127.                                                         if(!in_array($row['followuid'], $ateduids)) {
  128.                                                                 $this->atlist[$row[fuid]] = $row['fusername'];
  129.                                                         }
  130.                                                 }
  131.                                         }
  132.                                 } else {
  133.                                         foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
  134.                                                 if(!in_array($row['uid'], $ateduids)) {
  135.                                                         $this->atlist[$row[uid]] = $row['username'];
  136.                                                 }
  137.                                                 if(count($this->atlist) == $maxselect) {
  138.                                                         break;
  139.                                                 }
  140.                                         }
  141.                                 }
  142.                                 if($this->atlist) {
  143.                                         foreach($this->atlist as $atuid => $atusername) {
  144.                                                 $atsearch[] = "/@$atusername /i";
  145.                                                 $atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
  146.                                         }
  147.                                         $parameters['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
  148.                                         $parameters['message'] = substr($parameters['message'], 0, strlen($parameters['message']) - 1);
  149.                                 }
  150.                         }
  151.                 }
  152.         }

  153.         public function after_editpost() {
  154.                 if($this->group['allowat'] && $this->atlist) {
  155.                         foreach($this->atlist as $atuid => $atusername) {
  156.                                 notification_add($atuid, 'at', 'at_message', array('from_id' => $this->thread['tid'], 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->thread['tid'], 'subject' => $this->thread['subject'], 'pid' => $this->post['pid'], 'message' => messagecutstr($this->param['message'], 150)));
  157.                         }
  158.                         set_atlist_cookie(array_keys($this->atlist));
  159.                 }
  160.         }
  161. }
复制代码


由以上代码定义我们看到程序处理@功能分别有三处:发帖、回帖、编辑;
首先以正则表达式方式从内容文本中抽取@会员信息存入atlist,并将内容文本中的@会员解析出会员的空间地址([url]标签),然后在所有的after_xxx方法中向被@的会员发送通知,这里需要说明一点是:before_xxx方法是内容入库前的操作,after_xxx方法是内容入库后的操作!

回复 马甲回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|网站地图|小黑屋|展会网|Discuz站长论坛 |天天打卡

GMT+8, 2024-5-6 06:37 , Processed in 0.035424 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表