Is there a simpler way to achieve this style of user messaging?

前端 未结 13 1301
轮回少年
轮回少年 2020-12-24 12:20

I have created a messaging system for users, it allows them to send a message to another user. If it is the first time they have spoken then a new conversation is initiated,

13条回答
  •  别那么骄傲
    2020-12-24 13:03

    If you're asking for a way to be able to keep all of your current functionality and work flows, yet keep the data in a single table I think you're pretty close.

    Instead of having the conversationId be a key to a different table, I would instead have it point to the ID of the message that began the conversation. This would create a parent-child relationship between messages that began a conversation and all those that followed after it. To be able to see all conversations, you would just select all messages where the conversationId is null. Below is a representation of a 2 message conversation:

    +----+---------+------+------------------+----------------+--------+----------+
    | id | message | read | time             | conversationId | toUser | fromUser |
    +----+---------+------+------------------+----------------+--------+----------+
    | 1  |  test 1 |  0   | (some timestamp) |  null          |  3     |   4      |
    | 2  |  test 2 |  0   | (some timestamp) |   1            |  4     |   3      |
    +----+---------+------+------------------+----------------+--------+----------+
    

    The conversation was initiated by user 3. All messages in the conversation can be filter by conversationId. One limitation of this design is that only 2 users can be apart of the conversation.

    Update

    You could get the last message given a conversation id this way:

    SELECT id, message 
    FROM userMessages 
    WHERE conversationId = {conversationId} 
    ORDER BY time DESC 
    LIMIT 1
    

提交回复
热议问题