I want to implement user mentions with @username like in Twitter. But, in my app, the username may change. My approach is to parse the post before saving into the database and convert all the @username to @userId. What do you think of this? Does anyone has any better alternative?
3 Answers
Store the original text, as is, and create a table of related records with the uid and username.
Example schema:
post [table]
id
text
user_mention [table]
id
post_id
user_id_mentioned
user_name_mentioned
When a post is saved, your code should go through and create all the user_mention records. You can loop through the mention table to send e-mails or whatever else you want to do.
If the user changes their user name, you now have the option of updating the post with the new username, or having the old username link to the correct user.
My rule is to never, ever modify original unstructured text before saving to the database (but do sanity check it to avoid injections and whatnot) and modify the data only on output. This means you always have the user entered data, and you never know when that will be valuable.
Maybe one day you are interested in who changed their username after being mentioned n number of times? Or some other report that you can't get because you modified unstructured data. You never know when you will want the original data and, in this case, you get the added bonus of having your text easier to work with.

- 2,211
- 2
- 19
- 27
Yeah, I think checking the username list at the post time, and converting them internally to a user ID makes sense. Then, whenever you display the post, translate the user ID back to the current username.
Note that this will be more difficult for non-dynamic content, such as emails sent, etc.
Also, I'd make sure that the usernames are displayed in a way that makes it clear that they're not words the OP posted, otherwise, that would give a way for users to inject text into someone else's post.

- 14,573
- 6
- 35
- 54
-
Thank you for your reply. I do convert the userId back to the current username before displaying. The only problem is I send emails to the users who have been mentioned after the post is successfully put into the database. The text then contains the ID instead of the username. Also could you please elaborate on your third paragraph. I did not quite understand it. Thank you for your time and response. – nezgerland Sep 22 '11 at 19:21
-
Also, in order to extract mentions from the post, I use the following regex: "/@(\w+)/is" I have seen much complex regex for this purporse. But I think this is just enough. What is your opinion? – nezgerland Sep 22 '11 at 19:23
-
1I think `/@(\w+)/` is optimistic - it would match `user@example.com`, for instance. – zigdon Sep 22 '11 at 20:12
-
The problem I was trying to point out in the 3rd paragraph - if you display the username inline, and I post something to "I think user1 has a point". Then user1 can go, change their ID to "I will send user1 all my money because he", and the original post would become "I think I will send user1 all my money because he has a point". See the problem? – zigdon Sep 22 '11 at 20:15
Yes I think that is good. Twitter itself doesn't just use Usernames
it uses UserIDs
.
It gets the tweeters user ID then looks it up to get the the actual username.
Documentation : Mentions and Lookup
Each user should have a unique ID. You should match the ID's with the username before you send it anywhere which would be visible for users.

- 10,958
- 6
- 41
- 58
-
-
It's not a good practice to modify the original entered text/post, Ted's answer goes more in-depth on why it is not a good idea to do that – Musa Oct 14 '18 at 13:10