3 * Data class to store local tag subscriptions
7 * @category TagSubPlugin
9 * @author Brion Vibber <brion@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 if (!defined('STATUSNET')) {
35 * For storing the tag subscriptions
37 * @category PollPlugin
39 * @author Brion Vibber <brion@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41 * @link http://status.net/
45 class TagSub extends Managed_DataObject
47 public $__table = 'tagsub'; // table name
49 public $profile_id; // int -> profile.id
50 public $created; // datetime
53 * Get an instance by key
55 * This is a utility method to get a single instance with a given key value.
57 * @param string $k Key to use to lookup (usually 'user_id' for this class)
58 * @param mixed $v Value to lookup
60 * @return TagSub object found, or null for no hits
63 function staticGet($k, $v=null)
65 return Memcached_DataObject::staticGet('TagSub', $k, $v);
69 * Get an instance by compound key
71 * This is a utility method to get a single instance with a given set of
72 * key-value pairs. Usually used for the primary key for a compound key; thus
75 * @param array $kv array of key-value mappings
77 * @return TagSub object found, or null for no hits
82 return Memcached_DataObject::pkeyGet('TagSub', $kv);
86 * The One True Thingy that must be defined and declared.
88 public static function schemaDef()
91 'description' => 'TagSubPlugin tag subscription records',
93 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this subscription'),
94 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
95 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
97 'primary key' => array('tag', 'profile_id'),
98 'foreign keys' => array(
99 'tagsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
102 'tagsub_created_idx' => array('created'),
103 'tagsub_profile_id_tag_idx' => array('profile_id', 'tag'),
109 * Start a tag subscription!
111 * @param profile $profile subscriber
112 * @param string $tag subscribee
115 static function start(Profile $profile, $tag)
119 $ts->profile_id = $profile->id;
120 $ts->created = common_sql_now();
122 self::blow('tagsub:by_profile:%d', $profile->id);
127 * End a tag subscription!
129 * @param profile $profile subscriber
130 * @param string $tag subscribee
132 static function cancel(Profile $profile, $tag)
134 $ts = TagSub::pkeyGet(array('tag' => $tag,
135 'profile_id' => $profile->id));
138 self::blow('tagsub:by_profile:%d', $profile->id);
142 static function forProfile(Profile $profile)
146 $keypart = sprintf('tagsub:by_profile:%d', $profile->id);
147 $tagstring = self::cacheGet($keypart);
149 if ($tagstring !== false) { // cache hit
150 if (!empty($tagstring)) {
151 $tags = explode(',', $tagstring);
154 $tagsub = new TagSub();
155 $tagsub->profile_id = $profile->id;
156 $tagsub->selectAdd();
157 $tagsub->selectAdd('tag');
159 if ($tagsub->find()) {
160 $tags = $tagsub->fetchAll('tag');
163 self::cacheSet($keypart, implode(',', $tags));