git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Conversation.php 6.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Data class for Conversations
 *
 * 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/>.
 *
 * @category  Data
 * @package   StatusNet
 * @author    Zach Copley <zach@status.net>
25
 * @author    Mikael Nordfeldth <mmn@hethane.se>
26
 * @copyright 2010 StatusNet Inc.
27
 * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
28 29 30 31
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

32
if (!defined('GNUSOCIAL')) { exit(1); }
33

34
class Conversation extends Managed_DataObject
35
{
36
    public $__table = 'conversation';        // table name
37
    public $id;                              // int(4)  primary_key not_null auto_increment
38
    public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
39 40 41
    public $created;                         // datetime   not_null
    public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP

42 43 44 45
    public static function schemaDef()
    {
        return array(
            'fields' => array(
46
                'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'),
47
                'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'),
48 49 50 51 52 53 54 55 56 57
                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
            ),
            'primary key' => array('id'),
            'unique keys' => array(
                'conversation_uri_key' => array('uri'),
            ),
        );
    }

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    static public function beforeSchemaUpdate()
    {
        $table = strtolower(get_called_class());
        $schema = Schema::get();
        $schemadef = $schema->getTableDef($table);

        // 2016-01-06 We have to make sure there is no conversation with id==0 since it will screw up auto increment resequencing
        if ($schemadef['fields']['id']['auto_increment']) {
            // since we already have auto incrementing ('serial') we can continue
            return;
        }

        // The conversation will be recreated in upgrade.php, which will
        // generate a new URI, but that's collateral damage for you.
        $conv = new Conversation();
        $conv->id = 0;
        if ($conv->find()) {
            while ($conv->fetch()) {
                // Since we have filtered on 0 this only deletes such entries
                // which I have been afraid wouldn't work, but apparently does!
                // (I thought it would act as null or something and find _all_ conversation entries)
                $conv->delete();
            }
        }
    }

84
    /**
85 86 87 88
     * Factory method for creating a new conversation.
     *
     * Use this for locally initiated conversations. Remote notices should
     * preferrably supply their own conversation URIs in the OStatus feed.
89 90 91
     *
     * @return Conversation the new conversation DO
     */
92
    static function create($uri=null, $created=null)
93
    {
94
        // Be aware that the Notice does not have an id yet since it's not inserted!
95
        $conv = new Conversation();
96 97
        $conv->created = $created ?: common_sql_now();
        $conv->uri = $uri ?: sprintf('%s%s=%s:%s=%s',
98
                             TagURI::mint(),
99
                             'objectType', 'thread',
100 101 102
                             'nonce', common_random_hexstr(8));
        // This insert throws exceptions on failure
        $conv->insert();
103 104 105

        return $conv;
    }
106 107 108 109 110 111 112 113 114 115 116 117 118

    static function noticeCount($id)
    {
        $keypart = sprintf('conversation:notice_count:%d', $id);

        $cnt = self::cacheGet($keypart);

        if ($cnt !== false) {
            return $cnt;
        }

        $notice               = new Notice();
        $notice->conversation = $id;
119
        $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb'));
120 121 122 123 124 125
        $cnt                  = $notice->count();

        self::cacheSet($keypart, $cnt);

        return $cnt;
    }
126 127 128

    static public function getUrlFromNotice(Notice $notice, $anchor=true)
    {
129 130
        $conv = Conversation::getByID($notice->conversation);
        return $conv->getUrl($anchor ? $notice->getID() : null);
131 132 133 134 135 136 137 138 139 140
    }

    public function getUri()
    {
        return $this->uri;
    }

    public function getUrl($noticeId=null)
    {
        // FIXME: the URL router should take notice-id as an argument...
141
        return common_local_url('conversation', array('id' => $this->getID())) .
142 143
                ($noticeId===null ? '' : "#notice-{$noticeId}");
    }
144 145

    // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
146
    public function getNotices(Profile $scoped=null, $offset=0, $limit=500)
147
    {
148
        $stream = new ConversationNoticeStream($this->getID(), $scoped);
149 150 151
        $notices = $stream->getNotices($offset, $limit);
        return $notices;
    }
152 153 154 155 156 157 158 159 160 161

    public function insert()
    {
        $result = parent::insert();
        if ($result === false) {
            common_log_db_error($this, 'INSERT', __FILE__);
            throw new ServerException(_('Failed to insert Conversation into database'));
        }
        return $result;
    }
162
}