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 { ...@@ -97,7 +97,7 @@ dependencies {
compile 'com.google.firebase:firebase-core:10.0.0' compile 'com.google.firebase:firebase-core:10.0.0'
compile 'com.google.firebase:firebase-crash: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 rootProject.ext.picasso
compile 'com.facebook.stetho:stetho:1.4.1' compile 'com.facebook.stetho:stetho:1.4.1'
......
...@@ -23,7 +23,9 @@ import chat.rocket.android.model.ServerConfig; ...@@ -23,7 +23,9 @@ import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState; import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message; import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.model.ddp.RoomSubscription; 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.LoadMessageProcedure;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmModelListAdapter; import chat.rocket.android.realm_helper.RealmModelListAdapter;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
...@@ -48,6 +50,8 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -48,6 +50,8 @@ public class RoomFragment extends AbstractChatRoomFragment
private String roomId; private String roomId;
private RealmObjectObserver<RoomSubscription> roomObserver; private RealmObjectObserver<RoomSubscription> roomObserver;
private String hostname; private String hostname;
private String userId;
private String token;
private LoadMoreScrollListener scrollListener; private LoadMoreScrollListener scrollListener;
private RealmObjectObserver<LoadMessageProcedure> procedureObserver; private RealmObjectObserver<LoadMessageProcedure> procedureObserver;
private MessageComposerManager messageComposerManager; private MessageComposerManager messageComposerManager;
...@@ -79,6 +83,10 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -79,6 +83,10 @@ public class RoomFragment extends AbstractChatRoomFragment
.equalTo("serverConfigId", serverConfigId) .equalTo("serverConfigId", serverConfigId)
.isNotNull("hostname") .isNotNull("hostname")
.findFirst()).getHostname(); .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 roomObserver = realmHelper
.createObjectObserver(realm -> realm.where(RoomSubscription.class).equalTo("rid", roomId)) .createObjectObserver(realm -> realm.where(RoomSubscription.class).equalTo("rid", roomId))
.setOnUpdateListener(this::onRenderRoom); .setOnUpdateListener(this::onRenderRoom);
...@@ -102,7 +110,7 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -102,7 +110,7 @@ public class RoomFragment extends AbstractChatRoomFragment
realm -> realm.where(Message.class) realm -> realm.where(Message.class)
.equalTo("rid", roomId) .equalTo("rid", roomId)
.findAllSorted("ts", Sort.DESCENDING), .findAllSorted("ts", Sort.DESCENDING),
context -> new MessageListAdapter(context, hostname) context -> new MessageListAdapter(context, hostname, userId, token)
); );
listView.setAdapter(adapter); listView.setAdapter(adapter);
adapter.setOnItemClickListener(this); adapter.setOnItemClickListener(this);
......
...@@ -15,12 +15,16 @@ import java.util.List; ...@@ -15,12 +15,16 @@ import java.util.List;
public class MessageListAdapter public class MessageListAdapter
extends ExtRealmModelListAdapter<Message, PairedMessage, MessageViewHolder> { extends ExtRealmModelListAdapter<Message, PairedMessage, MessageViewHolder> {
private final String hostname; private final String hostname;
private final String userId;
private final String token;
private boolean hasNext; private boolean hasNext;
private boolean isLoaded; private boolean isLoaded;
public MessageListAdapter(Context context, String hostname) { public MessageListAdapter(Context context, String hostname, String userId, String token) {
super(context); super(context);
this.hostname = hostname; this.hostname = hostname;
this.userId = userId;
this.token = token;
} }
/** /**
...@@ -53,7 +57,7 @@ public class MessageListAdapter ...@@ -53,7 +57,7 @@ public class MessageListAdapter
} }
@Override protected MessageViewHolder onCreateRealmModelViewHolder(int viewType, View itemView) { @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) { @Override protected List<PairedMessage> mapResultsToViewModel(List<Message> results) {
......
...@@ -23,6 +23,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -23,6 +23,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
private final TextView timestamp; private final TextView timestamp;
private final View userAndTimeContainer; private final View userAndTimeContainer;
private final String hostname; private final String hostname;
private final String userId;
private final String token;
private final RocketChatMessageLayout body; private final RocketChatMessageLayout body;
private final RocketChatMessageUrlsLayout urls; private final RocketChatMessageUrlsLayout urls;
private final RocketChatMessageAttachmentsLayout attachments; private final RocketChatMessageAttachmentsLayout attachments;
...@@ -32,7 +34,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -32,7 +34,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
/** /**
* constructor WITH hostname. * constructor WITH hostname.
*/ */
public MessageViewHolder(View itemView, String hostname) { public MessageViewHolder(View itemView, String hostname, String userId, String token) {
super(itemView); super(itemView);
avatar = (ImageView) itemView.findViewById(R.id.user_avatar); avatar = (ImageView) itemView.findViewById(R.id.user_avatar);
username = (TextView) itemView.findViewById(R.id.username); username = (TextView) itemView.findViewById(R.id.username);
...@@ -45,6 +47,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -45,6 +47,8 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
newDayContainer = itemView.findViewById(R.id.newday_container); newDayContainer = itemView.findViewById(R.id.newday_container);
newDayText = (TextView) itemView.findViewById(R.id.newday_text); newDayText = (TextView) itemView.findViewById(R.id.newday_text);
this.hostname = hostname; this.hostname = hostname;
this.userId = userId;
this.token = token;
} }
/** /**
...@@ -57,7 +61,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> { ...@@ -57,7 +61,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
.timestampInto(timestamp) .timestampInto(timestamp)
.bodyInto(body) .bodyInto(body)
.urlsInto(urls) .urlsInto(urls)
.attachmentsInto(attachments, hostname); .attachmentsInto(attachments, hostname, userId, token);
if (pairedMessage.target != null) { if (pairedMessage.target != null) {
int syncstate = pairedMessage.target.getSyncstate(); int syncstate = pairedMessage.target.getSyncstate();
......
...@@ -105,7 +105,7 @@ public class MessageRenderer extends AbstractRenderer<Message> { ...@@ -105,7 +105,7 @@ public class MessageRenderer extends AbstractRenderer<Message> {
* show urls in RocketChatMessageUrlsLayout. * show urls in RocketChatMessageUrlsLayout.
*/ */
public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout, public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout,
String hostname) { String hostname, String userId, String token) {
if (!shouldHandle(attachmentsLayout)) { if (!shouldHandle(attachmentsLayout)) {
return this; return this;
} }
...@@ -116,6 +116,7 @@ public class MessageRenderer extends AbstractRenderer<Message> { ...@@ -116,6 +116,7 @@ public class MessageRenderer extends AbstractRenderer<Message> {
} else { } else {
attachmentsLayout.setVisibility(View.VISIBLE); attachmentsLayout.setVisibility(View.VISIBLE);
attachmentsLayout.setHostname(hostname); attachmentsLayout.setHostname(hostname);
attachmentsLayout.setCredential(userId, token);
attachmentsLayout.setAttachments(attachments); attachmentsLayout.setAttachments(attachments);
} }
......
...@@ -17,7 +17,9 @@ ext { ...@@ -17,7 +17,9 @@ ext {
rxJava = 'io.reactivex:rxjava:1.2.2' rxJava = 'io.reactivex:rxjava:1.2.2'
boltsTask = 'com.parse.bolts:bolts-tasks:1.4.0' boltsTask = 'com.parse.bolts:bolts-tasks:1.4.0'
timber = 'com.jakewharton.timber:timber:4.3.1' timber = 'com.jakewharton.timber:timber:4.3.1'
okhttp3 = 'com.squareup.okhttp3:okhttp:3.4.1'
picasso = 'com.squareup.picasso:picasso:2.5.2' picasso = 'com.squareup.picasso:picasso:2.5.2'
picasso2Okhttp3Downloader = 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
} }
subprojects { project -> subprojects { project ->
......
...@@ -40,5 +40,7 @@ dependencies { ...@@ -40,5 +40,7 @@ dependencies {
compile rootProject.ext.supportAppCompat compile rootProject.ext.supportAppCompat
compile rootProject.ext.supportDesign compile rootProject.ext.supportDesign
compile 'org.nibor.autolink:autolink:0.5.0' compile 'org.nibor.autolink:autolink:0.5.0'
compile rootProject.ext.okhttp3
compile rootProject.ext.picasso compile rootProject.ext.picasso
compile rootProject.ext.picasso2Okhttp3Downloader
} }
...@@ -14,7 +14,13 @@ import android.widget.LinearLayout; ...@@ -14,7 +14,13 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import chat.rocket.android.widget.R; import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.ImageFormat; import chat.rocket.android.widget.helper.ImageFormat;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso; 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -25,6 +31,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout { ...@@ -25,6 +31,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
private LayoutInflater inflater; private LayoutInflater inflater;
private String hostname; private String hostname;
private String userId;
private String token;
private OkHttp3Downloader downloader;
public RocketChatMessageAttachmentsLayout(Context context) { public RocketChatMessageAttachmentsLayout(Context context) {
super(context); super(context);
initialize(context, null); initialize(context, null);
...@@ -56,6 +66,32 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout { ...@@ -56,6 +66,32 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
this.hostname = hostname; 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) { public void setAttachments(String attachmentsString) {
removeAllViews(); removeAllViews();
...@@ -87,9 +123,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout { ...@@ -87,9 +123,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false); View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false);
imageURL = absolutize(imageURL); new Picasso.Builder(getContext())
Picasso.with(getContext()) .downloader(getDownloader())
.load(imageURL) .build()
.load(absolutize(imageURL))
.placeholder(R.drawable.image_dummy) .placeholder(R.drawable.image_dummy)
.error(R.drawable.image_error) .error(R.drawable.image_error)
.into((ImageView) attachmentView.findViewById(R.id.image)); .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