public function post_update($args) { if (!isset($_POST['status']) || empty($_POST['status'])) { throw new midcom_exception_httperror("Missing status message", 400); } $message = new net_nemein_avaudu_message(); $message->text = $_POST['status']; if (isset($_POST['lang'])) { $message->language = $_POST['lang']; } if (isset($_POST['in_reply_to_status_id'])) { $parent = new net_nemein_avaudu_message($_POST['in_reply_to_status_id']); if (!$parent || !$parent->guid) { throw new midcom_exception_notfound("Status message {$_POST['in_reply_to_status_id']} not found"); } $message->replyto = $parent->id; } $user_guid = $_MIDCOM->context->host->get_parameter('net_nemein_avaudu', 'user'); if (!$user_guid) { throw new midcom_exception_notfound("No user found, check your settings"); } $user = new net_nemein_avaudu_contact($user_guid); if (!$user->guid) { // Invalid user setting $user_guid = $_MIDCOM->context->host->set_parameter('net_nemein_avaudu', 'user', ''); throw new midcom_exception_notfound("No user found, check your settings"); } $message->user = $user->id; $message->metadata->published->modify('now'); $message->source = 'Avaudu'; $message->create(); $this->data[] = net_nemein_avaudu_controllers_timeline::message2status($message); }
public function fetch_qaiku() { if (!$this->qaiku_apikey) { return; } /* $qb = new midgard_query_builder('net_nemein_avaudu_message'); $messages = $qb->execute(); foreach ($messages as $message) { $message->delete(); } die("here"); */ $qaiku_json = @file_get_contents('http://www.qaiku.com/api/statuses/friends_timeline.json?apikey=' . $this->qaiku_apikey); if (!$qaiku_json) { // TODO: Log return; } $qaikus = json_decode($qaiku_json); $i = 0; foreach ($qaikus as $qaiku) { $i++; if ($i > 5) { // Safety for Create/Update crashing break; } $qb = new midgard_query_builder('net_nemein_avaudu_message'); $qb->add_constraint('qaikuid', '=', $qaiku->id); $messages = $qb->execute(); if (count($messages) > 0) { // We already have this one continue; // TODO: Should we check for content updates? } else { $message = new net_nemein_avaudu_message(); } $message->text = $qaiku->text; $message->qaikuid = $qaiku->id; $message->language = $qaiku->language; $message->source = $qaiku->source; $message->user = $this->import_contact($qaiku->user, 'qaiku'); $message->metadata->published->modify($qaiku->created_at); if ($qaiku->in_reply_to_status_id) { $qb = new midgard_query_builder('net_nemein_avaudu_message'); $qb->add_constraint('qaikuid', '=', $qaiku->in_reply_to_status_id); $parents = $qb->execute(); if (count($parents) > 0) { $message->replyto = $parents[0]->id; } } if ($message->guid) { $message->update(); } else { $message->create(); } } }