70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Jobs\App;
|
||
|
|
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
|
use App\Http\Response\ResponseJson;
|
||
|
|
use App\Services\IMService;
|
||
|
|
use App\Models\Linking;
|
||
|
|
class SyncIMFriend implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
|
use ResponseJson;
|
||
|
|
public $tries = 1;
|
||
|
|
protected $user;
|
||
|
|
/**
|
||
|
|
* Create a new job instance.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function __construct($user)
|
||
|
|
{
|
||
|
|
$this->user = $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the job.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
if (empty($this->user)) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
$user_id = $this->user->id;
|
||
|
|
$links = Linking::with('user','userLink')->where(function($sql) use($user_id){
|
||
|
|
$sql->where('user_id', $user_id)->orWhere('user_linking_id', $user_id);
|
||
|
|
})->where('is_im', 0)->get();
|
||
|
|
foreach ($links as $link) {
|
||
|
|
if (empty($link->user) || empty($link->userLink)) {
|
||
|
|
$link->delete();
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ($user_id == $link->user->id) {
|
||
|
|
$mine = $link->userLink;
|
||
|
|
$user = $link->user;
|
||
|
|
}else{
|
||
|
|
$user = $link->userLink;
|
||
|
|
$mine = $link->user;
|
||
|
|
}
|
||
|
|
$result = $mine->addIMFriend($user);
|
||
|
|
if ($result) {
|
||
|
|
$link->is_im = 1;
|
||
|
|
$link->save();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->getError($e);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|