-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.js
205 lines (178 loc) · 5.7 KB
/
User.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { Alert} from 'react-native'
import { storeData, getData, removeValue, getUser, setScreen, setUser, PAGES, generateUniqueMID, getAllKeys } from './Utility'
import { sha224 } from 'js-sha256'
import { pushAccountToDatabase, pushUsernameToDatabase, removeAccountFromDatabase, addPeerToDatabase, removePeerFromDatabase, doesUsernameExist, parseRemoteLogin, parseRemoteLoginEncrypted, pullAccountFromDatabase, sendNotification, removeNotification, removePostFromDatabase } from './firebaseConfig'
import { DebugInstructions } from 'react-native/Libraries/NewAppScreen'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { async } from '@firebase/util'
export class User {
constructor (username, password, realName, biography, MiD, myPosts, myPeers, notifications) {
this.username = username
this.password = password
this.realName = realName
this.biography = biography
if (MiD === 'new') { this.MiD = generateUniqueMID() } else { this.MiD = MiD }
if (myPosts === 'new') { this.myPosts = [] } else { this.myPosts = myPosts }
if (myPeers === 'new') { this.myPeers = [] } else { this.myPeers = myPeers }
if (notifications === 'new') { this.notifications = [] } else { this.notifications = notifications }
}
getUsername () {
return this.username
}
getDisplayName () {
return this.realName
}
getBiography () {
return this.biography
}
getMiD () {
return this.MiD
}
getHashedPass () {
return this.password
}
addPost (p) {
this.myPosts.push(p)
}
removePost (p) {
for (let i = 0; i < this.myPosts.length; i++) {
if (this.myPosts[i] == p.postID) {
this.myPosts.splice(i, 1)
console.log('Removed post locally. Removing from firebase...')
}
}
removePostFromDatabase(p)
this.storeLocally()
}
getMyPosts () {
return this.myPosts
}
getAllPeers () {
return this.myPeers
}
getNotifications () {
return this.notifications
}
storeLocally () {
storeData(this.MiD, this)
}
addPeer (MID) {
console.log('Adding peer: ' + MID)
if (MID.length === 16 && MID.substring(0, 5) === 'meso-') { // validates format, not existence
console.log('Format validated!')
this.myPeers.push(MID)
addPeerToDatabase(this, MID)
sendNotification(this, MID)
}
this.storeLocally()
}
removePeer (MID) {
const index = this.myPeers.indexOf(MID)
if (index > -1) {
this.myPeers.splice(index, 1)
removePeerFromDatabase(this, MID)
removeNotification(this, MID)
}
this.storeLocally()
}
setRealName (newName) {
this.realName = newName
}
setNewPassword (newPassword) {
this.password = newPassword
}
setNewBiography (newBiography) {
this.biography = newBiography
}
}
export async function checkLogin (username, password) {
console.log("Checking for " + username + ", " + password);
const passedID = await parseRemoteLogin(username, password)
console.log('Passed MID: ' + passedID)
if (passedID === '') {
Alert.alert('Error','Incorrect username and/or password.')
return
}
if (passedID !== '') {
const retrievedUser = await pullAccountFromDatabase(passedID)
setUser(retrievedUser)
await storeData('lastRememberedUser', retrievedUser)
setScreen(PAGES.ACCOUNTPAGE)
}
}
export async function makeAcc (username, password, realName, bio) {
if (username.length < 3 || password.length < 3 || username.length > 25 || password.length > 25) {
Alert.alert('Error','Your username or password must be between 3 and 25 characters.')
return
}
if (realName.length > 25 || realName.length < 3) {
Alert.alert('Error','You must enter a display name between 3 and 25 characters.')
return
}
if (await doesUsernameExist(username) === true) {
Alert.alert('Error','This username already exists, please choose a different one.')
return
}
const u = new User(username, String(sha224(String(password))), realName, bio, 'new', 'new', 'new', 'new')
pushAccountToDatabase(u)
pushUsernameToDatabase(username)
setUser(u)
await storeData('rememberMe', JSON.stringify(false)) // used for toggling autologin
await storeData('lastRememberedUser', u) // used for autologin
setScreen(PAGES.ACCOUNTPAGE)
}
export function changeRealName () {
Alert.alert('Error',(this.MiD))
}
export async function deleteCurrUser () {
const u = getUser()
removeAccountFromDatabase(u)
removeValue(u.getMiD())
removeValue('lastRememberedUser')
setUser(null)
setScreen(PAGES.LOGIN)
}
export function dataOccupied (user) { // returns number of bytes
let bytes = 0
const posts = user.getMyPosts()
for (let i = 0; i < user.myPosts.length; i++) {
bytes += ~-encodeURI(
posts[i].attachedMiD +
posts[i].postID +
posts[i].score +
posts[i].textContent +
posts[i].timestamp
).split(/%..|./).length
}
return bytes
}
export async function lru () {
var data = null;
await AsyncStorage.getItem('lastRememberedUser').then(value => {
data = JSON.parse(value)
})
if (data != null) {
const passedID = await parseRemoteLoginEncrypted(data.username, data.password)
console.log('Passed MID: ' + passedID)
if (passedID === '') {
Alert.alert('Error',('Incorrect username and/or password.'))
return
}
if (passedID !== '') {
const retrievedUser = await pullAccountFromDatabase(passedID)
setUser(retrievedUser)
await storeData('lastRememberedUser', retrievedUser)
setScreen(PAGES.ACCOUNTPAGE)
}
} else {
return null
}
}
export async function usernameValidation (username) {
if (await doesUsernameExist(username) === true) {
return true
} else {
return false
}
}
export default User