added remote db source
This commit is contained in:
@@ -272,7 +272,11 @@ class Email extends Model
|
||||
|
||||
public static function parseEmail($email, $deleted = []): array
|
||||
{
|
||||
$messages = self::fetchEmailFromDB($email);
|
||||
if (config('app.fetch_from_remote_db')) {
|
||||
$messages = RemoteEmail::fetchEmailFromDB($email);
|
||||
} else {
|
||||
$messages = self::fetchEmailFromDB($email);
|
||||
}
|
||||
$limit = json_decode(config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
|
||||
$count = 1;
|
||||
$response = [
|
||||
@@ -282,9 +286,18 @@ class Email extends Model
|
||||
|
||||
foreach ($messages as $message) {
|
||||
|
||||
// fix for null attachments
|
||||
if ($message['attachments'] === null) {
|
||||
$message['attachments'] = [];
|
||||
}
|
||||
|
||||
if (in_array($message['message_id'], $deleted)) {
|
||||
// If it exists, delete the matching record from the 'emails' table
|
||||
Email::where('message_id', $message['message_id'])->delete();
|
||||
if (config('app.fetch_from_remote_db')) {
|
||||
RemoteEmail::where('message_id', $message['message_id'])->delete();
|
||||
} else {
|
||||
Email::where('message_id', $message['message_id'])->delete();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -331,7 +344,11 @@ class Email extends Model
|
||||
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
Email::where('message_id', $message['message_id'])->update(['is_seen' => true]);
|
||||
if (config('app.fetch_from_remote_db')) {
|
||||
RemoteEmail::where('message_id', $message['message_id'])->update(['is_seen' => true]);
|
||||
} else {
|
||||
Email::where('message_id', $message['message_id'])->update(['is_seen' => true]);
|
||||
}
|
||||
if (++$count > $limit) {
|
||||
break;
|
||||
}
|
||||
@@ -404,7 +421,11 @@ class Email extends Model
|
||||
|
||||
public static function mailToDBStatus(): bool
|
||||
{
|
||||
$latestRecord = self::orderBy('timestamp', 'desc')->first();
|
||||
if (config('app.fetch_from_remote_db')) {
|
||||
$latestRecord = RemoteEmail::orderBy('timestamp', 'desc')->first();
|
||||
} else {
|
||||
$latestRecord = self::orderBy('timestamp', 'desc')->first();
|
||||
}
|
||||
if (!$latestRecord) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use Carbon\Carbon;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use function Laravel\Prompts\confirm;
|
||||
|
||||
class PremiumEmail extends Model
|
||||
{
|
||||
@@ -44,7 +45,11 @@ class PremiumEmail extends Model
|
||||
public static function createEmail($message, $email): void
|
||||
{
|
||||
$initialData = $message;
|
||||
$utcTime = CarbonImmutable::instance($message['timestamp'])->setTimezone('UTC')->toDateTimeString();
|
||||
if (config('app.fetch_from_db') && config('app.fetch_from_remote_db')) {
|
||||
$utcTime = CarbonImmutable::parse($message['timestamp'])->setTimezone('UTC')->toDateTimeString();
|
||||
} else {
|
||||
$utcTime = CarbonImmutable::instance($message['timestamp'])->setTimezone('UTC')->toDateTimeString();
|
||||
}
|
||||
$messageId = Carbon::parse($utcTime)->format('Ymd').$initialData['id'];
|
||||
$userId = \auth()->user()->id;
|
||||
$exists = PremiumEmail::where('user_id', $userId)->where('message_id', $messageId)->exists();
|
||||
|
||||
24
app/Models/RemoteEmail.php
Normal file
24
app/Models/RemoteEmail.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class RemoteEmail extends Model
|
||||
{
|
||||
protected $connection = 'mysql_remote';
|
||||
protected $table = 'emails';
|
||||
|
||||
public static function fetchEmailFromDB($email)
|
||||
{
|
||||
$validator = Validator::make(['email' => $email], [
|
||||
'email' => 'required|email'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return [];
|
||||
}
|
||||
return self::whereJsonContains('to', $email)->orderBy('timestamp', 'desc')->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user