Skip to content

Commit

Permalink
fixed name edits on Para not reflected in Scoold, added option para.n…
Browse files Browse the repository at this point in the history
…ame_edits_allowed, closes #114
  • Loading branch information
albogdano committed May 28, 2019
1 parent 2004d83 commit e858df9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -143,6 +143,10 @@ para.fb_app_id = "123456789"
para.google_client_id = "123-abcd.apps.googleusercontent.com"
# If true, the default space will be accessible by everyone
para.is_default_space_public = true
# If true, users can change their profile pictures
para.avatar_edits_enabled = true
# If true, users can change their names
para.name_edits_enabled = true
```

On startup, Scoold will try to connect to Para 10 times, with a 10 second interval between retries. After that it will
Expand Down
39 changes: 27 additions & 12 deletions src/main/java/com/erudika/scoold/controllers/ProfileController.java
Expand Up @@ -108,6 +108,7 @@ public String get(@PathVariable(required = false) String id, HttpServletRequest
model.addAttribute("itemcount2", itemcount2);
model.addAttribute("questionslist", questionslist);
model.addAttribute("answerslist", answerslist);
model.addAttribute("nameEditsAllowed", Config.getConfigBoolean("name_edits_enabled", true));
return "base";
}

Expand Down Expand Up @@ -148,10 +149,6 @@ public String edit(@PathVariable(required = false) String id, @RequestParam(requ

boolean updateProfile = false;

if (!StringUtils.isBlank(name)) {
showUser.setName(name);
updateProfile = true;
}
if (!StringUtils.isBlank(location)) {
showUser.setLocation(location);
updateProfile = true;
Expand All @@ -164,8 +161,9 @@ public String edit(@PathVariable(required = false) String id, @RequestParam(requ
showUser.setAboutme(aboutme);
updateProfile = true;
}
if (Config.getConfigBoolean("avatar_edits_enabled", true)) {
updateProfile = updatePicture(showUser, picture);
if (Config.getConfigBoolean("avatar_edits_enabled", true) ||
Config.getConfigBoolean("name_edits_enabled", true)) {
updateProfile = updateUserPictureAndName(showUser, picture, name);
}

boolean isComplete = showUser.isComplete() && isMyid(authUser, showUser.getId());
Expand All @@ -176,17 +174,34 @@ public String edit(@PathVariable(required = false) String id, @RequestParam(requ
return "redirect:" + PROFILELINK;
}

private boolean updatePicture(Profile showUser, String picture) {
if (!StringUtils.isBlank(picture) && (Utils.isValidURL(picture) || picture.startsWith("data:"))) {
private boolean updateUserPictureAndName(Profile showUser, String picture, String name) {
boolean updateProfile = false;
boolean updateUser = false;
User u = showUser.getUser();
if (Config.getConfigBoolean("avatar_edits_enabled", true) &&
!StringUtils.isBlank(picture) && (Utils.isValidURL(picture) || picture.startsWith("data:"))) {
showUser.setPicture(picture);
User u = showUser.getUser();
if (!u.getPicture().equals(picture) && !picture.contains("gravatar.com")) {
u.setPicture(picture);
utils.getParaClient().update(u);
updateUser = true;
}
updateProfile = true;
}
if (Config.getConfigBoolean("name_edits_enabled", true) && !StringUtils.isBlank(name)) {
showUser.setName(name);
if (StringUtils.isBlank(showUser.getOriginalName())) {
showUser.setOriginalName(name);
}
if (!u.getName().equals(name)) {
u.setName(name);
updateUser = true;
}
return true;
updateProfile = true;
}
if (updateUser) {
utils.getParaClient().update(u);
}
return false;
return updateProfile;
}

private boolean isMyid(Profile authUser, String id) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/erudika/scoold/core/Profile.java
Expand Up @@ -39,6 +39,7 @@ public class Profile extends Sysprop {

private static final long serialVersionUID = 1L;

@Stored private String originalName;
@Stored private Long lastseen;
@Stored private String location;
@Stored private String latlng;
Expand Down Expand Up @@ -290,6 +291,14 @@ public void setAboutme(String aboutme) {
this.aboutme = aboutme;
}

public String getOriginalName() {
return originalName;
}

public void setOriginalName(String originalName) {
this.originalName = originalName;
}

@SuppressWarnings("unchecked")
public List<Question> getAllQuestions(Pager pager) {
if (getId() == null) {
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Expand Up @@ -169,12 +169,7 @@ public Profile checkAuth(HttpServletRequest req, HttpServletResponse res) throws
update = true;
}
authUser.setUser(u);
if (!StringUtils.equals(u.getPicture(), authUser.getPicture()) &&
!StringUtils.contains(authUser.getPicture(), "gravatar.com")) {
authUser.setPicture(u.getPicture());
update = true;
}
if (update) {
if (update || updateProfilePictureAndName(authUser, u)) {
authUser.update();
}
} else {
Expand All @@ -191,6 +186,7 @@ private Profile getOrCreateProfile(User u, HttpServletRequest req) {
Profile authUser = pc.read(Profile.id(u.getId()));
if (authUser == null) {
authUser = new Profile(u.getId(), u.getName());
authUser.setOriginalName(u.getName());
authUser.setPicture(u.getPicture());
authUser.setAppid(u.getAppid());
authUser.setCreatorid(u.getId());
Expand All @@ -207,6 +203,24 @@ private Profile getOrCreateProfile(User u, HttpServletRequest req) {
return authUser;
}

private boolean updateProfilePictureAndName(Profile authUser, User u) {
boolean update = false;
if (!StringUtils.equals(u.getPicture(), authUser.getPicture())
&& !StringUtils.contains(authUser.getPicture(), "gravatar.com")
&& !Config.getConfigBoolean("avatar_edits_enabled", true)) {
authUser.setPicture(u.getPicture());
update = true;
}
if (!StringUtils.equals(u.getName(), authUser.getName())) {
if (!Config.getConfigBoolean("name_edits_enabled", true)) {
authUser.setName(u.getName());
}
authUser.setOriginalName(u.getName());
update = true;
}
return update;
}

public void sendWelcomeEmail(User user, boolean verifyEmail, HttpServletRequest req) {
// send welcome email notification
if (user != null) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/resources/templates/profile.vm
Expand Up @@ -58,6 +58,9 @@
#end
<div class="viewbox">
<span class="largeText">$!{showUser.name}</span>
#if ($isAdmin && $showUser.originalName)
<div class="tooltipped" data-tooltip="Original name">$!{showUser.originalName}</div>
#end
<div>
<span class="chip largeText phl tooltipped" data-tooltip="$!lang.get('reputation')">$!showUser.votes</span> &nbsp;&nbsp;
#if($badgesCount > 0)
Expand Down Expand Up @@ -176,11 +179,13 @@
<div id="about-edit" class="editbox lightgraybg pal ${hideedit1}">
<form method="post" id="about-edit-form" action="$profilelink/$!authUser.creatorid">
#sectoken(false)
<div class="input-field">
<i class="fa fa-user-circle prefix"></i>
<label for="name_text">$!{lang.get("profile.name")}</label>
<input id="name_text" name="name" class="validate" type="text" value="$!{showUser.name}">
</div>
#if($nameEditsAllowed)
<div class="input-field">
<i class="fa fa-user-circle prefix"></i>
<label for="name_text">$!{lang.get("profile.name")}</label>
<input id="name_text" name="name" class="validate" type="text" value="$!{showUser.name}">
</div>
#end

<div class="input-field">
<i class="fa fa-map-marker prefix"></i>
Expand Down

0 comments on commit e858df9

Please sign in to comment.