Looks like im not a good friend of the json api/frontend stuff, I added a hasMany relationship in the backend but to get it to the front end , can't seem to let that happen ..
It also comes with a new model ProfileView
, added a model both in the backend and frontend. Both seem to work.
import Model from 'flarum/Model';
export default class ProfileView extends Model {
//comes from ProfileViewSerializer (viewer(), viewedUser(), visited_at)
visitedAt = Model.attribute('visited_at', Model.transformDate);
viewer = Model.hasOne('viewer');
viewedUser = Model.hasOne('viewedUser');
}
class UserProfileView extends AbstractModel
{
protected $table = 'user_profile_views';
protected $dates = ['visited_at'];
public function viewedUser()
{
return $this->belongsTo(User::class, 'viewed_user_id');
}
public function viewer()
{
return $this->belongsTo(User::class, 'viewer_id');
}
}
In the listener i add this api/backend relationship:
const RELATIONSHIP = "profileViews"; //$user->profileViews()
public function getModelRelationship(GetModelRelationship $event)
{
if($event->isRelationship(User::class, self::RELATIONSHIP))
{
return $event->model->hasMany(UserProfileView::class, 'viewed_user_id');
}
}
//...
public function getApiRelationship(GetApiRelationship $event)
{
if ($event->isRelationship(UserSerializer::class, self::RELATIONSHIP)) {
return $event->serializer->hasMany($event->model, UserProfileViewSerializer::class, self::RELATIONSHIP);
}
}
public function includeTagsRelationship(WillGetData $event)
{
if($event->controller->serializer == UserSerializer::class)
$event->addInclude([self::RELATIONSHIP, self::RELATIONSHIP.'.viewer', self::RELATIONSHIP.'.viewedUser']);//".x" comes from model relationship UserProfileView
}
The serializer:
class UserProfileViewSerializer extends AbstractSerializer
{
protected $type = 'userprofileview';
protected function getDefaultAttributes($profileview)
{
return [
'visited_at' => $this->formatDate($profileview->visited_at)
];
}
protected function viewedUser($profileview)
{
return $this->hasOne($profileview, UserSerializer::class);
}
protected function viewer($profileview)
{
return $this->hasOne($profileview, UserSerializer::class);
}
}
And now the frontend part:
User.prototype.profileViews = Model.hasMany('profileViews');
//...
extend(UserPage.prototype, 'sidebarItems', function(items) {
console.log(this.user.profileViews());
$.each(this.user.profileViews(), function(index, view) {
console.log(view);//doesnt get printed
});
});
console.log(this.user.profileViews());
returns a list of undefined elements while the json api does return entries:
{
"type": "users",
"id": "3",
"attributes": {
"username": "Thomas",
"displayName": "Thomas",
"avatarUrl": null,
"joinTime": "2018-12-17T14:35:48+00:00",
"discussionCount": 1,
"commentCount": 1,
"canEdit": true,
"canDelete": true,
"lastSeenAt": "2019-01-12T11:48:01+00:00",
"isEmailConfirmed": true,
"email": "noreply@gmail.com",
"suspendedUntil": null,
"canSuspend": true
},
"relationships": {
"groups": {
"data": []
},
"profileViews": {
"data": [{
"type": "userprofileview",
"id": "2"
}]
}
}
}
{
"type": "userprofileview",
"id": "2",
"attributes": {
"visited_at": "2019-01-19T15:00:44+00:00"
},
"relationships": {
"viewer": {
"data": {
"type": "users",
"id": "1"
}
},
"viewedUser": {
"data": {
"type": "users",
"id": "3"
}
}
}
}
Anyone can see why it returns undefined for each profile view model? Maybe cuz i dont use the new js model nowhere?