chat icon indicating copy to clipboard operation
chat copied to clipboard

get private conversations with title

Open MostafaNobaghi opened this issue 6 years ago • 10 comments

is there a way to get private conversations with title (name of other user)

MostafaNobaghi avatar Sep 15 '18 15:09 MostafaNobaghi

Can you use $conversation->users() ? or you want to have that when you get a list of conversations?

musonza avatar Sep 15 '18 19:09 musonza

Want it when get list of conversatons

On Sun, 16 Sep 2018 00:13 Tinashe Musonza, [email protected] wrote:

Can you use $conversation->users() ? or you want to have that when you get a list of conversations?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/musonza/chat/issues/104#issuecomment-421623557, or mute the thread https://github.com/notifications/unsubscribe-auth/AXvlZLXuZvN9-oxtl49769ImLPh3LZdqks5ubVhTgaJpZM4WqZWa .

MostafaNobaghi avatar Sep 15 '18 23:09 MostafaNobaghi

You can get the message from last_message which has sender

0 => array:6 [
      "id" => 1
      "private" => "1"
      "data" => []
      "created_at" => "2018-09-16 02:19:56"
      "updated_at" => "2018-09-16 02:19:56"
      "last_message" => array:13 [
        "id" => 2
        "message_id" => "2"
        "conversation_id" => "1"
        "user_id" => "1"
        "is_seen" => "1"
        "is_sender" => "1"
        "flagged" => false
        "created_at" => "2018-09-16 02:19:56"
        "updated_at" => "2018-09-16 02:19:56"
        "deleted_at" => null
        "body" => "Hello 2"
        "type" => "text"
        "sender" => array:7 [
          "id" => 1
          "name" => "Heloise McKenzie"
          "email" => "[email protected]"
          "created_at" => "2018-09-16 02:19:56"
          "updated_at" => "2018-09-16 02:19:56"
        ]
      ]
    ]
  ]

musonza avatar Sep 16 '18 02:09 musonza

@musonza I think he has meant that he want the name of another user in conversation(1:1 chat) in conversation list. Right now, I have the same problem as him because in chat list screen the conversation is listed by participant's name and profile image and latest message. The sender name in latest message cannot be used because if the user him/herself is the latest sender.

fokoz avatar Oct 11 '18 13:10 fokoz

@musonza any hint how we can achieve this? I want to build an inbox in my app

        $conversations = Chat::conversation(Chat::conversations()->conversation)
            ->for(auth()->user())
            ->get();
        return response()->json($conversations);

But I can't build what classic messaging apps do, because I don't have the other user in my return (but the Inbox usally shows the name of the other user + his avatar and the last message)

(I know I could iterate through and add $conversation->users() but maybe there is a better way of doing so.)

hirbod avatar Oct 27 '19 12:10 hirbod

@Hirbod i see the issue for 1:1 conversations. I will look at it as soon as i get a chance. However, i'm working on this major update which might be worth looking at if you are just starting to use the package in your app https://github.com/musonza/chat/blob/multiple-model-recipients/README.md

musonza avatar Oct 27 '19 12:10 musonza

Hi @musonza, I just started integrating that chat to my app, but I can't wait because I am running out of time. Your major upgrade looks very promising, however, it looks like a BC how we add users to a conversation (currently user ids), your major upgrade adds models. Will this be backwards compatible? Or will we have to add the UserModel in the future?

I am asking because this would make big changes to my current codebase.

hirbod avatar Oct 27 '19 13:10 hirbod

@Hirbod Got it. That's right it will be a breaking change but i'm going to have different table names or different prefixes and create a script to migrate from the current version. It won't be too much work because right now we are using one type of model so we know what model those ids represent

musonza avatar Oct 27 '19 13:10 musonza

Ok, thanks that you keep your existing users in mind :)

hirbod avatar Oct 27 '19 13:10 hirbod

For everybody looking for a workaround until we're covered:

    public function index(Request $request)
    {

        $conversations = Chat::conversation(Chat::conversations()->conversation)
            ->for($request->user())
            ->get();


        foreach($conversations as $key => $conversation) {
            $conversations[$key]['recipient'] = $conversation->users()
                ->where('user_id', '<>', $request->user()->id)
                ->first()
                ->only(['id', 'name', 'first_name', 'last_name', 'avatar']);
        }

        return response()->json($conversations);
    }

So I grab all the users conversations, iterate through them and add a new key "recipient" to it, grabbing all users except for the current user-id, filtering all the fields I need using only(). If there is a better/more sexy way doing it, please don't hesitate to show your approaches.

Note: this will only work for 1:1 because I limit it with first() (and only() won't work on an array)

hirbod avatar Oct 27 '19 14:10 hirbod