示例#1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     //
     $interactor = new CurlInteractor();
     $interactor->setResponseFactory(new SlackResponseFactory());
     $commander = new Commander($_ENV['SLACK_KEY'], $interactor);
     $response = $commander->execute('channels.list');
     $responseBody = $response->getBody();
     if (!$responseBody or !$responseBody['ok']) {
         throw new Exception('Sth Error Happened!');
     }
     foreach ($responseBody['channels'] as $chan) {
         if (!$chan['is_channel']) {
             continue;
         }
         $chanData = ['sid' => $chan['id'], 'name' => $chan['name'], 'created' => $chan['created'], 'creator' => $chan['creator'], 'purpose' => (object) $chan['purpose'], 'is_archived' => $chan['is_archived'], 'is_member' => $chan['is_member'], 'num_members' => $chan['num_members'], 'members' => $chan['members'], 'topic' => (object) $chan['topic']];
         if ($channel = Channel::where('sid', $chan['id'])->first()) {
             foreach ($chanData as $k => $v) {
                 $channel->{$k} = $v;
             }
             $channel->save();
         } else {
             $chanData['latest'] = 0;
             Channel::create($chanData);
         }
     }
 }
示例#2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     //
     $interactor = new CurlInteractor();
     $interactor->setResponseFactory(new SlackResponseFactory());
     $commander = new Commander($_ENV['SLACK_KEY'], $interactor);
     $channels = Channel::where('is_member', true)->get();
     foreach ($channels as $channel) {
         $latest = $channel->latest ?: 0;
         do {
             $response = $commander->execute('channels.history', ['channel' => $channel->sid, 'oldest' => $latest, 'count' => 1000]);
             $responseBody = $response->getBody();
             foreach ($responseBody['messages'] as $msg) {
                 $latest = $msg['ts'] > $latest ? $msg['ts'] : $latest;
                 $message = new Message();
                 foreach ($msg as $k => $v) {
                     $message->{$k} = is_string($v) ? $v : (object) $v;
                 }
                 $message->channel = $channel->sid;
                 $message->save();
             }
         } while ($responseBody['has_more']);
         $channel->latest = $latest;
         $channel->save();
     }
 }
示例#3
0
 public function infinite($chan, $direction, $id)
 {
     $channel = Channel::where('name', $chan)->firstOrFail();
     $log = Message::findOrFail($id);
     // Build the query to fetch logs with
     if ($direction === 'up') {
         $logs = Message::where('channel', $channel->sid)->where('ts', '<', "{$log->ts}")->orderBy('ts', 'desc')->take($this->ajaxLoad)->get();
     } else {
         $logs = Message::where('channel', $channel->sid)->where('ts', '>', "{$log->ts}")->orderBy('ts', 'asc')->take($this->ajaxLoad)->get();
     }
     $logs = Message\Repository::convertCollection($logs);
     $loadMore = null;
     if (count($logs) === $this->ajaxLoad) {
         $loadMore = end($logs)->_id;
     }
     if ($direction === 'up') {
         $logs = array_reverse($logs);
     }
     return View::make('partials.logs', compact('chan', 'logs'))->with('more' . $direction, $loadMore);
 }
示例#4
0
 public static function parseText($text)
 {
     preg_match_all('/<(.*?)>/', $text, $matches, PREG_SET_ORDER);
     if (!count($matches)) {
         return $text;
     }
     $fromText = [];
     $toText = [];
     foreach ($matches as $match) {
         // User link case
         if (preg_match('/^@U/', $match[1])) {
             list($uid, ) = explode("|", $match[1]);
             $fromText[] = $match[0];
             //if uid non-existent at db, return original uid.
             $username = ($select = User::where('sid', substr($uid, 1))->first()) == null ? "{$uid}" : $select->name;
             $toText[] = $username == '@USLACKBOT' ? '@slackbot' : $username;
             continue;
         }
         if (preg_match('/^#C/', $match[1])) {
             list($cid, ) = explode("|", $match[1]);
             $fromText[] = $match[0];
             $toText[] = '#' . Channel::where('sid', substr($cid, 1))->first()->name;
             continue;
         }
         if (preg_match('/^http/i', $match[1])) {
             list($url, ) = explode("|", $match[1]);
             $fromText[] = $match[0];
             $toText[] = $url;
             continue;
         }
     }
     if (count($fromText)) {
         $text = str_replace($fromText, $toText, $text);
     }
     return $text;
 }