-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve handling of deleted users in templates
- Loading branch information
Showing
54 changed files
with
216 additions
and
121 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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package smithereen.templates; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.pebbletemplates.pebble.error.PebbleException; | ||
import io.pebbletemplates.pebble.extension.Filter; | ||
import io.pebbletemplates.pebble.template.EvaluationContext; | ||
import io.pebbletemplates.pebble.template.PebbleTemplate; | ||
import smithereen.model.Group; | ||
import smithereen.model.User; | ||
|
||
public class NameFilter implements Filter{ | ||
@Override | ||
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException{ | ||
if(input instanceof User user){ | ||
String format=(String) args.getOrDefault("format", "full"); | ||
return switch(format){ | ||
case "first" -> user.firstName; | ||
case "last" -> user.lastName; | ||
case "middle" -> user.middleName; | ||
case "maiden" -> user.maidenName; | ||
case "full" -> user.getFullName(); | ||
case "complete" -> user.getCompleteName(); | ||
case "firstAndGender" -> user.getFirstAndGender(); | ||
case "fullAndGender" -> user.getFirstLastAndGender(); | ||
default -> throw new IllegalStateException("Unexpected value: "+format); | ||
}; | ||
}else if(input instanceof Group group){ | ||
return group.name; | ||
} | ||
return "DELETED"; | ||
} | ||
|
||
@Override | ||
public List<String> getArgumentNames(){ | ||
return List.of("format"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/smithereen/templates/ProfileUrlFunction.java
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,41 @@ | ||
package smithereen.templates; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.pebbletemplates.pebble.extension.Function; | ||
import io.pebbletemplates.pebble.template.EvaluationContext; | ||
import io.pebbletemplates.pebble.template.PebbleTemplate; | ||
import smithereen.model.Group; | ||
import smithereen.model.User; | ||
|
||
public class ProfileUrlFunction implements Function{ | ||
@Override | ||
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber){ | ||
int id=(Integer)args.get("id"); | ||
if(id>0){ | ||
Map<Integer, User> users=(Map<Integer, User>) context.getVariable("users"); | ||
if(users==null) | ||
return "/id"+id; | ||
User user=users.get(id); | ||
if(user==null) | ||
return "/id"+id; | ||
return user.getProfileURL(); | ||
}else if(id<0){ | ||
id=-id; | ||
Map<Integer, Group> groups=(Map<Integer, Group>) context.getVariable("groups"); | ||
if(groups==null) | ||
return "/club"+id; | ||
Group group=groups.get(id); | ||
if(group==null) | ||
return "/club"+id; | ||
return group.getProfileURL(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<String> getArgumentNames(){ | ||
return List.of("id"); | ||
} | ||
} |
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
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
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
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
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
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
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.