threadednoticelist.php 19 KB
Newer Older
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
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * widget for displaying a list of notices
 *
 * PHP version 5
 *
 * LICENCE: 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/>.
 *
 * @category  UI
 * @package   StatusNet
 * @author    Brion Vibber <brion@status.net>
 * @copyright 2011 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

30
if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/**
 * widget for displaying a list of notices
 *
 * There are a number of actions that display a list of notices, in
 * reverse chronological order. This widget abstracts out most of the
 * code for UI for notice lists. It's overridden to hide some
 * data for e.g. the profile page.
 *
 * @category UI
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 * @see      Notice
 * @see      NoticeListItem
 * @see      ProfileNoticeList
 */
class ThreadedNoticeList extends NoticeList
{
51 52
    protected $userProfile;

53
    function __construct(Notice $notice, HTMLOutputter $out=null, $profile=-1)
54 55
    {
        parent::__construct($notice, $out);
56 57 58
        if (is_int($profile) && $profile == -1) {
            $profile = Profile::current();
        }
59 60 61
        $this->userProfile = $profile;
    }

62 63 64 65 66 67 68 69 70 71 72
    /**
     * show the list of notices
     *
     * "Uses up" the stream by looping through it. So, probably can't
     * be called twice on the same list.
     *
     * @return int count of notices listed.
     */
    function show()
    {
        $this->out->elementStart('div', array('id' =>'notices_primary'));
73 74
        // TRANS: Header for Notices section.
        $this->out->element('h2', null, _m('HEADER','Notices'));
75
        $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
76

77
		$notices = $this->notice->fetchAll();
78
		$total = count($notices);
79 80
		$notices = array_slice($notices, 0, NOTICES_PER_PAGE);
		
mmn's avatar
mmn committed
81 82
        $allnotices = self::_allNotices($notices);
    	self::prefill($allnotices);
83
    	
84
        $conversations = array();
85 86
        
        foreach ($notices as $notice) {
87

88
            // Collapse repeats into their originals...
89
            
90
            if ($notice->repeat_of) {
91
                $orig = Notice::getKV('id', $notice->repeat_of);
92
                if ($orig instanceof Notice) {
93 94 95 96
                    $notice = $orig;
                }
            }
            $convo = $notice->conversation;
97 98 99 100 101 102 103
            if (!empty($conversations[$convo])) {
                // Seen this convo already -- skip!
                continue;
            }
            $conversations[$convo] = true;

            // Get the convo's root notice
104
            $root = $notice->conversationRoot($this->userProfile);
105
            if ($root instanceof Notice) {
106 107
                $notice = $root;
            }
108 109

            try {
110
                $item = $this->newListItem($notice);
111 112 113 114 115 116 117 118 119 120 121
                $item->show();
            } catch (Exception $e) {
                // we log exceptions and continue
                common_log(LOG_ERR, $e->getMessage());
                continue;
            }
        }

        $this->out->elementEnd('ol');
        $this->out->elementEnd('div');

122
        return $total;
123 124
    }

125 126 127 128 129 130 131
    function _allNotices($notices)
    {
        $convId = array();
        foreach ($notices as $notice) {
            $convId[] = $notice->conversation;
        }
        $convId = array_unique($convId);
132
        $allMap = Notice::listGet('conversation', $convId);
133 134 135 136 137 138 139
        $allArray = array();
        foreach ($allMap as $convId => $convNotices) {
            $allArray = array_merge($allArray, $convNotices);
        }
        return $allArray;
    }

140 141 142 143 144 145 146 147 148 149
    /**
     * returns a new list item for the current notice
     *
     * Recipe (factory?) method; overridden by sub-classes to give
     * a different list item class.
     *
     * @param Notice $notice the current notice
     *
     * @return NoticeListItem a list item for displaying the notice
     */
150
    function newListItem(Notice $notice)
151
    {
152
        return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    }
}

/**
 * widget for displaying a single notice
 *
 * This widget has the core smarts for showing a single notice: what to display,
 * where, and under which circumstances. Its key method is show(); this is a recipe
 * that calls all the other show*() methods to build up a single notice. The
 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
 * author info (since that's implicit by the data in the page).
 *
 * @category UI
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 * @see      NoticeList
 * @see      ProfileNoticeListItem
 */
class ThreadedNoticeListItem extends NoticeListItem
{
175 176
    protected $userProfile = null;

177
    function __construct(Notice $notice, HTMLOutputter $out=null, $profile=null)
178 179 180 181 182
    {
        parent::__construct($notice, $out);
        $this->userProfile = $profile;
    }

183 184 185 186
    function initialItems()
    {
        return 3;
    }
187

188 189 190 191 192
    function showContext()
    {
        // Silence!
    }

193 194 195 196 197 198 199 200 201
    /**
     * finish the notice
     *
     * Close the last elements in the notice list item
     *
     * @return void
     */
    function showEnd()
    {
202
        $max = $this->initialItems();
203
        if (!$this->repeat) {
204 205
            $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
            $notice = $stream->getNotices(0, $max + 2);
206 207 208 209
            $notices = array();
            $cnt = 0;
            $moreCutoff = null;
            while ($notice->fetch()) {
Evan Prodromou's avatar
Evan Prodromou committed
210
                if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
211 212 213 214 215
                    // Don't list repeats as separate notices in a conversation
                    if (!empty($notice->repeat_of)) {
                        continue;
                    }

Evan Prodromou's avatar
Evan Prodromou committed
216 217 218 219 220 221 222 223 224 225 226 227
                    if ($notice->id == $this->notice->id) {
                        // Skip!
                        continue;
                    }
                    $cnt++;
                    if ($cnt > $max) {
                        // boo-yah
                        $moreCutoff = clone($notice);
                        break;
                    }
                    $notices[] = clone($notice); // *grumble* inefficient as hell
                    Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
228
                }
229
            }
230

231 232 233 234 235 236 237 238 239 240
            if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
                $this->out->elementStart('ul', 'notices threaded-replies xoxo');

                $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
                $hasFaves = $item->show();

                $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
                $hasRepeats = $item->show();

                if ($notices) {
Evan Prodromou's avatar
Evan Prodromou committed
241

242
                    if ($moreCutoff) {
Evan Prodromou's avatar
Evan Prodromou committed
243
                        $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
244 245 246 247
                        $item->show();
                    }
                    foreach (array_reverse($notices) as $notice) {
                        if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
248
                            $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
249 250 251 252
                            $item->show();
                            Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
                        }
                    }
253
                }
254 255 256 257 258 259 260 261

                if ($notices || $hasFaves || $hasRepeats) {
                    // @fixme do a proper can-post check that's consistent
                    // with the JS side
                    if (common_current_user()) {
                        $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
                        $item->show();
                    }
262
                }
263 264
                $this->out->elementEnd('ul');
                Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
265
            }
266 267 268 269 270
        }

        parent::showEnd();
    }
}
Brion Vibber's avatar
Brion Vibber committed
271

272
// @todo FIXME: needs documentation.
Brion Vibber's avatar
Brion Vibber committed
273 274
class ThreadedNoticeListSubItem extends NoticeListItem
{
275 276
    protected $root = null;

277
    function __construct(Notice $notice, $root, $out)
278 279 280 281 282
    {
        $this->root = $root;
        parent::__construct($notice, $out);
    }

Brion Vibber's avatar
Brion Vibber committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296
    function avatarSize()
    {
        return AVATAR_STREAM_SIZE; // @fixme would like something in between
    }

    function showNoticeLocation()
    {
        //
    }

    function showNoticeSource()
    {
        //
    }
297 298 299 300 301

    function showContext()
    {
        //
    }
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    function getReplyProfiles()
    {
        $all = parent::getReplyProfiles();

        $profiles = array();

        $rootAuthor = $this->root->getProfile();

        foreach ($all as $profile) {
            if ($profile->id != $rootAuthor->id) {
                $profiles[] = $profile;
            }
        }

        return $profiles;
    }

320 321 322 323
    function showEnd()
    {
        $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
        $hasFaves = $item->show();
324 325
        $item = new ThreadedNoticeListInlineRepeatsItem($this->notice, $this->out);
        $hasRepeats = $item->show();
326 327
        parent::showEnd();
    }
328 329 330
}

/**
331
 * Placeholder for loading more replies...
332
 */
333
class ThreadedNoticeListMoreItem extends NoticeListItem
334
{
Evan Prodromou's avatar
Evan Prodromou committed
335 336
    protected $cnt;

337
    function __construct(Notice $notice, HTMLOutputter $out, $cnt)
Evan Prodromou's avatar
Evan Prodromou committed
338 339 340 341 342
    {
        parent::__construct($notice, $out);
        $this->cnt = $cnt;
    }

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    /**
     * recipe function for displaying a single notice.
     *
     * This uses all the other methods to correctly display a notice. Override
     * it or one of the others to fine-tune the output.
     *
     * @return void
     */
    function show()
    {
        $this->showStart();
        $this->showMiniForm();
        $this->showEnd();
    }

Brion Vibber's avatar
Brion Vibber committed
358 359 360 361 362 363 364
    /**
     * start a single notice.
     *
     * @return void
     */
    function showStart()
    {
365
        $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
Brion Vibber's avatar
Brion Vibber committed
366 367
    }

368 369
    function showMiniForm()
    {
370
        $id = $this->notice->conversation;
371
        $url = common_local_url('conversation', array('id' => $id));
372

373 374
        $n = Conversation::noticeCount($id) - 1;

375 376 377
        // TRANS: Link to show replies for a notice.
        // TRANS: %d is the number of replies to a notice and used for plural.
        $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
378 379

        $this->out->element('a', array('href' => $url), $msg);
380 381
    }
}
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/**
 * Placeholder for reply form...
 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
 */
class ThreadedNoticeListReplyItem extends NoticeListItem
{
    /**
     * recipe function for displaying a single notice.
     *
     * This uses all the other methods to correctly display a notice. Override
     * it or one of the others to fine-tune the output.
     *
     * @return void
     */
    function show()
    {
        $this->showStart();
        $this->showMiniForm();
        $this->showEnd();
    }

    /**
     * start a single notice.
     *
     * @return void
     */
    function showStart()
    {
        $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
    }

    function showMiniForm()
    {
        $this->out->element('input', array('class' => 'placeholder',
417
                                           // TRANS: Field label for reply mini form.
418
                                           'value' => _('Write a reply...')));
419
    }
420 421 422 423 424
}

/**
 * Placeholder for showing faves...
 */
425
abstract class NoticeListActorsItem extends NoticeListItem
426
{
427 428 429 430 431 432 433
    /**
     * @return array of profile IDs
     */
    abstract function getProfiles();

    abstract function getListMessage($count, $you);

434 435
    function show()
    {
436
        $links = array();
437
        $you = false;
438 439 440
        $cur = common_current_user();
        foreach ($this->getProfiles() as $id) {
            if ($cur && $cur->id == $id) {
441
                $you = true;
442
                // TRANS: Reference to the logged in user in favourite list.
443
                array_unshift($links, _m('FAVELIST', 'You'));
444
            } else {
445
                $profile = Profile::getKV('id', $id);
446
                if ($profile) {
Evan Prodromou's avatar
Evan Prodromou committed
447
                    $links[] = sprintf('<a href="%s">%s</a>',
448
                                       htmlspecialchars($profile->profileurl),
Evan Prodromou's avatar
Evan Prodromou committed
449
                                       htmlspecialchars($profile->getBestName()));
450
                }
451 452 453 454 455
            }
        }

        if ($links) {
            $count = count($links);
456
            $msg = $this->getListMessage($count, $you);
457
            $out = sprintf($msg, $this->magicList($links));
458

459
            $this->showStart();
460
            $this->out->raw($out);
461
            $this->showEnd();
462 463 464 465 466
            return $count;
        } else {
            return 0;
        }
    }
467 468 469 470 471 472 473 474 475 476

    function magicList($items)
    {
        if (count($items) == 0) {
            return '';
        } else if (count($items) == 1) {
            return $items[0];
        } else {
            $first = array_slice($items, 0, -1);
            $last = array_slice($items, -1, 1);
477
            // TRANS: Separator in list of user names like "Jim, Bob, Mary".
Brion Vibber's avatar
Brion Vibber committed
478
            $separator = _(', ');
479
            // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
480 481
            // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
            return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
482 483
        }
    }
484
}
485

486 487 488 489 490 491 492
/**
 * Placeholder for showing faves...
 */
class ThreadedNoticeListFavesItem extends NoticeListActorsItem
{
    function getProfiles()
    {
493
        $faves = $this->notice->getFaves();
494
        $profiles = array();
495
        foreach ($faves as $fave) {
496 497 498 499 500
            $profiles[] = $fave->user_id;
        }
        return $profiles;
    }

501 502 503 504 505 506 507 508 509
    function magicList($items)
    {
        if (count($items) > 4) {
            return parent::magicList(array_slice($items, 0, 3));
        } else {
            return parent::magicList($items);
        }
    }

510 511 512 513
    function getListMessage($count, $you)
    {
        if ($count == 1 && $you) {
            // darn first person being different from third person!
514
            // TRANS: List message for notice favoured by logged in user.
515 516 517
            return _m('FAVELIST', 'You like this.');
        } else if ($count > 4) {
            // TRANS: List message for when more than 4 people like something.
518 519 520 521 522
            // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
            // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
            return sprintf(_m('%%s and %d others like this.',
                              '%%s and %d others like this.',
                              $count),
523
                           $count - 3);
524
        } else {
525
            // TRANS: List message for favoured notices.
526 527
            // TRANS: %%s is a list of users liking a notice.
            // TRANS: Plural is based on the number of of users that have favoured a notice.
528 529
            return sprintf(_m('%%s likes this.',
                              '%%s like this.',
530 531
                              $count),
                           $count);
532 533 534 535 536 537 538 539 540 541 542 543 544 545
        }
    }

    function showStart()
    {
        $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
    }

    function showEnd()
    {
        $this->out->elementEnd('li');
    }
}

546
// @todo FIXME: needs documentation.
547 548 549 550
class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
{
    function showStart()
    {
mmn's avatar
mmn committed
551
        $this->out->elementStart('div', array('class' => 'e-content notice-faves'));
552 553 554 555 556 557 558
    }

    function showEnd()
    {
        $this->out->elementEnd('div');
    }
}
559 560

/**
561
 * Placeholder for showing repeats...
562 563 564 565 566
 */
class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
{
    function getProfiles()
    {
Evan Prodromou's avatar
Evan Prodromou committed
567
        $repeats = $this->notice->getRepeats();
568 569

        $profiles = array();
Evan Prodromou's avatar
Evan Prodromou committed
570 571

        foreach ($repeats as $rep) {
572 573
            $profiles[] = $rep->profile_id;
        }
Evan Prodromou's avatar
Evan Prodromou committed
574

575 576 577
        return $profiles;
    }

578 579 580 581 582 583 584 585 586
    function magicList($items)
    {
        if (count($items) > 4) {
            return parent::magicList(array_slice($items, 0, 3));
        } else {
            return parent::magicList($items);
        }
    }

587 588 589 590
    function getListMessage($count, $you)
    {
        if ($count == 1 && $you) {
            // darn first person being different from third person!
591
            // TRANS: List message for notice repeated by logged in user.
592 593 594 595 596 597 598 599 600
            return _m('REPEATLIST', 'You repeated this.');
        } else if ($count > 4) {
            // TRANS: List message for when more than 4 people repeat something.
            // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
            // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
            return sprintf(_m('%%s and %d other repeated this.',
                              '%%s and %d others repeated this.',
                              $count - 3),
                           $count - 3);
601
        } else {
602 603 604 605 606
            // TRANS: List message for favoured notices.
            // TRANS: %%s is a list of users liking a notice.
            // TRANS: Plural is based on the number of of users that have favoured a notice.
            return sprintf(_m('%%s repeated this.',
                              '%%s repeated this.',
607 608
                              $count),
                           $count);
609 610 611 612 613 614 615 616 617 618 619 620 621
        }
    }

    function showStart()
    {
        $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
    }

    function showEnd()
    {
        $this->out->elementEnd('li');
    }
}
622 623 624 625 626 627

// @todo FIXME: needs documentation.
class ThreadedNoticeListInlineRepeatsItem extends ThreadedNoticeListRepeatsItem
{
    function showStart()
    {
mmn's avatar
mmn committed
628
        $this->out->elementStart('div', array('class' => 'e-content notice-repeats'));
629 630 631 632 633 634 635
    }

    function showEnd()
    {
        $this->out->elementEnd('div');
    }
}