Skip to content

Commit

Permalink
fix(user): 修复 At 我的获取最新预览消息用户名错为自身
Browse files Browse the repository at this point in the history
zhiyicx/thinksns-plus-android#2304
  • Loading branch information
medz committed Sep 6, 2018
1 parent 0d9606b commit 33cefe3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/AtMessage/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(ResourceManagerInterface $manager, Model $model, Pus
public function send(UserModel $sender, UserModel $user, $resource): void
{
$resource = $this->manager->resource($resource, $sender);
$atMessage = $this->message($resource, $user);
$atMessage = $this->message($resource, $user, $sender);
$this->save(function () use ($resource, $atMessage, $user) {
$atMessage->save();
$this->updateAtMessageCount($user);
Expand All @@ -82,14 +82,16 @@ public function send(UserModel $sender, UserModel $user, $resource): void
* Create a at message model.
* @param \Zhiyi\Plus\AtMessage\ResourceInterface $resource
* @param \Zhiyi\Plus\Models\User $user
* @param \Zhiyi\Plus\Models\User $sender
* @return \Zhiyi\Plus\Models\AtMessage
*/
public function message(ResourceInterface $resource, UserModel $user): Model
public function message(ResourceInterface $resource, UserModel $user, UserModel $sender): Model
{
$message = $this->model->newInstance();
$message->resourceable_type = $resource->type();
$message->resourceable_id = $resource->id();
$message->user_id = $user->id;
$message->sender_id = $sender->id;

return $message;
}
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/APIs/V2/UserUnreadCountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public function index(Request $request, CommentModel $commentModel, LikeModel $l

// at 最近三个用户
$atMeUsers = (new AtMessageModel)->query()
->with('user')
->with('sender')
->where('user_id', $user->id)
->limit(3)
->orderBy('id', 'desc')
->limit(3)
->get();
$atLatestTimestamp = ($atMeUsersLatest = $atMeUsers->first()) ? $this->dateTimeToIso8601ZuluString($atMeUsersLatest->{AtMessageModel::CREATED_AT}) : null;

Expand All @@ -122,8 +122,8 @@ public function index(Request $request, CommentModel $commentModel, LikeModel $l
'system' => $lastSystem,
'atme' => $atMeUsers->isEmpty() ? null : [
'users' => $atMeUsers->map(function ($item) {
return $item->user->name ?? null;
})->filter()->all(),
return $item->sender->name ?? null;
})->filter()->unique()->all(),
'latest_at' => $atLatestTimestamp,
],
], function ($item) {
Expand Down
9 changes: 9 additions & 0 deletions app/Models/AtMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public function user(): HasOne
{
return $this->hasOne(User::class, 'id', 'user_id');
}

/**
* The model sender relation.
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function sender(): HasOne
{
return $this->hasOne(User::class, 'id', 'sender_id');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function up()
$table->string('resourceable_type')->comment('资源类型');
$table->integer('resourceable_id')->unsigned()->comment('资源 ID');
$table->integer('user_id')->unsigned()->comment('接受 At Message 消息用户');
$table->integer('sender_id')->unsigned()->comment('发送 At 消息的用户ID');
$table->timestamps();

$table->index('user_id');
Expand Down

0 comments on commit 33cefe3

Please sign in to comment.