/** * @param IQueryBuilder $query * @param string $user * @param int $since * @param string $sort * * @return array Headers that should be set on the response * * @throws \OutOfBoundsException If $since is not owned by $user */ protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) { if ($since) { $queryBuilder = $this->connection->getQueryBuilder(); $queryBuilder->select('*')->from('activity')->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter((int) $since))); $result = $queryBuilder->execute(); $activity = $result->fetch(); $result->closeCursor(); if ($activity) { if ($activity['affecteduser'] !== $user) { throw new \OutOfBoundsException('Invalid since', 2); } $timestamp = (int) $activity['timestamp']; if ($sort === 'DESC') { $query->andWhere($query->expr()->lte('timestamp', $query->createNamedParameter($timestamp))); $query->andWhere($query->expr()->lt('activity_id', $query->createNamedParameter($since))); } else { $query->andWhere($query->expr()->gte('timestamp', $query->createNamedParameter($timestamp))); $query->andWhere($query->expr()->gt('activity_id', $query->createNamedParameter($since))); } return []; } } /** * Couldn't find the since, so find the oldest one and set the header */ $fetchQuery = $this->connection->getQueryBuilder(); $fetchQuery->select('activity_id')->from('activity')->where($fetchQuery->expr()->eq('affecteduser', $fetchQuery->createNamedParameter($user)))->orderBy('timestamp', $sort)->setMaxResults(1); $result = $fetchQuery->execute(); $activity = $result->fetch(); $result->closeCursor(); if ($activity !== false) { return ['X-Activity-First-Known' => (int) $activity['activity_id']]; } return []; }
/** * Add where statements to a query builder matching the given notification * * @param IQueryBuilder $sql * @param INotification $notification */ protected function sqlWhere(IQueryBuilder $sql, INotification $notification) { if ($notification->getApp() !== '') { $sql->andWhere($sql->expr()->eq('app', $sql->createParameter('app'))); $sql->setParameter('app', $notification->getApp()); } if ($notification->getUser() !== '') { $sql->andWhere($sql->expr()->eq('user', $sql->createParameter('user')))->setParameter('user', $notification->getUser()); } if ($notification->getDateTime()->getTimestamp() !== 0) { $sql->andWhere($sql->expr()->eq('timestamp', $sql->createParameter('timestamp')))->setParameter('timestamp', $notification->getDateTime()->getTimestamp()); } if ($notification->getObjectType() !== '') { $sql->andWhere($sql->expr()->eq('object_type', $sql->createParameter('objectType')))->setParameter('objectType', $notification->getObjectType()); } if ($notification->getObjectId() !== '') { $sql->andWhere($sql->expr()->eq('object_id', $sql->createParameter('objectId')))->setParameter('objectId', $notification->getObjectId()); } if ($notification->getSubject() !== '') { $sql->andWhere($sql->expr()->eq('subject', $sql->createParameter('subject')))->setParameter('subject', $notification->getSubject()); } if ($notification->getMessage() !== '') { $sql->andWhere($sql->expr()->eq('message', $sql->createParameter('message')))->setParameter('message', $notification->getMessage()); } if ($notification->getLink() !== '') { $sql->andWhere($sql->expr()->eq('link', $sql->createParameter('link')))->setParameter('link', $notification->getLink()); } }