slack/src/Models/Team.php
2021-08-12 14:09:40 +10:00

97 lines
1.7 KiB
PHP

<?php
namespace Slack\Models;
use Illuminate\Database\Eloquent\Model;
use Leenooks\Traits\ScopeActive;
use Slack\API;
class Team extends Model
{
use ScopeActive;
protected $fillable = ['team_id'];
protected $table = 'slack_teams';
/* RELATIONS */
public function admins()
{
return $this->hasMany(User::class,'team_id','id')->where('admin','=',TRUE);
}
public function bot()
{
return $this->hasOne(User::class,'id','bot_id');
}
public function channels()
{
return $this->hasMany(Channel::class);
}
public function owner()
{
return $this->belongsTo(User::class,'admin_id');
}
// Tokens applicable to this team
// @todo team_id can now be null, so we need to get it from the enterprise_id.
public function token()
{
return $this->hasOne(Token::class);
}
public function users()
{
return $this->hasMany(User::class);
}
/* ATTRIBUTES */
/**
* Provide an obfuscated token.
*
* @note Some tokens have 3 fields (separated by a dash), some have 4
* @return string
*/
public function getAppTokenObfuscateAttribute(): string
{
$attrs = explode('-',$this->getAppTokenAttribute()->token);
$items = count($attrs)-1;
$attrs[$items] = '...'.substr($attrs[$items],-5);
return implode('-',$attrs);
}
/**
* Return the team's slack URL
* @return string
*/
public function getUrlAttribute(): string
{
return $this->team_name ? sprintf('%s.slack.com',$this->team_name) : '';
}
/* METHODS */
/**
* Join the owner and the admins together.
* @deprecated ?
*/
public function admin_users()
{
return $this->admins->merge($this->owner->get());
}
/**
* Return an instance of the API ready to interact with Slack
*
* @return API
*/
public function slackAPI(): API
{
return new API($this);
}
}