protected function updateTwitter($items) { // Get the user $users = new Users(); $user = $users->getUser($this->getUserID()); // Get twitter credentials $properties = new Properties(array(Properties::KEY => $user->id)); $auth = $properties->getProperty('twitter_auth'); $services = $properties->getProperty('twitter_services'); $username = $properties->getProperty('twitter_username'); $password = $properties->getProperty('twitter_password'); $has_preamble = $properties->getProperty('preamble', true); // Return if not all conditions are met if (!$auth || !in_array($this->getID(), unserialize($services))) { return; } // Get an item $count = count($items); $data = new Data(); $source_id = $this->_source['id']; if ($count <= 3) { foreach ($items as $id) { $item = $data->getItem($source_id, $id); $title = strip_tags($item->getTitle()); $service = $this->getServiceName(); if ($item->getType() == SourceItem::STATUS_TYPE && strlen($title) < 140) { $tweet = $title; } else { $preamble = $has_preamble ? $item->getPreamble() : ""; $tweet = $preamble . $title; if (strlen($tweet) > 119) { $tweet = substr($tweet, 0, 115) . "[..]"; } $db_ShortUrls = new ShortUrls(); $hash = $db_ShortUrls->addUrlForItem($user->id, $source_id, $id); $tweet = "{$tweet} http://st.tl/{$hash}"; } try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) { } } } else { $selection = array(); foreach ($items as $i) { $selection[] = array($source_id, $i); } $db_ShortUrls = new ShortUrls(); $hash = $db_ShortUrls->addUrlForSelection($user->id, serialize($selection)); $url = "http://st.tl/{$hash}"; $tweet = sprintf($this->_update_tweet, $count, $url); try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) { } } }
private function notifyTwitter($item_id, $source_id) { // Get twitter credentials $username = $this->_properties->getProperty('twitter_username'); $password = $this->_properties->getProperty('twitter_password'); $preamble = $this->_properties->getProperty('preamble', true); // Get item $data = new Data(); $item = $data->getItem($source_id, $item_id); $pream = $preamble ? $item->getPreamble() : ""; $title = $pream . $item->getTitle(); // Assemble tweet depending on type if ($item->getType() == SourceItem::STATUS_TYPE && strlen($title) < 140) { $tweet = $title; } else { if (strlen($title) > 121) { $title = substr($title, 0, 117) . "[..]"; } $db_ShortUrls = new ShortUrls(); $hash = $db_ShortUrls->addUrlForItem($this->_user->id, $source_id, $item_id); $tweet = "{$title} http://st.tl/{$hash}"; } try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) { throw new Stuffpress_Exception("Twitter notification generated exception {$e}"); } }
/** * @return \yii\db\ActiveQuery */ public function getShortUrl() { return $this->hasOne(ShortUrls::className(), ['id' => 'short_url_id']); }