Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user labels #1434

Merged
merged 8 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions lib/account/models/user_label.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'package:drift/drift.dart';
import 'package:flutter/foundation.dart';

import 'package:thunder/core/database/database.dart';
import 'package:thunder/main.dart';
import 'package:thunder/utils/instance.dart';

class UserLabel {
micahmo marked this conversation as resolved.
Show resolved Hide resolved
final String id;
micahmo marked this conversation as resolved.
Show resolved Hide resolved
final String username;
final String label;

const UserLabel({
required this.id,
required this.username,
required this.label,
});

UserLabel copyWith({String? id}) => UserLabel(
id: id ?? this.id,
username: username,
label: label,
);

static Future<UserLabel?> upsertUserLabel(UserLabel userLabel) async {
try {
// Check if the userLabel with the given username already exists
final existingUserLabel = await (database.select(database.userLabels)..where((t) => t.username.equals(userLabel.username))).getSingleOrNull();

if (existingUserLabel == null) {
// Insert new userLabel if it doesn't exist
int id = await database.into(database.userLabels).insert(
UserLabelsCompanion.insert(
username: userLabel.username,
label: userLabel.label,
),
);
return userLabel.copyWith(id: id.toString());
} else {
// Update existing userLabel if it exists
await database.update(database.userLabels).replace(
UserLabelsCompanion(
id: Value(existingUserLabel.id),
username: Value(userLabel.username),
label: Value(userLabel.label),
),
);
return userLabel.copyWith(id: existingUserLabel.id.toString());
}
} catch (e) {
debugPrint(e.toString());
return null;
}
}

static Future<UserLabel?> fetchUserLabel(String username) async {
if (username.isEmpty) return null;

try {
return await (database.select(database.userLabels)..where((t) => t.username.equals(username))).getSingleOrNull().then((userLabel) {
if (userLabel == null) return null;
return UserLabel(
id: userLabel.id.toString(),
username: userLabel.username,
label: userLabel.label,
);
});
} catch (e) {
debugPrint(e.toString());
return null;
}
}

static Future<void> deleteUserLabel(String username) async {
try {
await (database.delete(database.userLabels)..where((t) => t.username.equals(username))).go();
} catch (e) {
debugPrint(e.toString());
}
}

/// Generates a username string that can be used to uniquely identify entries in the UserLabels table
static String usernameFromParts(String username, String actorId) {
return '$username@${fetchInstanceNameFromUrl(actorId)}';
}
}
14 changes: 12 additions & 2 deletions lib/core/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ import 'package:thunder/core/database/tables.dart';

part 'database.g.dart';

@DriftDatabase(tables: [Accounts, Favorites, LocalSubscriptions])
@DriftDatabase(tables: [Accounts, Favorites, LocalSubscriptions, UserLabels])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());

@override
int get schemaVersion => 1;
int get schemaVersion => 2;

@override
MigrationStrategy get migration => MigrationStrategy(
onUpgrade: (migrator, from, to) async {
if (from == 1) {
// The only change from schema 1 is the UserLabels table
await migrator.createTable(userLabels);
}
},
);
}

/// Opens a connection to the database.
Expand Down
185 changes: 184 additions & 1 deletion lib/core/database/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/core/database/tables.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ class LocalSubscriptions extends Table {
TextColumn get actorId => text()();
TextColumn get icon => text().nullable()();
}

class UserLabels extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get username => text()();
TextColumn get label => text()();
}
12 changes: 12 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
"@addToFavorites": {
"description": "Action to add a community in drawer to favorites"
},
"addUserLabel": "Add User Label",
"@addUserLabel": {
"description": "Action for adding a user label"
},
"addedCommunityToSubscriptions": "Subscribed to community",
"@addedCommunityToSubscriptions": {},
"addedInstanceMod": "Added Instance Mod",
Expand Down Expand Up @@ -939,6 +943,10 @@
"@keywordFilters": {
"description": "Subcategory in Settings -> Filters"
},
"label": "Label",
"@label": {
"description": "Text field label for a user label"
},
"language": "Language",
"@language": {
"description": "Label when creating or editing a post."
Expand Down Expand Up @@ -2309,6 +2317,10 @@
"@userFormat": {
"description": "Setting for user full name format"
},
"userLabelHint": "This is my favorite user",
"@userLabelHint": {
"description": "Hint text for adding a new user label"
},
"userNameColor": "User Name Color",
"@userNameColor": {
"description": "Setting for username color"
Expand Down
Loading
Loading