123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- class SubMirror extends Managed_DataObject
- {
- public $__table = 'submirror';
- public $subscriber;
- public $subscribed;
- public $style;
- public $created;
- public $modified;
-
- function table()
- {
- return array('subscriber' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
- 'subscribed' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
- 'style' => DB_DATAOBJECT_STR,
- 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
- 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
- }
- static function schemaDef()
- {
-
- return array(new ColumnDef('subscriber', 'integer',
- null, false, 'PRI'),
- new ColumnDef('subscribed', 'integer',
- null, false, 'PRI'),
- new ColumnDef('style', 'varchar',
- 16, true),
- new ColumnDef('created', 'datetime',
- null, false),
- new ColumnDef('modified', 'datetime',
- null, false));
- }
-
- static function fixIndexes($schema)
- {
- try {
- $schema->createIndex('submirror', array('subscribed', 'subscriber'));
- } catch (Exception $e) {
- common_log(LOG_ERR, __METHOD__ . ': ' . $e->getMessage());
- }
- }
-
- function keys()
- {
- return array_keys($this->keyTypes());
- }
-
- function keyTypes()
- {
-
-
- return array('subscriber' => 'K', 'subscribed' => 'K');
- }
- function sequenceKey()
- {
- return array(false, false, false);
- }
-
- public static function saveMirror($subscriber, $subscribed, $style='repeat')
- {
-
- $mirror = new SubMirror();
- $mirror->subscriber = $subscriber->id;
- $mirror->subscribed = $subscribed->id;
- $mirror->style = $style;
- $mirror->created = common_sql_now();
- $mirror->modified = common_sql_now();
- $mirror->insert();
- return $mirror;
- }
-
- public function mirrorNotice(Notice $notice)
- {
- $profile = Profile::getKV('id', $this->subscriber);
- if (!($profile instanceof Profile)) {
- common_log(LOG_ERR, "SubMirror plugin skipping auto-repeat of notice $notice->id for missing user $profile->id");
- return false;
- }
- if ($this->style == 'copy') {
- return $this->copyNotice($profile, $notice);
- } else {
- return $this->repeatNotice($profile, $notice);
- }
- }
-
- protected function repeatNotice(Profile $profile, Notice $notice)
- {
- if($profile->hasRepeated($notice)) {
- common_log(LOG_INFO, "SubMirror plugin skipping auto-repeat of notice $notice->id for user $profile->id; already repeated.");
- return true;
- } else {
- common_log(LOG_INFO, "SubMirror plugin auto-repeating notice $notice->id for $profile->id");
- return $notice->repeat($profile, 'mirror');
- }
- }
-
- protected function copyNotice($profile, $notice)
- {
- $options = array('is_local' => Notice::LOCAL_PUBLIC,
- 'url' => $notice->getUrl(),
- 'rendered' => $notice->getRendered());
- $saved = Notice::saveNew($profile->id,
- $notice->content,
- 'feed',
- $options);
- return $saved;
- }
-
- public static function getMirror($subscriber, $subscribed)
- {
- return self::pkeyGet(array('subscriber' => $subscriber->id,
- 'subscribed' => $subscribed->id));
- }
- }
|