Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/Http/Controllers/Api/TimelineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function getIndex(Request $request)
if ($item->imgs) {
$item->imgs = TimelineImg::getImgs($item->imgs);
}
})->reject(function($item){
if (Auth::user()->check()) {
$user = Auth::user()->user();
//remove blocked
$timelines = $timelines->reject(function($item)use($user){
return $user->isBlocked($item->author->id);
});
}
})->toArray();

return $timelines;
Expand Down
196 changes: 196 additions & 0 deletions app/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,200 @@ public function anyGetVerificationCode(Request $request)

return ['get verification code successful'];
}

/**
* Get the user following list
*
* @return array of User model or failure info
*/
public function getFollowing(Request $request)
{
$user = User::findOrFail($request->input('id'));
return $user->following;
}

/**
* Get the user's followers list
*
* @return array of User model or failure info
*/
public function getFollowers(Request $request)
{
$user = User::findOrFail($request->input('id'));
return $user->followers;
}

/**
* Add a following relationship
*
* @param \Illuminate\Http\Request $request
* @return array of status model or failure info
*/
public function postFollowing(Request $request)
{
if (Auth::user()->check()) {
$user = Auth::user()->user();

if ($user->id == $request->input('toUserId')) {
$result = ['status' => 'cannot_follow_yourself'];
} else {
$exist = $user->following()
->where('to_user_id', $request->input('toUserId'))
->first();
if (!$exist) {
$toUser = User::findOrFail($request->input('toUserId'));
$user->following()->attach($toUser);
$result = ['status' => 'success'];
} else {
$result = ['status' => 'has_been_following'];
}
}
return $result;
} else {
return ['status' => 'not_login'];
}
}

/**
* Remove a following relationship
*
* @param \Illuminate\Http\Request $request
* @return array of status model or failure info
*/
public function postUnFollowing(Request $request)
{
if (Auth::user()->check()) {
$user = Auth::user()->user();

$exist = $user->following()
->where('to_user_id', $request->input('toUserId'))
->first();
if (!$exist) {
$result = ['status' => 'success'];
} else {
$toUser = User::findOrFail($request->input('toUserId'));
$user->following()->detach($toUser);
$result = ['status' => 'success'];
}

return $result;
} else {
return ['status' => 'not_login'];
}
}

/**
* Is userId followed by current user.
*
* @param \Illuminate\Http\Request $request
* @return array of status or failure info
*/
public function postIsFollowed(Request $request)
{
if (Auth::user()->check()) {
$user = Auth::user()->user();
if ($user->isFollowed($request->input('toUserId'))) {
return ['status' => 'success'];
} else {
return ['status' => 'failed'];
}
} else {
return ['status' => 'not_login'];
}
}

/**
* Is userId blocked by current user.
*
* @param \Illuminate\Http\Request $request
* @return array of status or failure info
*/
public function postIsBlocked(Request $request)
{
if (Auth::user()->check()) {
$user = Auth::user()->user();
if ($user->isBlocked($request->input('toUserId'))) {
return ['status' => 'success'];
} else {
return ['status' => 'failed'];
}
} else {
return ['status' => 'not_login'];
}
}

/**
* Add a block user
*
* @param \Illuminate\Http\Request $request
* @return object User model or failure info
*/
public function postBlock(Request $request)
{
if (Auth::user()->check()) {
$user = Auth::user()->user();
if ($user->id == $request->input('toUserId')) {
return ['status' => 'cannot_block_yourself'];
}
if ($user->toBlock($request->input('toUserId'))) {
return ['status' => 'success'];
} else {
return ['status' => 'failed'];
}
} else {
return ['status' => 'not_login'];
}
}

/**
* unblock a user
*
* @param \Illuminate\Http\Request $request
* @return object User model or failure info
*/
public function postUnBlock(Request $request)
{
if (Auth::user()->check()) {
$user = Auth::user()->user();
if ($user->unBlock($request->input('toUserId'))) {
return ['status' => 'success'];
} else {
return ['status' => 'failed'];
}
} else {
return ['status' => 'not_login'];
}
}

/**
* get block list
*
* @return array of object User model or failure info
*/
public function getBlock()
{
if (Auth::user()->check()) {
$user = Auth::user()->user();
$blockList = $user->getBlock();
return $blockList;
} else {
return ['status' => 'not_login'];
}
}

/**
* get block list
*
* @return array of relationship nums or failure info
*/
public function getRelationshipNums(Request $request)
{
$user = User::findOrFail($request->input('id'));
$result = [
'follower' => count($user->followers),
'following' => count($user->following),
'block' => count($user->getBlock())
];
return $result;
}
}
26 changes: 24 additions & 2 deletions app/Http/Controllers/TimelineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,31 @@ public function __construct()
public function getIndex()
{
$timelines = Timeline::latest()->paginate();
$users = User::limit(5)->orderByRaw('RAND()')->get();
if (Auth::user()->check()) {
$user = Auth::user()->user();
//remove blocked
$timelines = $timelines->reject(function($item)use($user){
return $user->isBlocked($item->author->id);
});
//remove myself
$userModel = User::where('id', '!=', $user->id);
} else {
$userModel = User;
}

$currentUser = isset($user) ? $user : null;

$users = $userModel->limit(5)
->orderByRaw('RAND()')
->get();
//remove blocked
if (Auth::user()->check()) {
$users = $users->reject(function($item)use($user){
return $user->isBlocked($item->id);
});
}

return view('timeline.index', compact('timelines', 'users'));
return view('timeline.index', compact('timelines', 'users', 'currentUser'));
}

/**
Expand Down
103 changes: 103 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ public function timelineComments()
return $this->hasMany('App\TimelineComment', 'user_id')->orderBy('created_at', 'desc')->with('author');
}

/**
* The user's following list
*/
public function following()
{
return $this->belongsToMany('App\User', 'user_relationship', 'from_user_id', 'to_user_id')
->withTimestamps();
}

/**
* The user's follower list
*/
public function followers()
{
return $this->belongsToMany('App\User', 'user_relationship', 'to_user_id', 'from_user_id')
->withTimestamps();
}

/**
* The user's relationship
*/
public function userRelationship()
{
return $this->hasMany('App\UserRelationship', 'from_user_id');
}

/*
*
*/
Expand Down Expand Up @@ -91,4 +117,81 @@ public function getAvatarAttribute($url)
{
return \App\Helpers\FileSystem::getFullUrl($url);
}

/**
* Is someone followed by current user?
* @param int $toUserId
*/
public function isFollowed($toUserId)
{
$relationship = $this->userRelationship()
->where('to_user_id', $toUserId)
->first();
return $relationship ? true : false;
}

/**
* Is someone blocked by current user?
* @param int $toUserId
*/
public function isBlocked($toUserId)
{
$relationship = $this->userRelationship()
->where('to_user_id', $toUserId)
->where('is_block', UserRelationship::BLOCKED)
->first();
return $relationship ? true : false;
}

/**
* Block someone
* @param int $toUserId
*/
public function toBlock($toUserId)
{
$relationship = UserRelationship::where([
'from_user_id' => $this->id,
'to_user_id' => $toUserId,
])
->first();
if (!$relationship) {
$relationship = new UserRelationship;
$relationship->from_user_id = $this->id;
$relationship->to_user_id = $toUserId;
$relationship->is_block = UserRelationship::BLOCKED;
} else {
$relationship->is_block = UserRelationship::BLOCKED;
}
return $relationship->save();
}

/**
* Unblock someone
* @param int $userId
*/
public function unBlock($userId)
{
$relationship = UserRelationship::where([
'from_user_id' => $this->id,
'to_user_id' => $userId,
])
->firstOrFail();
$relationship->is_block = UserRelationship::UNBLOCKED;
return $relationship->save();
}

/**
* Get block list
*/
public function getBlock()
{
$relationship = $this->userRelationship()
->where('from_user_id', $this->id)
->where('is_block', UserRelationship::BLOCKED)
->get();
$relationship = $relationship->map(function($item){
return $item->toUser;
});
return $relationship;
}
}
31 changes: 31 additions & 0 deletions app/UserRelationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserRelationship extends Model
{
use SoftDeletes;

const BLOCKED = 1;
const UNBLOCKED = 0;

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'user_relationship';

public function fromUser()
{
return $this->belongsTo('App\User', 'from_user_id');
}

public function toUser()
{
return $this->belongsTo('App\User', 'to_user_id');
}
}
Loading