I have have created a Private messaging system using PHP and mySQL with notification a bit like Facebook.
The database table has following fields( not all listed):
MessageID
senderUserID
RecUserID
Message
Subject
DateTime
Status - whether read or not
RepliedStatus - how should i use this?
DeleteRec - delete from inbox
DelSender - delete sender inbox
RepliedUserId - When user reply to orginal message this is change to receiver's id
All replies are stored in a second table, since each message create a thread. The second table looks a bit like this:
messageID - FK
repuserID
Mesage
DateTime
At the moment when a new message is sent out to a user i change the 'status' of the message to unread, From this can run a count query to list all of the unread messages in notification.
But if the users replies back to that message i cant set the original 'status' field to unread, since this will appear on both users notification. so i created another field called 'RepliedStatus ' but i am unsure how would i use this to show notification on message reply?
thanks guys.
If you have a replies table then you don't need a replied status column on your first status. By virtue of there existing a record in the replies table you know that a user has replied to a message
Why dont you add an INT and nullable column to the first table (let's say, "messages" table) named "previous_message"?
ALTER TABLE messages ADD COLUMN previous_message INT DEFAULT NULL;
So every message will have in the same table the previous one, and you can work out the sequence. If it helps you can have a "next_message" column with same definition and update the relevant record on reply.
Doing so you can use the status column on each reply.
If you want to keep the same DB organisation I would suggest to add a column status on the second table (let's say "replies").
Hope this helps
Me I'll put deleted column once and the same things for the read or not-read like:
[{"0":"both", "1":"Sender", "2":"receiver"}];
And then fetching a tread messaging like:
$sql = "SELECT * FROM messagetreads
WHERE (senderID OR receiverID = ".$_SESSION['MyCurrentId'].")
AND deleted !== 0
ORDER by TreadID AND DateTime ASC";
When a sender "delete" is tread... All tread relatedID in the database change for 1 or 0 if delete colomn is 2...
But I think it's better to creat another colomn for getting of the repeated deleted and notifications data like
TreadID (FK_message_Table)
delete (0=both deleted UserID=don't appear to this sender or receiver)
notify (0=both read UserID=read to this sender or receiver)
Then it's ezee to change a status or a delete statement.
The SELECT will be sometings like this:
$SQL = "SELECT *
FROM messagetreads
WHERE (senderID OR receiverID = ".$_SESSION['MyCurrentId'].")
IN (SELECT TreadID WHERE delete !== (0 OR ".$_SESSION['MyCurrentId']."))";
If is our member id involve in the colomn delete all the tread don't appear but if is the id of the receiver when the sender will delete 0 can be attributed so that both of the members can "delete" the message. Same thing for notifications!
very far later a cron-job can come delete very-old message read(0)...
PS: this can be notification system too like notifying for a new post in wall or comment on a photos or new events on calendar ... just add more information in column data and faormat it with php or java-ajaxed alike...
Related
Just a question about what the best way is to send a message to a group (Like whatsapp group messaging).
I save the members per group into a separate database table and once a user sends a message to the group it retrieves the users from this table. I'm a bit stuck on part two:
As I see it there would be three methods.
Insert the message into a database for the entire group. Send the message back to the different users based on groups they are member of.
Insert the message in the database per user of the group. So loop the members and insert for every member. This has the advantage over the first method that downloaded, read and deleted status can be tracked.
Create a table for messages and one for the message-recipient link. But I'm not really sure how a would query in the most optimum way as to retrieve all the data (downloaded, read, etc) for a group of users. Is that possible in a single query?
Would anyone know what the best method would be? I can imagine that method 2 will fill up a database pretty fast, but method 1 doesn't have the ability of tracking status for a message.
At the moment I am using method 1, but I run into the problem that when a user deletes a message. It would still be returned on other devices because there would be no easy way of setting a certain deleted flag for group messages.
Does anyone know how apps like telegram, whatsapp and so on do this? And would method 2 give problems later on when millions of messages are sent?
Regards,
Ron
I would seperate it like this.
Messages
id, message, owner_id, deleted
Groups
id, name
Message_Group
message_id, group_id
User_Group
user_id, group_id
Then you can don't need to add a message to all users in that group. When you insert a new message you first inserts it into Messages, catch the id and inserts it into Message_Group with the message id and group id. To get the messages of a group just join Message_Group and Messages and you are pretty much done. Of course you also needs to join the user table to catch who wrote that messages.
I have a basic private messaging system and I am trying to add a threaded message feature, so the users can msg each other without starting a new msg.
I have two tables one for the messages and the other for the conversation.
What I am actually doing is that I have inbox.php where I show all the messages, then I have view.php where I show the message detail and on that page I have a reply box so if the user sends a reply the new message will be inserted to conversation table with the parent id being inserted to message_id column in conversation table.
The problem
As I said I am inserting the reply message to conversation table and now user2 won't be able to see the reply in his inbox as I am fetching that data from messages table.
I tried inserting the reply to both messages and conversation table but that will insert it as a new id and the message_id will be different.
So how would I be able to implement this messaging system where the reply can go to both users?
messages table
id
from_user
to_user
message
date
conversation table
id
from
to
message
message_id
date
Have One table for all your messages and just expand it to accommodate one more field for which message id the message was being sent to.
id
user
to_user
message
date
in_reply_to_messageId
I want to create a MySQL database for a project in which users can come and make comments on other profile. Every profile has a unique id to identify it, now when a user comes and makes comment on other profile I'll need to store the user id of the person who made the comment and the person on whose profile the comment was made, along with that I'll need to store the comment in the database.
As many users can make comments on a single profile, I'll need to store all the comments and the users who made them on a single profile. For this how many tables should I create and how many columns should they have? For this I'm thinking about creating a table for named user_comments and that has column user_id, commenter_id (all the commenter who commented their id separated by comma), comments (then all the user comments separated by comma).
Will this work?
For this I'm thinking about creating a table for named user_comments
and that has column user_id ,commenter_id(all the commenter who
commented their id seprated by comma) ,comments(then All the user
comments seprated by comma)
God no! You are almost there:
Table comments
id INT AUTO_INCREMENT
recipient_id
sender_id
message TEXT
[ sent DATETIME ]
[ other meta data ]
Store one message in message. Create one row per message. Never store several records in the same field separated by anything.
I'd have a profile_comment table:
id, text, profile_user_id, commenter_user_id, created_at
And a user table:
id, name, email
You can see here that the first table has two foreign keys to the user table - one points to the owner of the profile, and the other points to the owner of the comment. You can sort them in order of created_at to list them as you would on a blog, either in forward or reverse order.
Now, when you are rendering a profile page, you can get the profile id from your query string:
$profileId = isset( $_GET['profile_id'] ) ? $_GET['profile_id'] : null;
From there, you can add it into a SQL query:
SELECT * FROM profile_comment
WHERE profile_user_id = :profile_id
ORDER BY created_at
The colon mark here is a placeholder you can use with a parameterised query, which helps protect against SQL injection. However, you can build the statement as a string if you are careful to untaint any user input you insert into it.
I'm looking at creating a user messaging system (similar to Facebook).
What I want to know is essentially what should the table structure look like? The requirements I have are as follows:
Messages are exchanged between users - a sender can select multiple users to send a message to.
Messages are displayed in a thread-style layout, as a 1-1 conversation. i.e. each recipient's reply will appear in it's own thread.
Individual messages cannot be deleted, however a thread can be deleted. Deleting a thread doesn't delete any messages, it just removes that thread from the user's inbox. However the other user can still access the thread if he/she hasn't deleted it from his/her inbox.
Here is what I have at the moment:
Table messages
==============
id (PK)
user_id (from)
subject
body
sent_at
Table message_recipients
========================
message_id (PK)
user_id (PK)
read_status
EDIT: What about the following:
Table messages
==============
id (PK)
thread_id
user_id (from)
body
sent_at
Table threads
=============
id (PK)
user_id (from)
subject
Table thread_recipients
=======================
thread_id (PK)
user_id (PK)
read_status
sender_deleted
recipient_deleted
I would suggest having the following at least:
Users, Threads, Messages
All messages would have a thread
foreign key: thread_id
All threads would have at least one message and at least one recipient (as well as sender)
foreign key: to_user_id, from_user_id, message_id
From there you could simply assign a couple flags to your thread (to_user_deleted, from_user_deleted) that would be updated accordingly.
There a lot more things to consider of course, such what kinds of things you want to account for. For example:
Do you want to display the current message as opposed to the starting message?
Do you want to allow users to mark individual messages as read, or just threads?
You need to take all of these into account while designing your database.
Why not use something like Jabber (example: OpenFire or Web Client)
If you need PHP to interact with it you could use something like:
http://code.google.com/p/xmpphp/ or http://code.google.com/p/jaxl/
#Angelo R. Would like to know why we require the Thread Table? This isn't any discussion board. If you want to retrieve the whole thread/conversation of messages you can simply query by source AND recipient ID.
Plus, if you use Thread, nothing bad. But what in this situation
If new message - new thread_id (automatically created), If replied to existing conversation, you have the thread_id, but what if you are creating a new message (say like facebook popup), you don't know whether there was any previous conversation or if thread_id is available or not unless you execute a special query to it.
This was my thought. Tell me if I might be wrong somewhere.
I am developing a messaging inbox system that uses a thread-based layout for messages. What I'm struggling with at the moment is updating the read status of the messages. Perhaps I have got the structure wrong so I could do with some guidance.
Here is what I have:
Table messages
==============
id
thread_id
from_user_id
subject
body
sent_date
Table message_threads
=====================
id
message_id
owner_id
member_id
owner_read
member_read
In the message_threads table, 'owner' refers to the user who started the thread, and 'member' refers to the other participant. A user's inbox will contain threads they have created themselves (owner), and also threads created by other users to which they have replied to (member).
The logic for setting the read status is as follows:
if owner sends a reply: 'member_read' field is set to 0 (unread), 'owner_read' field is set to 1 (read)
if member sends a reply: 'owner_read' field is set to 0 (unread), 'member_read' field is set to 1 (read)
Now I don't think this is entirely the correct approach, because suppose a member wants to sort messages in their inbox by read status - however a user can be both an owner and a member, and there are two read fields in the table. So this sort would not be correct.
Any suggestions on how I should go about this?
EDIT: Here is an exaple scenario:
User 1 sends Thread A to User 2
Thread A appears in User 2's inbox (status 0: unread)
User 2 opens Thread A
Thread A status set to 1 (read)
User 2 replies to Thread A
Thread A status in User 1's inbox (status 0: unread)
Thread A status in User 2's inbox (status 1: read)
User 1 opens Thread A
Thread A status set to 1 (read)
User 1 replies to Thread A
Thread A status in User 1's inbox (status 1: read)
Thread A status in User 2's inbox (status 0: unread)
The only "read" status is for messages that didn't originate from you. a reply to a recipient is marked read when opened. The sender doesn't care if it was read unless you want "receipt confirmation"
So - with that in mind here is what i think it should look like (i didn't include all fields in messages):
Table "users"
id
name
table "messages"
id (int)
parent_id (int)
from_user_id (id)
to_user_id
read (bit/bool)
message (text)
Let me know if I didn't understand you right.