nahid / talk

Talk is a real-time users messaging and chatting system for Laravel.
MIT License
1.61k stars 327 forks source link

How to make group for conversation? #66

Open awolad opened 7 years ago

awolad commented 7 years ago

Can you please help me. How to make group for conversation. I want to make a group add members to the group then chat between them ...etc. Please help me.

rajaneeshdwivedi commented 7 years ago

I also needed to manage group conversations so I reasoned like this - the role of the conversation class/table is to represent the relationships between two peers. But my app already holds these relationships between users (some one-to-one, others groups). So I thought that I should remove the conversation class/table and replace it with an interface that my current classes could implement. That interface would do things like find receiver(s) for a given sender and model type & id. In other words through that interface, the responsibility of representing peer relationships is transferred back to my existing classes.

So I did this:

Added a ConversationInterface that my own app classes can use (class x extends Model implements ConversationInterface). That interface requires your own class to provide all the functions that are currently in ConversationRepository, e.g. providing a receiver_id for a given sender_id. Some of these are static functions unless they refer to a specific model instance.

I added columns to the Messages table: model_type and model_id (which replaces conversation_id).

I added a registerModel function in Talk and a protected array of modelType => modelClass, which holds all the subscribing conversation ‘plugins’.

In AppServiceProvider I call Talk::registerModel(Model::type(), ‘class\name’) for each conversation type of model - ConversationInterface also requires each subscribing class to provide a type() integer which will end up as model_type in the messages table.

I changed most functions in Talk:: to broker calls to the appropriate model (i.e. supplying the model type), instead of directly to $this->conversation->

I then re-integrated ConversationRepository so it too implements ConversationInterface in case I ever want to manage exchanges that occur outside of the interactions that already happen within my app (e.g. pure chat between two strangers, or customer <-> admin support).

There’s quite a lot of changes, and I don’t know if this is the best way of managing this but it works and it ‘feels’ right for my case. I would be open to other suggestions though.

Big thanks to Nahid btw for kindly sharing this fantastic piece of work.

carbonvader commented 6 years ago

Hey @nahid,

First of all, thanks for your great job. I have been waiting for group feature for long time. As you have replied to that issue #74, it was being developed. Did you have time to complete it?