aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-09 16:23:01 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commitdd0ebb715123dfa126a82d4e4fe3a04064ae77b8 (patch)
treee0741f35b31c66f09f7d9ad808b224ef86151bb1 /shared/extra-utils/users
parent9293139fde7091e9badcafa9b570b83cea9a10ad (diff)
downloadPeerTube-dd0ebb715123dfa126a82d4e4fe3a04064ae77b8.tar.gz
PeerTube-dd0ebb715123dfa126a82d4e4fe3a04064ae77b8.tar.zst
PeerTube-dd0ebb715123dfa126a82d4e4fe3a04064ae77b8.zip
Introduce notifications command
Diffstat (limited to 'shared/extra-utils/users')
-rw-r--r--shared/extra-utils/users/index.ts3
-rw-r--r--shared/extra-utils/users/notifications-command.ts87
-rw-r--r--shared/extra-utils/users/notifications.ts (renamed from shared/extra-utils/users/user-notifications.ts)129
3 files changed, 114 insertions, 105 deletions
diff --git a/shared/extra-utils/users/index.ts b/shared/extra-utils/users/index.ts
index 9f760d7fd..ed166c756 100644
--- a/shared/extra-utils/users/index.ts
+++ b/shared/extra-utils/users/index.ts
@@ -2,6 +2,7 @@ export * from './accounts-command'
2export * from './accounts' 2export * from './accounts'
3export * from './blocklist-command' 3export * from './blocklist-command'
4export * from './login' 4export * from './login'
5export * from './user-notifications' 5export * from './notifications'
6export * from './notifications-command'
6export * from './subscriptions-command' 7export * from './subscriptions-command'
7export * from './users' 8export * from './users'
diff --git a/shared/extra-utils/users/notifications-command.ts b/shared/extra-utils/users/notifications-command.ts
new file mode 100644
index 000000000..dfe574ca1
--- /dev/null
+++ b/shared/extra-utils/users/notifications-command.ts
@@ -0,0 +1,87 @@
1import { ResultList } from '@shared/models'
2import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
3import { UserNotification, UserNotificationSetting } from '../../models/users'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class NotificationsCommand extends AbstractCommand {
7
8 updateMySettings (options: OverrideCommandOptions & {
9 settings: UserNotificationSetting
10 }) {
11 const path = '/api/v1/users/me/notification-settings'
12
13 return this.putBodyRequest({
14 ...options,
15
16 path,
17 fields: options.settings,
18 implicitToken: true,
19 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
20 })
21 }
22
23 list (options: OverrideCommandOptions & {
24 start?: number
25 count?: number
26 unread?: boolean
27 sort?: string
28 }) {
29 const { start, count, unread, sort = '-createdAt' } = options
30 const path = '/api/v1/users/me/notifications'
31
32 return this.getRequestBody<ResultList<UserNotification>>({
33 ...options,
34
35 path,
36 query: {
37 start,
38 count,
39 sort,
40 unread
41 },
42 implicitToken: true,
43 defaultExpectedStatus: HttpStatusCode.OK_200
44 })
45 }
46
47 markAsRead (options: OverrideCommandOptions & {
48 ids: number[]
49 }) {
50 const { ids } = options
51 const path = '/api/v1/users/me/notifications/read'
52
53 return this.postBodyRequest({
54 ...options,
55
56 path,
57 fields: { ids },
58 implicitToken: true,
59 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
60 })
61 }
62
63 markAsReadAll (options: OverrideCommandOptions) {
64 const path = '/api/v1/users/me/notifications/read-all'
65
66 return this.postBodyRequest({
67 ...options,
68
69 path,
70 implicitToken: true,
71 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
72 })
73 }
74
75 async getLastest (options: OverrideCommandOptions = {}) {
76 const { total, data } = await this.list({
77 ...options,
78 start: 0,
79 count: 1,
80 sort: '-createdAt'
81 })
82
83 if (total === 0) return undefined
84
85 return data[0]
86 }
87}
diff --git a/shared/extra-utils/users/user-notifications.ts b/shared/extra-utils/users/notifications.ts
index 961cfcc0f..81f0729fa 100644
--- a/shared/extra-utils/users/user-notifications.ts
+++ b/shared/extra-utils/users/notifications.ts
@@ -3,86 +3,32 @@
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { inspect } from 'util' 4import { inspect } from 'util'
5import { AbuseState, PluginType } from '@shared/models' 5import { AbuseState, PluginType } from '@shared/models'
6import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users' 6import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users'
8import { MockSmtpServer } from '../mock-servers/mock-email' 7import { MockSmtpServer } from '../mock-servers/mock-email'
9import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
10import { doubleFollow } from '../server/follows' 8import { doubleFollow } from '../server/follows'
11import { flushAndRunMultipleServers, ServerInfo } from '../server/servers' 9import { flushAndRunMultipleServers, ServerInfo } from '../server/servers'
12import { setAccessTokensToServers, userLogin } from './login' 10import { setAccessTokensToServers, userLogin } from './login'
13import { createUser, getMyUserInformation } from './users' 11import { createUser, getMyUserInformation } from './users'
14 12
15function updateMyNotificationSettings ( 13function getAllNotificationsSettings (): UserNotificationSetting {
16 url: string, 14 return {
17 token: string, 15 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
18 settings: UserNotificationSetting, 16 newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
19 statusCodeExpected = HttpStatusCode.NO_CONTENT_204 17 abuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
20) { 18 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
21 const path = '/api/v1/users/me/notification-settings' 19 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
22 20 myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
23 return makePutBodyRequest({ 21 myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
24 url, 22 commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
25 path, 23 newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
26 token, 24 newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
27 fields: settings, 25 newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
28 statusCodeExpected 26 abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
29 }) 27 abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
30} 28 autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
31 29 newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
32async function getUserNotifications ( 30 newPluginVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
33 url: string, 31 }
34 token: string,
35 start: number,
36 count: number,
37 unread?: boolean,
38 sort = '-createdAt',
39 statusCodeExpected = HttpStatusCode.OK_200
40) {
41 const path = '/api/v1/users/me/notifications'
42
43 return makeGetRequest({
44 url,
45 path,
46 token,
47 query: {
48 start,
49 count,
50 sort,
51 unread
52 },
53 statusCodeExpected
54 })
55}
56
57function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
58 const path = '/api/v1/users/me/notifications/read'
59
60 return makePostBodyRequest({
61 url,
62 path,
63 token,
64 fields: { ids },
65 statusCodeExpected
66 })
67}
68
69function markAsReadAllNotifications (url: string, token: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
70 const path = '/api/v1/users/me/notifications/read-all'
71
72 return makePostBodyRequest({
73 url,
74 path,
75 token,
76 statusCodeExpected
77 })
78}
79
80async function getLastNotification (serverUrl: string, accessToken: string) {
81 const res = await getUserNotifications(serverUrl, accessToken, 0, 1, undefined, '-createdAt')
82
83 if (res.body.total === 0) return undefined
84
85 return res.body.data[0] as UserNotification
86} 32}
87 33
88type CheckerBaseParams = { 34type CheckerBaseParams = {
@@ -104,7 +50,7 @@ async function checkNotification (
104 const check = base.check || { web: true, mail: true } 50 const check = base.check || { web: true, mail: true }
105 51
106 if (check.web) { 52 if (check.web) {
107 const notification = await getLastNotification(base.server.url, base.token) 53 const notification = await base.server.notificationsCommand.getLastest({ token: base.token })
108 54
109 if (notification || checkType !== 'absence') { 55 if (notification || checkType !== 'absence') {
110 notificationChecker(notification, checkType) 56 notificationChecker(notification, checkType)
@@ -680,27 +626,6 @@ async function checkNewPluginVersion (base: CheckerBaseParams, pluginType: Plugi
680 await checkNotification(base, notificationChecker, emailNotificationFinder, type) 626 await checkNotification(base, notificationChecker, emailNotificationFinder, type)
681} 627}
682 628
683function getAllNotificationsSettings (): UserNotificationSetting {
684 return {
685 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
686 newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
687 abuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
688 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
689 blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
690 myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
691 myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
692 commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
693 newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
694 newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
695 newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
696 abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
697 abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
698 autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
699 newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL,
700 newPluginVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
701 }
702}
703
704async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: any = {}) { 629async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: any = {}) {
705 const userNotifications: UserNotification[] = [] 630 const userNotifications: UserNotification[] = []
706 const adminNotifications: UserNotification[] = [] 631 const adminNotifications: UserNotification[] = []
@@ -739,11 +664,11 @@ async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: an
739 }) 664 })
740 const userAccessToken = await userLogin(servers[0], user) 665 const userAccessToken = await userLogin(servers[0], user)
741 666
742 await updateMyNotificationSettings(servers[0].url, userAccessToken, getAllNotificationsSettings()) 667 await servers[0].notificationsCommand.updateMySettings({ token: userAccessToken, settings: getAllNotificationsSettings() })
743 await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, getAllNotificationsSettings()) 668 await servers[0].notificationsCommand.updateMySettings({ settings: getAllNotificationsSettings() })
744 669
745 if (serversCount > 1) { 670 if (serversCount > 1) {
746 await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, getAllNotificationsSettings()) 671 await servers[1].notificationsCommand.updateMySettings({ settings: getAllNotificationsSettings() })
747 } 672 }
748 673
749 { 674 {
@@ -777,11 +702,11 @@ async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: an
777// --------------------------------------------------------------------------- 702// ---------------------------------------------------------------------------
778 703
779export { 704export {
705 getAllNotificationsSettings,
706
780 CheckerBaseParams, 707 CheckerBaseParams,
781 CheckerType, 708 CheckerType,
782 getAllNotificationsSettings,
783 checkNotification, 709 checkNotification,
784 markAsReadAllNotifications,
785 checkMyVideoImportIsFinished, 710 checkMyVideoImportIsFinished,
786 checkUserRegistered, 711 checkUserRegistered,
787 checkAutoInstanceFollowing, 712 checkAutoInstanceFollowing,
@@ -791,14 +716,10 @@ export {
791 checkNewCommentOnMyVideo, 716 checkNewCommentOnMyVideo,
792 checkNewBlacklistOnMyVideo, 717 checkNewBlacklistOnMyVideo,
793 checkCommentMention, 718 checkCommentMention,
794 updateMyNotificationSettings,
795 checkNewVideoAbuseForModerators, 719 checkNewVideoAbuseForModerators,
796 checkVideoAutoBlacklistForModerators, 720 checkVideoAutoBlacklistForModerators,
797 checkNewAbuseMessage, 721 checkNewAbuseMessage,
798 checkAbuseStateChange, 722 checkAbuseStateChange,
799 getUserNotifications,
800 markAsReadNotifications,
801 getLastNotification,
802 checkNewInstanceFollower, 723 checkNewInstanceFollower,
803 prepareNotificationsTest, 724 prepareNotificationsTest,
804 checkNewCommentAbuseForModerators, 725 checkNewCommentAbuseForModerators,