Browse Source

nextcloud_push_proxy: Minimal library implementation

pull/19/head
jld3103 2 years ago
parent
commit
5509319a8b
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 30
      .github/workflows/dart.yml
  2. 5
      .idea/nextcloud-harbour.iml
  3. 10
      packages/nextcloud_push_proxy/.gitignore
  4. 12
      packages/nextcloud_push_proxy/LICENSE
  5. 5
      packages/nextcloud_push_proxy/analysis_options.yaml
  6. 143
      packages/nextcloud_push_proxy/lib/nextcloud_push_proxy.dart
  7. 7
      packages/nextcloud_push_proxy/mono_pkg.yaml
  8. 15
      packages/nextcloud_push_proxy/pubspec.yaml

30
.github/workflows/dart.yml

@ -35,16 +35,16 @@ jobs:
- name: mono_repo self validate - name: mono_repo self validate
run: dart pub global run mono_repo generate --validate run: dart pub global run mono_repo generate --validate
job_002: job_002:
name: "analyze; PKGS: packages/file_icons, packages/harbour, packages/nextcloud, packages/settings, packages/sort_box, packages/spec_templates; `dart format --output=none --set-exit-if-changed --line-length 120 .`" name: "analyze; PKGS: packages/file_icons, packages/harbour, packages/nextcloud, packages/nextcloud_push_proxy, packages/settings, packages/sort_box, packages/spec_templates; `dart format --output=none --set-exit-if-changed --line-length 120 .`"
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Cache Pub hosted dependencies - name: Cache Pub hosted dependencies
uses: actions/cache@4504faf7e9bcf8f3ed0bc863c4e1d21499ab8ef8 uses: actions/cache@4504faf7e9bcf8f3ed0bc863c4e1d21499ab8ef8
with: with:
path: "~/.pub-cache/hosted" path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/settings-packages/sort_box-packages/spec_templates;commands:format" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/nextcloud_push_proxy-packages/settings-packages/sort_box-packages/spec_templates;commands:format"
restore-keys: | restore-keys: |
os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/settings-packages/sort_box-packages/spec_templates os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/file_icons-packages/harbour-packages/nextcloud-packages/nextcloud_push_proxy-packages/settings-packages/sort_box-packages/spec_templates
os:ubuntu-latest;pub-cache-hosted;sdk:stable os:ubuntu-latest;pub-cache-hosted;sdk:stable
os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest;pub-cache-hosted
os:ubuntu-latest os:ubuntu-latest
@ -80,6 +80,15 @@ jobs:
if: "always() && steps.packages_nextcloud_pub_upgrade.conclusion == 'success'" if: "always() && steps.packages_nextcloud_pub_upgrade.conclusion == 'success'"
working-directory: packages/nextcloud working-directory: packages/nextcloud
run: "dart format --output=none --set-exit-if-changed --line-length 120 ." run: "dart format --output=none --set-exit-if-changed --line-length 120 ."
- id: packages_nextcloud_push_proxy_pub_upgrade
name: packages/nextcloud_push_proxy; flutter pub pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: packages/nextcloud_push_proxy
run: flutter pub pub upgrade
- name: "packages/nextcloud_push_proxy; dart format --output=none --set-exit-if-changed --line-length 120 ."
if: "always() && steps.packages_nextcloud_push_proxy_pub_upgrade.conclusion == 'success'"
working-directory: packages/nextcloud_push_proxy
run: "dart format --output=none --set-exit-if-changed --line-length 120 ."
- id: packages_settings_pub_upgrade - id: packages_settings_pub_upgrade
name: packages/settings; flutter pub pub upgrade name: packages/settings; flutter pub pub upgrade
if: "always() && steps.checkout.conclusion == 'success'" if: "always() && steps.checkout.conclusion == 'success'"
@ -158,16 +167,16 @@ jobs:
needs: needs:
- job_001 - job_001
job_004: job_004:
name: "analyze; PKGS: packages/nextcloud, packages/sort_box, packages/spec_templates; `dart analyze`" name: "analyze; PKGS: packages/nextcloud, packages/nextcloud_push_proxy, packages/sort_box, packages/spec_templates; `dart analyze`"
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Cache Pub hosted dependencies - name: Cache Pub hosted dependencies
uses: actions/cache@4504faf7e9bcf8f3ed0bc863c4e1d21499ab8ef8 uses: actions/cache@4504faf7e9bcf8f3ed0bc863c4e1d21499ab8ef8
with: with:
path: "~/.pub-cache/hosted" path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/sort_box-packages/spec_templates;commands:analyze_1" key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/nextcloud_push_proxy-packages/sort_box-packages/spec_templates;commands:analyze_1"
restore-keys: | restore-keys: |
os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/sort_box-packages/spec_templates os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:packages/nextcloud-packages/nextcloud_push_proxy-packages/sort_box-packages/spec_templates
os:ubuntu-latest;pub-cache-hosted;sdk:stable os:ubuntu-latest;pub-cache-hosted;sdk:stable
os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest;pub-cache-hosted
os:ubuntu-latest os:ubuntu-latest
@ -185,6 +194,15 @@ jobs:
if: "always() && steps.packages_nextcloud_pub_upgrade.conclusion == 'success'" if: "always() && steps.packages_nextcloud_pub_upgrade.conclusion == 'success'"
working-directory: packages/nextcloud working-directory: packages/nextcloud
run: dart analyze run: dart analyze
- id: packages_nextcloud_push_proxy_pub_upgrade
name: packages/nextcloud_push_proxy; dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: packages/nextcloud_push_proxy
run: dart pub upgrade
- name: packages/nextcloud_push_proxy; dart analyze
if: "always() && steps.packages_nextcloud_push_proxy_pub_upgrade.conclusion == 'success'"
working-directory: packages/nextcloud_push_proxy
run: dart analyze
- id: packages_sort_box_pub_upgrade - id: packages_sort_box_pub_upgrade
name: packages/sort_box; dart pub upgrade name: packages/sort_box; dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'" if: "always() && steps.checkout.conclusion == 'success'"

5
.idea/nextcloud-harbour.iml

@ -44,6 +44,11 @@
<excludeFolder url="file://$MODULE_DIR$/external" /> <excludeFolder url="file://$MODULE_DIR$/external" />
<excludeFolder url="file://$MODULE_DIR$/packages/nextcloud/lib/src/clients/common" /> <excludeFolder url="file://$MODULE_DIR$/packages/nextcloud/lib/src/clients/common" />
<excludeFolder url="file://$MODULE_DIR$/packages/nextcloud/lib/src/clients/generated" /> <excludeFolder url="file://$MODULE_DIR$/packages/nextcloud/lib/src/clients/generated" />
<excludeFolder url="file://$MODULE_DIR$/packages/harbour/android/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/packages/harbour/linux/flutter/ephemeral" />
<excludeFolder url="file://$MODULE_DIR$/packages/nextcloud_push_proxy/build" />
<excludeFolder url="file://$MODULE_DIR$/packages/nextcloud_push_proxy/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/packages/nextcloud_push_proxy/.pub" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

10
packages/nextcloud_push_proxy/.gitignore vendored

@ -0,0 +1,10 @@
# Files and directories created by pub.
.dart_tool/
.packages
# Conventional directory for build outputs.
build/
# Omit committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

12
packages/nextcloud_push_proxy/LICENSE

@ -0,0 +1,12 @@
Copyright (c) 2022, jld3103
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

5
packages/nextcloud_push_proxy/analysis_options.yaml

@ -0,0 +1,5 @@
include: package:nit_picking/dart.yaml
linter:
rules:
prefer_final_parameters: false # Disabled until super.X is no longer complained about in constructors

143
packages/nextcloud_push_proxy/lib/nextcloud_push_proxy.dart

@ -0,0 +1,143 @@
// ignore_for_file: public_member_api_docs
library nextcloud_push_proxy;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
/// Implements the listening part of a Nextcloud push proxy
class NextcloudPushProxy {
HttpServer? _server;
late StreamController<PushProxyDevice> _onNewDeviceController;
late StreamController<PushProxyNotification> _onNewNotificationController;
Stream<PushProxyDevice>? _onNewDeviceStream;
Stream<PushProxyNotification>? _onNewNotificationStream;
/// Listens for new devices
Stream<PushProxyDevice> get onNewDevice {
if (_onNewDeviceStream == null) {
throw Exception('Server not created');
}
return _onNewDeviceStream!;
}
/// Listens for new notifications
Stream<PushProxyNotification> get onNewNotification {
if (_onNewNotificationStream == null) {
throw Exception('Server not created');
}
return _onNewNotificationStream!;
}
late final _router = Router()
..post('/devices', _devicesHandler)
..post('/notifications', _notificationsHandler);
Future<Response> _devicesHandler(Request request) async {
final data = Uri(query: await request.readAsString()).queryParameters;
_onNewDeviceController.add(
PushProxyDevice(
pushToken: data['pushToken']!,
deviceIdentifier: data['deviceIdentifier']!,
deviceIdentifierSignature: data['deviceIdentifierSignature']!,
userPublicKey: data['userPublicKey']!,
),
);
return Response.ok('');
}
Future<Response> _notificationsHandler(Request request) async {
final data = Uri(query: await request.readAsString()).queryParameters;
for (final notification in data.values) {
final notificationData = json.decode(notification) as Map<String, dynamic>;
_onNewNotificationController.add(
PushProxyNotification(
deviceIdentifier: notificationData['deviceIdentifier']! as String,
pushTokenHash: notificationData['pushTokenHash']! as String,
subject: notificationData['subject']! as String,
signature: notificationData['signature']! as String,
priority: notificationData['priority']! as String,
type: notificationData['type']! as String,
),
);
}
return Response.ok('');
}
/// Creates a server listening on the [port]
Future create({
final bool logging = true,
final int port = 8080,
}) async {
if (_server != null) {
throw Exception('Server already created');
}
_onNewDeviceController = StreamController<PushProxyDevice>();
_onNewNotificationController = StreamController<PushProxyNotification>();
_onNewDeviceStream = _onNewDeviceController.stream.asBroadcastStream();
_onNewNotificationStream = _onNewNotificationController.stream.asBroadcastStream();
var handler = Cascade().add(_router).handler;
if (logging) {
handler = logRequests().addHandler(handler);
}
final server = await serve(
handler,
InternetAddress.anyIPv4,
port,
);
server.autoCompress = true;
_server = server;
}
/// Closes the server
Future close() async {
if (_server != null) {
await _server!.close();
_server = null;
await _onNewDeviceController.close();
await _onNewNotificationController.close();
}
}
}
class PushProxyDevice {
PushProxyDevice({
required this.pushToken,
required this.deviceIdentifier,
required this.deviceIdentifierSignature,
required this.userPublicKey,
});
final String pushToken;
final String deviceIdentifier;
final String deviceIdentifierSignature;
final String userPublicKey;
}
class PushProxyNotification {
PushProxyNotification({
required this.deviceIdentifier,
required this.pushTokenHash,
required this.subject,
required this.signature,
required this.priority,
required this.type,
});
final String deviceIdentifier;
final String pushTokenHash;
final String subject;
final String signature;
final String priority;
final String type;
}

7
packages/nextcloud_push_proxy/mono_pkg.yaml

@ -0,0 +1,7 @@
sdk:
- stable
stages:
- analyze:
- analyze
- format: --output=none --set-exit-if-changed --line-length 120 .

15
packages/nextcloud_push_proxy/pubspec.yaml

@ -0,0 +1,15 @@
name: nextcloud_push_proxy
version: 1.0.0
environment:
sdk: '>=2.17.0 <3.0.0'
dependencies:
shelf: ^1.3.1
shelf_router: ^1.1.3
dev_dependencies:
nit_picking:
git:
url: https://github.com/stack11/dart_nit_picking
ref: f29382f
Loading…
Cancel
Save