评论或文章里添加@用户名,被@用户可以收到通知的功能怎么实现?
评论或文章里添加@用户名,该用户名自动加上一个用户主页地址的标签,同时被@用户可以收到通知,请问这个功能怎么实现呢?
回复内容:
每一条评论和文章发完之后 会对@后面的内容匹配分组,比如到一个分隔符一类的规则,然后异步压入一个消息队列。这个队列处理是否有存在用户,对存在的用户做消息广播。
后端逻辑大概这样子吧。编辑一类不会有处理,只在insert这一步做。
这里可以抽象出来一个查找at用户的接口,可以抽象出一个检测at后面是否用户合法的接口。
前端我也做过,web的话,直接对富文本里的change做变化,发现一个at之后,操作range选择最近的一个到at或者分隔符的字符再去请求这个字符key列出当前用户的好友或者全站的用户like的提示,展现上就是个浮层加光标位置的定位计算。鼠标选择后 可能会对文本打标记。
这一步主要是增加后端匹配速度,发现标记就不用再对文本做正则了。可以把标记的通知用户在前端就解析出来发给后端了。
这里可能每个人实现逻辑都不一样。大概的步骤应该就是以上。 来来来,我们一起研究一下知乎是怎么做的。
首先明白一点,在答案中at某人的时候,显示的是昵称,而昵称在知乎里是可以重复的,那么,很明显,是不能直接根据这个昵称来判断at的是哪个人的。
我们可以试着在答案里@某个人 ,首先我们能看到,@后面的用户名部分字体颜色变了,和前后的文字样式是不一样的,说明at部分应该是用某个标签包围起来的,F12打开浏览器调试功能,定位到答案中@部分的html代码,如下图:

可见,知乎用一个a标签把@和用户名包围了起来,注意红色圈起来的data-hash的属性和class=“member_mention”,class属性可以改变样式也可以表示这里at了某个人,而data-hash属性很可能就是用于识别被at用户的唯一标识符,而这样答案提交之后,知乎服务器就可以根据这个member_mention和hash这两个关键属性准确快速识别出您at的用户了(当然,我不是知乎员工, 这只是个人猜测,不一定是知乎的真正实现方式,但是这种方式是可行的),然后就可以给相应的用户发送通知了,识别出用户之后,至于显示用户名片的功能,只是给简单的js和css问题而已。 百度贴吧就是直接@某人 然后解析的。
知乎这种是输入@ 之后选一个人,是确定的,不需要再去解析。直接把这个人的id提交给后台。
可能这个跟百度贴吧id唯一,知乎昵称不唯一也有一定关系。 在应用层专门加一个解析逻辑,从用户提交的内容里抓出at就行,考虑到知乎的编辑器还可以实时显示,说明后台有个api把这个功能暴露出来了。 解析出@后面的第一个合法用户名,然后想怎么发通知都可以。。。
也可以像知乎这样,随着你打字实时提示让你选取 正则匹配后看用户名存在否
Discuz有这方面的源码 看情况。
如果内容不可再编辑,怎么实现都行。
如果可以,那么你的业务需求提得并不足够明确。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
