public function registerAssets() { $view = $this->getView(); if ($this->node) { $this->options['rooms'] = self::generateRoomIds(Yii::$app->user->id, PoprigunChatDialog::getUserDialogs(Yii::$app->user->id)); $this->options['socketUrl'] = isset($this->socketUrl) ? $this->count : 'http://' . $_SERVER['SERVER_ADDR'] . ':8080'; ChatAssets::register($view); } else { ChatWithoutNodeAssets::register($view); } $script = ' poprigunChat = new PoprigunChat(' . Json::encode($this->options) . '); '; $view->registerJs($script, View::POS_END); echo $this->renderFile($this->template, ['model' => new PoprigunChatMessage(), 'options' => $this->options, 'rooms' => self::generateRoomIds(Yii::$app->user->id, PoprigunChatDialog::getUserDialogs(Yii::$app->user->id))]); }
/** * Get new message count * @param null|integer $dialogId * @return int|string */ public function getNewCount($dialogId = null) { $query = $this->hasMany(PoprigunChatMessage::className(), ['dialog_id' => 'id'])->innerJoinWith('chatUserRel')->andWhere([PoprigunChatUserRel::tableName() . '.is_view' => PoprigunChatUserRel::NEW_MESSAGE]); if (null !== $dialogId) { $query->andWhere([PoprigunChatDialog::tableName() . '.id' => $dialogId]); } return $query->count(); }
/** * @return \yii\db\ActiveQuery */ public function getDialog() { return $this->hasOne(PoprigunChatDialog::className(), ['id' => 'dialog_id']); }
/** * Send message * @param string|null $title * @return bool */ public function sendMessage($title = null) { try { switch ($this->messageType) { case self::MESSAGE_TO_USER: $dialog = PoprigunChatDialog::getMessageDialog($this->author_id, $this->receiverId, $title); break; case self::MESSAGE_TO_DIALOG: $dialog = PoprigunChatDialog::getDialog($this->author_id, $this->receiverId); if (null === $dialog) { throw new \BadMethodCallException(); } break; default: throw new \BadMethodCallException(); } $result = $dialog->addMessageToDialog($this->author_id, $this->message); } catch (\Exception $e) { $result = $e->getMessage(); } return $result; }
/** * Delete dialog (change status) * * @param $dialogId * @return array */ public function actionDeleteDialog($dialogId) { return ['status' => PoprigunChatDialog::deleteDialog(Chat::decodeDialogId($dialogId)) ? 'success' : 'error']; }