Debian Codegraph @

src/notifications/ManagerMac.mm

raw
1 // SPDX-FileCopyrightText: 2021 Nheko Contributors
2 // SPDX-FileCopyrightText: 2022 Nheko Contributors
3 //
4 // SPDX-License-Identifier: GPL-3.0-or-later
5
6 #include "notifications/MacNotificationDelegate.h"
7 #include "notifications/Manager.h"
8 #include "notifications/NotificationManagerProxy.h"
9
10 #include "ChatPage.h"
11
12 #import <AppKit/NSImage.h>
13 #import <Foundation/Foundation.h>
14 #import <UserNotifications/UserNotifications.h>
15
16 #include <QImage>
17
18 @interface UNNotificationAttachment (UNNotificationAttachmentAdditions)
19 + (UNNotificationAttachment*)createFromImageData:(NSData*)imgData
20 identifier:(NSString*)imageFileIdentifier
21 options:
22 (NSDictionary*)attachmentOptions;
23 @end
24
25 @implementation UNNotificationAttachment (UNNotificationAttachmentAdditions)
26 + (UNNotificationAttachment*)createFromImageData:(NSData*)imgData
27 identifier:(NSString*)imageFileIdentifier
28 options:
29 (NSDictionary*)attachmentOptions
30 {
31 NSFileManager* fileManager = [NSFileManager defaultManager];
32 NSString* tmpSubFolderName =
33 [[NSProcessInfo processInfo] globallyUniqueString];
34 NSURL* tmpSubFolderURL = [NSURL
35 fileURLWithPath:[NSTemporaryDirectory()
36 stringByAppendingPathComponent:tmpSubFolderName]
37 isDirectory:true];
38 NSError* error = nil;
39 [fileManager createDirectoryAtURL:tmpSubFolderURL
40 withIntermediateDirectories:true
41 attributes:nil
42 error:&error];
43 if (error) {
44 NSLog(@"%@", [error localizedDescription]);
45 return nil;
46 }
47 NSURL* fileURL =
48 [tmpSubFolderURL URLByAppendingPathComponent:imageFileIdentifier];
49 [imgData writeToURL:fileURL atomically:true];
50 UNNotificationAttachment* imageAttachment =
51 [UNNotificationAttachment attachmentWithIdentifier:@""
52 URL:fileURL
53 options:attachmentOptions
54 error:&error];
55 if (error) {
56 NSLog(@"%@", [error localizedDescription]);
57 return nil;
58 }
59 return imageAttachment;
60 }
61 @end
62
63 void NotificationsManager::objCxxPostNotification(
64 const QString room_name,
65 const QString room_id,
66 const QString event_id,
67 const QString subtitle,
68 const QString informativeText,
69 const QString bodyImagePath,
70 const bool enableSound)
71 {
72 // Request permissions for alerts (the generic type of notification), sound playback,
73 // and badges (which allows the Nheko app icon to show the little red bubble with unread count).
74 // NOTE: Possible macOS bug... the 'Play sound for notification checkbox' doesn't appear in
75 // the Notifications and Focus settings unless UNAuthorizationOptionBadges is also
76 // specified
77 UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge;
78 UNUserNotificationCenter* center =
79 [UNUserNotificationCenter currentNotificationCenter];
80
81 // TODO: Move this somewhere that isn't dependent on receiving a notification
82 // to actually request notification access.
83 [center requestAuthorizationWithOptions:options
84 completionHandler:^(BOOL granted,
85 NSError* _Nullable error) {
86 if (!granted) {
87 NSLog(@"No notification access");
88 if (error) {
89 NSLog(@"%@", [error localizedDescription]);
90 }
91 }
92 }];
93
94 UNTextInputNotificationAction* replyAction = [UNTextInputNotificationAction actionWithIdentifier:@"ReplyAction"
95 title:respondStr.toNSString()
96 options:UNNotificationActionOptionNone
97 textInputButtonTitle:sendStr.toNSString()
98 textInputPlaceholder:placeholder.toNSString()];
99
100 UNNotificationCategory* replyCategory = [UNNotificationCategory categoryWithIdentifier:@"ReplyCategory"
101 actions:@[ replyAction ]
102 intentIdentifiers:@[]
103 options:UNNotificationCategoryOptionNone];
104 UNNotificationCategory* summaryCategory = [UNNotificationCategory categoryWithIdentifier:@"SummaryCategory"
105 actions:@[]
106 intentIdentifiers:@[]
107 options:UNNotificationCategoryOptionNone];
108
109 NSString* title = room_name.toNSString();
110 NSString* sub = subtitle.toNSString();
111 NSString* body = informativeText.toNSString();
112 NSString* threadIdentifier = room_id.toNSString();
113 NSString* identifier = event_id.toNSString();
114 NSString* imgUrl = bodyImagePath.toNSString();
115
116 NSSet* categories = [NSSet setWithArray:@[ summaryCategory, replyCategory ]];
117 [center setNotificationCategories:categories];
118 [center getNotificationSettingsWithCompletionHandler:^(
119 UNNotificationSettings* _Nonnull settings) {
120 if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
121 UNMutableNotificationContent* content =
122 [[UNMutableNotificationContent alloc] init];
123
124 content.title = title;
125 content.subtitle = sub;
126 content.body = body;
127 if (enableSound) {
128 content.sound = [UNNotificationSound defaultSound];
129 }
130 if (!room_id.isEmpty()) {
131 content.threadIdentifier = threadIdentifier;
132 content.categoryIdentifier = @"ReplyCategory";
133 } else {
134 content.categoryIdentifier = @"SummaryCategory";
135 }
136
137 if ([imgUrl length] != 0) {
138 NSURL* imageURL = [NSURL fileURLWithPath:imgUrl];
139 NSData* img = [NSData dataWithContentsOfURL:imageURL];
140 NSArray* attachments = [NSMutableArray array];
141 UNNotificationAttachment* attachment = [UNNotificationAttachment
142 createFromImageData:img
143 identifier:@"attachment_image.jpeg"
144 options:nil];
145 if (attachment) {
146 attachments = [NSMutableArray arrayWithObjects:attachment, nil];
147 content.attachments = attachments;
148 }
149 }
150
151 UNNotificationRequest* notificationRequest =
152 [UNNotificationRequest requestWithIdentifier:identifier
153 content:content
154 trigger:nil];
155
156 [center addNotificationRequest:notificationRequest
157 withCompletionHandler:^(NSError* _Nullable error) {
158 if (error != nil) {
159 NSLog(@"Unable to Add Notification Request: %@", [error localizedDescription]);
160 }
161 }];
162
163 [content autorelease];
164 }
165 }];
166 }
167
168 void NotificationsManager::attachToMacNotifCenter()
169 {
170 UNUserNotificationCenter* center =
171 [UNUserNotificationCenter currentNotificationCenter];
172
173 std::unique_ptr<NotificationManagerProxy> proxy = std::make_unique<NotificationManagerProxy>();
174
175 connect(proxy.get(), &NotificationManagerProxy::notificationReplied, ChatPage::instance(), &ChatPage::sendNotificationReply);
176
177 MacNotificationDelegate* notifDelegate = [[MacNotificationDelegate alloc] initWithProxy:std::move(proxy)];
178
179 center.delegate = notifDelegate;
180 }
181
182 // unused
183 void NotificationsManager::actionInvoked(uint, QString) { }
184
185 void NotificationsManager::activationToken(uint, QString) { }
186
187 void NotificationsManager::notificationReplied(uint, QString) { }
188
189 void NotificationsManager::notificationClosed(uint, uint) { }
190
191 void NotificationsManager::removeNotification(const QString&, const QString&) { }