Skip to content

Commit 11fc522

Browse files
committed
fixup! feat: mail snippets
Signed-off-by: Hamza Mahjoubi <[email protected]>
1 parent 05fa1b7 commit 11fc522

14 files changed

+157
-80
lines changed

appinfo/routes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@
502502
],
503503
[
504504
'name' => 'snippet#getShares',
505-
'url' => '/api/snippets/share/shares',
505+
'url' => '/api/snippets/share/shares/{id}',
506506
'verb' => 'GET',
507507
],
508508
[
@@ -517,7 +517,7 @@
517517
],
518518
[
519519
'name' => 'snippet#delete',
520-
'url' => '/api/snippets',
520+
'url' => '/api/snippets/{id}',
521521
'verb' => 'DELETE',
522522
],
523523
[

lib/Controller/SnippetController.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public function __construct(
2929
private SnippetService $snippetService,
3030
) {
3131
parent::__construct(Application::APP_ID, $request);
32-
33-
$this->snippetService = $snippetService;
3432
$this->uid = $userId;
3533
}
3634

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

7876
/**
7977
* @NoAdminRequired
80-
* @param int $snippetId
78+
* @param int $id
8179
* @param string $title
8280
* @param string $content
8381
*
8482
* @return JsonResponse
8583
*/
8684
#[TrapError]
87-
public function update(int $snippetId, string $title, string $content): JsonResponse {
85+
public function update(int $id, string $title, string $content): JsonResponse {
8886

89-
$snippet = $this->snippetService->find($snippetId, $this->uid);
87+
$snippet = $this->snippetService->find($id, $this->uid);
9088

9189
if ($snippet === null) {
9290
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND);
9391
}
9492

95-
$this->snippetService->update($snippetId, $this->uid, $title, $content);
93+
$this->snippetService->update($id, $this->uid, $title, $content);
9694

9795
return JsonResponse::success($snippet, Http::STATUS_OK);
9896
}
9997

100-
public function delete($snippetId): JsonResponse {
98+
public function delete(int $id): JsonResponse {
10199
try {
102-
$this->snippetService->delete($snippetId, $this->uid);
100+
$this->snippetService->delete($id, $this->uid);
103101
return JsonResponse::success();
104102
} catch (DoesNotExistException $e) {
105103
return JsonResponse::fail('Snippet not found', Http::STATUS_NOT_FOUND);
@@ -135,14 +133,14 @@ public function share(int $snippetId, string $shareWith, string $type): JsonResp
135133

136134
}
137135

138-
public function getShares($id): JsonResponse {
139-
$snippet = $this->snippetService->find($snippetId, $this->uid);
136+
public function getShares(int $id): JsonResponse {
137+
$snippet = $this->snippetService->find($id, $this->uid);
140138

141139
if ($snippet === null) {
142140
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND);
143141
}
144142

145-
$shares = $this->snippetService->getShares($this->uid, $snippetId);
143+
$shares = $this->snippetService->getShares($this->uid, $id);
146144

147145
return JsonResponse::success($shares);
148146
}

lib/Db/Snippet.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @method string getOwner()
1818
* @method void setOwner(string $owner)
19-
* @method string geTitle()
19+
* @method string getTitle()
2020
* @method void setTitle(string $title)
2121
* @method string getContent()
2222
* @method void setContent(string $content)
@@ -37,7 +37,7 @@ public function jsonSerialize() {
3737
return [
3838
'id' => $this->getId(),
3939
'owner' => $this->getOwner(),
40-
'title' => $this->geTitle(),
40+
'title' => $this->getTitle(),
4141
'content' => $this->getContent(),
4242
];
4343
}

lib/Db/SnippetMapper.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public function findSharedWithMe(string $userId, array $groups): array {
6666
$qb = $this->db->getQueryBuilder();
6767
$qb->select('s.*')
6868
->from($this->getTableName(), 's')
69-
->join('s', 'mail_snippets_shares', 'share', $qb->expr()->eq('s.id', 'sshare.snippet_id'))
69+
->join('s', 'mail_snippets_shares', 'share', $qb->expr()->eq('s.id', 'share.snippet_id'))
7070
->where($qb->expr()->andX(
71-
$qb->expr()->eq('sshare.share_with', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
72-
$qb->expr()->in('sshare.type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR))
71+
$qb->expr()->eq('share.share_with', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
72+
$qb->expr()->eq('share.type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR))
7373
))
7474
->orWhere(
7575
$qb->expr()->andX(
76-
$qb->expr()->in('sshare.share_with', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)),
77-
$qb->expr()->in('sshare.type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR))
76+
$qb->expr()->in('share.share_with', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)),
77+
$qb->expr()->eq('share.type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR))
7878
)
7979
);
8080
return $this->findEntities($qb);

lib/Db/SnippetShareMapper.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,12 @@ public function findAllShares(string $owner): array {
8282
*
8383
* @return SnippetShare[]
8484
*/
85-
public function findSnippetShares(string $owner, int $snippetId): array {
85+
public function findSnippetShares(int $snippetId): array {
8686
$qb = $this->db->getQueryBuilder();
87-
$qb->select('sshare.*')
88-
->from($this->getTableName(), 'sshare')
87+
$qb->select('*')
88+
->from($this->getTableName())
8989
->where(
90-
$qb->expr()->eq('s.owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR))
91-
)
92-
->andWhere(
93-
$qb->expr()->eq('sshare.snippet_id', $qb->createNamedParameter($snippetId, IQueryBuilder::PARAM_INT))
90+
$qb->expr()->eq('snippet_id', $qb->createNamedParameter($snippetId, IQueryBuilder::PARAM_INT))
9491
);
9592

9693
return $this->findEntities($qb);

lib/Service/SnippetService.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class SnippetService {
3232
/** @var IGroupManager */
3333
private $groupManager;
3434

35-
public function __construct(SnippetMapper $snippetMapper, SnippetShareMapper $snippetShareMapper, IUserManager $userManager, IGroupManager $groupManager) {
35+
public function __construct(SnippetMapper $snippetMapper, SnippetShareMapper $snippetShareMapper, IGroupManager $groupManager, IUserManager $userManager) {
3636
$this->snippetMapper = $snippetMapper;
3737
$this->snippetShareMapper = $snippetShareMapper;
3838
$this->userManager = $userManager;
39-
$this->$groupManager = $groupManager;
39+
$this->groupManager = $groupManager;
4040
}
4141

4242
/**
@@ -52,7 +52,8 @@ public function findAll(string $userId): array {
5252
* @return Snippet[]
5353
*/
5454
public function findAllSharedWithMe(string $userId): array {
55-
$groups = $this->groupManager->getUserGroupIds($userId);
55+
$user = $this->userManager->get($userId);
56+
$groups = $this->groupManager->getUserGroupIds($user);
5657
return $this->snippetMapper->findSharedWithMe($userId, $groups);
5758
}
5859
/**
@@ -128,7 +129,7 @@ public function share(int $snippetId, string $shareWith): void {
128129
}
129130

130131
public function getShares(string $uid, int $snippetId): array {
131-
return $this->snippetShareMapper->findSnippetShares($uid, $snippetId);
132+
return $this->snippetShareMapper->findSnippetShares($snippetId);
132133
}
133134

134135
/**

src/components/AppSettingsMenu.vue

+55-6
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,26 @@
292292
</dl>
293293
</NcAppSettingsSection>
294294
<NcAppSettingsSection id="snippets" :name="t('mail', 'Snippets')">
295+
<NcButton type="primary" @click="() => snippetDialogOpen = true">
296+
{{ t('mail', 'Create new snippet') }}
297+
</NcButton>
295298
<h6>{{ t('mail','My snippets') }}</h6>
296-
<List snippets="mySnippets" />
299+
<List :snippets="mySnippets" />
297300
<h6>{{ t('mail','Shared with me') }}</h6>
298-
<List snippets="shareSnippet"
301+
<List :snippets="sharedSnippet"
299302
:shared="true" />
300303
</NcAppSettingsSection>
304+
<NcDialog :open.sync="snippetDialogOpen"
305+
:name="t('mail','New snippet')"
306+
:is-form="true"
307+
:buttons="snippetButtons"
308+
size="normal">
309+
<NcInputField :value.sync="localSnippet.title" :label="t('mail','Title of the snippet')" />
310+
<NcTextArea rows="7"
311+
:value.sync="localSnippet.content"
312+
:label="t('mail','Content of the snippet')"
313+
resize="horizontal" />
314+
</NcDialog>
301315
</NcAppSettingsDialog>
302316
</div>
303317
</template>
@@ -307,7 +321,7 @@ import { generateUrl } from '@nextcloud/router'
307321
import { showError } from '@nextcloud/dialogs'
308322
import CompactMode from 'vue-material-design-icons/ReorderHorizontal.vue'
309323

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

312326
import IconAdd from 'vue-material-design-icons/Plus.vue'
313327
import IconEmail from 'vue-material-design-icons/Email.vue'
@@ -321,6 +335,8 @@ import InternalAddress from './InternalAddress.vue'
321335
import isMobile from '@nextcloud/vue/dist/Mixins/isMobile.js'
322336
import { mapGetters } from 'vuex'
323337
import List from './snippets/List.vue'
338+
import IconCancel from '@mdi/svg/svg/cancel.svg?raw'
339+
import IconCheck from '@mdi/svg/svg/check.svg?raw'
324340

325341
export default {
326342
name: 'AppSettingsMenu',
@@ -340,6 +356,9 @@ export default {
340356
VerticalSplit,
341357
HorizontalSplit,
342358
List,
359+
NcDialog,
360+
NcInputField,
361+
NcTextArea,
343362
},
344363
mixins: [isMobile],
345364
props: {
@@ -372,6 +391,32 @@ export default {
372391
showMailSettings: true,
373392
selectedAccount: null,
374393
mailvelopeIsAvailable: false,
394+
snippetDialogOpen: false,
395+
localSnippet: {
396+
title: '',
397+
content: '',
398+
},
399+
snippetButtons: [
400+
{
401+
label: 'Cancel',
402+
icon: IconCancel,
403+
callback: () => {
404+
this.snippetDialogOpen = false
405+
this.localSnippet = {
406+
title: '',
407+
content: '',
408+
}
409+
},
410+
},
411+
{
412+
label: 'Ok',
413+
type: 'primary',
414+
icon: IconCheck,
415+
callback: () => {
416+
this.$store.dispatch('createSnippet', { ...this.localSnippet })
417+
},
418+
},
419+
],
375420
}
376421
},
377422
computed: {
@@ -409,10 +454,10 @@ export default {
409454
return this.$store.getters.getPreference('layout-mode', 'vertical-split')
410455
},
411456
mySnippets() {
412-
return this.$store.getters.getSnippets()
457+
return this.$store.getters.getMySnippets
413458
},
414-
shareSnippet() {
415-
return this.$store.getters.getSharedSnippets()
459+
sharedSnippet() {
460+
return this.$store.getters.getSharedSnippets
416461
},
417462
},
418463
watch: {
@@ -430,6 +475,10 @@ export default {
430475
mounted() {
431476
this.sortOrder = this.$store.getters.getPreference('sort-order', 'newest')
432477
document.addEventListener.call(window, 'mailvelope', () => this.checkMailvelope())
478+
if (!this.$store.getters.areSnippetsFetched) {
479+
this.$store.dispatch('fetchMySnippets')
480+
this.$store.dispatch('fetchSharedSnippets')
481+
}
433482
},
434483
updated() {
435484
this.checkMailvelope()

src/components/Composer.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ export default {
993993
if (this.sendAt && this.isSendAtCustom) {
994994
this.selectedDate = new Date(this.sendAt)
995995
}
996-
if (this.snippets.length === 0) {
996+
if (!this.$store.getters.areSnippetsFetched) {
997997
this.$store.dispatch('fetchSharedSnippets')
998998
this.$store.dispatch('fetchMySnippets')
999999
}

0 commit comments

Comments
 (0)