74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\MailCalendarSync\Db;
|
|
|
|
use OCP\AppFramework\Db\QBMapper;
|
|
use OCP\IDBConnection;
|
|
|
|
/**
|
|
* @extends QBMapper<LogEntry>
|
|
*/
|
|
class LogMapper extends QBMapper {
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
parent::__construct($db, 'mcs_log', LogEntry::class);
|
|
}
|
|
|
|
/**
|
|
* Get recent log entries for a user.
|
|
*
|
|
* @return LogEntry[]
|
|
*/
|
|
public function findByUserId(string $userId, int $limit = 50, int $offset = 0): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
|
|
->orderBy('created_at', 'DESC')
|
|
->setMaxResults($limit)
|
|
->setFirstResult($offset);
|
|
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* Create a log entry.
|
|
*/
|
|
public function log(
|
|
string $userId,
|
|
string $eventUid,
|
|
string $action,
|
|
?string $eventSummary = null,
|
|
?string $attendeeEmail = null,
|
|
?string $fromEmail = null,
|
|
?string $message = null,
|
|
): LogEntry {
|
|
$entry = new LogEntry();
|
|
$entry->setUserId($userId);
|
|
$entry->setEventUid($eventUid);
|
|
$entry->setAction($action);
|
|
$entry->setEventSummary($eventSummary);
|
|
$entry->setAttendeeEmail($attendeeEmail);
|
|
$entry->setFromEmail($fromEmail);
|
|
$entry->setMessage($message);
|
|
$entry->setCreatedAt(date('Y-m-d H:i:s'));
|
|
|
|
return $this->insert($entry);
|
|
}
|
|
|
|
/**
|
|
* Delete old log entries (older than 30 days).
|
|
*/
|
|
public function cleanupOld(int $days = 30): int {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$cutoff = (new \DateTime("-{$days} days"))->format('Y-m-d H:i:s');
|
|
|
|
$qb->delete($this->getTableName())
|
|
->where($qb->expr()->lt('created_at', $qb->createNamedParameter($cutoff)));
|
|
|
|
return $qb->executeStatement();
|
|
}
|
|
}
|