Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch custom emojis with auth token #577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ private AccountSessionManager(){
File file=new File(MastodonApp.context.getFilesDir(), "accounts.json");
if(!file.exists())
return;
HashSet<String> domains=new HashSet<>();
HashMap<String, Token> domains=new HashMap<>();
try(FileInputStream in=new FileInputStream(file)){
SessionsStorageWrapper w=MastodonAPIController.gson.fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), SessionsStorageWrapper.class);
for(AccountSession session:w.accounts){
domains.add(session.domain.toLowerCase());
domains.put(session.domain.toLowerCase(), session.token);
sessions.put(session.getID(), session);
}
}catch(Exception x){
Expand All @@ -126,7 +126,7 @@ public void addAccount(Instance instance, Token token, Account self, Application
wrapper.instance = instance;
MastodonAPIController.runInBackground(()->writeInstanceInfoFile(wrapper, instance.uri));

updateMoreInstanceInfo(instance, instance.uri);
updateMoreInstanceInfo(instance, instance.uri, token);
if (!UnifiedPush.getDistributor(context).isEmpty()) {
UnifiedPush.registerApp(
context,
Expand Down Expand Up @@ -303,9 +303,9 @@ public void maybeUpdateLocalInfo(){

public void maybeUpdateLocalInfo(AccountSession activeSession){
long now=System.currentTimeMillis();
HashSet<String> domains=new HashSet<>();
HashMap<String, Token> domains=new HashMap<>();
for(AccountSession session:sessions.values()){
domains.add(session.domain.toLowerCase());
domains.put(session.domain.toLowerCase(), session.token);
if(session == activeSession || now-session.infoLastUpdated>24L*3600_000L){
session.reloadPreferences(null);
updateSessionLocalInfo(session);
Expand All @@ -319,12 +319,12 @@ public void maybeUpdateLocalInfo(AccountSession activeSession){
}
}

private void maybeUpdateCustomEmojis(Set<String> domains, String activeDomain){
private void maybeUpdateCustomEmojis(HashMap<String, Token> domains, String activeDomain){
long now=System.currentTimeMillis();
for(String domain:domains){
for(String domain:domains.keySet()){
Long lastUpdated=instancesLastUpdated.get(domain);
if(domain.equals(activeDomain) || lastUpdated==null || now-lastUpdated>24L*3600_000L){
updateInstanceInfo(domain);
updateInstanceInfo(domain, domains.get(domain));
}
}
}
Expand Down Expand Up @@ -366,13 +366,13 @@ public void onError(ErrorResponse error){
.exec(session.getID());
}

public void updateInstanceInfo(String domain){
public void updateInstanceInfo(String domain, Token token){
new GetInstance()
.setCallback(new Callback<>(){
@Override
public void onSuccess(Instance instance){
instances.put(domain, instance);
updateMoreInstanceInfo(instance, domain);
updateMoreInstanceInfo(instance, domain, token);
}

@Override
Expand All @@ -383,22 +383,22 @@ public void onError(ErrorResponse error){
.execNoAuth(domain);
}

public void updateMoreInstanceInfo(Instance instance, String domain) {
public void updateMoreInstanceInfo(Instance instance, String domain, Token token) {
new GetInstance.V2().setCallback(new Callback<>() {
@Override
public void onSuccess(Instance.V2 v2) {
if (instance != null) instance.v2 = v2;
updateInstanceEmojis(instance, domain);
updateInstanceEmojis(instance, domain, token);
}

@Override
public void onError(ErrorResponse errorResponse) {
updateInstanceEmojis(instance, domain);
updateInstanceEmojis(instance, domain, token);
}
}).execNoAuth(instance.uri);
}

private void updateInstanceEmojis(Instance instance, String domain){
private void updateInstanceEmojis(Instance instance, String domain, Token token){
new GetCustomEmojis()
.setCallback(new Callback<>(){
@Override
Expand All @@ -420,7 +420,7 @@ public void onError(ErrorResponse error){
MastodonAPIController.runInBackground(()->writeInstanceInfoFile(wrapper, domain));
}
})
.execNoAuth(domain);
.exec(domain, token);
}

private File getInstanceInfoFile(String domain){
Expand All @@ -440,8 +440,8 @@ private void writeInstanceInfoFile(InstanceInfoStorageWrapper emojis, String dom
}
}

private void readInstanceInfo(Set<String> domains){
for(String domain:domains){
private void readInstanceInfo(HashMap<String, Token> domains){
for(String domain:domains.keySet()){
try(FileInputStream in=new FileInputStream(getInstanceInfoFile(domain))){
InputStreamReader reader=new InputStreamReader(in, StandardCharsets.UTF_8);
InstanceInfoStorageWrapper emojis=MastodonAPIController.gson.fromJson(reader, InstanceInfoStorageWrapper.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void onCreate(Bundle savedInstanceState){
return;
}
if(customEmojis.isEmpty()){
AccountSessionManager.getInstance().updateInstanceInfo(instanceDomain);
AccountSessionManager.getInstance().updateInstanceInfo(instanceDomain, session.token);
}

Bundle bundle = savedInstanceState != null ? savedInstanceState : getArguments();
Expand Down