feat(notes): Add notes feature with editor page and drawer integration
This commit is contained in:
214
lib/core/models/note.dart
Normal file
214
lib/core/models/note.dart
Normal file
@@ -0,0 +1,214 @@
|
||||
// ignore_for_file: invalid_annotation_target
|
||||
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'note.freezed.dart';
|
||||
part 'note.g.dart';
|
||||
|
||||
/// Content structure for a note, supporting multiple formats.
|
||||
@freezed
|
||||
sealed class NoteContent with _$NoteContent {
|
||||
const factory NoteContent({
|
||||
/// Raw JSON content from rich text editor (if any)
|
||||
Object? json,
|
||||
|
||||
/// HTML representation
|
||||
@Default('') String html,
|
||||
|
||||
/// Markdown representation
|
||||
@Default('') String md,
|
||||
}) = _NoteContent;
|
||||
|
||||
factory NoteContent.fromJson(Map<String, dynamic> json) =>
|
||||
_$NoteContentFromJson(json);
|
||||
}
|
||||
|
||||
/// Data payload for a note, containing content and optional files/versions.
|
||||
@freezed
|
||||
sealed class NoteData with _$NoteData {
|
||||
const factory NoteData({
|
||||
/// The main content of the note
|
||||
@Default(NoteContent()) NoteContent content,
|
||||
|
||||
/// Previous versions for undo/history
|
||||
@Default([]) List<NoteContent> versions,
|
||||
|
||||
/// Attached files (if any)
|
||||
@_FileListConverter() List<Map<String, dynamic>>? files,
|
||||
}) = _NoteData;
|
||||
|
||||
factory NoteData.fromJson(Map<String, dynamic> json) =>
|
||||
_$NoteDataFromJson(json);
|
||||
}
|
||||
|
||||
/// Converter for files list which can be null or a list of maps.
|
||||
class _FileListConverter
|
||||
implements JsonConverter<List<Map<String, dynamic>>?, Object?> {
|
||||
const _FileListConverter();
|
||||
|
||||
@override
|
||||
List<Map<String, dynamic>>? fromJson(Object? json) {
|
||||
if (json == null) return null;
|
||||
if (json is List) {
|
||||
return json.whereType<Map<String, dynamic>>().toList();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Object? toJson(List<Map<String, dynamic>>? object) => object;
|
||||
}
|
||||
|
||||
/// User information associated with a note.
|
||||
@freezed
|
||||
sealed class NoteUser with _$NoteUser {
|
||||
const factory NoteUser({
|
||||
required String id,
|
||||
String? name,
|
||||
String? email,
|
||||
@JsonKey(name: 'profile_image_url') String? profileImageUrl,
|
||||
}) = _NoteUser;
|
||||
|
||||
factory NoteUser.fromJson(Map<String, dynamic> json) =>
|
||||
_$NoteUserFromJson(json);
|
||||
}
|
||||
|
||||
/// A Note model matching the OpenWebUI notes API structure.
|
||||
@freezed
|
||||
sealed class Note with _$Note {
|
||||
const Note._();
|
||||
|
||||
const factory Note({
|
||||
required String id,
|
||||
@JsonKey(name: 'user_id') required String userId,
|
||||
required String title,
|
||||
|
||||
/// Note content and associated data
|
||||
@_NoteDataConverter() @Default(NoteData()) NoteData data,
|
||||
|
||||
/// Additional metadata
|
||||
@_MetadataConverter() Map<String, dynamic>? meta,
|
||||
|
||||
/// Access control settings
|
||||
@JsonKey(name: 'access_control')
|
||||
@_MetadataConverter()
|
||||
Map<String, dynamic>? accessControl,
|
||||
|
||||
/// Creation timestamp in nanoseconds
|
||||
@JsonKey(name: 'created_at') required int createdAt,
|
||||
|
||||
/// Last update timestamp in nanoseconds
|
||||
@JsonKey(name: 'updated_at') required int updatedAt,
|
||||
|
||||
/// User who created the note (optional, from extended response)
|
||||
NoteUser? user,
|
||||
}) = _Note;
|
||||
|
||||
factory Note.fromJson(Map<String, dynamic> json) => _$NoteFromJson(json);
|
||||
|
||||
/// Get created date as DateTime
|
||||
DateTime get createdDateTime =>
|
||||
DateTime.fromMicrosecondsSinceEpoch(createdAt ~/ 1000);
|
||||
|
||||
/// Get updated date as DateTime
|
||||
DateTime get updatedDateTime =>
|
||||
DateTime.fromMicrosecondsSinceEpoch(updatedAt ~/ 1000);
|
||||
|
||||
/// Get the markdown content of the note
|
||||
String get markdownContent => data.content.md;
|
||||
|
||||
/// Get the HTML content of the note
|
||||
String get htmlContent => data.content.html;
|
||||
|
||||
/// Check if the note has content
|
||||
bool get hasContent =>
|
||||
data.content.md.isNotEmpty || data.content.html.isNotEmpty;
|
||||
}
|
||||
|
||||
/// Converter for NoteData that handles both object and null cases.
|
||||
class _NoteDataConverter implements JsonConverter<NoteData, Object?> {
|
||||
const _NoteDataConverter();
|
||||
|
||||
@override
|
||||
NoteData fromJson(Object? json) {
|
||||
if (json == null) return const NoteData();
|
||||
if (json is Map<String, dynamic>) {
|
||||
// Handle the nested content structure
|
||||
final contentJson = json['content'];
|
||||
NoteContent content = const NoteContent();
|
||||
if (contentJson is Map<String, dynamic>) {
|
||||
content = NoteContent.fromJson(contentJson);
|
||||
}
|
||||
|
||||
// Handle versions
|
||||
final versionsJson = json['versions'];
|
||||
List<NoteContent> versions = [];
|
||||
if (versionsJson is List) {
|
||||
versions = versionsJson
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map((v) => NoteContent.fromJson(v))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Handle files
|
||||
final filesJson = json['files'];
|
||||
List<Map<String, dynamic>>? files;
|
||||
if (filesJson is List) {
|
||||
files = filesJson.whereType<Map<String, dynamic>>().toList();
|
||||
}
|
||||
|
||||
return NoteData(content: content, versions: versions, files: files);
|
||||
}
|
||||
return const NoteData();
|
||||
}
|
||||
|
||||
@override
|
||||
Object? toJson(NoteData object) => object.toJson();
|
||||
}
|
||||
|
||||
/// Converter for metadata maps.
|
||||
class _MetadataConverter
|
||||
implements JsonConverter<Map<String, dynamic>?, Object?> {
|
||||
const _MetadataConverter();
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? fromJson(Object? json) {
|
||||
if (json == null) return null;
|
||||
if (json is Map<String, dynamic>) return json;
|
||||
if (json is Map) {
|
||||
return json.map((key, value) => MapEntry(key.toString(), value));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Object? toJson(Map<String, dynamic>? object) => object;
|
||||
}
|
||||
|
||||
/// Form data for creating a new note.
|
||||
@freezed
|
||||
sealed class NoteForm with _$NoteForm {
|
||||
const factory NoteForm({
|
||||
required String title,
|
||||
NoteData? data,
|
||||
Map<String, dynamic>? meta,
|
||||
@JsonKey(name: 'access_control') Map<String, dynamic>? accessControl,
|
||||
}) = _NoteForm;
|
||||
|
||||
factory NoteForm.fromJson(Map<String, dynamic> json) =>
|
||||
_$NoteFormFromJson(json);
|
||||
}
|
||||
|
||||
/// Form data for updating a note.
|
||||
@freezed
|
||||
sealed class NoteUpdateForm with _$NoteUpdateForm {
|
||||
const factory NoteUpdateForm({
|
||||
String? title,
|
||||
NoteData? data,
|
||||
Map<String, dynamic>? meta,
|
||||
@JsonKey(name: 'access_control') Map<String, dynamic>? accessControl,
|
||||
}) = _NoteUpdateForm;
|
||||
|
||||
factory NoteUpdateForm.fromJson(Map<String, dynamic> json) =>
|
||||
_$NoteUpdateFormFromJson(json);
|
||||
}
|
||||
@@ -2156,6 +2156,22 @@ class FoldersFeatureEnabledNotifier extends Notifier<bool> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks whether the notes feature is enabled on the server.
|
||||
/// When the server returns 403 for notes endpoint, this becomes false.
|
||||
final notesFeatureEnabledProvider =
|
||||
NotifierProvider<NotesFeatureEnabledNotifier, bool>(
|
||||
NotesFeatureEnabledNotifier.new,
|
||||
);
|
||||
|
||||
class NotesFeatureEnabledNotifier extends Notifier<bool> {
|
||||
@override
|
||||
bool build() => true;
|
||||
|
||||
void setEnabled(bool enabled) {
|
||||
state = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
// Folders provider
|
||||
@Riverpod(keepAlive: true)
|
||||
class Folders extends _$Folders {
|
||||
|
||||
@@ -16,6 +16,8 @@ import '../../features/auth/views/connection_issue_page.dart';
|
||||
import '../../features/auth/views/server_connection_page.dart';
|
||||
import '../../features/chat/views/chat_page.dart';
|
||||
import '../../features/navigation/views/splash_launcher_page.dart';
|
||||
import '../../features/notes/views/notes_list_page.dart';
|
||||
import '../../features/notes/views/note_editor_page.dart';
|
||||
import '../../features/profile/views/app_customization_page.dart';
|
||||
import '../../features/profile/views/profile_page.dart';
|
||||
import '../../l10n/app_localizations.dart';
|
||||
@@ -242,6 +244,22 @@ final goRouterProvider = Provider<GoRouter>((ref) {
|
||||
name: RouteNames.appCustomization,
|
||||
builder: (context, state) => const AppCustomizationPage(),
|
||||
),
|
||||
GoRoute(
|
||||
path: Routes.notes,
|
||||
name: RouteNames.notes,
|
||||
builder: (context, state) => const NotesListPage(),
|
||||
),
|
||||
GoRoute(
|
||||
path: Routes.noteEditor,
|
||||
name: RouteNames.noteEditor,
|
||||
builder: (context, state) {
|
||||
final noteId = state.pathParameters['id'];
|
||||
if (noteId == null || noteId.isEmpty) {
|
||||
return const NotesListPage();
|
||||
}
|
||||
return NoteEditorPage(noteId: noteId);
|
||||
},
|
||||
),
|
||||
];
|
||||
|
||||
final router = GoRouter(
|
||||
|
||||
@@ -2392,55 +2392,6 @@ class ApiService {
|
||||
return response.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> getNotes() async {
|
||||
_traceApi('Fetching notes');
|
||||
final response = await _dio.get('/api/v1/notes/');
|
||||
final data = response.data;
|
||||
if (data is List) {
|
||||
return data.cast<Map<String, dynamic>>();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> createNote({
|
||||
required String title,
|
||||
required String content,
|
||||
List<String>? tags,
|
||||
}) async {
|
||||
_traceApi('Creating note: $title');
|
||||
final response = await _dio.post(
|
||||
'/api/v1/notes/',
|
||||
data: {
|
||||
'title': title,
|
||||
'content': content,
|
||||
if (tags != null) 'tags': tags,
|
||||
},
|
||||
);
|
||||
return response.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<void> updateNote(
|
||||
String id, {
|
||||
String? title,
|
||||
String? content,
|
||||
List<String>? tags,
|
||||
}) async {
|
||||
_traceApi('Updating note: $id');
|
||||
await _dio.put(
|
||||
'/api/v1/notes/$id',
|
||||
data: {
|
||||
if (title != null) 'title': title,
|
||||
if (content != null) 'content': content,
|
||||
if (tags != null) 'tags': tags,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> deleteNote(String id) async {
|
||||
_traceApi('Deleting note: $id');
|
||||
await _dio.delete('/api/v1/notes/$id');
|
||||
}
|
||||
|
||||
// Team Collaboration
|
||||
Future<List<Map<String, dynamic>>> getChannels() async {
|
||||
_traceApi('Fetching channels');
|
||||
@@ -3585,6 +3536,226 @@ class ApiService {
|
||||
|
||||
// ==================== END ADVANCED CHAT FEATURES ====================
|
||||
|
||||
// ==================== NOTES ====================
|
||||
|
||||
/// Get all notes with user information.
|
||||
/// Returns a record with (notes data, feature enabled flag).
|
||||
/// When the notes feature is disabled server-side (403), returns ([], false).
|
||||
Future<(List<Map<String, dynamic>>, bool)> getNotes() async {
|
||||
try {
|
||||
_traceApi('Fetching notes');
|
||||
final response = await _dio.get('/api/v1/notes/');
|
||||
DebugLogger.log(
|
||||
'fetch-status',
|
||||
scope: 'api/notes',
|
||||
data: {'code': response.statusCode},
|
||||
);
|
||||
DebugLogger.log('fetch-ok', scope: 'api/notes');
|
||||
|
||||
final data = response.data;
|
||||
if (data is List) {
|
||||
_traceApi('Found ${data.length} notes');
|
||||
return (data.cast<Map<String, dynamic>>(), true);
|
||||
} else {
|
||||
DebugLogger.warning(
|
||||
'unexpected-type',
|
||||
scope: 'api/notes',
|
||||
data: {'type': data.runtimeType},
|
||||
);
|
||||
return (const <Map<String, dynamic>>[], true);
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
// 403 indicates notes feature is disabled server-side
|
||||
if (e.response?.statusCode == 403) {
|
||||
DebugLogger.log(
|
||||
'feature-disabled',
|
||||
scope: 'api/notes',
|
||||
data: {'status': 403},
|
||||
);
|
||||
return (const <Map<String, dynamic>>[], false);
|
||||
}
|
||||
DebugLogger.error('fetch-failed', scope: 'api/notes', error: e);
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
DebugLogger.error('fetch-failed', scope: 'api/notes', error: e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get paginated note list (title, id, timestamps only)
|
||||
Future<List<Map<String, dynamic>>> getNoteList({int? page}) async {
|
||||
_traceApi('Fetching note list, page: $page');
|
||||
final queryParams = <String, dynamic>{};
|
||||
if (page != null) queryParams['page'] = page;
|
||||
|
||||
final response = await _dio.get(
|
||||
'/api/v1/notes/list',
|
||||
queryParameters: queryParams.isNotEmpty ? queryParams : null,
|
||||
);
|
||||
final data = response.data;
|
||||
if (data is List) {
|
||||
return data.cast<Map<String, dynamic>>();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/// Get a single note by ID
|
||||
Future<Map<String, dynamic>> getNoteById(String id) async {
|
||||
_traceApi('Fetching note: $id');
|
||||
final response = await _dio.get('/api/v1/notes/$id');
|
||||
return response.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
/// Create a new note
|
||||
Future<Map<String, dynamic>> createNote({
|
||||
required String title,
|
||||
Map<String, dynamic>? data,
|
||||
Map<String, dynamic>? meta,
|
||||
Map<String, dynamic>? accessControl,
|
||||
}) async {
|
||||
_traceApi('Creating note: $title');
|
||||
final response = await _dio.post(
|
||||
'/api/v1/notes/create',
|
||||
data: {
|
||||
'title': title,
|
||||
if (data != null) 'data': data,
|
||||
if (meta != null) 'meta': meta,
|
||||
if (accessControl != null) 'access_control': accessControl,
|
||||
},
|
||||
);
|
||||
return response.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
/// Update an existing note
|
||||
Future<Map<String, dynamic>> updateNote(
|
||||
String id, {
|
||||
String? title,
|
||||
Map<String, dynamic>? data,
|
||||
Map<String, dynamic>? meta,
|
||||
Map<String, dynamic>? accessControl,
|
||||
}) async {
|
||||
_traceApi('Updating note: $id');
|
||||
final response = await _dio.post(
|
||||
'/api/v1/notes/$id/update',
|
||||
data: {
|
||||
if (title != null) 'title': title,
|
||||
if (data != null) 'data': data,
|
||||
if (meta != null) 'meta': meta,
|
||||
if (accessControl != null) 'access_control': accessControl,
|
||||
},
|
||||
);
|
||||
return response.data as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
/// Delete a note by ID
|
||||
Future<bool> deleteNote(String id) async {
|
||||
_traceApi('Deleting note: $id');
|
||||
final response = await _dio.delete('/api/v1/notes/$id/delete');
|
||||
return response.data == true;
|
||||
}
|
||||
|
||||
/// Generate a title for note content using AI
|
||||
Future<String?> generateNoteTitle(
|
||||
String content, {
|
||||
required String modelId,
|
||||
}) async {
|
||||
_traceApi('Generating title for note content with model: $modelId');
|
||||
|
||||
final prompt =
|
||||
'''### Task:
|
||||
Generate a concise, 3-5 word title with an emoji summarizing the content in the content's primary language.
|
||||
### Guidelines:
|
||||
- The title should clearly represent the main theme or subject of the content.
|
||||
- Use emojis that enhance understanding of the topic, but avoid quotation marks or special formatting.
|
||||
- Write the title in the content's primary language.
|
||||
- Prioritize accuracy over excessive creativity; keep it clear and simple.
|
||||
- Your entire response must consist solely of the JSON object, without any introductory or concluding text.
|
||||
- The output must be a single, raw JSON object, without any markdown code fences or other encapsulating text.
|
||||
- Ensure no conversational text, affirmations, or explanations precede or follow the raw JSON output, as this will cause direct parsing failure.
|
||||
### Output:
|
||||
JSON format: { "title": "your concise title here" }
|
||||
### Examples:
|
||||
- { "title": "📉 Stock Market Trends" },
|
||||
- { "title": "🍪 Perfect Chocolate Chip Recipe" },
|
||||
- { "title": "Evolution of Music Streaming" },
|
||||
- { "title": "Remote Work Productivity Tips" },
|
||||
- { "title": "Artificial Intelligence in Healthcare" },
|
||||
- { "title": "🎮 Video Game Development Insights" }
|
||||
### Content:
|
||||
<content>
|
||||
$content
|
||||
</content>''';
|
||||
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'/api/chat/completions',
|
||||
data: {
|
||||
'model': modelId,
|
||||
'stream': false,
|
||||
'messages': [
|
||||
{'role': 'user', 'content': prompt},
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
final responseText =
|
||||
response.data?['choices']?[0]?['message']?['content'] as String? ??
|
||||
'';
|
||||
|
||||
_traceApi('Title generation response: $responseText');
|
||||
|
||||
// Parse JSON from response
|
||||
final jsonStart = responseText.indexOf('{');
|
||||
final jsonEnd = responseText.lastIndexOf('}');
|
||||
|
||||
if (jsonStart != -1 && jsonEnd != -1) {
|
||||
final jsonStr = responseText.substring(jsonStart, jsonEnd + 1);
|
||||
final parsed = jsonDecode(jsonStr) as Map<String, dynamic>;
|
||||
return (parsed['title'] as String?)?.trim();
|
||||
}
|
||||
} catch (e) {
|
||||
_traceApi('Failed to generate note title: $e');
|
||||
rethrow;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Enhance note content using AI
|
||||
Future<String?> enhanceNoteContent(
|
||||
String content, {
|
||||
required String modelId,
|
||||
}) async {
|
||||
_traceApi('Enhancing note content with AI, model: $modelId');
|
||||
|
||||
const systemPrompt =
|
||||
'''Enhance existing notes using the content's primary language. Your task is to make the notes more useful and comprehensive.
|
||||
|
||||
# Output Format
|
||||
|
||||
Provide the enhanced notes in markdown format. Use markdown syntax for headings, lists, task lists ([ ]) where tasks or checklists are strongly implied, and emphasis to improve clarity and presentation. Ensure that all integrated content is accurately reflected. Return only the markdown formatted note.''';
|
||||
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
'/api/chat/completions',
|
||||
data: {
|
||||
'model': modelId,
|
||||
'stream': false,
|
||||
'messages': [
|
||||
{'role': 'system', 'content': systemPrompt},
|
||||
{'role': 'user', 'content': '<notes>$content</notes>'},
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
return response.data?['choices']?[0]?['message']?['content'] as String?;
|
||||
} catch (e) {
|
||||
_traceApi('Failed to enhance note content: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== END NOTES ====================
|
||||
|
||||
// Legacy streaming wrapper methods removed
|
||||
}
|
||||
|
||||
|
||||
@@ -103,6 +103,8 @@ class Routes {
|
||||
static const String authentication = '/authentication';
|
||||
static const String profile = '/profile';
|
||||
static const String appCustomization = '/profile/customization';
|
||||
static const String notes = '/notes';
|
||||
static const String noteEditor = '/notes/:id';
|
||||
}
|
||||
|
||||
/// Friendly names for GoRouter routes to support context.pushNamed.
|
||||
@@ -115,4 +117,6 @@ class RouteNames {
|
||||
static const String authentication = 'authentication';
|
||||
static const String profile = 'profile';
|
||||
static const String appCustomization = 'app-customization';
|
||||
static const String notes = 'notes';
|
||||
static const String noteEditor = 'note-editor';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user