]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Conversation.php
Use an Event to present notices conversations
[quix0rs-gnu-social.git] / classes / Conversation.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Data class for Conversations
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Data
23  * @package   StatusNet
24  * @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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
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
38     public $uri;                             // varchar(255)  unique_key
39     public $created;                         // datetime   not_null
40     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
41
42     public static function schemaDef()
43     {
44         return array(
45             'fields' => array(
46                 'id' => array('type' => 'int', 'not null' => true, 'description' => 'should be set from root notice id (since 2014-03-01 commit)'),
47                 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI of the conversation'),
48                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
49                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
50             ),
51             'primary key' => array('id'),
52             'unique keys' => array(
53                 'conversation_uri_key' => array('uri'),
54             ),
55         );
56     }
57
58     /**
59      * Factory method for creating a new conversation.
60      *
61      * Use this for locally initiated conversations. Remote notices should
62      * preferrably supply their own conversation URIs in the OStatus feed.
63      *
64      * @return Conversation the new conversation DO
65      */
66     static function create(Notice $notice)
67     {
68         if (empty($notice->id)) {
69             throw new ServerException(_('Tried to create conversation for not yet inserted notice'));
70         }
71         $conv = new Conversation();
72         $conv->created = common_sql_now();
73         $conv->id = $notice->id;
74         $conv->uri = sprintf('%s%s=%d:%s=%s',
75                              TagURI::mint(),
76                              'noticeId', $notice->id,
77                              'objectType', 'thread');
78         $result = $conv->insert();
79
80         if ($result === false) {
81             common_log_db_error($conv, 'INSERT', __FILE__);
82             throw new ServerException(_('Failed to create conversation for notice'));
83         }
84
85         return $conv;
86     }
87
88     static function noticeCount($id)
89     {
90         $keypart = sprintf('conversation:notice_count:%d', $id);
91
92         $cnt = self::cacheGet($keypart);
93
94         if ($cnt !== false) {
95             return $cnt;
96         }
97
98         $notice               = new Notice();
99         $notice->conversation = $id;
100         $cnt                  = $notice->count();
101
102         self::cacheSet($keypart, $cnt);
103
104         return $cnt;
105     }
106
107     static public function getUrlFromNotice(Notice $notice, $anchor=true)
108     {
109         $conv = self::getKV('id', $notice->conversation);
110         return $conv->getUrl($anchor ? $notice->id : null);
111     }
112
113     public function getUri()
114     {
115         return $this->uri;
116     }
117
118     public function getUrl($noticeId=null)
119     {
120         // FIXME: the URL router should take notice-id as an argument...
121         return common_local_url('conversation', array('id' => $this->id)) .
122                 ($noticeId===null ? '' : "#notice-{$noticeId}");
123     }
124
125     // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
126     public function getNotices($offset=0, $limit=500, Profile $scoped=null)
127     {
128         if ($scoped === null) {
129             $scoped = Profile::current();
130         }
131         $stream = new ConversationNoticeStream($this->id, $scoped);
132         $notices = $stream->getNotices($offset, $limit);
133         return $notices;
134     }
135 }