Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 65565538 authored by AVINASH GUSAIN's avatar AVINASH GUSAIN
Browse files

Merge branch 'dev/newsletter-integration' into 'main'

newsletter subscription

See merge request !90
parents 3c915028 aeff1236
Loading
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -9,12 +9,14 @@ namespace OCA\EcloudAccounts\Controller;
use Exception;
use OCA\EcloudAccounts\AppInfo\Application;
use OCA\EcloudAccounts\Service\CaptchaService;
use OCA\EcloudAccounts\Service\NewsLetterService;
use OCA\EcloudAccounts\Service\UserService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
@@ -25,11 +27,14 @@ class AccountController extends Controller {
	protected $appName;
	protected $request;
	private $userService;
	private $newsletterService;
	private $captchaService;
	protected $l10nFactory;
	private $session;
	private $userSession;
	private $urlGenerator;
	/** @var IConfig */
	private IConfig $config;
	private const SESSION_USERNAME_CHECK = 'username_check_passed';
	private const CAPTCHA_VERIFIED_CHECK = 'captcha_verified';

@@ -37,19 +42,23 @@ class AccountController extends Controller {
		$AppName,
		IRequest $request,
		UserService $userService,
		NewsLetterService $newsletterService,
		CaptchaService $captchaService,
		IFactory $l10nFactory,
		IUserSession $userSession,
		IURLGenerator $urlGenerator,
		ISession $session
		ISession $session,
		IConfig $config
	) {
		parent::__construct($AppName, $request);
		$this->appName = $AppName;
		$this->userService = $userService;
		$this->newsletterService = $newsletterService;
		$this->captchaService = $captchaService;
		$this->l10nFactory = $l10nFactory;
		$this->session = $session;
		$this->userSession = $userSession;
		$this->config = $config;
		$this->urlGenerator = $urlGenerator;
	}

@@ -86,10 +95,12 @@ class AccountController extends Controller {
	 * @param string $username         User's username
	 * @param string $password         User's password
	 * @param string $language         User's language preference
	 * @param bool $newsletterEos     Users's subscribe to eos newsletter
	 * @param bool $newsletterProduct Users's subscribe to murena product newsletter
	 *
	 * @return \OCP\AppFramework\Http\DataResponse
	 */
	public function create(string $displayname = '', string $recoveryEmail = '', string $username = '', string $password = '', string $language = ''): DataResponse {
	public function create(string $displayname = '', string $recoveryEmail = '', string $username = '', string $password = '', string $language = '', bool $newsletterEos = false, bool $newsletterProduct = false): DataResponse {
		
		$response = new DataResponse();
		
@@ -125,12 +136,12 @@ class AccountController extends Controller {
			$userEmail = $username.'@'.$mainDomain;

			$newUserEntry = $this->userService->registerUser($displayname, $recoveryEmail, $username, $userEmail, $password);
			
			$this->userService->setAccountDataLocally($username, $userEmail, $newUserEntry['quota']);
			$this->userService->createHMEAlias($username, $userEmail);
			$this->userService->createNewDomainAlias($username, $userEmail);
			$this->userService->setTOS($username, true);
			$this->userService->setUserLanguage($username, $language);
			$this->newsletterService->setNewsletterSignup($newsletterEos, $newsletterProduct, $userEmail, $language);
			
			if($recoveryEmail !== '') {
				$this->userService->setUnverifiedRecoveryEmail($username, $recoveryEmail);
+7 −1
Original line number Diff line number Diff line
@@ -82,6 +82,13 @@ class CurlService {
				break;
			case 'POST':
				$options[CURLOPT_POST] = true;
				$jsonContent = in_array('Content-Type: application/json', $headers);
				if ($jsonContent) {
					$params = json_encode($params);
					if (json_last_error() !== JSON_ERROR_NONE) {
						throw new Exception('JSON encoding failed: ' . json_last_error_msg());
					}
				}
				$options[CURLOPT_POSTFIELDS] = $params;
				break;
			case 'DELETE':
@@ -104,7 +111,6 @@ class CurlService {
		

		if ($errno = curl_errno($ch)) {
			var_dump($errno);
			$errorMessage = curl_strerror($errno);
			throw new Exception("Curl error $errno - $errorMessage");
		}
+81 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace OCA\EcloudAccounts\Service;

require __DIR__ . '/../../vendor/autoload.php';

use Exception;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserManager;

class NewsLetterService {
	/** @var IUserManager */
	private $userManager;
	/** @var IConfig */
	private $config;
	/** @var CurlService */
	private $curl;
	/** @var ILogger */
	private $logger;

	public function __construct($appName, IUserManager $userManager, IConfig $config, CurlService $curlService, ILogger $logger) {
		$this->userManager = $userManager;
		$this->config = $config;
		$this->curl = $curlService;
		$this->logger = $logger;
	}

	public function setNewsletterSignup(bool $newsletterEos, bool $newsletterProduct, string $userEmail, string $language): void {
		try {
			if ($newsletterEos || $newsletterProduct) {
				$listIds = [];
				$newsletterListIds = $this->config->getSystemValue('newsletter_list_ids');
				if ($newsletterEos) {
					$listIds[] = $newsletterListIds['eos'];
				}

				if ($newsletterProduct) {
					$listIds[] = $newsletterListIds['product'];
				}
				
				if (!empty($listIds)) {
					
					$this->signupForNewsletter($userEmail, $listIds, $language);
					
				}
			}
		} catch (Exception $e) {
			$this->logger->error('Signup for newsletter failed: ' . $e->getMessage());
		}
	}

	private function signupForNewsletter(string $userEmail, array $listIds, string $userLanguage): void {
		$newsletterApiUrl = $this->config->getSystemValue('newsletter_base_url', '');

		if (empty($newsletterApiUrl)) {
			return;
		}

		$endpoint = '/api/signup';
		$url = $newsletterApiUrl . $endpoint;

		$params = [
			'email' => $userEmail,
			'list_ids' => $listIds,
			'contact_language' => $userLanguage
		];
		$params_string = json_encode($params);
		$headers = [
			'Content-Type: application/json',
			'Content-Length: ' . strlen($params_string)
		];
		$this->curl->post($url, $params, $headers);

		if ($this->curl->getLastStatusCode() !== 200) {
			throw new Exception('Error adding email ' . $userEmail . ' to newsletter app');
		}
	}
}
+4 −2
Original line number Diff line number Diff line
@@ -40,8 +40,8 @@ export default {
				captchaInput: '',
				email: '',
				accepttns: false,
				newsletter_eos: false,
				newsletter_product: false,
				newsletterEos: false,
				newsletterProduct: false,
				selectedLanguage: 'en',
			},
			appName: APPLICATION_NAME,
@@ -74,6 +74,8 @@ export default {
					password: this.formData.password,
					recoveryEmail: this.formData.email,
					language: this.formData.selectedLanguage,
					newsletterEos: this.formData.newsletterEos,
					newsletterProduct: this.formData.newsletterProduct,
				}
				this.submitForm(data)
			}
+2 −2
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@
					<div class="control">
						<span class="action-checkbox">
							<input id="action-newsletter_eos"
								v-model="formData.newsletter_eos"
								v-model="formData.newsletterEos"
								type="checkbox"
								class="checkbox action-checkbox__checkbox focusable">
							<label for="action-newsletter_eos" class="action-checkbox__label">{{ t(appName,'I want to receive news about /e/OS') }}</label>
@@ -139,7 +139,7 @@
					<div class="control">
						<span class="action-checkbox">
							<input id="action-newsletter_product"
								v-model="formData.newsletter_product"
								v-model="formData.newsletterProduct"
								type="checkbox"
								class="checkbox action-checkbox__checkbox focusable">
							<label for="action-newsletter_product" class="action-checkbox__label">{{ t(appName,'I want to receive news about Murena products and promotions') }}</label>