Qvitter/actions/apiqvitternotifications.php

223 lines
7.9 KiB
PHP
Raw Normal View History

2014-09-25 06:20:35 +09:00
<?php
/* · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
2014-09-25 06:20:35 +09:00
· ·
· ·
· Q V I T T E R ·
· ·
2016-01-18 09:24:37 +09:00
· https://git.gnu.io/h2p/Qvitter ·
2014-09-25 06:20:35 +09:00
· ·
· ·
· <o) ·
· /_//// ·
· (____/ ·
· (o< ·
· o> \\\\_\ ·
· \\) \____) ·
· ·
· ·
2014-09-25 06:20:35 +09:00
· ·
· 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. ·
· ·
2014-09-25 06:20:35 +09:00
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
if (!defined('STATUSNET')) {
exit(1);
}
class ApiQvitterNotificationsAction extends ApiPrivateAuthAction
{
var $notifications = array();
2014-09-25 06:20:35 +09:00
var $notices = null;
var $profiles = null;
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*
*/
protected function prepare(array $args=array())
2014-09-25 06:20:35 +09:00
{
parent::prepare($args);
2016-03-01 00:08:06 +09:00
$this->format = 'json';
2016-02-02 02:22:41 +09:00
2014-09-25 06:20:35 +09:00
$this->notifications = $this->getNotifications();
return true;
}
/**
* Handle the request
*
* Just show the notices
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
protected function handle()
2014-09-25 06:20:35 +09:00
{
parent::handle();
2014-09-25 06:20:35 +09:00
$this->showTimeline();
}
/**
* Show the timeline of notices
*
* @return void
*/
function showTimeline()
{
$notice = null;
2014-09-25 06:20:35 +09:00
$notifications_populated = array();
2014-11-26 05:38:00 +09:00
foreach ($this->notifications as $notification) {
// all but follow has an notice
if ($notification->ntype != 'follow') {
// we need a notice id here, skip this notification if notice id is null
if($notification->notice_id === null) {
continue;
} else {
$notice_object = Notice::getKV($notification->notice_id);
if($notice_object instanceof Notice) {
$notice = self::twitterSimpleStatusArray($notice_object);
} else {
2016-03-01 00:08:06 +09:00
// if the referenced notice is missing, delete this corrupt notification!
$notification->delete();
continue;
}
}
}
$from_profile = Profile::getKV($notification->from_profile_id);
2014-11-26 05:38:00 +09:00
// a user might have deleted their profile, don't show these notifications
if ($from_profile instanceof Profile) {
$notifications_populated[] = array(
'id'=> $notification->id,
'from_profile'=> self::twitterUserArray($from_profile),
'ntype'=> $notification->ntype,
'notice'=> $notice,
'created_at'=>self::dateTwitter($notification->created),
'is_seen'=>$notification->is_seen
);
2016-03-01 00:08:06 +09:00
} else {
// if the referenced from_profile is missing, delete this corrupt notification!
$notification->delete();
}
2014-10-06 18:56:34 +09:00
// mark as seen
if($notification->is_seen == 0) {
$orig = clone($notification);
$notification->is_seen = 1;
$notification->update($orig);
}
}
2014-09-25 06:20:35 +09:00
$this->initDocument('json');
$this->showJsonObjects($notifications_populated);
$this->endDocument('json');
}
/**
* Get notices
*
* @return array notices
*/
function getNotifications()
{
$notices = array();
$profile = ($this->auth_user) ? $this->auth_user->getProfile() : null;
2014-10-05 21:07:31 +09:00
if(!$profile instanceof Profile) {
return false;
}
2014-09-25 06:20:35 +09:00
$stream = new NotificationStream($profile);
$notifications = $stream->getNotifications(($this->page - 1) * $this->count,
$this->count,
$this->since_id,
$this->max_id);
2014-09-25 06:20:35 +09:00
$notifications = $notifications->fetchAll();
return $notifications;
}
/**
* Is this action read only?
*
* @param array $args other arguments
*
* @return boolean true
*/
function isReadOnly($args)
{
return true;
}
/**
* When was this feed last modified?
*
* @return string datestamp of the latest notice in the stream
*/
function lastModified()
{
if (!empty($this->notifications) && (count($this->notifications) > 0)) {
return strtotime($this->notifications[0]->created);
}
return null;
}
/**
* An entity tag for this stream
*
* Returns an Etag based on the action name, language, and
* timestamps of the first and last notice in the timeline
*
* @return string etag
*/
function etag()
{
if (!empty($this->notifications) && (count($this->notifications) > 0)) {
$last = count($this->notifications) - 1;
return '"' . implode(
':',
array($this->arg('action'),
common_user_cache_hash($this->auth_user),
common_language(),
strtotime($this->notifications[0]->created),
strtotime($this->notifications[$last]->created))
)
. '"';
}
return null;
}
2014-09-25 06:20:35 +09:00
}