sup.php 2.95 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21

22
// @todo FIXME: documentation needed.
23 24
class SupAction extends Action
{
25 26
    function handle($args)
    {
27
        parent::handle($args);
Evan Prodromou's avatar
Evan Prodromou committed
28

29
        $seconds = $this->trimmed('seconds');
Evan Prodromou's avatar
Evan Prodromou committed
30

31 32 33
        if (!$seconds) {
            $seconds = 15;
        }
34

Evan Prodromou's avatar
Evan Prodromou committed
35 36
        $updates = $this->getUpdates($seconds);

37
        header('Content-Type: application/json; charset=utf-8');
Evan Prodromou's avatar
Evan Prodromou committed
38

39 40
        print json_encode(array('updated_time' => date('c'),
                                'since_time' => date('c', time() - $seconds),
Evan Prodromou's avatar
Evan Prodromou committed
41
                                'available_periods' => $this->availablePeriods(),
42 43 44
                                'period' => $seconds,
                                'updates' => $updates));
    }
Evan Prodromou's avatar
Evan Prodromou committed
45 46

    function availablePeriods()
47
    {
48
        static $periods = array(86400, 43200, 21600, 7200,
Evan Prodromou's avatar
Evan Prodromou committed
49
                                3600, 1800, 600, 300, 120,
Evan Prodromou's avatar
Evan Prodromou committed
50
                                60, 30, 15);
51 52 53 54 55
        $available = array();
        foreach ($periods as $period) {
            $available[$period] = common_local_url('sup',
                                                   array('seconds' => $period));
        }
Evan Prodromou's avatar
Evan Prodromou committed
56

57 58
        return $available;
    }
Evan Prodromou's avatar
Evan Prodromou committed
59 60

    function getUpdates($seconds)
61
    {
62
        $notice = new Notice();
63

64 65
        // XXX: cache this. Depends on how big this protocol becomes;
        // Re-doing this query every 15 seconds isn't the end of the world.
66

Evan Prodromou's avatar
Evan Prodromou committed
67 68
        $divider = common_sql_date(time() - $seconds);

69
        $notice->query('SELECT profile_id, max(id) AS max_id ' .
70 71
                       'FROM ( ' .
                       'SELECT profile_id, id FROM notice ' .
72 73
                        ((common_config('db','type') == 'pgsql') ?
                       'WHERE extract(epoch from created) > (extract(epoch from now()) - ' . $seconds . ') ' :
Evan Prodromou's avatar
Evan Prodromou committed
74
                       'WHERE created > "'.$divider.'" ' ) .
75
                       ') AS latest ' .
76
                       'GROUP BY profile_id');
Evan Prodromou's avatar
Evan Prodromou committed
77

78
        $updates = array();
Evan Prodromou's avatar
Evan Prodromou committed
79

80 81 82
        while ($notice->fetch()) {
            $updates[] = array($notice->profile_id, $notice->max_id);
        }
Evan Prodromou's avatar
Evan Prodromou committed
83

84 85
        return $updates;
    }
Evan Prodromou's avatar
Evan Prodromou committed
86

87
    function isReadOnly($args)
88
    {
89 90
        return true;
    }
91
}