|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use DateTime, DateInterval; |
| 7 | + |
| 8 | +class DiscordNotification extends Model |
| 9 | +{ |
| 10 | + public static $TIME_UNITS = [ |
| 11 | + 'minutes' => 1, |
| 12 | + 'hours' => 60, |
| 13 | + 'days' => 1440, |
| 14 | + ]; |
| 15 | + |
| 16 | + const MAX_MINUTES_BEFORE = 43200; // 30 days |
| 17 | + |
| 18 | + public function sends() { |
| 19 | + return $this->hasMany('\App\DiscordNotificationSend'); |
| 20 | + } |
| 21 | + |
| 22 | + public function createdBy() { |
| 23 | + return $this->belongsTo('\App\User', 'created_by'); |
| 24 | + } |
| 25 | + |
| 26 | + public function lastModifiedBy() { |
| 27 | + return $this->belongsTo('\App\User', 'last_modified_by'); |
| 28 | + } |
| 29 | + |
| 30 | + // Events with this tag that should be announced |
| 31 | + public function eventsQuery() { |
| 32 | + $tag = $this->tag; |
| 33 | + return Event::whereHas('tags', function($query) use($tag){ |
| 34 | + $query->where('tag', $tag); |
| 35 | + }) |
| 36 | + ->where('unlisted', 0) |
| 37 | + ->where('is_template', 0) |
| 38 | + ->whereNotIn('status', ['cancelled', 'postponed']) |
| 39 | + ->whereNotNull('sort_date'); |
| 40 | + } |
| 41 | + |
| 42 | + // Events that start within the notification window and haven't been announced yet at their current start time |
| 43 | + public function eventsDue(DateTime $now) { |
| 44 | + $until = clone $now; |
| 45 | + $until->add(new DateInterval('PT'.$this->minutes_before.'M')); |
| 46 | + |
| 47 | + $events = $this->eventsQuery() |
| 48 | + ->where('sort_date', '>', $now->format('Y-m-d H:i:s')) |
| 49 | + ->where('sort_date', '<=', $until->format('Y-m-d H:i:s')) |
| 50 | + ->orderBy('sort_date', 'asc') |
| 51 | + ->get(); |
| 52 | + |
| 53 | + return $events->filter(function($event){ |
| 54 | + return !$this->wasSentFor($event); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + public function nextEvent() { |
| 59 | + return $this->eventsQuery() |
| 60 | + ->where('sort_date', '>', gmdate('Y-m-d H:i:s')) |
| 61 | + ->orderBy('sort_date', 'asc') |
| 62 | + ->first(); |
| 63 | + } |
| 64 | + |
| 65 | + public function wasSentFor(Event $event) { |
| 66 | + return $this->sends() |
| 67 | + ->where('event_id', $event->id) |
| 68 | + ->where('event_start', self::eventStart($event)) |
| 69 | + ->exists(); |
| 70 | + } |
| 71 | + |
| 72 | + // The event's UTC start as stored in sort_date |
| 73 | + public static function eventStart(Event $event) { |
| 74 | + if($event->sort_date instanceof \DateTimeInterface) |
| 75 | + return $event->sort_date->format('Y-m-d H:i:s'); |
| 76 | + return (new DateTime($event->sort_date))->format('Y-m-d H:i:s'); |
| 77 | + } |
| 78 | + |
| 79 | + // Returns [number, unit] for displaying minutes_before in the largest unit it divides evenly into |
| 80 | + public function timeBeforeParts() { |
| 81 | + foreach(array_reverse(self::$TIME_UNITS) as $unit => $minutes) { |
| 82 | + if($this->minutes_before && $this->minutes_before % $minutes == 0) |
| 83 | + return [$this->minutes_before / $minutes, $unit]; |
| 84 | + } |
| 85 | + return [$this->minutes_before, 'minutes']; |
| 86 | + } |
| 87 | + |
| 88 | + public function timeBeforeText() { |
| 89 | + list($number, $unit) = $this->timeBeforeParts(); |
| 90 | + return trans_choice('discord.durations.'.$unit, $number); |
| 91 | + } |
| 92 | +} |
0 commit comments