Skip to content

Commit

Permalink
fixup! feat: mail snippets
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Oct 31, 2024
1 parent 05fa1b7 commit 11fc522
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 80 deletions.
4 changes: 2 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
],
[
'name' => 'snippet#getShares',
'url' => '/api/snippets/share/shares',
'url' => '/api/snippets/share/shares/{id}',
'verb' => 'GET',
],
[
Expand All @@ -517,7 +517,7 @@
],
[
'name' => 'snippet#delete',
'url' => '/api/snippets',
'url' => '/api/snippets/{id}',
'verb' => 'DELETE',
],
[
Expand Down
20 changes: 9 additions & 11 deletions lib/Controller/SnippetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public function __construct(
private SnippetService $snippetService,
) {
parent::__construct(Application::APP_ID, $request);

$this->snippetService = $snippetService;
$this->uid = $userId;

Check warning on line 32 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L31-L32

Added lines #L31 - L32 were not covered by tests
}

Expand Down Expand Up @@ -77,29 +75,29 @@ public function create(string $title, string $content): JsonResponse {

/**
* @NoAdminRequired
* @param int $snippetId
* @param int $id
* @param string $title
* @param string $content
*
* @return JsonResponse
*/
#[TrapError]

Check warning on line 84 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L84

Added line #L84 was not covered by tests
public function update(int $snippetId, string $title, string $content): JsonResponse {
public function update(int $id, string $title, string $content): JsonResponse {

$snippet = $this->snippetService->find($snippetId, $this->uid);
$snippet = $this->snippetService->find($id, $this->uid);

Check failure on line 87 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:87:47: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\SnippetService::find cannot be null, possibly null value provided (see https://psalm.dev/078)

Check warning on line 87 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L87

Added line #L87 was not covered by tests

if ($snippet === null) {

Check failure on line 89 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainNull

lib/Controller/SnippetController.php:89:7: TypeDoesNotContainNull: OCA\Mail\Db\Snippet does not contain null (see https://psalm.dev/090)
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND);

Check warning on line 90 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L89-L90

Added lines #L89 - L90 were not covered by tests
}

$this->snippetService->update($snippetId, $this->uid, $title, $content);
$this->snippetService->update($id, $this->uid, $title, $content);

Check warning on line 93 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L93

Added line #L93 was not covered by tests

return JsonResponse::success($snippet, Http::STATUS_OK);

Check warning on line 95 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L95

Added line #L95 was not covered by tests
}

public function delete($snippetId): JsonResponse {
public function delete(int $id): JsonResponse {

Check warning on line 98 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L98

Added line #L98 was not covered by tests
try {
$this->snippetService->delete($snippetId, $this->uid);
$this->snippetService->delete($id, $this->uid);

Check failure on line 100 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:100:39: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\SnippetService::delete cannot be null, possibly null value provided (see https://psalm.dev/078)
return JsonResponse::success();
} catch (DoesNotExistException $e) {
return JsonResponse::fail('Snippet not found', Http::STATUS_NOT_FOUND);

Check warning on line 103 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L100-L103

Added lines #L100 - L103 were not covered by tests
Expand Down Expand Up @@ -135,14 +133,14 @@ public function share(int $snippetId, string $shareWith, string $type): JsonResp

}

public function getShares($id): JsonResponse {
$snippet = $this->snippetService->find($snippetId, $this->uid);
public function getShares(int $id): JsonResponse {
$snippet = $this->snippetService->find($id, $this->uid);

Check failure on line 137 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:137:47: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\SnippetService::find cannot be null, possibly null value provided (see https://psalm.dev/078)

Check warning on line 137 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L136-L137

Added lines #L136 - L137 were not covered by tests

if ($snippet === null) {

Check failure on line 139 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainNull

lib/Controller/SnippetController.php:139:7: TypeDoesNotContainNull: OCA\Mail\Db\Snippet does not contain null (see https://psalm.dev/090)
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND);

Check warning on line 140 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L139-L140

Added lines #L139 - L140 were not covered by tests
}

$shares = $this->snippetService->getShares($this->uid, $snippetId);
$shares = $this->snippetService->getShares($this->uid, $id);

Check warning on line 143 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L143

Added line #L143 was not covered by tests

return JsonResponse::success($shares);

Check warning on line 145 in lib/Controller/SnippetController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/SnippetController.php#L145

Added line #L145 was not covered by tests
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @method string getOwner()
* @method void setOwner(string $owner)
* @method string geTitle()
* @method string getTitle()
* @method void setTitle(string $title)
* @method string getContent()
* @method void setContent(string $content)
Expand All @@ -37,7 +37,7 @@ public function jsonSerialize() {
return [
'id' => $this->getId(),
'owner' => $this->getOwner(),
'title' => $this->geTitle(),
'title' => $this->getTitle(),
'content' => $this->getContent(),
];

Check warning on line 42 in lib/Db/Snippet.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/Snippet.php#L37-L42

Added lines #L37 - L42 were not covered by tests
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Db/SnippetMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public function findSharedWithMe(string $userId, array $groups): array {
$qb = $this->db->getQueryBuilder();
$qb->select('s.*')
->from($this->getTableName(), 's')
->join('s', 'mail_snippets_shares', 'share', $qb->expr()->eq('s.id', 'sshare.snippet_id'))
->join('s', 'mail_snippets_shares', 'share', $qb->expr()->eq('s.id', 'share.snippet_id'))
->where($qb->expr()->andX(
$qb->expr()->eq('sshare.share_with', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->in('sshare.type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR))
$qb->expr()->eq('share.share_with', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('share.type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR))
))
->orWhere(
$qb->expr()->andX(
$qb->expr()->in('sshare.share_with', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)),
$qb->expr()->in('sshare.type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR))
$qb->expr()->in('share.share_with', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)),
$qb->expr()->eq('share.type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR))
)
);
return $this->findEntities($qb);

Check warning on line 80 in lib/Db/SnippetMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SnippetMapper.php#L65-L80

Added lines #L65 - L80 were not covered by tests
Expand Down
11 changes: 4 additions & 7 deletions lib/Db/SnippetShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ public function findAllShares(string $owner): array {
*
* @return SnippetShare[]
*/
public function findSnippetShares(string $owner, int $snippetId): array {
public function findSnippetShares(int $snippetId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('sshare.*')
->from($this->getTableName(), 'sshare')
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('s.owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR))
)
->andWhere(
$qb->expr()->eq('sshare.snippet_id', $qb->createNamedParameter($snippetId, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('snippet_id', $qb->createNamedParameter($snippetId, IQueryBuilder::PARAM_INT))
);

Check warning on line 91 in lib/Db/SnippetShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SnippetShareMapper.php#L85-L91

Added lines #L85 - L91 were not covered by tests

return $this->findEntities($qb);

Check warning on line 93 in lib/Db/SnippetShareMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SnippetShareMapper.php#L93

Added line #L93 was not covered by tests
Expand Down
9 changes: 5 additions & 4 deletions lib/Service/SnippetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class SnippetService {
/** @var IGroupManager */
private $groupManager;

public function __construct(SnippetMapper $snippetMapper, SnippetShareMapper $snippetShareMapper, IUserManager $userManager, IGroupManager $groupManager) {
public function __construct(SnippetMapper $snippetMapper, SnippetShareMapper $snippetShareMapper, IGroupManager $groupManager, IUserManager $userManager) {
$this->snippetMapper = $snippetMapper;
$this->snippetShareMapper = $snippetShareMapper;
$this->userManager = $userManager;
$this->$groupManager = $groupManager;
$this->groupManager = $groupManager;

Check warning on line 39 in lib/Service/SnippetService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SnippetService.php#L35-L39

Added lines #L35 - L39 were not covered by tests
}

/**
Expand All @@ -52,7 +52,8 @@ public function findAll(string $userId): array {
* @return Snippet[]
*/
public function findAllSharedWithMe(string $userId): array {
$groups = $this->groupManager->getUserGroupIds($userId);
$user = $this->userManager->get($userId);
$groups = $this->groupManager->getUserGroupIds($user);
return $this->snippetMapper->findSharedWithMe($userId, $groups);

Check warning on line 57 in lib/Service/SnippetService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SnippetService.php#L54-L57

Added lines #L54 - L57 were not covered by tests
}
/**
Expand Down Expand Up @@ -128,7 +129,7 @@ public function share(int $snippetId, string $shareWith): void {
}

public function getShares(string $uid, int $snippetId): array {
return $this->snippetShareMapper->findSnippetShares($uid, $snippetId);
return $this->snippetShareMapper->findSnippetShares($snippetId);

Check warning on line 132 in lib/Service/SnippetService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SnippetService.php#L131-L132

Added lines #L131 - L132 were not covered by tests
}

/**
Expand Down
61 changes: 55 additions & 6 deletions src/components/AppSettingsMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,26 @@
</dl>
</NcAppSettingsSection>
<NcAppSettingsSection id="snippets" :name="t('mail', 'Snippets')">
<NcButton type="primary" @click="() => snippetDialogOpen = true">
{{ t('mail', 'Create new snippet') }}
</NcButton>
<h6>{{ t('mail','My snippets') }}</h6>
<List snippets="mySnippets" />
<List :snippets="mySnippets" />
<h6>{{ t('mail','Shared with me') }}</h6>
<List snippets="shareSnippet"
<List :snippets="sharedSnippet"
:shared="true" />
</NcAppSettingsSection>
<NcDialog :open.sync="snippetDialogOpen"
:name="t('mail','New snippet')"
:is-form="true"
:buttons="snippetButtons"
size="normal">
<NcInputField :value.sync="localSnippet.title" :label="t('mail','Title of the snippet')" />
<NcTextArea rows="7"
:value.sync="localSnippet.content"
:label="t('mail','Content of the snippet')"
resize="horizontal" />
</NcDialog>
</NcAppSettingsDialog>
</div>
</template>
Expand All @@ -307,7 +321,7 @@ import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import CompactMode from 'vue-material-design-icons/ReorderHorizontal.vue'

import { NcAppSettingsSection, NcAppSettingsDialog, NcButton, NcLoadingIcon as IconLoading, NcCheckboxRadioSwitch } from '@nextcloud/vue'
import { NcAppSettingsSection, NcAppSettingsDialog, NcButton, NcLoadingIcon as IconLoading, NcCheckboxRadioSwitch, NcDialog, NcInputField, NcTextArea } from '@nextcloud/vue'

import IconAdd from 'vue-material-design-icons/Plus.vue'
import IconEmail from 'vue-material-design-icons/Email.vue'
Expand All @@ -321,6 +335,8 @@ import InternalAddress from './InternalAddress.vue'
import isMobile from '@nextcloud/vue/dist/Mixins/isMobile.js'
import { mapGetters } from 'vuex'
import List from './snippets/List.vue'
import IconCancel from '@mdi/svg/svg/cancel.svg?raw'

Check failure on line 338 in src/components/AppSettingsMenu.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Unable to resolve path to module '@mdi/svg/svg/cancel.svg?raw'
import IconCheck from '@mdi/svg/svg/check.svg?raw'

Check failure on line 339 in src/components/AppSettingsMenu.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Unable to resolve path to module '@mdi/svg/svg/check.svg?raw'

export default {
name: 'AppSettingsMenu',
Expand All @@ -340,6 +356,9 @@ export default {
VerticalSplit,
HorizontalSplit,
List,
NcDialog,
NcInputField,
NcTextArea,
},
mixins: [isMobile],
props: {
Expand Down Expand Up @@ -372,6 +391,32 @@ export default {
showMailSettings: true,
selectedAccount: null,
mailvelopeIsAvailable: false,
snippetDialogOpen: false,
localSnippet: {
title: '',
content: '',
},
snippetButtons: [
{
label: 'Cancel',
icon: IconCancel,
callback: () => {
this.snippetDialogOpen = false
this.localSnippet = {
title: '',
content: '',
}
},
},
{
label: 'Ok',
type: 'primary',
icon: IconCheck,
callback: () => {
this.$store.dispatch('createSnippet', { ...this.localSnippet })
},
},
],
}
},
computed: {
Expand Down Expand Up @@ -409,10 +454,10 @@ export default {
return this.$store.getters.getPreference('layout-mode', 'vertical-split')
},
mySnippets() {
return this.$store.getters.getSnippets()
return this.$store.getters.getMySnippets
},
shareSnippet() {
return this.$store.getters.getSharedSnippets()
sharedSnippet() {
return this.$store.getters.getSharedSnippets
},
},
watch: {
Expand All @@ -430,6 +475,10 @@ export default {
mounted() {
this.sortOrder = this.$store.getters.getPreference('sort-order', 'newest')
document.addEventListener.call(window, 'mailvelope', () => this.checkMailvelope())
if (!this.$store.getters.areSnippetsFetched) {
this.$store.dispatch('fetchMySnippets')
this.$store.dispatch('fetchSharedSnippets')
}
},
updated() {
this.checkMailvelope()
Expand Down
2 changes: 1 addition & 1 deletion src/components/Composer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ export default {
if (this.sendAt && this.isSendAtCustom) {
this.selectedDate = new Date(this.sendAt)
}
if (this.snippets.length === 0) {
if (!this.$store.getters.areSnippetsFetched) {
this.$store.dispatch('fetchSharedSnippets')
this.$store.dispatch('fetchMySnippets')
}
Expand Down
Loading

0 comments on commit 11fc522

Please sign in to comment.