Skip to content

Commit

Permalink
(feat): messenger work
Browse files Browse the repository at this point in the history
  • Loading branch information
markharding committed Jan 24, 2017
1 parent f72316a commit bbf0e10
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 53 deletions.
30 changes: 16 additions & 14 deletions src/app/modules/messenger/decryption.component.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { Component, Input, ChangeDetectorRef } from '@angular/core';
import { Component, Input, ChangeDetectorRef, NgZone } from '@angular/core';

import { Storage } from '../../common/services/storage';

@Component({
selector: 'm-decrypt',
template: `
{{message}}
{{decrypted}}
`
})

export class DecryptionComponent {

message : string = "decrypting...";
@Input('message') message : string;
decrypted : string = "decrypting...";

storage = new Storage();

constructor(private cd : ChangeDetectorRef) {
constructor(private cd : ChangeDetectorRef, private zone : NgZone) {
}

ngAfterViewInit(){
this.decrypt();
}

@Input('message') set encrypted(value: string) {

(<any>window).Crypt.setPrivateKey(this.storage.get('private-key'));

(<any>window).Crypt.decrypt(value, (success) => {

this.message = success;
this.cd.markForCheck();
this.cd.detectChanges();
decrypt() {

(<any>window).Crypt.setPrivateKey(this.storage.get('private-key'));

});
(<any>window).Crypt.decrypt(this.message, (success) => {
this.decrypted = success;
//console.log('decrypted ' + success);
this.cd.markForCheck();
this.cd.detectChanges();
});

}

Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/messenger/messenger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { Client } from '../../common/services/api/client';

import { MessengerList } from './list.component';
import { MessengerView } from './view.component';
import { MessengerViewService } from './view.service';
import { MessengerSetup } from './setup.component';
import { DecryptionComponent } from './decryption.component';
import { DecryptionPipe } from './decryption.pipe';

@NgModule({
imports: [ IonicModule, CommonModule, HeaderModule ],
providers: [ Client ],
providers: [ Client, MessengerViewService ],
declarations: [ MessengerList, MessengerView, MessengerSetup, DecryptionComponent, DecryptionPipe ],
exports: [ MessengerList ],
entryComponents: [ MessengerList, MessengerView, MessengerSetup ]
Expand Down
20 changes: 18 additions & 2 deletions src/app/modules/messenger/view.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@
<ion-spinner *ngIf="inProgress"></ion-spinner>

<ion-list class="m-ionic-messenger--list">
<div class="m-ionic-messenger--message" *ngFor="let message of messages">
<m-decrypt [message]="message.message"></m-decrypt>
<div class="m-ionic-messenger--message"
*ngFor="let message of messages"
[class.odd]="message.owner.guid == storage.get('user_guid')"
>
<img [src]="'https://edge.minds.com/icon/' + message.owner.guid + '/small'"
*ngIf="message.owner.guid == storage.get('user_guid')">

<div class="m-ionic-messenger--message-bubble">
<m-decrypt [message]="message.message" allow-text-select></m-decrypt>
</div>


<img [src]="'https://edge.minds.com/icon/' + message.owner.guid + '/small'"
*ngIf="message.owner.guid != storage.get('user_guid')">

<div class="m-ionic-messenger--message-ts">
{{ message.time_created * 1000 | date: 'medium' }}
</div>
</div>
</ion-list>

Expand Down
29 changes: 21 additions & 8 deletions src/app/modules/messenger/view.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@


.m-ionic-messenger--message{
display:flex;
align-items:column;
padding:8px;
position: relative;
text-align: right;
display: block;
width: 100%;
padding:16px;

.m-ionic-body{
margin-left:8px;
&.odd{
text-align:left;
}

.m-ionic-messenger--message-bubble{
padding: 8px 16px;
margin: 0 12px;
font-size:18px;
background: #EEE;
border-radius: 3px;
max-width:65%;
display: inline-block;
vertical-align: middle;
}

p{
margin:0;
padding:0;
img{
vertical-align: middle;
border-radius: 50%;
}
}

Expand Down
60 changes: 32 additions & 28 deletions src/app/modules/messenger/view.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Component, OnInit, OnDestroy, Input, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { NavParams } from 'ionic-angular';
import { Component, OnInit, OnDestroy, Input, ChangeDetectionStrategy, ChangeDetectorRef, ViewChild } from '@angular/core';
import { Content, NavParams } from 'ionic-angular';

import { ChannelComponent } from '../channel/channel.component';
import { Client } from '../../common/services/api/client';
import { Storage } from '../../common/services/storage';

import { MessengerViewService } from './view.service';

@Component({
moduleId: 'module.id',
selector: 'messenger-view',
Expand All @@ -15,6 +17,7 @@ import { Storage } from '../../common/services/storage';

export class MessengerView {

@ViewChild('scrollArea') scrollArea : Content;
@Input() conversation : any;

inProgress : boolean = false;
Expand All @@ -29,48 +32,49 @@ export class MessengerView {

storage = new Storage();

constructor(private client : Client, private cd : ChangeDetectorRef, private params: NavParams ){}
constructor(private client : Client, private cd : ChangeDetectorRef, private params: NavParams,
private service : MessengerViewService){}

ngOnInit(){
this.conversation = this.params.get('conversation');
this.load();
this.service.setGuid(this.conversation.guid);
setTimeout(() => {
this.load();
}, 300);
}

load(){

this.inProgress = true;
this.client.get('api/v2/conversations/' + this.conversation.guid)
.then((response : any) => {

let offset = "";
if(this.messages.length > 0)
offset = this.messages[0].guid;

this.service.getFromRemote(12, offset)
.then((messages : Array<any>) => {
this.inProgress = false;
if(!response.messages){
return false;

if(offset){
messages.shift();
}

//if (opts.finish) {
// this.messages = this.messages.concat(response.messages);
// this.scrollEmitter.next(true);
//} else if(opts.offset){
// let scrollTop = scrollView.scrollTop;
// let scrollHeight = scrollView.scrollHeight;
// response.messages.shift();
// this.messages = response.messages.concat(this.messages);
// this.offset = response['load-previous'];
// setTimeout(() => {
// scrollView.scrollTop = scrollTop + scrollView.scrollHeight - scrollHeight +60;
// });
//} else {
this.messages = response.messages;
this.offset = response['load-previous'];
//}
this.messages = messages.concat(this.messages);

this.cd.markForCheck();
this.cd.detectChanges();

//this.blocked = !!response.blocked;
//this.unavailable = !!response.unavailable;
if(!offset){
this.scrollArea.scrollToBottom();
setTimeout(() => {
this.scrollArea.scrollToBottom();
}, 1000);
}
})
.catch(() => {
this.inProgress = false;
});
//failure to get any messages
})

}

loadEarlier(puller){
Expand Down
132 changes: 132 additions & 0 deletions src/app/modules/messenger/view.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';

import { Client } from '../../common/services/api/client';


@Injectable()
export class MessengerViewService {

db : SQLite;
guid : string;
offset : string = "";

messages : Array<any> = [];

constructor(private client : Client){
this.db = new SQLite();
this.db.openDatabase({name: "minds4.db", location: "default"})
.then(() => {
this.db.executeSql("CREATE TABLE IF NOT EXISTS messages (guid TEXT PRIMARY KEY, conversation_guid TEXT, data TEXT)", []);
});
}

setGuid(guid : string){
this.guid = guid;
}

get(limit : number = 10, offset : string = ""){
return new Promise((resolve, reject) => {
this.getFromLocal(limit, offset)
.then((messages : Array<any>) => {
resolve(messages);
})
.catch((err) => {
console.log('error on local fetch, will try remote');
console.log(err);

this.getFromRemote(limit, offset)
.then((messages : Array<any>) => {
resolve(messages);
})
.catch((err) => {
reject(err);
})

})
});
}

getFromLocal(limit : number, offset : string = ""){
return new Promise((resolve, reject) => {
let request;

if(offset){
request = this.db.executeSql(`SELECT * FROM messages
WHERE conversation_guid = ?
AND guid < ?
ORDER BY guid ASC
LIMIT ? `,
[ this.guid, offset, limit ])
} else {
request = this.db.executeSql(`SELECT * FROM messages
WHERE conversation_guid = ?
ORDER BY guid ASC
LIMIT ? `,
[ this.guid, limit ])
}
request.then((data) => {
if(data.rows.length > 0) {
let messages = [];
for(var i = 0; i < data.rows.length; i++) {
messages.push(JSON.parse(data.rows.item(i).data));
}
this.offset = messages[0].guid;
console.log('got from local :' + this.offset);
resolve(messages);

} else {
reject("No results on local");
}
})
.catch((err) => {
reject(err);
});
});
}

getFromRemote(limit : number, offset : string = ""){
return new Promise((resolve, reject) => {
this.client.get('api/v2/conversations/' + this.guid, {
limit: 8,
offset: this.offset
})
.then((response : any) => {
if(!response.messages){
reject("No results on remote");
return false;
}

let messages = response.messages;

//if(!this.offset)
this.sync(response.messages);

this.offset = response['load-previous'];

resolve(messages);
})
.catch(() => {
});
});
}

sync(data){
for(let item of data){
this.db.executeSql("INSERT OR REPLACE INTO messages (guid, conversation_guid, data) VALUES (?, ?, ?)", [
item.guid,
this.guid,
JSON.stringify(item)
])
.then((success) => {
console.log('sync success');
})
.catch((err) => {
console.log('sync error');
console.log(err);
})
}
}


}

0 comments on commit bbf0e10

Please sign in to comment.