|
15 | 15 | use OCP\Activity\IExtension; |
16 | 16 | use OCP\Activity\IFilter; |
17 | 17 | use OCP\Activity\IManager; |
| 18 | +use OCP\DB\Exception; |
18 | 19 | use OCP\DB\QueryBuilder\IQueryBuilder; |
19 | 20 | use OCP\IConfig; |
20 | 21 | use OCP\IDBConnection; |
21 | 22 | use Psr\Log\LoggerInterface; |
| 23 | +use Throwable; |
22 | 24 |
|
23 | 25 | /** |
24 | 26 | * @brief Class for managing the data in the activities |
@@ -91,6 +93,72 @@ public function send(IEvent $event): int { |
91 | 93 | return $this->insertActivity->getLastInsertId(); |
92 | 94 | } |
93 | 95 |
|
| 96 | + /** |
| 97 | + * Bulk sends an event into the activity stream |
| 98 | + * for a batch of users that are affected by the same event |
| 99 | + * (ex. Call Started, Call ended) |
| 100 | + * |
| 101 | + * @param IEvent $event |
| 102 | + * @param array $affectedUsers |
| 103 | + * @return array<int, string> |
| 104 | + * @throws Exception |
| 105 | + */ |
| 106 | + public function bulkSend(IEvent $event, array $affectedUsers): array { |
| 107 | + $this->connection->beginTransaction(); |
| 108 | + |
| 109 | + $activityIds = []; |
| 110 | + try { |
| 111 | + $qb = $this->connection->getQueryBuilder(); |
| 112 | + $qb->insert('activity') |
| 113 | + ->values([ |
| 114 | + 'app' => $this->insertActivity->createParameter('app'), |
| 115 | + 'subject' => $this->insertActivity->createParameter('subject'), |
| 116 | + 'subjectparams' => $this->insertActivity->createParameter('subjectparams'), |
| 117 | + 'message' => $this->insertActivity->createParameter('message'), |
| 118 | + 'messageparams' => $this->insertActivity->createParameter('messageparams'), |
| 119 | + 'file' => $this->insertActivity->createParameter('object_name'), |
| 120 | + 'link' => $this->insertActivity->createParameter('link'), |
| 121 | + 'user' => $this->insertActivity->createParameter('user'), |
| 122 | + 'affecteduser' => $this->insertActivity->createParameter('affecteduser'), |
| 123 | + 'timestamp' => $this->insertActivity->createParameter('timestamp'), |
| 124 | + 'priority' => $this->insertActivity->createParameter('priority'), |
| 125 | + 'type' => $this->insertActivity->createParameter('type'), |
| 126 | + 'object_type' => $this->insertActivity->createParameter('object_type'), |
| 127 | + 'object_id' => $this->insertActivity->createParameter('object_id'), |
| 128 | + ]); |
| 129 | + |
| 130 | + $qb->setParameters([ |
| 131 | + 'app' => $event->getApp(), |
| 132 | + 'type' => $event->getType(), |
| 133 | + 'user' => $event->getAuthor(), |
| 134 | + 'timestamp' => $event->getTimestamp(), |
| 135 | + 'subject' => $event->getSubject(), |
| 136 | + 'subjectparams' => json_encode($event->getSubjectParameters()), |
| 137 | + 'message' => $event->getMessage(), |
| 138 | + 'messageparams' => json_encode($event->getMessageParameters()), |
| 139 | + 'priority' => IExtension::PRIORITY_MEDIUM, |
| 140 | + 'object_type' => $event->getObjectType(), |
| 141 | + 'object_id' => $event->getObjectId(), |
| 142 | + 'object_name' => $event->getObjectName(), |
| 143 | + 'link' => $event->getLink(), |
| 144 | + ]); |
| 145 | + |
| 146 | + foreach ($affectedUsers as $affectedUser) { |
| 147 | + $qb->setParameter('affecteduser', $affectedUser); |
| 148 | + $qb->executeStatement(); |
| 149 | + $activityIds[$qb->getLastInsertId()] = (string)$affectedUser; |
| 150 | + } |
| 151 | + |
| 152 | + $this->connection->commit(); |
| 153 | + } catch (Throwable) { |
| 154 | + // Make sure to always roll back, otherwise the outer code runs in a failed transaction |
| 155 | + $this->connection->rollBack(); |
| 156 | + return []; |
| 157 | + } |
| 158 | + |
| 159 | + return $activityIds; |
| 160 | + } |
| 161 | + |
94 | 162 | /** |
95 | 163 | * Send an event as email |
96 | 164 | * |
|
0 commit comments