inboxnoticestream.php 4.74 KB
Newer Older
1 2 3 4 5
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2011, StatusNet, Inc.
 *
mmn's avatar
mmn committed
6
 * Stream of notices for a profile's "all" feed
Siebrand Mazeland's avatar
Siebrand Mazeland committed
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * PHP version 5
 *
 * 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/>.
 *
23
 * @category  NoticeStream
24 25
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
mmn's avatar
mmn committed
26
 * @author    Mikael Nordfeldth <mmn@hethane.se>
27
 * @copyright 2011 StatusNet, Inc.
mmn's avatar
mmn committed
28
 * @copyright 2014 Free Software Foundation, Inc.
29 30 31 32
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */

33
if (!defined('GNUSOCIAL')) { exit(1); }
34 35

/**
mmn's avatar
mmn committed
36
 * Stream of notices for a profile's "all" feed
37 38 39 40
 *
 * @category  General
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
mmn's avatar
mmn committed
41
 * @author    Mikael Nordfeldth <mmn@hethane.se>
42
 * @copyright 2011 StatusNet, Inc.
mmn's avatar
mmn committed
43
 * @copyright 2014 Free Software Foundation, Inc.
44 45 46
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */
47
class InboxNoticeStream extends ScopingNoticeStream
48 49 50 51
{
    /**
     * Constructor
     *
mmn's avatar
mmn committed
52 53
     * @param Profile $target Profile to get a stream for
     * @param Profile $scoped Currently scoped profile (if null, it is fetched)
54
     */
mmn's avatar
mmn committed
55
    function __construct(Profile $target, Profile $scoped=null)
56
    {
mmn's avatar
mmn committed
57 58
        if ($scoped === null) {
            $scoped = Profile::current();
59
        }
60
        // FIXME: we don't use CachingNoticeStream - but maybe we should?
61
        parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped);
62 63 64 65
    }
}

/**
mmn's avatar
mmn committed
66
 * Raw stream of notices for the target's inbox
67 68 69 70 71 72 73 74
 *
 * @category  General
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2011 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
 */
mmn's avatar
mmn committed
75
class RawInboxNoticeStream extends FullNoticeStream
76
{
mmn's avatar
mmn committed
77
    protected $target  = null;
78 79 80 81 82
    protected $inbox = null;

    /**
     * Constructor
     *
mmn's avatar
mmn committed
83
     * @param Profile $target Profile to get a stream for
84
     */
mmn's avatar
mmn committed
85
    function __construct(Profile $target)
86
    {
87
        parent::__construct();
mmn's avatar
mmn committed
88
        $this->target  = $target;
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    /**
     * Get IDs in a range
     *
     * @param int $offset   Offset from start
     * @param int $limit    Limit of number to get
     * @param int $since_id Since this notice
     * @param int $max_id   Before this notice
     *
     * @return Array IDs found
     */
    function getNoticeIds($offset, $limit, $since_id, $max_id)
    {
mmn's avatar
mmn committed
103 104
        $notice = new Notice();
        $notice->selectAdd();
105 106 107 108 109 110
        $notice->selectAdd('id');
        $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
        // Reply:: is a table of mentions
        // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
        $notice->whereAdd(
                sprintf('notice.id IN (SELECT notice_id FROM reply WHERE profile_id=%1$d) ' .
111 112
                        'OR notice.profile_id IN (SELECT subscribed FROM subscription WHERE subscriber=%1$d) ' .
                        'OR notice.id IN (SELECT notice_id FROM group_inbox WHERE group_id IN (SELECT group_id FROM group_member WHERE profile_id=%1$d))' .
113 114
                        'OR notice.id IN (SELECT notice_id FROM attention WHERE profile_id=%1$d)',
                    $this->target->id)
115
            );
116 117 118 119 120 121
        if (!empty($since_id)) {
            $notice->whereAdd(sprintf('notice.id > %d', $since_id));
        }
        if (!empty($max_id)) {
            $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
        }
122 123 124

        self::filterVerbs($notice, $this->selectVerbs);

mmn's avatar
mmn committed
125
        $notice->limit($offset, $limit);
126 127 128 129 130
        // notice.id will give us even really old posts, which were
        // recently imported. For example if a remote instance had
        // problems and just managed to post here. Another solution
        // would be to have a 'notice.imported' field and order by it.
        $notice->orderBy('notice.id DESC');
mmn's avatar
mmn committed
131 132 133

        if (!$notice->find()) {
            return array();
134 135
        }

136
        $ids = $notice->fetchAll('id');
137 138 139

        return $ids;
    }
Siebrand Mazeland's avatar
Siebrand Mazeland committed
140
}