Skip to content

Commit

Permalink
Add support for custom user labels (thunder-app#1434)
Browse files Browse the repository at this point in the history
* Add support for user labels

* Reformat auto-gen db file

* Fix tests

* Move dialog tertiary action to left

* Add comments to UserLabel model parameters

* Add comment to UserLabel class

* Add downgrade strategy
  • Loading branch information
micahmo authored Jun 10, 2024
1 parent b2be10c commit 596e36c
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 99 deletions.
94 changes: 94 additions & 0 deletions lib/account/models/user_label.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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';

/// Represents a UserLabel, which is used to associate a textual description along with a Lemmy user.
/// Contains helper methods to load/save corresponding objects in the database.
class UserLabel {
/// The ID of the object in the database (should never need to be set explicitly).
final String id;

/// The username of the user being labeled (in the form [email protected]).
/// Use [usernameFromParts] to consistently generate this.
final String username;

/// The label which is being applied to the user.
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)}';
}
}
21 changes: 19 additions & 2 deletions lib/core/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@ 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 we are migrating from 1 to anything higher
if (from == 1 && to > 1) {
// Create the UserLabels table
await migrator.createTable(userLabels);
}

// If we are downgrading from 2 or higher to 1
if (from >= 2 && to == 1) {
// Delete the UserBales table
await migrator.deleteTable('user_labels');
}
},
);
}

/// 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 @@ -979,6 +983,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 @@ -2365,6 +2373,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

0 comments on commit 596e36c

Please sign in to comment.