forked from yokowasis/firegun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.ts
131 lines (118 loc) · 3.87 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Gun from "gun";
import Firegun from "./firegun";
export declare type FiregunUser = {
alias : string,
pair : {
priv : string,
pub : string,
epriv : string,
epub : string
}
err? : any
}
export declare type Ack = {
"@"?: string,
err: undefined,
ok: { "": number } | string,
"#"?: string
} | {
err: Error,
ok: any
} | void;
export declare type Pubkey = {
pub: string,
epub?: string,
}
export declare type chatType = {
_self : boolean,
alias : string,
msg : string,
timestamp: string,
id : string,
status : string,
}
const zeroPad = (num:number, places:number) => String(num).padStart(places, '0')
export const common = {
/**
* Generate Public Certificate for Logged in User
* @returns
*/
async generatePublicCert(fg:Firegun) : Promise<{data : Ack[],error : Ack[]}> {
return new Promise(async (resolve, reject) => {
if (fg.user.alias) {
// BUG Blacklist Work Around
// await fg.userPut("chat-blacklist",{
// "t" : "_"
// })
let cert = await (Gun as any).SEA.certify("*", [{ "*" : "chat-with","+" : "*"}], fg.user.pair,null,{
// block : 'chat-blacklist' //ADA BUG DARI GUN JADI BELUM BISA BLACKLIST
});
let ack = await fg.userPut("chat-cert",cert);
resolve (ack);
} else {
reject ("User belum Login")
}
});
},
/**
* Sort and array based of their property
*
* e.g. arrays.sort(dynamicSort("timestamp"));
*
* @param property
* @returns
*/
dynamicSort : (property:string) => {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a : any,b : any) {
/* next line works with strings and numbers,
* and you may want to customize it to your needs
*/
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
},
fileTobase64 : async (fileElement:HTMLInputElement):Promise<{info:{name:string,size:number,type:string},content:string | ArrayBuffer | null}> => {
return new Promise((resolve) => {
if (fileElement.files !== null) {
let file = fileElement.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
let fileInfo = {
name : file.name,
size : file.size,
type : file.type,
}
reader.onload = function () {
let data = {
info : fileInfo,
content : reader.result,
}
resolve((data));
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
})
},
/**
* Generate Current date and time object
* @returns
*/
getDate : () => {
let currentdate = new Date();
let year = currentdate.getFullYear().toString();
let month = zeroPad(currentdate.getMonth()+1,2);
let date = zeroPad(currentdate.getDate(),2)
let hour = zeroPad(currentdate.getHours(),2)
let minutes = zeroPad(currentdate.getMinutes(),2)
let seconds = zeroPad(currentdate.getSeconds(),2)
let miliseconds = zeroPad(currentdate.getMilliseconds(),3)
return ( {year : year, month : month, date : date, hour : hour, minutes : minutes, seconds : seconds, miliseconds : miliseconds} )
}
}