创新实训(六)系统消息 博客评论和回复通知
在系统消息中,用户可以接收比赛报名成功的通知或者博客中别人的评论和别人评论中@自己的评论,并且附带直接跳转到相关评论的链接。
博客通知消息的生成
这部分功能在博客相关文件blog.php
中的函数$comment_form->handle = function()
实现
1. 处理评论的表单逻辑
$comment_form->handle = function() {
global $myUser, $blog, $comment_form;
$comment = HTML::escape($_POST['comment']);
list($comment, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
$esc_comment = DB::escape($comment);
DB::insert("insert into blogs_comments (poster, blog_id, content, reply_id, post_time, zan) values ('{$myUser['username']}', '{$blog['id']}', '$esc_comment', 0, now(), 0)");
$comment_id = DB::insert_id();
$rank = DB::selectCount("select count(*) from blogs_comments where blog_id = {$blog['id']} and reply_id = 0 and id < {$comment_id}");
$page = floor($rank / 20) + 1;
$uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}";
foreach ($referrers as $referrer) {
$content = '有人在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($referrer, '有人提到你', $content);
}
if ($blog['poster'] !== $myUser['username']) {
$content = '有人回复了您的博客 ' . $blog['title'] . ' :<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($blog['poster'], '博客新回复通知', $content);
}
$comment_form->succ_href = getLongTablePageRawUri($page);
};
$comment_form->ctrl_enter_submit = true;
代码详解
-
获取并转义评论内容:
$comment = HTML::escape($_POST['comment']);
从 POST 请求中获取评论内容,并使用
HTML::escape
函数进行转义以防止 XSS 攻击。 -
处理 @ 提及:
list($comment, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
通过
uojHandleAtSign
函数处理评论中的 @ 提及,将提到的用户名提取出来,并生成对应的链接。 -
插入评论到数据库:
$esc_comment = DB::escape($comment); DB::insert("insert into blogs_comments (poster, blog_id, content, reply_id, post_time, zan) values ('{$myUser['username']}', '{$blog['id']}', '$esc_comment', 0, now(), 0)"); $comment_id = DB::insert_id();
将评论插入
blogs_comments
表中,并获取新插入的评论 ID。 -
计算评论所在的页数:
$rank = DB::selectCount("select count(*) from blogs_comments where blog_id = {$blog['id']} and reply_id = 0 and id < {$comment_id}"); $page = floor($rank / 20) + 1;
计算当前评论在所有评论中的位置,并据此计算它所在的页数。假设每页显示 20 条评论。
-
生成评论链接:
$uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}";
生成指向该评论的 URI 链接,便于用户点击查看具体评论。
-
发送系统消息通知:
foreach ($referrers as $referrer) { $content = '有人在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>'; sendSystemMsg($referrer, '有人提到你', $content); } if ($blog['poster'] !== $myUser['username']) { $content = '有人回复了您的博客 ' . $blog['title'] . ' :<a href="' . $uri . '">点击此处查看</a>'; sendSystemMsg($blog['poster'], '博客新回复通知', $content); }
如果评论中提到了其他用户,给这些用户发送系统消息通知。如果评论不是博客作者自己发的,则给博客作者发送通知。
-
设置表单成功后跳转的 URI:
$comment_form->succ_href = getLongTablePageRawUri($page);
设置表单成功处理后的跳转地址,跳转到包含新评论的页面。
-
启用 Ctrl+Enter 提交:
$comment_form->ctrl_enter_submit = true;
设置表单支持 Ctrl+Enter 快捷键提交。
-
运行表单处理逻辑:
$comment_form->runAtServer();
运行表单的服务器端处理逻辑。
2. 处理回复的表单逻辑
这段代码是用于处理博客评论回复的表单逻辑。它与之前的评论处理逻辑类似,但主要是针对回复操作的处理。这部分功能在博客相关文件blog.php
中的函数$reply_form->handle = function(&$vdata)
实现
$reply_form->handle = function(&$vdata) {
global $myUser, $blog, $reply_form;
$comment = HTML::escape($_POST['reply_comment']);
list($comment, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
$reply_id = $_POST['reply_id'];
$esc_comment = DB::escape($comment);
DB::insert("insert into blogs_comments (poster, blog_id, content, reply_id, post_time, zan) values ('{$myUser['username']}', '{$blog['id']}', '$esc_comment', $reply_id, now(), 0)");
$comment_id = DB::insert_id();
$rank = DB::selectCount("select count(*) from blogs_comments where blog_id = {$blog['id']} and reply_id = 0 and id < {$reply_id}");
$page = floor($rank / 20) + 1;
$uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}";
foreach ($referrers as $referrer) {
$content = '有人在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($referrer, '有人提到你', $content);
}
$parent = $vdata['parent'];
$notified = array();
if ($parent['poster'] !== $myUser['username']) {
$notified[] = $parent['poster'];
$content = '有人回复了您在博客 ' . $blog['title'] . ' 下的评论 :<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($parent['poster'], '评论新回复通知', $content);
}
if ($blog['poster'] !== $myUser['username'] && !in_array($blog['poster'], $notified)) {
$notified[] = $blog['poster'];
$content = '有人回复了您的博客 ' . $blog['title'] . ' :<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($blog['poster'], '博客新回复通知', $content);
}
$reply_form->succ_href = getLongTablePageRawUri($page);
};
$reply_form->ctrl_enter_submit = true;
代码详解
-
获取并转义回复内容:
$comment = HTML::escape($_POST['reply_comment']);
从 POST 请求中获取回复的内容,并使用
HTML::escape
函数进行转义,防止 XSS 攻击。 -
处理 @ 提及:
list($comment, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
通过
uojHandleAtSign
函数处理回复中的 @ 提及,将提到的用户名提取出来,并生成对应的链接。 -
获取回复的评论 ID:
$reply_id = $_POST['reply_id'];
获取要回复的评论的 ID。
-
插入回复到数据库:
$esc_comment = DB::escape($comment); DB::insert("insert into blogs_comments (poster, blog_id, content, reply_id, post_time, zan) values ('{$myUser['username']}', '{$blog['id']}', '$esc_comment', $reply_id, now(), 0)"); $comment_id = DB::insert_id();
将回复插入到
blogs_comments
表中,并获取新插入的回复的评论 ID。 -
计算回复所在的页数:
$rank = DB::selectCount("select count(*) from blogs_comments where blog_id = {$blog['id']} and reply_id = 0 and id < {$reply_id}"); $page = floor($rank / 20) + 1;
计算当前回复在所有评论中的位置,并据此计算它所在的页数。假设每页显示 20 条评论。
-
生成回复链接:
$uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}";
生成指向该回复的 URI 链接,便于用户点击查看具体回复。
-
发送系统消息通知:
foreach ($referrers as $referrer) { $content = '有人在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>'; sendSystemMsg($referrer, '有人提到你', $content); } $parent = $vdata['parent']; $notified = array(); if ($parent['poster'] !== $myUser['username']) { $notified[] = $parent['poster']; $content = '有人回复了您在博客 ' . $blog['title'] . ' 下的评论 :<a href="' . $uri . '">点击此处查看</a>'; sendSystemMsg($parent['poster'], '评论新回复通知', $content); } if ($blog['poster'] !== $myUser['username'] && !in_array($blog['poster'], $notified)) { $notified[] = $blog['poster']; $content = '有人回复了您的博客 ' . $blog['title'] . ' :<a href="' . $uri . '">点击此处查看</a>'; sendSystemMsg($blog['poster'], '博客新回复通知', $content); }
- @ 提及通知:如果回复中提到了其他用户,给这些用户发送系统消息通知。
- 评论回复通知:如果回复的对象不是博客作者本人,则给评论的作者发送通知。
- 博客回复通知:如果回复不是博客作者自己发的,则给博客作者发送通知。
-
设置表单成功后跳转的 URI:
$reply_form->succ_href = getLongTablePageRawUri($page);
设置表单成功处理后的跳转地址,跳转到包含新回复的页面。
-
启用 Ctrl+Enter 提交:
$reply_form->ctrl_enter_submit = true;
设置表单支持 Ctrl+Enter 快捷键提交。
-
运行表单处理逻辑:
$reply_form->runAtServer();
运行表单的服务器端处理逻辑。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库