3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package SubMirrorPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 class SubMirror extends Memcached_DataObject
27 public $__table = 'submirror';
37 public /*static*/ function staticGet($k, $v=null)
39 return parent::staticGet(__CLASS__, $k, $v);
43 * return table definition for DB_DataObject
45 * DB_DataObject needs to know something about the table to manipulate
46 * instances. This method provides all the DB_DataObject needs to know.
48 * @return array array of column definitions
53 return array('subscriber' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
54 'subscribed' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
56 'style' => DB_DATAOBJECT_STR,
58 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
59 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
62 static function schemaDef()
64 // @fixme need a reverse key on (subscribed, subscriber) as well
65 return array(new ColumnDef('subscriber', 'integer',
67 new ColumnDef('subscribed', 'integer',
70 new ColumnDef('style', 'varchar',
73 new ColumnDef('created', 'datetime',
75 new ColumnDef('modified', 'datetime',
80 * return key definitions for DB_DataObject
82 * DB_DataObject needs to know about keys that the table has; this function
85 * @return array key definitions
90 return array_keys($this->keyTypes());
94 * return key definitions for Memcached_DataObject
96 * Our caching system uses the same key definitions, but uses a different
99 * @return array key definitions
105 // need a sane key for reverse lookup too
106 return array('subscriber' => 'K', 'subscribed' => 'K');
109 function sequenceKey()
111 return array(false, false, false);
115 * @param Profile $subscribed
116 * @param Profile $subscribed
118 * @throws ServerException
120 public static function saveMirror($subscriber, $subscribed, $style='repeat')
122 // @fixme make sure they're subscribed!
123 $mirror = new SubMirror();
125 $mirror->subscriber = $subscriber->id;
126 $mirror->subscribed = $subscribed->id;
127 $mirror->style = $style;
129 $mirror->created = common_sql_now();
130 $mirror->modified = common_sql_now();
137 * @param Notice $notice
138 * @return mixed Notice on successful mirroring, boolean if not
140 public function mirrorNotice($notice)
142 $profile = Profile::staticGet('id', $this->subscriber);
144 common_log(LOG_ERROR, "SubMirror plugin skipping auto-repeat of notice $notice->id for missing user $profile->id");
148 if ($this->style == 'copy') {
149 return $this->copyNotice($profile, $notice);
150 } else { // default to repeat mode
151 return $this->repeatNotice($profile, $notice);
156 * Mirror a notice using StatusNet's repeat functionality.
157 * This retains attribution within the site, and other nice things,
158 * but currently ends up looking like 'RT @foobar bla bla' when
159 * bridged out over OStatus or TwitterBridge.
161 * @param Notice $notice
162 * @return mixed Notice on successful repeat, true if already repeated, false on failure
164 protected function repeatNotice($profile, $notice)
166 if($profile->hasRepeated($notice->id)) {
167 common_log(LOG_INFO, "SubMirror plugin skipping auto-repeat of notice $notice->id for user $profile->id; already repeated.");
170 common_log(LOG_INFO, "SubMirror plugin auto-repeating notice $notice->id for $profile->id");
171 return $notice->repeat($profile->id, 'mirror');
176 * Mirror a notice by emitting a new notice with the same contents.
177 * Kind of dirty, but if pulling an external data feed into an account
178 * that may be what you want.
180 * @param Notice $notice
181 * @return mixed Notice on successful repeat, true if already repeated, false on failure
183 protected function copyNotice($profile, $notice)
185 $options = array('is_local' => Notice::LOCAL_PUBLIC,
186 'url' => $notice->bestUrl(), // pass through the foreign link...
187 'rendered' => $notice->rendered);
189 $saved = Notice::saveNew($profile->id,
196 public /*static*/ function pkeyGet($v)
198 return parent::pkeyGet(__CLASS__, $v);
202 * Get the mirroring setting for a pair of profiles, if existing.
204 * @param Profile $subscriber
205 * @param Profile $subscribed
206 * @return mixed Profile or empty
208 public static function getMirror($subscriber, $subscribed)
210 return self::pkeyGet(array('subscriber' => $subscriber->id,
211 'subscribed' => $subscribed->id));