Database Design for storing Chat Messages between people

不羁的心 提交于 2019-12-20 09:55:08

问题


I am trying to build a messaging/chat system. which can store conversation between two people in a chronological order. Also if User A deletes the conversation User B still should have access the conversation until he wishes to delete them.

  1. Inbox - All the messages recieved by the user from various users will be displayed with the latest message from that particular thread.

  2. Conversation Screen - Chronological order of the conversation between the User A and User B

This is the basic structure of the database i have come up with. Should i store the messages twice in the database ?

  1. id
  2. to_id
  3. from_id
  4. message
  5. timestamp
  6. read

回答1:


I would use a lookup table for the messages that would store who has the rights to view that message

table->message                   |    table->messageUsers
id->0, message->'hi', user_id->1      user_id->1, message_id->0
                                      user_id->2, message_id->0

That way if a user deletes their message they are actually just deleting their relationship to the message not the message itself. you just remove them from the messageUsers table. or set a active field to 1 or 0.




回答2:


At first I thought that when one person deleted it you could just turn either To or From to null but that would make you lose who sent the message or to whom it was addressed.

You should just add a field deleted_by which will contain the id of the person who deleted it or will be null. So when selecting records from the inbox you have something like:

Select * From Messages where to_id = MyID and deleted_by <> MyID

when you delete the message you check if the deleted_by is null, if it is you update the deleted_by field with MyID, if it is not (means that the other party deleted it as well) you delete the record.

If you want to have the same functionality for threads instead of messages (i.e. manage the conversation and not one message at a time) you should have another table (MessageThreads) in which you have the from_id, to_id fields, deleted_by along with a thread_id field. in the Messages table you subsitute the from_id to_id and deleted_by with the thread_id.




回答3:


There will be two tables. nodes node_user

In nodes table,

  • node_id
  • title
  • message
  • timestamp

In node_user table,

  • node_user_id(PK)
  • node_id
  • parent_node_id(for threaded)
  • from_id
  • to_id
  • timestamp
  • read

When user A send a message to user B, firstly store the message in nodes table. And then, add two records in node_user table. When user A delete the message, only delete the first record in node_user table. When user B delete the message, you can delete records from both nodes and node_user table.

Threaded Message,

  • Use parent_node_id


来源:https://stackoverflow.com/questions/8105927/database-design-for-storing-chat-messages-between-people

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!