2010-12-18 16:27:14 +09:00
|
|
|
<?php
|
2020-08-12 19:12:36 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2010-12-18 16:27:14 +09:00
|
|
|
/**
|
|
|
|
* Data class to mark notices as bookmarks
|
|
|
|
*
|
2020-08-12 19:12:36 +09:00
|
|
|
* @category Data
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2009 StatusNet, Inc.
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2010-12-18 16:27:14 +09:00
|
|
|
*/
|
|
|
|
|
2020-08-12 19:12:36 +09:00
|
|
|
defined('GNUSOCIAL') || die();
|
2010-12-18 16:27:14 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For storing the fact that a notice is a bookmark
|
|
|
|
*
|
2020-08-12 19:12:36 +09:00
|
|
|
* @category Bookmark
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2010-12-18 16:27:14 +09:00
|
|
|
*
|
2020-08-12 19:12:36 +09:00
|
|
|
* @see DB_DataObject
|
2010-12-18 16:27:14 +09:00
|
|
|
*/
|
2013-08-18 19:10:44 +09:00
|
|
|
class Bookmark extends Managed_DataObject
|
2010-12-18 16:27:14 +09:00
|
|
|
{
|
2010-12-25 13:34:15 +09:00
|
|
|
public $__table = 'bookmark'; // table name
|
2010-12-31 06:21:14 +09:00
|
|
|
public $id; // char(36) primary_key not_null
|
|
|
|
public $profile_id; // int(4) not_null
|
2015-02-13 02:18:55 +09:00
|
|
|
public $url; // varchar(191) not_null not 255 because utf8mb4 takes more space
|
|
|
|
public $title; // varchar(191) not 255 because utf8mb4 takes more space
|
|
|
|
public $uri; // varchar(191) not 255 because utf8mb4 takes more space
|
2010-12-31 06:21:14 +09:00
|
|
|
public $description; // text
|
|
|
|
public $created; // datetime
|
2010-12-18 16:27:14 +09:00
|
|
|
|
2013-08-18 22:03:06 +09:00
|
|
|
public static function schemaDef()
|
2010-12-18 16:27:14 +09:00
|
|
|
{
|
2013-08-18 22:03:06 +09:00
|
|
|
return array(
|
|
|
|
'fields' => array(
|
|
|
|
'id' => array('type' => 'char',
|
|
|
|
'length' => 36,
|
|
|
|
'not null' => true),
|
|
|
|
'profile_id' => array('type' => 'int', 'not null' => true),
|
|
|
|
'uri' => array('type' => 'varchar',
|
2015-02-13 02:18:55 +09:00
|
|
|
'length' => 191,
|
2013-08-18 22:03:06 +09:00
|
|
|
'not null' => true),
|
|
|
|
'url' => array('type' => 'varchar',
|
2015-02-13 02:18:55 +09:00
|
|
|
'length' => 191,
|
2013-08-18 22:03:06 +09:00
|
|
|
'not null' => true),
|
2015-02-13 02:18:55 +09:00
|
|
|
'title' => array('type' => 'varchar', 'length' => 191),
|
2013-08-18 22:03:06 +09:00
|
|
|
'description' => array('type' => 'text'),
|
|
|
|
'created' => array('type' => 'datetime', 'not null' => true),
|
|
|
|
),
|
2015-10-11 05:20:53 +09:00
|
|
|
'primary key' => array('uri'),
|
2013-08-18 22:03:06 +09:00
|
|
|
'unique keys' => array(
|
2015-10-11 05:20:53 +09:00
|
|
|
'bookmark_id_key' => array('id'),
|
2013-08-18 22:03:06 +09:00
|
|
|
),
|
|
|
|
'foreign keys' => array(
|
2015-10-11 04:53:45 +09:00
|
|
|
'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
|
|
|
|
'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
|
2013-08-18 22:03:06 +09:00
|
|
|
),
|
2020-09-15 22:59:27 +09:00
|
|
|
'indexes' => array(
|
|
|
|
'bookmark_url_idx' => array('url'),
|
|
|
|
'bookmark_profile_id_created_idx' => array('profile_id', 'created'),
|
2013-08-18 22:03:06 +09:00
|
|
|
),
|
|
|
|
);
|
2010-12-18 16:27:14 +09:00
|
|
|
}
|
2010-12-20 00:17:23 +09:00
|
|
|
|
2010-12-25 13:34:15 +09:00
|
|
|
/**
|
|
|
|
* Get a bookmark based on a notice
|
2011-04-06 23:36:35 +09:00
|
|
|
*
|
2015-10-11 04:40:02 +09:00
|
|
|
* @param Notice $stored Notice activity which represents the Bookmark
|
2010-12-25 13:34:15 +09:00
|
|
|
*
|
2015-10-11 04:40:02 +09:00
|
|
|
* @return Bookmark The found bookmark object.
|
|
|
|
* @throws NoResultException When you don't find it after all.
|
2010-12-25 13:34:15 +09:00
|
|
|
*/
|
2020-08-12 19:12:36 +09:00
|
|
|
public static function fromStored(Notice $stored)
|
2010-12-25 13:34:15 +09:00
|
|
|
{
|
2015-10-11 05:20:53 +09:00
|
|
|
return self::getByPK(array('uri' => $stored->getUri()));
|
2010-12-25 13:34:15 +09:00
|
|
|
}
|
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
public function getStored()
|
|
|
|
{
|
|
|
|
return Notice::getByKeys(array('uri' => $this->getUri()));
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:08:28 +09:00
|
|
|
public function getDescription()
|
|
|
|
{
|
|
|
|
return $this->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
public function getUri()
|
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:08:28 +09:00
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
if (empty($this->url)) {
|
|
|
|
throw new InvalidUrlException($this->url);
|
|
|
|
}
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
2010-12-22 00:32:35 +09:00
|
|
|
/**
|
|
|
|
* Get the bookmark that a user made for an URL
|
|
|
|
*
|
2010-12-23 02:35:45 +09:00
|
|
|
* @param Profile $profile Profile to check for
|
|
|
|
* @param string $url URL to check for
|
2010-12-22 00:32:35 +09:00
|
|
|
*
|
2010-12-25 13:34:15 +09:00
|
|
|
* @return Bookmark bookmark found or null
|
2010-12-22 00:32:35 +09:00
|
|
|
*/
|
2020-08-12 19:12:36 +09:00
|
|
|
public static function getByURL(Profile $profile, $url)
|
2010-12-22 00:32:35 +09:00
|
|
|
{
|
2010-12-25 13:34:15 +09:00
|
|
|
$nb = new Bookmark();
|
2011-04-06 23:36:35 +09:00
|
|
|
|
2015-10-11 06:47:43 +09:00
|
|
|
$nb->profile_id = $profile->getID();
|
2010-12-31 06:21:14 +09:00
|
|
|
$nb->url = $url;
|
2010-12-25 13:34:15 +09:00
|
|
|
|
2015-10-11 06:15:51 +09:00
|
|
|
if (!$nb->find(true)) {
|
|
|
|
throw new NoResultException($nb);
|
2010-12-22 00:32:35 +09:00
|
|
|
}
|
2015-10-11 06:15:51 +09:00
|
|
|
|
|
|
|
return $nb;
|
2010-12-22 00:32:35 +09:00
|
|
|
}
|
|
|
|
|
2015-10-11 06:47:43 +09:00
|
|
|
/**
|
|
|
|
* Store a Bookmark object
|
|
|
|
*
|
|
|
|
* @param Profile $profile To save the bookmark for
|
|
|
|
* @param string $title Title of the bookmark
|
|
|
|
* @param string $url URL of the bookmark
|
|
|
|
* @param string $description Description of the bookmark
|
|
|
|
*
|
|
|
|
* @return Bookmark the Bookmark object
|
|
|
|
*/
|
2020-08-12 19:12:36 +09:00
|
|
|
public static function saveActivityObject(
|
|
|
|
ActivityObject $actobj,
|
|
|
|
Notice $stored
|
|
|
|
): self {
|
2015-10-13 19:31:35 +09:00
|
|
|
$url = null;
|
|
|
|
// each extra element is array('tagname', array('attr'=>'val', ...), 'content')
|
|
|
|
foreach ($actobj->extra as $extra) {
|
2016-06-24 20:51:40 +09:00
|
|
|
if ($extra[0] !== ActivityUtils::LINK || $extra[1][ActivityUtils::REL] !== 'related') {
|
2015-10-13 19:31:35 +09:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-24 20:51:40 +09:00
|
|
|
if ($url===null && strlen($extra[1][ActivityUtils::HREF])>0) {
|
|
|
|
$url = $extra[1][ActivityUtils::HREF];
|
2015-10-13 19:31:35 +09:00
|
|
|
} elseif ($url !== null) {
|
|
|
|
// TRANS: Client exception thrown when a bookmark is formatted incorrectly.
|
2016-06-24 20:51:40 +09:00
|
|
|
throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got more than that.')));
|
2015-10-13 19:31:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_null($url)) {
|
2015-10-13 00:48:23 +09:00
|
|
|
// TRANS: Client exception thrown when a bookmark is formatted incorrectly.
|
2016-06-24 20:51:40 +09:00
|
|
|
throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got 0.')));
|
2015-10-13 00:48:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strlen($actobj->title)) {
|
2015-10-11 06:47:43 +09:00
|
|
|
throw new ClientException(_m('You must provide a non-empty title.'));
|
|
|
|
}
|
|
|
|
if (!common_valid_http_url($url)) {
|
|
|
|
throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$object = self::getByURL($stored->getProfile(), $url);
|
|
|
|
throw new ClientException(_m('You have already bookmarked this URL.'));
|
|
|
|
} catch (NoResultException $e) {
|
|
|
|
// Alright, so then we have to create it.
|
|
|
|
}
|
|
|
|
|
|
|
|
$nb = new Bookmark();
|
|
|
|
|
|
|
|
$nb->id = UUID::gen();
|
2015-10-13 00:48:23 +09:00
|
|
|
$nb->uri = $stored->getUri();
|
2015-10-11 06:47:43 +09:00
|
|
|
$nb->profile_id = $stored->getProfile()->getID();
|
2015-10-13 00:48:23 +09:00
|
|
|
$nb->title = $actobj->title;
|
2015-10-11 06:47:43 +09:00
|
|
|
$nb->url = $url;
|
2015-10-13 00:48:23 +09:00
|
|
|
$nb->description = $actobj->summary;
|
2015-10-11 06:47:43 +09:00
|
|
|
$nb->created = $stored->created;
|
|
|
|
|
|
|
|
$result = $nb->insert();
|
|
|
|
if ($result === false) {
|
|
|
|
throw new ServerException('Could not insert Bookmark into database!');
|
|
|
|
}
|
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
return $nb;
|
|
|
|
}
|
2015-10-11 06:47:43 +09:00
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
public function asActivityObject()
|
|
|
|
{
|
|
|
|
$stored = $this->getStored();
|
2015-10-11 06:47:43 +09:00
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
$object = new ActivityObject();
|
|
|
|
$object->id = $this->getUri();
|
|
|
|
$object->type = ActivityObject::BOOKMARK;
|
|
|
|
$object->title = $this->getTitle();
|
|
|
|
$object->summary = $this->getDescription();
|
|
|
|
$object->link = $stored->getUrl();
|
|
|
|
$object->content = $stored->getRendered();
|
|
|
|
|
|
|
|
// Attributes of the URL
|
|
|
|
|
|
|
|
$attachments = $stored->attachments();
|
|
|
|
|
|
|
|
if (count($attachments) != 1) {
|
|
|
|
// TRANS: Server exception thrown when a bookmark has multiple attachments.
|
|
|
|
throw new ServerException(_m('Bookmark notice with the '.
|
|
|
|
'wrong number of attachments.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookmarkedurl = $attachments[0];
|
|
|
|
|
|
|
|
$attrs = array('rel' => 'related',
|
|
|
|
'href' => $bookmarkedurl->getUrl());
|
|
|
|
|
|
|
|
if (!strlen($bookmarkedurl->title)) {
|
|
|
|
$attrs['title'] = $bookmarkedurl->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
$object->extra[] = array('link', $attrs, null);
|
|
|
|
|
|
|
|
// Attributes of the thumbnail, if any
|
|
|
|
|
|
|
|
try {
|
|
|
|
$thumbnail = $bookmarkedurl->getThumbnail();
|
|
|
|
$tattrs = array('rel' => 'preview',
|
|
|
|
'href' => $thumbnail->getUrl());
|
|
|
|
|
|
|
|
if (!empty($thumbnail->width)) {
|
|
|
|
$tattrs['media:width'] = $thumbnail->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($thumbnail->height)) {
|
|
|
|
$tattrs['media:height'] = $thumbnail->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
$object->extra[] = array('link', $tattrs, null);
|
|
|
|
} catch (UnsupportedMediaException $e) {
|
|
|
|
// No image thumbnail metadata available
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object;
|
2015-10-11 06:47:43 +09:00
|
|
|
}
|
|
|
|
|
2010-12-22 00:32:35 +09:00
|
|
|
/**
|
|
|
|
* Save a new notice bookmark
|
|
|
|
*
|
2010-12-23 02:35:45 +09:00
|
|
|
* @param Profile $profile To save the bookmark for
|
|
|
|
* @param string $title Title of the bookmark
|
|
|
|
* @param string $url URL of the bookmark
|
2015-10-11 06:15:51 +09:00
|
|
|
* @param array $rawtags array of tags
|
2010-12-23 02:35:45 +09:00
|
|
|
* @param string $description Description of the bookmark
|
|
|
|
* @param array $options Options for the Notice::saveNew()
|
2010-12-22 00:32:35 +09:00
|
|
|
*
|
|
|
|
* @return Notice saved notice
|
|
|
|
*/
|
2020-08-12 19:12:36 +09:00
|
|
|
public static function addNew(
|
|
|
|
Profile $actor,
|
|
|
|
$title,
|
|
|
|
$url,
|
|
|
|
array $rawtags,
|
|
|
|
$description,
|
|
|
|
array $options = []
|
|
|
|
) {
|
2015-10-13 00:48:23 +09:00
|
|
|
$act = new Activity();
|
|
|
|
$act->verb = ActivityVerb::POST;
|
|
|
|
$act->time = time();
|
|
|
|
$act->actor = $actor->asActivityObject();
|
2011-04-01 00:20:24 +09:00
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
$actobj = new ActivityObject();
|
|
|
|
$actobj->type = ActivityObject::BOOKMARK;
|
|
|
|
$actobj->title = $title;
|
|
|
|
$actobj->summary = $description;
|
2015-10-13 19:31:35 +09:00
|
|
|
$actobj->extra[] = array('link', array('rel'=>'related', 'href'=>$url), null);
|
2015-10-13 00:48:23 +09:00
|
|
|
$act->objects[] = $actobj;
|
2010-12-25 13:34:15 +09:00
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
$act->enclosures[] = $url;
|
2010-12-25 13:34:15 +09:00
|
|
|
|
2010-12-22 00:32:35 +09:00
|
|
|
$tags = array();
|
|
|
|
$replies = array();
|
|
|
|
|
|
|
|
// filter "for:nickname" tags
|
|
|
|
foreach ($rawtags as $tag) {
|
|
|
|
if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
|
2010-12-30 07:17:32 +09:00
|
|
|
// skip if done by caller
|
|
|
|
if (!array_key_exists('replies', $options)) {
|
2010-12-28 02:14:11 +09:00
|
|
|
$nickname = mb_substr($tag, 4);
|
2015-10-13 00:48:23 +09:00
|
|
|
$other = common_relative_profile($actor, $nickname);
|
2010-12-28 02:14:11 +09:00
|
|
|
if (!empty($other)) {
|
|
|
|
$replies[] = $other->getUri();
|
|
|
|
}
|
2010-12-22 00:32:35 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tags[] = common_canonical_tag($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$hashtags = array();
|
|
|
|
$taglinks = array();
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
$hashtags[] = '#'.$tag;
|
|
|
|
$attrs = array('href' => Notice_tag::url($tag),
|
|
|
|
'rel' => $tag,
|
|
|
|
'class' => 'tag');
|
|
|
|
$taglinks[] = XMLStringer::estring('a', $attrs, $tag);
|
|
|
|
}
|
|
|
|
|
2010-12-23 02:35:45 +09:00
|
|
|
// Use user's preferences for short URLs, if possible
|
2015-10-13 00:48:23 +09:00
|
|
|
// FIXME: Should be possible to with the Profile object...
|
2011-01-24 02:57:20 +09:00
|
|
|
try {
|
2015-10-13 00:48:23 +09:00
|
|
|
$user = $actor->getUser();
|
|
|
|
$shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
|
2011-01-24 02:57:20 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// Don't let this stop us.
|
|
|
|
$shortUrl = $url;
|
|
|
|
}
|
2010-12-23 02:35:45 +09:00
|
|
|
|
2011-04-06 23:36:35 +09:00
|
|
|
// TRANS: Rendered bookmark content.
|
|
|
|
// TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
|
2015-10-13 00:48:23 +09:00
|
|
|
// TRANS: %4$s is space separated list of hash tags.
|
2020-08-12 19:12:36 +09:00
|
|
|
$actobj->content = sprintf(
|
|
|
|
_m('<span class="xfolkentry">'
|
|
|
|
. '<a class="taggedlink" href="%1$s">%2$s</a> '
|
|
|
|
. '<span class="description">%3$s</span> '
|
|
|
|
. '<span class="meta">%4$s</span>'
|
|
|
|
. '</span>'),
|
|
|
|
htmlspecialchars($url),
|
|
|
|
htmlspecialchars($title),
|
|
|
|
htmlspecialchars($description),
|
|
|
|
implode(' ', $taglinks)
|
|
|
|
);
|
2015-10-13 00:48:23 +09:00
|
|
|
|
|
|
|
foreach ($tags as $term) {
|
|
|
|
$catEl = new AtomCategory();
|
|
|
|
$catEl->term = $term;
|
|
|
|
$activity->categories[] = $catEl;
|
|
|
|
}
|
2010-12-22 00:32:35 +09:00
|
|
|
|
2020-08-12 19:12:36 +09:00
|
|
|
$options = array_merge([
|
|
|
|
'urls' => [$url],
|
|
|
|
'rendered' => $actobj->content,
|
|
|
|
'tags' => $tags,
|
|
|
|
'replies' => $replies,
|
|
|
|
'object_type' => ActivityObject::BOOKMARK,
|
|
|
|
], $options);
|
2010-12-22 00:32:35 +09:00
|
|
|
|
2015-10-13 00:48:23 +09:00
|
|
|
return Notice::saveActivity($act, $actor, $options);
|
2010-12-22 00:32:35 +09:00
|
|
|
}
|
2010-12-18 16:27:14 +09:00
|
|
|
}
|