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 compound key
55 * This is a utility method to get a single instance with a given set of
56 * key-value pairs. Usually used for the primary key for a compound key; thus
59 * @param array $kv array of key-value mappings
61 * @return TagSub object found, or null for no hits
66 return Memcached_DataObject::pkeyGet('TagSub', $kv);
70 * The One True Thingy that must be defined and declared.
72 public static function schemaDef()
75 'description' => 'TagSubPlugin tag subscription records',
77 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this subscription'),
78 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
79 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
81 'primary key' => array('tag', 'profile_id'),
82 'foreign keys' => array(
83 'tagsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
86 'tagsub_created_idx' => array('created'),
87 'tagsub_profile_id_tag_idx' => array('profile_id', 'tag'),
93 * Start a tag subscription!
95 * @param profile $profile subscriber
96 * @param string $tag subscribee
99 static function start(Profile $profile, $tag)
103 $ts->profile_id = $profile->id;
104 $ts->created = common_sql_now();
106 self::blow('tagsub:by_profile:%d', $profile->id);
111 * End a tag subscription!
113 * @param profile $profile subscriber
114 * @param string $tag subscribee
116 static function cancel(Profile $profile, $tag)
118 $ts = TagSub::pkeyGet(array('tag' => $tag,
119 'profile_id' => $profile->id));
122 self::blow('tagsub:by_profile:%d', $profile->id);
126 static function forProfile(Profile $profile)
130 $keypart = sprintf('tagsub:by_profile:%d', $profile->id);
131 $tagstring = self::cacheGet($keypart);
133 if ($tagstring !== false) { // cache hit
134 if (!empty($tagstring)) {
135 $tags = explode(',', $tagstring);
138 $tagsub = new TagSub();
139 $tagsub->profile_id = $profile->id;
140 $tagsub->selectAdd();
141 $tagsub->selectAdd('tag');
143 if ($tagsub->find()) {
144 $tags = $tagsub->fetchAll('tag');
147 self::cacheSet($keypart, implode(',', $tags));