Skip to content

Commit

Permalink
Merge pull request #4 from kamilkisiela/fix-direct-channel
Browse files Browse the repository at this point in the history
Fixed direct channel
  • Loading branch information
tomermoshe authored Aug 27, 2017
2 parents 1799b80 + efbb268 commit 202d125
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 65 deletions.
88 changes: 57 additions & 31 deletions src/app/chat/chat-view/chat-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { ActivatedRoute, Router } from '@angular/router';
import { ChatService } from '../services/chat/chat.service';
import { MessagesQuery } from '../../graphql/types/types';
import { ChangeEvent, VirtualScrollComponent } from 'angular2-virtual-scroll';
import { ChannelsService } from '../services/channels/channels.service';
import { Subscription } from 'rxjs/Subscription';


@Component({
Expand All @@ -31,7 +33,8 @@ export class ChatViewComponent implements OnInit, OnDestroy {

public channel: MessagesQuery.Channel;
private routeParamsSub;
private messagesSub;
private messagesSub: Subscription;
private channelSub: Subscription;
public model = { message: undefined };
private chatContentScrollSubscription;
public isFirstLoad = true;
Expand All @@ -45,6 +48,7 @@ export class ChatViewComponent implements OnInit, OnDestroy {
constructor(private router: Router,
private route: ActivatedRoute,
public chatService: ChatService,
private channelsService: ChannelsService,
private cd: ChangeDetectorRef) {
}

Expand All @@ -67,43 +71,62 @@ export class ChatViewComponent implements OnInit, OnDestroy {
const isDirect = url.value[0].path === 'direct';
const channelName = params['id'];

const messagesQueryObservable = this.chatService.getMessages({
channelId: null,
channelDetails: { name: channelName, direct: isDirect },
count: this.PAGE_MESSAGE_COUNT,
cursor: null,
searchRegex: null,
excludeServer: true
}
);
let channelObservable;
if (isDirect) {
channelObservable = this.channelsService.getDirectChannelByUsername(channelName);
}
else {
channelObservable = this.channelsService.getChannelByName(channelName);
}

this.messagesSub = messagesQueryObservable.subscribe(({ data, loading }) => {
this.loadingMessages = loading && !data;
this.channelSub = channelObservable.subscribe((result) => {
const channelData = result.data;
const channelLoading = result.loading;
this.loadingMessages = channelLoading && !channelData;
if (this.loadingMessages) {
this.cd.markForCheck();
return;
}
this.channel = isDirect ? channelData.directChannel : channelData.channelByName;
this.cd.markForCheck();

if (data.messages === null) {
this.router.navigate(['channel-not-found']);
return;
}

this.messages = data.messages.messagesArray;

if (this.isFirstLoad) {
this.isFirstLoad = false;
this.channel = data.messages.channel;
this.chatService.subscribeToMessageAdded(this.channel.id);

this.scrollToBottom();
}

if (!this.isFirstLoad && this.messages && this.isScrolledToBottom()) {
this.scrollToBottom();
}
const messagesQueryObservable = this.chatService.getMessages({
channelId: this.channel.id,
channelDetails: { name: this.channel.name, direct: isDirect },
count: this.PAGE_MESSAGE_COUNT,
cursor: null,
searchRegex: null,
excludeServer: true
}
);

this.messagesSub = messagesQueryObservable.subscribe(({ data, loading }) => {
this.loadingMessages = loading && !data;
if (this.loadingMessages) {
this.cd.markForCheck();
return;
}

if (data.messages === null) {
this.router.navigate(['channel-not-found']);
return;
}

this.messages = data.messages.messagesArray;

if (this.isFirstLoad) {
this.isFirstLoad = false;
this.chatService.subscribeToMessageAdded(this.channel.id);

this.scrollToBottom();
}

if (!this.isFirstLoad && this.messages && this.isScrolledToBottom()) {
this.scrollToBottom();
}

this.cd.markForCheck();
this.cd.markForCheck();
});
});
this.cd.markForCheck();
});
Expand Down Expand Up @@ -156,6 +179,9 @@ export class ChatViewComponent implements OnInit, OnDestroy {
if (this.messagesSub) {
this.messagesSub.unsubscribe();
}
if (this.channelSub) {
this.channelSub.unsubscribe();
}
if (this.routeParamsSub) {
this.routeParamsSub.unsubscribe();
}
Expand Down
29 changes: 26 additions & 3 deletions src/app/chat/services/channels/channels.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Injectable } from '@angular/core';
import { Apollo } from 'apollo-angular';
import { MyChannelsQuery } from '../../../graphql/types/types';
import { Apollo, ApolloQueryObservable } from 'apollo-angular';
import {
ChannelByNameQuery, DirectChannelQuery, DirectChannelQueryArgs,
MyChannelsQuery
} from '../../../graphql/types/types';
import { AuthenticationService } from '../../../shared/services/authentication.service';
import { myChannelsQuery } from '../../../graphql/queries/my-channels.query';
import { channelByNameQuery } from '../../../graphql/queries/channel-by-name.query';
import { directChannelQuery } from '../../../graphql/queries/direct-channel.query';

@Injectable()
export class ChannelsService {

constructor(private apollo: Apollo,
private authenticationService: AuthenticationService) { }
private authenticationService: AuthenticationService) {
}

getMyChannels() {
const user: any = this.authenticationService.getUser() || {};
Expand All @@ -23,4 +29,21 @@ export class ChannelsService {
});
}

getChannelByName(channelName: string): ApolloQueryObservable<ChannelByNameQuery.Result> {
return this.apollo.watchQuery<ChannelByNameQuery.Result>({
query: channelByNameQuery,
variables: { name: channelName },
fetchPolicy: 'cache-and-network',
});
}

getDirectChannelByUsername(username: string): ApolloQueryObservable<DirectChannelQuery.Result> {
return this.apollo.watchQuery<DirectChannelQuery.Result>({
query: directChannelQuery,
variables: { username },
fetchPolicy: 'cache-and-network',
});

}

}
2 changes: 1 addition & 1 deletion src/app/chat/services/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class ChatService {
fetchPolicy: 'cache-and-network',
});

return this.messagesQueryObservable.do(({ data, loading}) => {
return this.messagesQueryObservable.do(({ data, loading }) => {
if (!loading && data && data.messages) {
this.cursor = data.messages.cursor;
if (this.cursor === null) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/graphql/queries/channel-by-name.query.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import gql from 'graphql-tag';

export const channelByNameQuery = gql`
query channelByName($name: String!, $isDirect: Boolean!){
channelByName(name: $name, isDirect: $isDirect){
query channelByName($name: String!){
channelByName(name: $name){
id
name
direct
Expand Down
12 changes: 12 additions & 0 deletions src/app/graphql/queries/direct-channel.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gql from 'graphql-tag';

export const directChannelQuery = gql`
query directChannel($username: String, $channelId: String){
directChannel(username: $username, channelId: $channelId){
id
name
direct
privateChannel
}
}
`;
19 changes: 18 additions & 1 deletion src/app/graphql/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ export interface PasswordType {
export namespace ChannelByNameQuery {
export type Variables = {
name: string;
isDirect: boolean;
}

export type Result = {
Expand Down Expand Up @@ -301,6 +300,24 @@ export namespace ChatMessageAddedSubscription {
} & MessageFragment.Fragment
}

export namespace DirectChannelQuery {
export type Variables = {
username: string | null;
channelId: string | null;
}

export type Result = {
directChannel: DirectChannel;
}

export type DirectChannel = {
id: string;
name: string;
direct: boolean;
privateChannel: boolean;
}
}

export namespace GetAllProvidersQuery {
export type Variables = {
}
Expand Down
Loading

0 comments on commit 202d125

Please sign in to comment.