the browser-facing portion of osu!
at master 78 lines 2.1 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Models\Traits\Es; 7 8use App\Models\Forum\Forum; 9use Carbon\Carbon; 10 11trait ForumPostSearch 12{ 13 use BaseDbIndexable; 14 15 public static function esIndexName() 16 { 17 return $GLOBALS['cfg']['osu']['elasticsearch']['prefix'].'posts'; 18 } 19 20 public static function esIndexingQuery() 21 { 22 $forumIds = Forum::where('enable_indexing', 1)->pluck('forum_id'); 23 24 return static::withoutGlobalScopes()->whereIn('forum_id', $forumIds)->with('forum')->with('topic'); 25 } 26 27 public static function esSchemaFile() 28 { 29 return config_path('schemas/posts.json'); 30 } 31 32 public function esRouting() 33 { 34 // Post and Topic should have the same routing for relationships to work. 35 return $this->topic_id; 36 } 37 38 public function esShouldIndex() 39 { 40 return $this->forum->enable_indexing && $this->topic !== null; 41 } 42 43 public function getEsId() 44 { 45 return "post-{$this->post_id}"; 46 } 47 48 public function toEsJson() 49 { 50 $mappings = static::esMappings(); 51 52 $document = []; 53 foreach ($mappings as $field => $mapping) { 54 switch ($field) { 55 case 'is_deleted': 56 $value = $this->trashed() || $this->topic->trashed(); 57 break; 58 case 'topic_title': 59 if ($this->topic !== null && $this->topic->topic_first_post_id === $this->getKey()) { 60 $value = $this->topic->topic_title; 61 } else { 62 $value = null; 63 } 64 break; 65 default: 66 $value = $this[$field]; 67 } 68 69 if ($value instanceof Carbon) { 70 $value = $value->toIso8601String(); 71 } 72 73 $document[$field] = $value; 74 } 75 76 return $document; 77 } 78}