Video plugin. still rough, but federation works.

This commit is contained in:
Ian Denhardt 2011-03-15 23:51:07 -04:00
parent 27ef3b1d90
commit b7aca97d3e
5 changed files with 363 additions and 26 deletions

View File

@ -30,8 +30,18 @@ if (!defined('STATUSNET')) {
exit(1);
}
class GNUsocialVideoPlugin extends Plugin
class GNUsocialVideoPlugin extends MicroAppPlugin
{
function onCheckSchema()
{
$schema = Schema::get();
$schema->ensureTable('video', Video::schemaDef());
return true;
}
function onAutoload($cls)
{
$dir = dirname(__FILE__);
@ -40,6 +50,15 @@ class GNUsocialVideoPlugin extends Plugin
case 'PostvideoAction':
include_once $dir . '/actions/postvideo.php';
break;
case 'Video':
include_once $dir . '/Video.php';
break;
case 'VideoForm':
include_once $dir . '/videoform.php';
break;
case 'ShowvideoAction':
include_once $dir . '/showvideo.php';
break;
default:
break;
}
@ -49,6 +68,81 @@ class GNUsocialVideoPlugin extends Plugin
function onRouterInitialized($m)
{
$m->connect('main/postvideo', array('action' => 'postvideo'));
$m->connect('showvideo/:id', array('action' => 'showvideo'));
return true;
}
function entryForm($out)
{
return new VideoForm($out);
}
function appTitle()
{
return _('video');
}
function tag()
{
return 'GNUsocialVideo';
}
function types()
{
return array(Video::OBJECT_TYPE);
}
function saveNoticeFromActivity($activity, $actor, $options=array())
{
if(count($activity->objects) != 1) {
throw new Exception('Too many activity objects.');
}
$videoObj = $activity->objects[0];
if ($videoObj->type != Video::OBJECT_TYPE) {
throw new Exception('Wrong type for object.');
}
// For now we read straight from the xml tree, no other way to get this information.
// When there's a better API for this, we should change to it.
$uri = ActivityUtils::getLink($activity->entry, 'enclosure');
$options['object_type'] = Video::OBJECT_TYPE;
Video::saveNew($actor, $uri, $options);
}
function activityObjectFromNotice($notice)
{
$object = new ActivityObject();
$object->id = $notice->uri;
$object->type = Video::OBJECT_TYPE;
$object->title = $notice->content;
$object->summary = $notice->content;
$object->link = $notice->bestUrl();
$vid = Video::getByNotice($notice);
if ($vid) {
$object->extra[] = array('link', array('rel' => 'enclosure', 'href' => $vid->url), array());
}
return $object;
}
function showNotice($notice, $out)
{
$vid = Video::getByNotice($notice);
if ($vid) {
$out->element('video', array('src' => $vid->url));
}
}
function deleteRelated($notice)
{
exit(1); // TODO: implement
}
}

View File

@ -0,0 +1,112 @@
<?php
/**
* GNU Social
* Copyright (C) 2010, Free Software Foundation, Inc.
*
* PHP version 5
*
* LICENCE:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package GNU Social
* @author Ian Denhardt <ian@zenhack.net>
* @copyright 2011 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
*/
if(!defined('STATUSNET')){
exit(1);
}
/**
* Data class for videos.
*/
class Video extends Managed_DataObject
{
const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/video';
public $__table = 'video'; // table name
public $id; // char (36) // UUID
public $uri; // varchar (255) // This is the corresponding notice's uri.
public $url; // varchar (255)
public $profile_id; // int
public function staticGet($k, $v=null)
{
return Memcached_DataObject::staticGet('Video', $k, $v);
}
public function getByNotice($notice)
{
return self::staticGet('uri', $notice->uri);
}
public function getNotice()
{
return Notice::staticGet('uri', $this->uri);
}
public static function schemaDef()
{
return array(
'description' => 'A video clip',
'fields' => array(
'id' => array('type' => 'char',
'length' => 36,
'not null' => true,
'description' => 'UUID'),
'uri' => array('type' => 'varchar',
'length' => 255,
'not null' => true),
'url' => array('type' => 'varchar',
'length' => 255,
'not null' => true),
'profile_id' => array('type' => 'int', 'not null' => true),
),
'primary key' => array('id'),
'foreign keys' => array('video_profile_id__key' => array('profile' => array('profile_id' => 'id'))),
);
}
function saveNew($profile, $url, $options=array())
{
$vid = new Video();
$vid->id = UUID::gen();
$vid->profile_id = $profile->id;
$vid->url = $url;
$options['object_type'] = Video::OBJECT_TYPE;
if (!array_key_exists('uri', $options)) {
$options['uri'] = common_local_url('showvideo', array('id' => $vid->id));
}
if (!array_key_exists('rendered', $options)) {
$options['rendered'] = sprintf("<video src=\"%s\">Sorry, your browser doesn't support the video tag.</video>", $url);
}
$vid->uri = $options['uri'];
$vid->insert();
return Notice::saveNew($profile->id,
'',
'web',
$options);
}
}

View File

@ -32,52 +32,55 @@ if (!defined('STATUSNET')) {
class PostvideoAction extends Action {
var $user = null;
var $url = null;
function prepare($args)
{
parent::prepare($args);
$this->user = common_current_user();
if(empty($this->user)){
throw new ClientException(_('Must be logged in to post a video'),
403);
}
if($this->isPost()){
$this->checkSessionToken();
}
$this->url = filter_var($this->trimmed('url'), FILTER_SANITIZE_URL);
$this->url = filter_var($this->url, FILTER_VALIDATE_URL);
return true;
}
function handle($args)
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($this->isPost()) {
$this->handlePost($args);
} else {
$this->showPage();
}
$this->showPage();
}
function handlePost($args)
{
if (!$this->arg('post')) {
return;
}
if (empty($_POST['video_uri'])) {
return;
}
$uri = $_POST['video_uri'];
$uri = filter_var($uri, FILTER_SANITIZE_URL);
$uri = filter_var($uri, FILTER_VALIDATE_URL);
if($uri) {
$rend = sprintf('<video src="%s", controls="controls">Sorry, your browser doesn\'t support the video tag.</video>', $uri);
Notice::saveNew($this->user->id, 'video : ' . $uri, 'web', array('rendered' => $rend));
if (empty($this->url)) {
throw new ClientException(_('Bad URL.'));
}
$profile = $this->user->getProfile();
$vid = Video::saveNew($profile, $this->url, array());
common_redirect($vid->uri, 303);
}
function showContent()
{
if(empty($this->user)) {
$this->element('p', array(), 'You are not logged in.');
} else {
$this->elementStart('form', array('method' => 'post',
'action' => common_local_url('postvideo')));
$this->element('input', array('name' => 'video_uri',
'type' => 'text',
'id' => 'video_uri'));
$this->submit('post', _('Post'));
$this->elementEnd('form');
}
$form = new VideoForm();
$form->show();
}
}

View File

@ -0,0 +1,67 @@
<?php
/**
* GNU Social
* Copyright (C) 2010, Free Software Foundation, Inc.
*
* PHP version 5
*
* LICENCE:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package GNU Social
* @author Ian Denhardt <ian@zenhack.net>
* @copyright 2011 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
*/
if(!defined('STATUSNET')){
exit(1);
}
class ShowvideoAction extends ShownoticeAction
{
protected $id = null;
protected $vid = null;
function prepare($args)
{
OwnerDesignAction::prepare($args);
$this->id = $this->trimmed('id');
$this->vid = Video::staticGet('id', $this->id);
if (empty($this->vid)) {
throw new ClientException(_('No such video.'), 404);
}
$this->notice = $this->vid->getNotice();
if (empty($this->notice)) {
throw new ClientException(_('No such video'), 404);
}
$this->user = User::staticGet('id', $this->vid->profile_id);
if (empty($this->user)) {
throw new ClientException(_('No such user.'), 404);
}
$this->profile = $this->user->getProfile();
if (empty($this->profile)) {
throw new ServerException(_('User without a profile.'));
}
return true;
}
}

View File

@ -0,0 +1,61 @@
<?php
/**
* GNU Social
* Copyright (C) 2010, Free Software Foundation, Inc.
*
* PHP version 5
*
* LICENCE:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package GNU Social
* @author Ian Denhardt <ian@zenhack.net>
* @copyright 2011 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
*/
if(!defined('STATUSNET')){
exit(1);
}
class VideoForm extends Form
{
function id()
{
return "form_new_video";
}
function action()
{
return common_local_url('postvideo');
}
function formData()
{
$this->out->elementStart('fieldset', array('id' => 'new_video_data'));
$this->out->elementStart('ul', 'form_data');
$this->li();
$this->out->input('url', _('URL'), null, _('URL of the video'));
$this->unli();
$this->out->elementEnd('ul');
$this->out->elementEnd('fieldset');
}
function formActions()
{
$this->out->submit('submit', _m('BUTTON', 'Save'));
}
}