<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Models;

use App\Events\NotificationDeleteEvent;
use App\Events\NotificationReadEvent;
use App\Libraries\Notification\BatchIdentities;
use DB;
use Illuminate\Database\Eloquent\Builder;

class UserNotification extends Model
{
    const DELIVERY_OFFSETS = [
        'push' => 0,
        'mail' => 1,
    ];

    protected $casts = [
        'is_read' => 'boolean',
    ];

    public static function batchDestroy(User $user, BatchIdentities $batchIdentities)
    {
        $notificationIds = $batchIdentities->getNotificationIds();
        $identities = $batchIdentities->getIdentities();

        $now = now();
        // obtain and filter valid notification ids
        $notificationIds = $user
            ->userNotifications()
            ->whereIn('notification_id', $notificationIds)
            ->where('created_at', '<=', $now)
            ->pluck('notification_id')
            ->all();

        if (count($notificationIds) > 0) {
            $unreadCountQuery = $user
                ->userNotifications()
                ->hasPushDelivery()
                ->where('is_read', false)
                ->whereIn('notification_id', $notificationIds);
            $unreadCountInitial = $unreadCountQuery->count();
            $user
                ->userNotifications()
                ->whereIn('notification_id', $notificationIds)
                ->delete();

            $unreadCountCurrent = $unreadCountQuery->count();
            $readCount = $unreadCountInitial - $unreadCountCurrent;

            event(new NotificationDeleteEvent($user->getKey(), [
                'notifications' => $identities,
                'read_count' => $readCount,
                'timestamp' => $now,
            ]));
        }
    }

    public static function batchMarkAsRead(User $user, BatchIdentities $batchIdentities)
    {
        $ids = $batchIdentities->getNotificationIds();
        $identities = $batchIdentities->getIdentities();

        $now = now();
        if ($ids instanceof Builder) {
            $instance = new static();<commit_msg>skip trying to mark as read if no ids where extracted from params<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Models;

use App\Events\NotificationDeleteEvent;
use App\Events\NotificationReadEvent;
use App\Libraries\Notification\BatchIdentities;
use DB;
use Illuminate\Database\Eloquent\Builder;

class UserNotification extends Model
{
    const DELIVERY_OFFSETS = [
        'push' => 0,
        'mail' => 1,
    ];

    protected $casts = [
        'is_read' => 'boolean',
    ];

    public static function batchDestroy(User $user, BatchIdentities $batchIdentities)
    {
        $notificationIds = $batchIdentities->getNotificationIds();
        $identities = $batchIdentities->getIdentities();

        $now = now();
        // obtain and filter valid notification ids
        $notificationIds = $user
            ->userNotifications()
            ->whereIn('notification_id', $notificationIds)
            ->where('created_at', '<=', $now)
            ->pluck('notification_id')
            ->all();

        if (count($notificationIds) > 0) {
            $unreadCountQuery = $user
                ->userNotifications()
                ->hasPushDelivery()
                ->where('is_read', false)
                ->whereIn('notification_id', $notificationIds);
            $unreadCountInitial = $unreadCountQuery->count();
            $user
                ->userNotifications()
                ->whereIn('notification_id', $notificationIds)
                ->delete();

            $unreadCountCurrent = $unreadCountQuery->count();
            $readCount = $unreadCountInitial - $unreadCountCurrent;

            event(new NotificationDeleteEvent($user->getKey(), [
                'notifications' => $identities,
                'read_count' => $readCount,
                'timestamp' => $now,
            ]));
        }
    }

    public static function batchMarkAsRead(User $user, BatchIdentities $batchIdentities)
    {
        $ids = $batchIdentities->getNotificationIds();
        $identities = $batchIdentities->getIdentities();
        $now = now();

        if (empty($ids)) {
            return;
        } else if ($ids instanceof Builder) {
            $instance = new static();
{'fraction_non_alphanumeric': 0.12076455256298871, 'fraction_numerical': 0.002172024326672459, 'mean_word_length': 2.254416961130742, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5501694', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1199, 'n_words': 281}