I have a simple message schema where a “thread” ties 2 or more users to a stream of messages. Each message belongs to a single thread. It works just like SMS messages, or Facebook messages.
I only need to count how many threads have new messages for a given user. When a user opens a thread, I need to update the db (for that user only), indicating that they have looked at the thread. Given these 2 tables, how should I enhance my schema to store this “updated thread” data for each user in each thread?
MessageThreads:
threadID
lastUpdated
MessageThreadUsers:
threadFK
userFK
I’m thinking along the lines of adding this table (but is there a better way???)
UserThreads
userFK
threadFK
lastChecked
You should be able to just add a lastChecked column to your MessageThreadUsers table. Then you can compare that value to the lastUpdated field in MessageThreads to determine whether the user has new messages. Or something like below should give you the total number of new messages.