Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
a7a2474e2a
|
|||
|
e15d70dd0e
|
|||
|
e98882a0c6
|
|||
|
24bf7cd434
|
|||
|
54c4f873fc
|
|||
|
b2de793758
|
@@ -205,7 +205,7 @@ class APIClient {
|
||||
fn: User.fromJson,
|
||||
authToken: auth.getToken(),
|
||||
query: {
|
||||
'confirm': ['true']
|
||||
'confirm': ['true'],
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -230,7 +230,7 @@ class APIClient {
|
||||
static Future<Client> updateClient(TokenSource auth, String clientID, {String? fcmToken, String? agentModel, String? name, String? agentVersion}) async {
|
||||
return await _request(
|
||||
name: 'updateClient',
|
||||
method: 'PUT',
|
||||
method: 'PATCH',
|
||||
relURL: 'users/${auth.getUserID()}/clients/$clientID',
|
||||
jsonBody: {
|
||||
if (fcmToken != null) 'fcm_token': fcmToken,
|
||||
@@ -259,7 +259,7 @@ class APIClient {
|
||||
method: 'GET',
|
||||
relURL: 'users/${auth.getUserID()}/channels',
|
||||
query: {
|
||||
'selector': [sel.apiKey]
|
||||
'selector': [sel.apiKey],
|
||||
},
|
||||
fn: (json) => ChannelWithSubscription.fromJsonArray(json['channels'] as List<dynamic>),
|
||||
authToken: auth.getToken(),
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:simplecloudnotifier/api/api_client.dart';
|
||||
import 'package:simplecloudnotifier/main_messaging.dart';
|
||||
import 'package:simplecloudnotifier/main_utils.dart';
|
||||
import 'package:simplecloudnotifier/components/layout/nav_layout.dart';
|
||||
@@ -68,15 +69,17 @@ void main() async {
|
||||
print('[INIT] Request Notification permissions...');
|
||||
await FirebaseMessaging.instance.requestPermission(provisional: true);
|
||||
|
||||
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
|
||||
try {
|
||||
setFirebaseToken(fcmToken);
|
||||
} catch (exc, trace) {
|
||||
ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace);
|
||||
}
|
||||
}).onError((dynamic err) {
|
||||
ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? ''));
|
||||
});
|
||||
FirebaseMessaging.instance.onTokenRefresh
|
||||
.listen((fcmToken) {
|
||||
try {
|
||||
setFirebaseToken(fcmToken);
|
||||
} catch (exc, trace) {
|
||||
ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace);
|
||||
}
|
||||
})
|
||||
.onError((dynamic err) {
|
||||
ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? ''));
|
||||
});
|
||||
|
||||
try {
|
||||
print('[INIT] Query firebase token...');
|
||||
@@ -96,6 +99,25 @@ void main() async {
|
||||
|
||||
await appAuth.tryMigrateFromV1();
|
||||
|
||||
if (appAuth.isAuth()) {
|
||||
print('[INIT] Load Client and potentially update...');
|
||||
|
||||
try {
|
||||
var client = await appAuth.loadClient(onlyCached: true);
|
||||
if (client != null) {
|
||||
if (client.agentModel != Globals().deviceModel || client.name != Globals().nameForClient() || client.agentVersion != Globals().version) {
|
||||
print('[INIT] Update Client info...');
|
||||
|
||||
final newClient = await APIClient.updateClient(appAuth, client.clientID, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version);
|
||||
appAuth.setClientAndClientID(newClient);
|
||||
await appAuth.save();
|
||||
}
|
||||
}
|
||||
} catch (exc, trace) {
|
||||
ApplicationLog.error('Failed to get client (on init): ' + exc.toString(), trace: trace);
|
||||
}
|
||||
}
|
||||
|
||||
print('[INIT] Load Notifications...');
|
||||
|
||||
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||
|
||||
@@ -40,11 +40,11 @@ void setFirebaseToken(String fcmToken) async {
|
||||
|
||||
if (client == null) {
|
||||
// should not really happen - perhaps someone externally deleted the client?
|
||||
final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
|
||||
final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType);
|
||||
acc.setClientAndClientID(newClient);
|
||||
await acc.save();
|
||||
} else {
|
||||
final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken: fcmToken, agentModel: Globals().deviceModel, name: Globals().hostname, agentVersion: Globals().version);
|
||||
final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken: fcmToken, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version);
|
||||
acc.setClientAndClientID(newClient);
|
||||
await acc.save();
|
||||
}
|
||||
|
||||
@@ -527,7 +527,7 @@ class _AccountRootPageState extends State<AccountRootPage> {
|
||||
|
||||
await Globals().setPrefFCMToken(fcmToken);
|
||||
|
||||
final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().hostname, Globals().clientType);
|
||||
final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().nameForClient(), Globals().clientType);
|
||||
|
||||
acc.set(user.user, user.clients[0], user.adminKey, user.sendKey);
|
||||
|
||||
|
||||
@@ -156,7 +156,7 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
|
||||
|
||||
final user = await APIClient.getUser(DirectTokenSource(uid, atokv), uid);
|
||||
|
||||
final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
|
||||
final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType);
|
||||
|
||||
acc.set(user, client, atokv, stokv);
|
||||
await acc.save();
|
||||
|
||||
@@ -12,6 +12,7 @@ import 'package:simplecloudnotifier/state/app_auth.dart';
|
||||
import 'package:simplecloudnotifier/state/application_log.dart';
|
||||
import 'package:simplecloudnotifier/utils/navi.dart';
|
||||
import 'package:simplecloudnotifier/utils/toaster.dart';
|
||||
import 'package:simplecloudnotifier/utils/dialogs.dart';
|
||||
import 'package:simplecloudnotifier/utils/ui.dart';
|
||||
|
||||
class ChannelScannerResultChannelSubscribe extends StatefulWidget {
|
||||
@@ -172,6 +173,38 @@ class _ChannelScannerResultChannelSubscribeState extends State<ChannelScannerRes
|
||||
|
||||
void _onSubscribe() async {
|
||||
final auth = Provider.of<AppAuth>(context, listen: false);
|
||||
|
||||
// Check if username is set
|
||||
try {
|
||||
final user = await auth.loadUser();
|
||||
if (user.username == null || user.username!.isEmpty) {
|
||||
// Show modal to set username
|
||||
var newusername = await UIDialogs.showUsernameRequiredDialog(context);
|
||||
|
||||
if (newusername == null) return; // User cancelled
|
||||
|
||||
newusername = newusername.trim();
|
||||
if (newusername.isEmpty) {
|
||||
Toaster.error("Error", 'Username cannot be empty');
|
||||
return;
|
||||
}
|
||||
|
||||
// Update username via API
|
||||
try {
|
||||
await APIClient.updateUser(auth, auth.userID!, username: newusername);
|
||||
await auth.loadUser(force: true); // Refresh cached user
|
||||
Toaster.success("Success", 'Username set');
|
||||
} catch (e) {
|
||||
Toaster.error("Error", 'Failed to set username: ${e.toString()}');
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
Toaster.error("Error", 'Failed to load user data: ${e.toString()}');
|
||||
return;
|
||||
}
|
||||
|
||||
// Proceed with subscription
|
||||
try {
|
||||
var sub = await APIClient.subscribeToChannelbyID(auth, widget.value.channelID, subscribeKey: widget.value.subscribeKey);
|
||||
if (sub.confirmed) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:simplecloudnotifier/api/api_client.dart';
|
||||
import 'package:simplecloudnotifier/state/app_settings.dart';
|
||||
import 'package:simplecloudnotifier/state/app_auth.dart';
|
||||
import 'package:simplecloudnotifier/state/application_log.dart';
|
||||
import 'package:simplecloudnotifier/state/globals.dart';
|
||||
import 'package:simplecloudnotifier/utils/notifier.dart';
|
||||
import 'package:simplecloudnotifier/utils/toaster.dart';
|
||||
import 'package:simplecloudnotifier/utils/ui.dart';
|
||||
@@ -65,22 +66,31 @@ class _DebugActionsPageState extends State<DebugActionsPage> {
|
||||
onPressed: _sendTokenToServer,
|
||||
text: 'Send FCM Token to Server',
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
UI.button(
|
||||
big: false,
|
||||
onPressed: _updateClient,
|
||||
text: 'Update Client on Server',
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
UI.button(
|
||||
big: false,
|
||||
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, null),
|
||||
text: 'Show local notification (generic)',
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
UI.button(
|
||||
big: false,
|
||||
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, 0),
|
||||
text: 'Show local notification (Prio = 0)',
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
UI.button(
|
||||
big: false,
|
||||
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, 1),
|
||||
text: 'Show local notification (Prio = 1)',
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
UI.button(
|
||||
big: false,
|
||||
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, 2),
|
||||
@@ -128,6 +138,26 @@ class _DebugActionsPageState extends State<DebugActionsPage> {
|
||||
}
|
||||
}
|
||||
|
||||
void _updateClient() async {
|
||||
try {
|
||||
final auth = AppAuth();
|
||||
|
||||
final clientID = auth.getClientID();
|
||||
if (clientID == null) {
|
||||
Toaster.error("Error", "No Client set");
|
||||
return;
|
||||
}
|
||||
|
||||
final newClient = await APIClient.updateClient(auth, clientID, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version);
|
||||
auth.setClientAndClientID(newClient);
|
||||
|
||||
Toaster.success("Success", "Client updated");
|
||||
} catch (exc, trace) {
|
||||
Toaster.error("Error", "An error occurred while updating the client: ${exc.toString()}");
|
||||
ApplicationLog.error("An error occurred while updating the client: ${exc.toString()}", trace: trace);
|
||||
}
|
||||
}
|
||||
|
||||
void _copyToken() async {
|
||||
try {
|
||||
final fcmToken = await FirebaseMessaging.instance.getToken();
|
||||
|
||||
@@ -135,7 +135,7 @@ class _DebugRequestViewPageState extends State<DebugRequestViewPage> {
|
||||
void _copyCurl() {
|
||||
final method = '-X ${widget.request.method}';
|
||||
final header = widget.request.requestHeaders.entries.map((v) => '-H "${v.key}: ${v.value}"').join(' ');
|
||||
final body = widget.request.requestBody.isNotEmpty ? '-d "${widget.request.requestBody}"' : '';
|
||||
final body = widget.request.requestBody.isNotEmpty ? '-d \'${widget.request.requestBody}\'' : '';
|
||||
|
||||
final curlParts = ['curl', method, header, '"${widget.request.url}"', body];
|
||||
|
||||
|
||||
@@ -284,7 +284,9 @@ class _SendRootPageState extends State<SendRootPage> {
|
||||
}
|
||||
|
||||
try {
|
||||
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgContent.text);
|
||||
var content = (_msgContent.text != '') ? _msgContent.text : null;
|
||||
|
||||
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgTitle.text, content: content);
|
||||
Toaster.success("Success", 'Message sent');
|
||||
setState(() {
|
||||
_msgTitle.clear();
|
||||
@@ -306,7 +308,11 @@ class _SendRootPageState extends State<SendRootPage> {
|
||||
}
|
||||
|
||||
try {
|
||||
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgContent.text, channel: _channelName.text, senderName: _senderName.text, priority: _priority);
|
||||
var content = (_msgContent.text != '') ? _msgContent.text : null;
|
||||
var channel = (_channelName.text != '') ? _channelName.text : null;
|
||||
var sender = (_senderName.text != '') ? _senderName.text : null;
|
||||
|
||||
await APIClient.sendMessage(acc.userID!, acc.tokenSend!, _msgTitle.text, content: content, channel: channel, senderName: sender, priority: _priority);
|
||||
Toaster.success("Success", 'Message sent');
|
||||
setState(() {
|
||||
_msgTitle.clear();
|
||||
|
||||
@@ -99,7 +99,7 @@ class AppAuth extends ChangeNotifier implements TokenSource {
|
||||
|
||||
final user = await APIClient.getUser(DirectTokenSource(oldUserID, oldUserKey), oldUserID);
|
||||
|
||||
final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
|
||||
final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType);
|
||||
|
||||
set(user, client, oldUserKey, newTokenSend.token);
|
||||
|
||||
@@ -232,7 +232,7 @@ class AppAuth extends ChangeNotifier implements TokenSource {
|
||||
return _user?.$1;
|
||||
}
|
||||
|
||||
Future<Client?> loadClient({bool force = false, Duration? forceIfOlder = null}) async {
|
||||
Future<Client?> loadClient({bool force = false, Duration? forceIfOlder = null, bool onlyCached = false}) async {
|
||||
if (forceIfOlder != null && _client != null && _client!.$2.difference(DateTime.now()) > forceIfOlder) {
|
||||
force = true;
|
||||
}
|
||||
@@ -245,6 +245,10 @@ class AppAuth extends ChangeNotifier implements TokenSource {
|
||||
throw Exception('Not authenticated');
|
||||
}
|
||||
|
||||
if (onlyCached) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
final client = await APIClient.getClient(this, _clientID!);
|
||||
|
||||
|
||||
@@ -92,4 +92,12 @@ class Globals {
|
||||
Future<bool> setPrefFCMToken(String value) {
|
||||
return sharedPrefs.setString("fcm.token", value);
|
||||
}
|
||||
|
||||
String nameForClient() {
|
||||
if (this.deviceName.isNotEmpty) {
|
||||
return this.deviceName;
|
||||
} else {
|
||||
return this.hostname;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,40 @@ class UIDialogs {
|
||||
);
|
||||
}
|
||||
|
||||
static Future<String?> showUsernameRequiredDialog(BuildContext context) {
|
||||
var _textFieldController = TextEditingController();
|
||||
|
||||
return showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text('Username Required'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Please set a public username to subscribe to channels from other users.'),
|
||||
SizedBox(height: 16),
|
||||
TextField(
|
||||
autofocus: true,
|
||||
controller: _textFieldController,
|
||||
decoration: InputDecoration(hintText: 'Enter username'),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(_textFieldController.text),
|
||||
child: Text('OK'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<bool> showConfirmDialog(BuildContext context, String title, {String? text, String? okText, String? cancelText}) {
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
|
||||
@@ -2,7 +2,7 @@ name: simplecloudnotifier
|
||||
description: "Receive push messages"
|
||||
publish_to: 'none'
|
||||
|
||||
version: 2.1.1+509
|
||||
version: 2.2.2+544
|
||||
|
||||
environment:
|
||||
sdk: '>=3.9.0 <4.0.0'
|
||||
|
||||
2
scnserver/.gitignore
vendored
2
scnserver/.gitignore
vendored
@@ -17,8 +17,8 @@ simple_cloud_notifier-*.sql
|
||||
identifier.sqlite
|
||||
|
||||
.idea/dataSources.xml
|
||||
|
||||
.idea/copilot*
|
||||
.idea/go.imports.xml
|
||||
|
||||
.swaggobin
|
||||
|
||||
|
||||
@@ -88,9 +88,9 @@ func (u User) MaxTitleLength() int {
|
||||
|
||||
func (u User) QuotaPerDay() int {
|
||||
if u.IsPro {
|
||||
return 5000
|
||||
return 15_000
|
||||
} else {
|
||||
return 50
|
||||
return 500
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
package push
|
||||
|
||||
import (
|
||||
scn "blackforestbytes.com/simplecloudnotifier"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
scn "blackforestbytes.com/simplecloudnotifier"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// https://firebase.google.com/docs/cloud-messaging/send-message#rest
|
||||
@@ -66,7 +67,13 @@ func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.Us
|
||||
"title": msg.Title,
|
||||
"body": msg.ShortContent(),
|
||||
},
|
||||
"apns": gin.H{},
|
||||
"apns": gin.H{
|
||||
"payload": gin.H{
|
||||
"aps": gin.H{
|
||||
"sound": "default",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
} else if client.Type == models.ClientTypeAndroid {
|
||||
jsonBody = gin.H{
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestRequestLogSimple(t *testing.T) {
|
||||
@@ -126,6 +127,8 @@ func TestRequestLogSimple(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequestLogAPI(t *testing.T) {
|
||||
t.Skip("Flaky test - and kinda hacky")
|
||||
|
||||
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
|
||||
defer stop()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user