git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

QvitterPlugin.php 11.3 KB
Newer Older
hannes's avatar
hannes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<?php

 /* · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·  
  ·                                                                             ·
  ·                                                                             ·
  ·                             Q V I T T E R                                   ·
  ·                                                                             ·
  ·              http://github.com/hannesmannerheim/qvitter                     ·
  ·                                                                             ·
  ·                                                                             ·
  ·                                 <o)                                         ·
  ·                                  /_////                                     ·
  ·                                 (____/                                      ·
  ·                                          (o<                                ·
  ·                                   o> \\\\_\                                 ·
  ·                                 \\)   \____)                                ·
  ·                                                                             ·
  ·                                                                             ·    
  ·                                                                             ·
  ·  Qvitter is free  software:  you can  redistribute it  and / or  modify it  ·
  ·  under the  terms of the GNU Affero General Public License as published by  ·
  ·  the Free Software Foundation,  either version three of the License or (at  ·
  ·  your option) any later version.                                            ·
  ·                                                                             ·
  ·  Qvitter is distributed  in hope that  it will be  useful but  WITHOUT ANY  ·
  ·  WARRANTY;  without even the implied warranty of MERCHANTABILTY or FITNESS  ·
  ·  FOR A PARTICULAR PURPOSE.  See the  GNU Affero General Public License for  ·
  ·  more details.                                                              ·
  ·                                                                             ·
  ·  You should have received a copy of the  GNU Affero General Public License  ·
  ·  along with Qvitter. If not, see <http://www.gnu.org/licenses/>.            ·
  ·                                                                             ·
  ·  Contact h@nnesmannerhe.im if you have any questions.                       ·
  ·                                                                             · 
  · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */

class QvitterPlugin extends Plugin {

	public function settings($setting)
	{
	
 		/* · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·  
         ·        															 ·
         ·							S E T T I N G S							 ·
         ·         															 ·
         · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */		

		// ENABLED BY DEFAULT (true/false)
		$settings['enabledbydefault'] = true;

		// DEFAULT BACKGROUND COLOR
		$settings['defaultbackgroundcolor'] = '#f4f4f4';

		// DEFAULT LINK COLOR
		$settings['defaultlinkcolor'] = '#0084B4';

		// TIME BETWEEN POLLING
		$settings['timebetweenpolling'] = 5000; // ms


		 /* · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
		  ·                                                                   · 
		  ·                (o>                                  >o)           ·
		  ·            \\\\_\                                    /_////       .
		  ·             \____)                                  (____/        · 
		  ·                                                                   ·
		  · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */	


		return $settings[$setting];
	}
	
    public function onRouterInitialized($m)
    {
		
        $m->connect('api/qvitter.json', array('action' => 'qvitterapi'));              
		$m->connect('api/statuses/public_and_external_timeline.:format',
					array('action' => 'ApiTimelinePublicAndExternal',
						  'format' => '(xml|json|rss|atom|as)'));
		$m->connect('api/qvitter/update_link_color.json',
					array('action' => 'apiqvitterupdatelinkcolor'));
		$m->connect('api/qvitter/update_background_color.json',
					array('action' => 'apiqvitterupdatebackgroundcolor'));
84
		$m->connect('api/qvitter/checklogin.json',
85
					array('action' => 'apiqvitterchecklogin'));						
hannes's avatar
hannes committed
86 87 88
		$m->connect('api/qvitter/allfollowing/:id.json',
					array('action' => 'apiqvitterallfollowing',
						  'id' => Nickname::INPUT_FMT));							
hannes's avatar
hannes committed
89 90
		$m->connect('api/qvitter/update_cover_photo.json',
					array('action' => 'ApiUpdateCoverPhoto'));	
91 92 93 94 95 96 97 98 99 100
		$m->connect('api/qvitter/statuses/friends_timeline.json',
					array('action' => 'apiqvitterfriends'));	
		$m->connect('api/qvitter/statuses/friends_timeline/:id.json',
					array('action' => 'apiqvitterfriends',
						  'id' => Nickname::INPUT_FMT));							
		$m->connect('api/qvitter/statuses/mentions/:id.json',
					array('action' => 'apiqvittermentions',
						  'id' => Nickname::INPUT_FMT));
		$m->connect('api/qvitter/statuses/mentions.:format',
					array('action' => 'apiqvittermentions'));
hannes's avatar
hannes committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        $m->connect('settings/qvitter',
                    array('action' => 'qvittersettings'));
        $m->connect('main/qlogin',
                    array('action' => 'qvitterlogin'));                    		
		
		// check if we should reroute UI to qvitter
		$logged_in_user = common_current_user();
		$qvitter_enabled_by_user = false;
		$qvitter_disabled_by_user = false;		
		if($logged_in_user) {
			try {
				$qvitter_enabled_by_user = Profile_prefs::getData($logged_in_user->getProfile(), 'qvitter', 'enable_qvitter');
			} catch (NoResultException $e) {
				$qvitter_enabled_by_user = false;
			}
			try {
				$qvitter_disabled_by_user = Profile_prefs::getData($logged_in_user->getProfile(), 'qvitter', 'disable_qvitter');
			} catch (NoResultException $e) {
				$qvitter_disabled_by_user = false;
			}		
		}					
		
		if((self::settings('enabledbydefault') && !$logged_in_user) ||
		   (self::settings('enabledbydefault') && !$qvitter_disabled_by_user) || 
		  (!self::settings('enabledbydefault') && $qvitter_enabled_by_user)) {
		
			$m->connect('', array('action' => 'qvitter'));      
			$m->connect('main/all', array('action' => 'qvitter'));              
			$m->connect('search/notice', array('action' => 'qvitter')); 
		
				
			URLMapperOverwrite::overwrite_variable($m, ':nickname',
									array('action' => 'showstream'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');
			URLMapperOverwrite::overwrite_variable($m, ':nickname/',
									array('action' => 'showstream'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');                                
			URLMapperOverwrite::overwrite_variable($m, ':nickname/all',
									array('action' => 'all'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');
			URLMapperOverwrite::overwrite_variable($m, ':nickname/subscriptions',
									array('action' => 'subscriptions'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');        
			URLMapperOverwrite::overwrite_variable($m, ':nickname/subscribers',
									array('action' => 'subscribers'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');     
			URLMapperOverwrite::overwrite_variable($m, ':nickname/groups',
									array('action' => 'usergroups'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');                  
			URLMapperOverwrite::overwrite_variable($m, ':nickname/replies',
									array('action' => 'replies'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');               
			URLMapperOverwrite::overwrite_variable($m, ':nickname/favorites',
									array('action' => 'showfavorites'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');                                                                                                                                                                                                                                                         
			URLMapperOverwrite::overwrite_variable($m, 'group/:nickname',
									array('action' => 'showgroup'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');                                
			URLMapperOverwrite::overwrite_variable($m, 'group/:nickname/members',
									array('action' => 'groupmembers'),
									array('nickname' => Nickname::DISPLAY_FMT), 
									'qvitter');                                 

			$m->connect('group/:nickname/admins',
						array('action' => 'qvitter'),
						array('nickname' => Nickname::DISPLAY_FMT));

			URLMapperOverwrite::overwrite_variable($m, 'tag/:tag',
									array('action' => 'showstream'),
									array('tag' => Router::REGEX_TAG), 
									'qvitter');                                 					
		}					
						
    }
    
    /**
     * Menu item for Qvitter
     *
     * @param Action $action action being executed
     *
     * @return boolean hook return
     */

    function onEndAccountSettingsNav($action)
    {
        $action_name = $action->trimmed('action');

        $action->menuItem(common_local_url('qvittersettings'),
                          // TRANS: Poll plugin menu item on user settings page.
                          _m('MENU', 'Qvitter'),
                          // TRANS: Poll plugin tooltip for user settings menu item.
                          _m('Enable/Disable Qvitter UI'),
                          $action_name === 'qvittersettings');

        return true;
    }
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247


    /**
     * Group addresses in API response
     *
     * @param Action $action action being executed
     *
     * @return boolean hook return
     */

    function onNoticeSimpleStatusArray($notice, &$twitter_status)
    {
    
		$notice_groups = $notice->getGroups();
		$group_addressees = false;
		foreach($notice_groups as $g) {
		$group_addressees .= '!'.$g->nickname.' ';
		}
		$group_addressees = trim($group_addressees);
		if($group_addressees == '') $group_addressees = false;
		$twitter_status['statusnet_in_groups'] = $group_addressees;    

        return true;
    }
    
    
    /**
     * Cover photo in API response
     *
     * @param Action $action action being executed
     *
     * @return boolean hook return
     */

    function onTwitterUserArray($profile, &$twitter_user)
    {

        $twitter_user['cover_photo'] = Profile_prefs::getConfigData($profile, 'qvitter', 'cover_photo');        

        return true;
    }    
    
hannes's avatar
hannes committed
248 249 250
}


251 252


hannes's avatar
hannes committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/**
 * Overwrites variables in URL-mapping
 *
 */
class URLMapperOverwrite extends URLMapper
{
    function overwrite_variable($m, $path, $args, $paramPatterns, $newaction)
    {
    
        $m->connect($path, array('action' => $newaction), $paramPatterns);	
		
		$regex = URLMapper::makeRegex($path, $paramPatterns);
	
		foreach($m->variables as $n=>$v)
			if($v[1] == $regex) 
				$m->variables[$n][0]['action'] = $newaction;
    }
}


?>