Fave.php 5.46 KB
Newer Older
1 2 3 4
<?php
/**
 * Table Definition for fave
 */
5
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6

Evan Prodromou's avatar
Evan Prodromou committed
7
class Fave extends Memcached_DataObject
8 9 10 11 12 13 14 15 16 17
{
    ###START_AUTOCODE
    /* the code below is auto generated do not remove the above tag */

    public $__table = 'fave';                            // table name
    public $notice_id;                       // int(4)  primary_key not_null
    public $user_id;                         // int(4)  primary_key not_null
    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP

    /* Static get */
18 19
    function staticGet($k,$v=null)
    { return Memcached_DataObject::staticGet('Fave',$k,$v); }
20 21 22

    /* the code above is auto generated do not remove the tag below */
    ###END_AUTOCODE
23

24 25 26 27 28 29 30 31 32
    /**
     * Save a favorite record.
     * @fixme post-author notification should be moved here
     *
     * @param Profile $profile the local or remote user who likes
     * @param Notice $notice the notice that is liked
     * @return mixed false on failure, or Fave record on success
     */
    static function addNew(Profile $profile, Notice $notice) {
33 34 35 36 37 38 39 40 41 42 43 44 45 46

        $fave = null;

        if (Event::handle('StartFavorNotice', array($profile, $notice, &$fave))) {

            $fave = new Fave();

            $fave->user_id   = $profile->id;
            $fave->notice_id = $notice->id;

            if (!$fave->insert()) {
                common_log_db_error($fave, 'INSERT', __FILE__);
                return false;
            }
47
            self::blow('fave:by_notice:%d', $fave->notice_id);
48 49

            Event::handle('EndFavorNotice', array($profile, $notice));
50
        }
51

52 53
        return $fave;
    }
Evan Prodromou's avatar
Evan Prodromou committed
54

55 56 57 58 59 60 61 62 63 64
    function delete()
    {
        $profile = Profile::staticGet('id', $this->user_id);
        $notice  = Notice::staticGet('id', $this->notice_id);

        $result = null;

        if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {

            $result = parent::delete();
65
            self::blow('fave:by_notice:%d', $this->notice_id);
66 67 68 69 70 71 72 73 74

            if ($result) {
                Event::handle('EndDisfavorNotice', array($profile, $notice));
            }
        }

        return $result;
    }

75
    function pkeyGet($kv)
76
    {
77 78
        return Memcached_DataObject::pkeyGet('Fave', $kv);
    }
Evan Prodromou's avatar
Evan Prodromou committed
79

80
    function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
Evan Prodromou's avatar
Evan Prodromou committed
81
    {
82
        $stream = new FaveNoticeStream($user_id, $own);
83 84

        return $stream->getNotices($offset, $limit, $since_id, $max_id);
Evan Prodromou's avatar
Evan Prodromou committed
85 86
    }

87 88
    function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
    {
89
        $stream = new FaveNoticeStream($user_id, $own);
90 91 92 93

        return $stream->getNoticeIds($offset, $limit, $since_id, $max_id);
    }

94 95 96 97 98 99 100 101
    function asActivity()
    {
        $notice  = Notice::staticGet('id', $this->notice_id);
        $profile = Profile::staticGet('id', $this->user_id);

        $act = new Activity();

        $act->verb = ActivityVerb::FAVORITE;
102 103 104

        // FIXME: rationalize this with URL below

105 106 107
        $act->id   = TagURI::mint('favor:%d:%d:%s',
                                  $profile->id,
                                  $notice->id,
108
                                  common_date_iso8601($this->modified));
109

110
        $act->time    = strtotime($this->modified);
111
        // TRANS: Activity title when marking a notice as favorite.
112
        $act->title   = _("Favor");
113 114
        // TRANS: Ntofication given when a user marks a notice as favorite.
        // TRANS: %1$s is a user nickname or full name, %2$s is a notice URI.
115
        $act->content = sprintf(_('%1$s marked notice %2$s as a favorite.'),
116 117 118
                               $profile->getBestName(),
                               $notice->uri);

119 120
        $act->actor     = ActivityObject::fromProfile($profile);
        $act->objects[] = ActivityObject::fromNotice($notice);
121

122 123 124 125 126 127 128
        $url = common_local_url('AtomPubShowFavorite',
                                          array('profile' => $this->user_id,
                                                'notice'  => $this->notice_id));

        $act->selfLink = $url;
        $act->editLink = $url;

129 130
        return $act;
    }
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

    /**
     * Fetch a stream of favorites by profile
     *
     * @param integer $profileId Profile that faved
     * @param integer $offset    Offset from last
     * @param integer $limit     Number to get
     *
     * @return mixed stream of faves, use fetch() to iterate
     *
     * @todo Cache results
     * @todo integrate with Fave::stream()
     */

    static function byProfile($profileId, $offset, $limit)
    {
        $fav = new Fave();

        $fav->user_id = $profileId;

        $fav->orderBy('modified DESC');

        $fav->limit($offset, $limit);

        $fav->find();

        return $fav;
    }
159 160 161 162 163 164 165 166 167

    /**
     * Grab a list of profile who have favored this notice.
     *
     * @return ArrayWrapper masquerading as a Fave
     */
    static function byNotice($noticeId)
    {
        $c = self::memcache();
168
        $key = Cache::key('fave:by_notice:' . $noticeId);
169 170 171 172 173 174 175 176

        $wrapper = $c->get($key);
        if (!$wrapper) {
            // @fixme caching & scalability!
            $fave = new Fave();
            $fave->notice_id = $noticeId;
            $fave->find();

177
            $list = array();
178 179 180 181 182 183 184 185
            while ($fave->fetch()) {
                $list[] = clone($fave);
            }
            $wrapper = new ArrayWrapper($list);
            $c->set($key, $wrapper);
        }
        return $wrapper;
    }
186
}