*/ protected function casts(): array { return [ 'talked_on' => 'date', 'is_fallback' => 'boolean', ]; } /** * Character that sent the message. * * @return BelongsTo */ public function fromCharacter(): BelongsTo { return $this->belongsTo(Character::class, 'from_character_id'); } /** * Agent that sent the message. * * @return BelongsTo */ public function fromAgent(): BelongsTo { return $this->belongsTo(AiAgent::class, 'from_agent_id'); } /** * Character that received the message. * * @return BelongsTo */ public function toCharacter(): BelongsTo { return $this->belongsTo(Character::class, 'to_character_id'); } /** * Agent that received the message. * * @return BelongsTo */ public function toAgent(): BelongsTo { return $this->belongsTo(AiAgent::class, 'to_agent_id'); } /** * Ordered conversation rows between a character and a peer. * * @param int $characterId the local character * @param string $peerKind PersonTalk::KIND_AGENT or PersonTalk::KIND_CHARACTER * @param int $peerId agent id or character id of the peer * @return Builder */ public static function conversation(int $characterId, string $peerKind, int $peerId): Builder { return self::query()->where(function (Builder $outer) use ($characterId, $peerKind, $peerId): void { if ($peerKind === self::KIND_AGENT) { $outer->where(function (Builder $q) use ($characterId, $peerId): void { $q->where('from_character_id', $characterId)->where('to_agent_id', $peerId); })->orWhere(function (Builder $q) use ($characterId, $peerId): void { $q->where('from_agent_id', $peerId)->where('to_character_id', $characterId); }); return; } $outer->where(function (Builder $q) use ($characterId, $peerId): void { $q->where('from_character_id', $characterId)->where('to_character_id', $peerId); })->orWhere(function (Builder $q) use ($characterId, $peerId): void { $q->where('from_character_id', $peerId)->where('to_character_id', $characterId); }); }); } }