refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s

This commit is contained in:
Kroonk
2026-05-22 16:19:55 +02:00
parent 38cc4c09ca
commit a213a1dba0
2215 changed files with 242567 additions and 6015 deletions

View File

@@ -0,0 +1,120 @@
<?php
namespace App\Services\Contact\Avatar;
use Illuminate\Support\Str;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Cache;
use Laravolt\Avatar\Facade as Avatar;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
class GenerateDefaultAvatar extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'contact_id' => 'required|integer|exists:contacts,id',
];
}
/**
* Generate the default image for the avatar, based on the initals of the
* contact and returns the filename.
*
* @param array $data
* @return Contact
*/
public function execute(array $data)
{
$this->validate($data);
$contact = Contact::find($data['contact_id']);
$contact = $this->generateContactUUID($contact);
// delete existing default avatar
$contact = $this->deleteExistingDefaultAvatar($contact);
// create new avatar
$filename = $this->createNewAvatar($contact);
$contact->avatar_default_url = $filename;
$contact->save();
Cache::forget('etag'.Str::before('?', $filename));
return $contact;
}
/**
* Create an uuid for the contact if it does not exist.
*
* @param Contact $contact
* @return Contact
*/
private function generateContactUUID(Contact $contact)
{
if (! $contact->uuid) {
$contact->uuid = Str::uuid()->toString();
$contact->save();
}
return $contact;
}
/**
* Create a new avatar for the contact based on the name of the contact.
*
* @param Contact $contact
* @return string
*/
private function createNewAvatar(Contact $contact)
{
$img = null;
try {
$img = Avatar::create($contact->name)
->setBackground($contact->default_avatar_color)
->getImageObject()
->encode('jpg');
$filename = 'avatars/'.$contact->uuid.'.jpg';
Storage::disk(config('filesystems.default'))
->put($filename, $img, config('filesystems.default_visibility'));
// This will force the browser to reload the new avatar
return $filename.'?'.now()->format('U');
} finally {
if ($img) {
$img->destroy();
}
}
}
/**
* Delete the existing default avatar.
*
* @param Contact $contact
* @return Contact
*/
private function deleteExistingDefaultAvatar(Contact $contact)
{
if ($contact->avatar_default_url !== null) {
try {
Storage::disk(config('filesystems.default'))
->delete($contact->avatar_default_url);
$contact->avatar_default_url = null;
} catch (FileNotFoundException $e) {
// ignore
}
}
return $contact;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services\Contact\Avatar;
use App\Services\BaseService;
class GetAdorableAvatarURL extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'uuid' => 'required|string',
'size' => 'nullable|integer|between:1,2000',
];
}
/**
* Get an url for an adorable avatar.
* - http://avatars.adorable.io/ gives avatars based on a random string.
*
* @param array $data
* @return string|null
*/
public function execute(array $data)
{
$this->validate($data);
$size = $this->size($data);
return $size.'/'.$data['uuid'].'.png';
}
/**
* Get the size for the avatar, based on a given parameter. Provides a
* default otherwise.
*
* @param array $data
* @return int
*/
private function size(array $data)
{
if (isset($data['size'])) {
return $data['size'];
}
return (int) config('monica.avatar_size');
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Services\Contact\Avatar;
use Illuminate\Support\Str;
use App\Services\BaseService;
use App\Models\Contact\Contact;
class GetAvatarsFromInternet extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'contact_id' => 'required|integer|exists:contacts,id',
];
}
/**
* Query both Gravatar and Adorable Avatars based on the email address of
* the contact.
*
* - http://avatars.adorable.io/ gives avatars based on a random string.
* This random string comes from the `avatar_adorable_uuid` field in the
* Contact object.
* - Gravatar only gives an avatar only if it's set.
*
* @param array $data
* @return Contact
*/
public function execute(array $data): Contact
{
$this->validate($data);
$contact = Contact::findOrFail($data['contact_id']);
$contact = $this->getAdorable($contact);
$contact = $this->getGravatar($contact);
return $contact;
}
/**
* Generate the UUID used to identify the contact in the Adorable service.
*
* @param Contact $contact
* @return Contact
*/
private function generateUUID(Contact $contact)
{
if (empty($contact->avatar_adorable_uuid)) {
$contact->avatar_adorable_uuid = Str::uuid()->toString();
$contact->save();
}
return $contact;
}
/**
* Get the adorable avatar.
*
* @param Contact $contact
* @return Contact
*/
private function getAdorable(Contact $contact)
{
// prevent timestamp update
$timestamps = $contact->timestamps;
$contact->timestamps = false;
$contact = $this->generateUUID($contact);
$contact->avatar_adorable_url = app(GetAdorableAvatarURL::class)->execute([
'uuid' => $contact->avatar_adorable_uuid,
'size' => 200,
]);
$contact->save();
$contact->timestamps = $timestamps;
return $contact;
}
/**
* Query Gravatar (if it exists) for the contact's email address.
*
* @param Contact $contact
* @return Contact
*/
private function getGravatar(Contact $contact)
{
return app(GetGravatar::class)->execute([
'contact_id' => $contact->id,
]);
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace App\Services\Contact\Avatar;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class GetGravatar extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'contact_id' => 'required|integer|exists:contacts,id',
];
}
public function execute(array $data): Contact
{
$this->validate($data);
/** @var Contact */
$contact = Contact::findOrFail($data['contact_id']);
// prevent timestamp update
$timestamps = $contact->timestamps;
$contact->timestamps = false;
$contact = $this->getGravatar($contact);
$contact->save();
$contact->timestamps = $timestamps;
return $contact;
}
/**
* Get the emails of the contact, based on the contact fields.
*
* @param Contact $contact
* @return Collection
*/
private function getEmails(Contact $contact)
{
$emails = collect();
$contactFields = $contact->contactFields()
->email()
->get();
foreach ($contactFields as $contactField) {
try {
$email = $contactField->data;
Validator::make(['email' => $email], ['email' => 'email'])
->validate();
$emails->push($email);
} catch (ModelNotFoundException $e) {
// Not found
} catch (ValidationException $e) {
// Not an email
}
}
return $emails;
}
/**
* Query Gravatar (if it exists) for the contact's email address.
*
* @param Contact $contact
* @return Contact
*/
private function getGravatar(Contact $contact)
{
$emails = $this->getEmails($contact);
$gravatarUrl = null;
foreach ($emails as $email) {
$gravatarUrl = app(GetGravatarURL::class)->execute([
'email' => $email,
'size' => config('monica.avatar_size'),
]);
if ($gravatarUrl) {
break;
}
}
if ($gravatarUrl) {
$contact->avatar_gravatar_url = $gravatarUrl;
} else {
// in this case we need to make sure that we reset the gravatar URL
$contact->avatar_gravatar_url = null;
if ($contact->avatar_source == 'gravatar') {
$contact->avatar_source = 'adorable';
}
}
return $contact;
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Services\Contact\Avatar;
use App\Services\BaseService;
use Illuminate\Support\Facades\App;
use Creativeorange\Gravatar\Facades\Gravatar;
class GetGravatarURL extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email',
'size' => 'nullable|integer|between:1,2000',
];
}
/**
* Get Gravatar, if it exists.
*
* @param array $data
* @return string|null
*/
public function execute(array $data): ?string
{
$this->validate($data);
if ($this->exists($data)) {
$size = $this->size($data);
return Gravatar::get($data['email'], [
'size' => $size,
'secure' => App::environment('production'),
]);
}
return null;
}
/**
* Test given email.
*
* @param array $data
* @return bool
*/
private function exists(array $data)
{
try {
return Gravatar::exists($data['email']);
} catch (\Exception $e) {
// catch invalid email
return false;
}
}
/**
* Get the size for the gravatar, based on a given parameter. Provides a
* default otherwise.
*
* @param array $data
* @return int
*/
private function size(array $data)
{
if (isset($data['size'])) {
return $data['size'];
}
return (int) config('monica.avatar_size');
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Services\Contact\Avatar;
use App\Models\Account\Photo;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use Illuminate\Validation\Rule;
/**
* Update the avatar of the contact.
*/
class UpdateAvatar extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'contact_id' => 'required|integer|exists:contacts,id',
'source' => [
'required',
Rule::in([
'default',
'adorable',
'gravatar',
'photo',
]),
],
'photo_id' => 'required_if:source,photo|integer|exists:photos,id',
];
}
/**
* Update message in a conversation.
*
* @param array $data
* @return Contact
*/
public function execute(array $data): Contact
{
$this->validate($data);
/** @var Contact */
$contact = Contact::where('account_id', $data['account_id'])
->findOrFail($data['contact_id']);
$contact->throwInactive();
if (isset($data['photo_id'])) {
Photo::where('account_id', $data['account_id'])
->findOrFail($data['photo_id']);
}
$contact->avatar_source = $data['source'];
switch ($contact->avatar_source) {
case 'photo':
// in case of a photo, set the photo as the avatar
$contact->avatar_photo_id = $this->nullOrValue($data, 'photo_id');
$contact->photos()->syncWithoutDetaching([$this->nullOrValue($data, 'photo_id')]);
break;
default:
$contact->avatar_photo_id = null;
break;
}
$contact->save();
return $contact;
}
}