| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | use FluentBoards\App\Services\Constant; |
| 6 | 6 | use FluentBoards\App\Services\PermissionManager; |
| 7 | 7 | use FluentBoards\App\Services\UserService; |
| 8 | 8 | use FluentBoards\Framework\Database\Orm\Builder; |
| 9 | -use FluentBoardsPro\App\Models\CustomField; | |
| 9 | +use FluentBoards\App\Models\CustomField; | |
| 10 | 10 | |
| 11 | 11 | class Board extends Model |
| 12 | 12 | { |
| 13 | 13 | protected $table = 'fbs_boards'; |
| @@ -16,8 +16,10 @@ | ||
| 16 | 16 | |
| 17 | 17 | protected $hidden = [ |
| 18 | 18 | 'created_at', |
| 19 | 19 | 'updated_at', |
| 20 | + 'boardMeta', | |
| 21 | + 'viewerMembership', | |
| 20 | 22 | ]; |
| 21 | 23 | |
| 22 | 24 | protected $fillable = [ |
| 23 | 25 | 'parent_id', |
| @@ -81,11 +83,34 @@ | ||
| 81 | 83 | } |
| 82 | 84 | |
| 83 | 85 | public function getMetaAttribute() |
| 84 | 86 | { |
| 85 | - return $this->getMeta(); | |
| 87 | + if (!$this->relationLoaded('boardMeta')) { | |
| 88 | + return $this->getMeta(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + $meta = []; | |
| 92 | + foreach ($this->getRelation('boardMeta') as $item) { | |
| 93 | + $meta[$item->key] = $item->value; | |
| 94 | + } | |
| 95 | + return $meta; | |
| 86 | 96 | } |
| 87 | 97 | |
| 98 | + /** Internal eager-loaded metadata for board-list serialization. */ | |
| 99 | + public function boardMeta() | |
| 100 | + { | |
| 101 | + return $this->hasMany(Meta::class, 'object_id') | |
| 102 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD); | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** Internal membership snapshot for the current board-list reader. */ | |
| 106 | + public function viewerMembership() | |
| 107 | + { | |
| 108 | + return $this->hasOne(Relation::class, 'object_id') | |
| 109 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 110 | + ->where('foreign_id', get_current_user_id()); | |
| 111 | + } | |
| 112 | + | |
| 88 | 113 | public function setBackgroundAttribute($background) |
| 89 | 114 | { |
| 90 | 115 | $this->attributes['background'] = \maybe_serialize($background); |
| 91 | 116 | } |
| @@ -301,8 +326,9 @@ | ||
| 301 | 326 | } |
| 302 | 327 | |
| 303 | 328 | public function updateMeta($key, $value) |
| 304 | 329 | { |
| 330 | + $this->unsetRelation('boardMeta'); | |
| 305 | 331 | $meta = Meta::where('object_id', $this->id) |
| 306 | 332 | ->where('object_type', Constant::OBJECT_TYPE_BOARD) |
| 307 | 333 | ->where('key', $key) |
| 308 | 334 | ->first(); |
| @@ -335,12 +361,16 @@ | ||
| 335 | 361 | $userId = get_current_user_id(); |
| 336 | 362 | if(PermissionManager::isAdmin()) { |
| 337 | 363 | return false; |
| 338 | 364 | } |
| 339 | - $boardPermissions = Relation::where('object_id', $this->id) | |
| 340 | - ->where('foreign_id', $userId) | |
| 341 | - ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 342 | - ->first(); | |
| 365 | + $boardPermissions = $this->relationLoaded('viewerMembership') ? $this->getRelation('viewerMembership') : null; | |
| 366 | + // Never reuse another user's eagerly loaded membership. | |
| 367 | + if (!$boardPermissions || (int) $boardPermissions->foreign_id !== (int) $userId) { | |
| 368 | + $boardPermissions = Relation::where('object_id', $this->id) | |
| 369 | + ->where('foreign_id', $userId) | |
| 370 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 371 | + ->first(); | |
| 372 | + } | |
| 343 | 373 | |
| 344 | 374 | return $boardPermissions->settings['is_viewer_only'] ?? false; |
| 345 | 375 | } |
| 346 | 376 | |