-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is exactly the same as the DisplayName enum in Damus iOS. Due to the various inconsistencies in `name` and `display_name` between clients, we have to generalize DisplayName into two variants: one name or two names. If we have two names, we treat it in the standard way of display_name is the real name, and `name` is the username. If only one is set, then it is considered both the username and "real name". Signed-off-by: William Casarin <[email protected]>
- Loading branch information
Showing
4 changed files
with
56 additions
and
19 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
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 |
---|---|---|
@@ -1,17 +1,39 @@ | ||
use nostrdb::ProfileRecord; | ||
|
||
pub fn get_profile_name<'a>(record: &'a ProfileRecord) -> Option<&'a str> { | ||
let profile = record.record().profile()?; | ||
let display_name = profile.display_name(); | ||
let name = profile.name(); | ||
pub enum DisplayName<'a> { | ||
One(&'a str), | ||
|
||
if display_name.is_some() && display_name.unwrap() != "" { | ||
return display_name; | ||
} | ||
Both { | ||
username: &'a str, | ||
display_name: &'a str, | ||
}, | ||
} | ||
|
||
if name.is_some() && name.unwrap() != "" { | ||
return name; | ||
impl<'a> DisplayName<'a> { | ||
pub fn username(&self) -> &'a str { | ||
match self { | ||
Self::One(n) => n, | ||
Self::Both { username, .. } => username, | ||
} | ||
} | ||
} | ||
|
||
None | ||
fn is_empty(s: &str) -> bool { | ||
s.chars().all(|c| c.is_whitespace()) | ||
} | ||
|
||
pub fn get_profile_name<'a>(record: &'a ProfileRecord) -> Option<DisplayName<'a>> { | ||
let profile = record.record().profile()?; | ||
let display_name = profile.display_name().filter(|n| !is_empty(n)); | ||
let name = profile.name().filter(|n| !is_empty(n)); | ||
|
||
match (display_name, name) { | ||
(None, None) => None, | ||
(Some(disp), None) => Some(DisplayName::One(disp)), | ||
(None, Some(username)) => Some(DisplayName::One(username)), | ||
(Some(display_name), Some(username)) => Some(DisplayName::Both { | ||
display_name, | ||
username, | ||
}), | ||
} | ||
} |
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