Commit 790d8fae authored by Yusuke Iwaki's avatar Yusuke Iwaki

add auth cookie header for downloading attachment images.

parent 2ed64d4b
......@@ -97,7 +97,7 @@ dependencies {
compile 'com.google.firebase:firebase-core:10.0.0'
compile 'com.google.firebase:firebase-crash:10.0.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile rootProject.ext.okhttp3
compile rootProject.ext.picasso
compile 'com.facebook.stetho:stetho:1.4.1'
......
......@@ -23,7 +23,9 @@ import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmModelListAdapter;
import chat.rocket.android.realm_helper.RealmObjectObserver;
......@@ -48,6 +50,8 @@ public class RoomFragment extends AbstractChatRoomFragment
private String roomId;
private RealmObjectObserver<RoomSubscription> roomObserver;
private String hostname;
private String userId;
private String token;
private LoadMoreScrollListener scrollListener;
private RealmObjectObserver<LoadMessageProcedure> procedureObserver;
private MessageComposerManager messageComposerManager;
......@@ -79,6 +83,10 @@ public class RoomFragment extends AbstractChatRoomFragment
.equalTo("serverConfigId", serverConfigId)
.isNotNull("hostname")
.findFirst()).getHostname();
userId = realmHelper.executeTransactionForRead(realm ->
User.queryCurrentUser(realm).findFirst()).get_id();
token = realmHelper.executeTransactionForRead(realm ->
realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID).findFirst()).getToken();
roomObserver = realmHelper
.createObjectObserver(realm -> realm.where(RoomSubscription.class).equalTo("rid", roomId))
.setOnUpdateListener(this::onRenderRoom);
......@@ -102,7 +110,7 @@ public class RoomFragment extends AbstractChatRoomFragment
realm -> realm.where(Message.class)
.equalTo("rid", roomId)
.findAllSorted("ts", Sort.DESCENDING),
context -> new MessageListAdapter(context, hostname)
context -> new MessageListAdapter(context, hostname, userId, token)
);
listView.setAdapter(adapter);
adapter.setOnItemClickListener(this);
......
......@@ -15,12 +15,16 @@ import java.util.List;
public class MessageListAdapter
extends ExtRealmModelListAdapter<Message, PairedMessage, MessageViewHolder> {
private final String hostname;
private final String userId;
private final String token;
private boolean hasNext;
private boolean isLoaded;
public MessageListAdapter(Context context, String hostname) {
public MessageListAdapter(Context context, String hostname, String userId, String token) {
super(context);
this.hostname = hostname;
this.userId = userId;
this.token = token;
}
/**
......@@ -53,7 +57,7 @@ public class MessageListAdapter
}
@Override protected MessageViewHolder onCreateRealmModelViewHolder(int viewType, View itemView) {
return new MessageViewHolder(itemView, hostname);
return new MessageViewHolder(itemView, hostname, userId, token);
}
@Override protected List<PairedMessage> mapResultsToViewModel(List<Message> results) {
......
......@@ -23,6 +23,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
private final TextView timestamp;
private final View userAndTimeContainer;
private final String hostname;
private final String userId;
private final String token;
private final RocketChatMessageLayout body;
private final RocketChatMessageUrlsLayout urls;
private final RocketChatMessageAttachmentsLayout attachments;
......@@ -32,7 +34,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
/**
* constructor WITH hostname.
*/
public MessageViewHolder(View itemView, String hostname) {
public MessageViewHolder(View itemView, String hostname, String userId, String token) {
super(itemView);
avatar = (ImageView) itemView.findViewById(R.id.user_avatar);
username = (TextView) itemView.findViewById(R.id.username);
......@@ -45,6 +47,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
newDayContainer = itemView.findViewById(R.id.newday_container);
newDayText = (TextView) itemView.findViewById(R.id.newday_text);
this.hostname = hostname;
this.userId = userId;
this.token = token;
}
/**
......@@ -57,7 +61,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
.timestampInto(timestamp)
.bodyInto(body)
.urlsInto(urls)
.attachmentsInto(attachments, hostname);
.attachmentsInto(attachments, hostname, userId, token);
if (pairedMessage.target != null) {
int syncstate = pairedMessage.target.getSyncstate();
......
......@@ -105,7 +105,7 @@ public class MessageRenderer extends AbstractRenderer<Message> {
* show urls in RocketChatMessageUrlsLayout.
*/
public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout,
String hostname) {
String hostname, String userId, String token) {
if (!shouldHandle(attachmentsLayout)) {
return this;
}
......@@ -116,6 +116,7 @@ public class MessageRenderer extends AbstractRenderer<Message> {
} else {
attachmentsLayout.setVisibility(View.VISIBLE);
attachmentsLayout.setHostname(hostname);
attachmentsLayout.setCredential(userId, token);
attachmentsLayout.setAttachments(attachments);
}
......
......@@ -17,7 +17,9 @@ ext {
rxJava = 'io.reactivex:rxjava:1.2.2'
boltsTask = 'com.parse.bolts:bolts-tasks:1.4.0'
timber = 'com.jakewharton.timber:timber:4.3.1'
okhttp3 = 'com.squareup.okhttp3:okhttp:3.4.1'
picasso = 'com.squareup.picasso:picasso:2.5.2'
picasso2Okhttp3Downloader = 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
}
subprojects { project ->
......
......@@ -40,5 +40,7 @@ dependencies {
compile rootProject.ext.supportAppCompat
compile rootProject.ext.supportDesign
compile 'org.nibor.autolink:autolink:0.5.0'
compile rootProject.ext.okhttp3
compile rootProject.ext.picasso
compile rootProject.ext.picasso2Okhttp3Downloader
}
......@@ -14,7 +14,13 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.ImageFormat;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -25,6 +31,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
private LayoutInflater inflater;
private String hostname;
private String userId;
private String token;
private OkHttp3Downloader downloader;
public RocketChatMessageAttachmentsLayout(Context context) {
super(context);
initialize(context, null);
......@@ -56,6 +66,32 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
this.hostname = hostname;
}
public void setCredential(String userId, String token) {
this.userId = userId;
this.token = token;
}
private OkHttp3Downloader getDownloader() {
if (downloader == null) {
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
// uid/token is required to download attachment files.
// see: RocketChat:lib/fileUpload.coffee
Request newRequest = chain.request().newBuilder()
.header("Cookie", "rc_uid=" + userId + ";rc_token=" + token)
.build();
return chain.proceed(newRequest);
}
};
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
downloader = new OkHttp3Downloader(okHttpClient);
}
return downloader;
}
public void setAttachments(String attachmentsString) {
removeAllViews();
......@@ -87,9 +123,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false);
imageURL = absolutize(imageURL);
Picasso.with(getContext())
.load(imageURL)
new Picasso.Builder(getContext())
.downloader(getDownloader())
.build()
.load(absolutize(imageURL))
.placeholder(R.drawable.image_dummy)
.error(R.drawable.image_error)
.into((ImageView) attachmentView.findViewById(R.id.image));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment