Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

Handling messages fixes #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,13 @@ public function postMessage(Message $message)
$options = [
'text' => $message->getText(),
'channel' => $message->data['channel'],
'as_user' => true,
'as_user' => $message->isAsUser(),
];

if ($message->getUsername()) {
$options['username'] = $message->getUsername();
}

if ($message->hasAttachments()) {
$options['attachments'] = json_encode($message->getAttachments());
}
Expand Down
21 changes: 21 additions & 0 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ public function getUser()
return $this->client->getUserById($this->data['user']);
}

/**
* Send message as user or not.
* By default sends as user.
*
* @return bool
*/
public function isAsUser()
{
return isset($this->data['as_user']) ? $this->data['as_user'] : true;
}

/**
* Get message's user display name
*
* @return string
*/
public function getUsername()
{
return isset($this->data['username']) ? $this->data['username'] : '';
}

/**
* {@inheritDoc}
*/
Expand Down
17 changes: 8 additions & 9 deletions src/RealTimeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,12 @@ private function onMessage(WebSocketMessageInterface $message)
break;

case 'channel_created':
$this->getChannelById($payload['channel']['id'])->then(function (Channel $channel) {
$this->channels[$channel->getId()] = $channel;
});
$channel = new Channel($this, $payload['channel']);
$this->channels[$channel->getId()] = $channel;
break;

case 'channel_deleted':
unset($this->channels[$payload['channel']['id']]);
unset($this->channels[$payload['channel']]);
break;

case 'channel_rename':
Expand All @@ -403,11 +402,11 @@ private function onMessage(WebSocketMessageInterface $message)
break;

case 'channel_archive':
$this->channels[$payload['channel']['id']]->data['is_archived'] = true;
$this->channels[$payload['channel']]->data['is_archived'] = true;
break;

case 'channel_unarchive':
$this->channels[$payload['channel']['id']]->data['is_archived'] = false;
$this->channels[$payload['channel']]->data['is_archived'] = false;
break;

case 'group_joined':
Expand All @@ -416,16 +415,16 @@ private function onMessage(WebSocketMessageInterface $message)
break;

case 'group_rename':
$this->groups[$payload['group']['id']]->data['name']
$this->groups[$payload['channel']['id']]->data['name']
= $payload['channel']['name'];
break;

case 'group_archive':
$this->groups[$payload['group']['id']]->data['is_archived'] = true;
$this->groups[$payload['channel']]->data['is_archived'] = true;
break;

case 'group_unarchive':
$this->groups[$payload['group']['id']]->data['is_archived'] = false;
$this->groups[$payload['channel']]->data['is_archived'] = false;
break;

case 'im_created':
Expand Down