Skip to content

Commit

Permalink
Detect LynxChan instances
Browse files Browse the repository at this point in the history
  • Loading branch information
TheStranjer committed Dec 20, 2019
1 parent 9b10115 commit aa4f22d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.github.adamantcheese.chan.core.presenter;

import android.content.Context;
import android.widget.Toast;

import com.github.adamantcheese.chan.R;
Expand Down Expand Up @@ -107,8 +108,8 @@ public void onShowDialogClicked() {
callback.showAddDialog();
}

public void onAddClicked(String url) {
siteService.addSite(url, new SiteService.SiteAddCallback() {
public void onAddClicked(String url, Context ctx) {
siteService.addSite(url, ctx, new SiteService.SiteAddCallback() {
@Override
public void onSiteAdded(Site site) {
siteAdded(site);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
import com.github.adamantcheese.chan.core.settings.json.JsonSettings;

import java.util.List;
import java.net.URI;
import java.net.URISyntaxException;

import com.github.adamantcheese.chan.core.site.sites.lynxchan.LynxChanIndexRequest;
import com.github.adamantcheese.chan.core.site.sites.lynxchan.LynxChanIndexResult;

import android.content.Context;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.HurlStack;

import javax.inject.Inject;

Expand All @@ -30,6 +44,8 @@ public class SiteService {

private boolean initialized = false;

RequestQueue requestQueue;

@Inject
public SiteService(SiteRepository siteRepository, SiteResolver resolver) {
this.siteRepository = siteRepository;
Expand All @@ -40,7 +56,7 @@ public boolean areSitesSetup() {
return !siteRepository.all().getAll().isEmpty();
}

public void addSite(String url, SiteAddCallback callback) {
public void addSite(String url, Context ctx, SiteAddCallback callback) {
Site existing = resolver.findSiteForUrl(url);
if (existing != null) {
callback.onSiteAddFailed("site already added");
Expand All @@ -53,7 +69,25 @@ public void addSite(String url, SiteAddCallback callback) {
if (resolve.match == SiteResolver.SiteResolverResult.Match.BUILTIN) {
siteClass = resolve.builtinResult;
} else if (resolve.match == SiteResolver.SiteResolverResult.Match.EXTERNAL) {
callback.onSiteAddFailed("external sites not hardcoded is not implemented yet");
callback.onSiteAddFailed("not detected in predefined list, checking if LynxChan instance...");

Cache cache = new DiskBasedCache(ctx.getCacheDir(), 1024 * 1024); // 1MB cap
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
try {
URI uri = new URI(url);

// hacky little trick that gets around the fact that new URI("16chan.xyz")
// sets the host to null and the path to "16chan.xyz" while
// new URI("https://16chan.xyz/") sets the host to "16chan.xyz" and the
// path to null
String host = uri.getHost() != null ? uri.getHost() : uri.getPath();
requestQueue.add(new LynxChanIndexRequest(host, response -> { lynxChanIndexResponse(response, callback); }, error -> { lynxChanIndexFailure(error, callback); }));
} catch (URISyntaxException e) {
callback.onSiteAddFailed("not a url");
}

return;
} else {
callback.onSiteAddFailed("not a url");
Expand All @@ -65,6 +99,18 @@ public void addSite(String url, SiteAddCallback callback) {
callback.onSiteAdded(site);
}

public void lynxChanIndexResponse(LynxChanIndexResult response, SiteAddCallback callback) {
if (response.IsLynxChanInstance()) {
callback.onSiteAddFailed("detected LynxChan instance, but not implemented yet");
} else {
callback.onSiteAddFailed("not a valid LynxChan instance");
}
}

public void lynxChanIndexFailure(VolleyError error, SiteAddCallback callback) {
callback.onSiteAddFailed("not in premade list and can't detect chansite instance");
}

public void updateUserSettings(Site site, JsonSettings jsonSettings) {
SiteModel siteModel = siteRepository.byId(site.id());
if (siteModel == null) throw new NullPointerException("siteModel == null");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Kuroba - *chan browser https://github.com/TheStranjer/Kuroba (fork of https://github.com/Adamantcheese/Kuroba)
* Copyright (C) 2019 TheStranjer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.adamantcheese.chan.core.site.sites.lynxchan;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import com.github.adamantcheese.chan.core.net.JsonReaderRequest;
import android.util.JsonReader;
import android.util.JsonToken;

import okhttp3.HttpUrl;

public class LynxChanIndexRequest extends JsonReaderRequest<LynxChanIndexResult> {
private static final String TAG = "LynxChanInstanceCheck";
public LynxChanIndexRequest(String hostname, Listener<LynxChanIndexResult> listener, ErrorListener errorListener) {
super(new HttpUrl.Builder().scheme("https").host(hostname).addPathSegment("index.json").build().toString(), listener, errorListener);
}

@Override
public LynxChanIndexResult readJson(JsonReader reader) throws Exception {
reader.beginObject();
while (reader.hasNext()) {
if (reader.peek() != JsonToken.NAME) {
reader.skipValue();
continue;
}

String key = reader.nextName();
switch (key) {
case "version":
String nextString = reader.nextString();
return new LynxChanIndexResult(nextString);
default:
break;
}
}

return new LynxChanIndexResult();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.adamantcheese.chan.core.site.sites.lynxchan;

public class LynxChanIndexResult {
public String version;

public LynxChanIndexResult(String version) {
this.version = version;
}

public LynxChanIndexResult() {}

public boolean IsLynxChanInstance() {
return version != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class SiteAddLayout
private Dialog dialog;
private SitesSetupPresenter presenter;

private Context ctx;

public SiteAddLayout(Context context) {
this(context, null);
}
Expand All @@ -29,6 +31,7 @@ public SiteAddLayout(Context context, AttributeSet attrs) {

public SiteAddLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.ctx = context;
}

@Override
Expand Down Expand Up @@ -60,7 +63,7 @@ protected void onDetachedFromWindow() {
}

public void onPositiveClicked() {
presenter.onAddClicked(url.getText().toString());
presenter.onAddClicked(url.getText().toString(), ctx);
}

@Override
Expand Down

0 comments on commit aa4f22d

Please sign in to comment.