forked from thunder-app/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom user labels (thunder-app#1434)
* 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
Showing
9 changed files
with
522 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.