Skip to content

Commit 0029164

Browse files
authored
Merge pull request #2497 from uProxy/user-cleanup
Clean up some code in user.ts
2 parents 67edce9 + 2cb272f commit 0029164

File tree

2 files changed

+107
-94
lines changed

2 files changed

+107
-94
lines changed

src/generic_ui/scripts/ui.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,24 @@ interface PromiseCallbacks {
5858
reject :Function;
5959
}
6060

61-
export function getImageData(userId :string, oldImageData :string,
62-
newImageData :string) : string {
63-
if (!oldImageData && !newImageData) {
64-
// Extra single-quotes are needed for CSS/Polymer parsing. This is safe
65-
// as long as jdenticon only uses '"' in the generated code...
66-
// The size is arbitrarily set to 100 pixels. SVG is scalable and our CSS
67-
// scales the image to fit the space, so this parameter has no effect.
68-
// We must also replace # with %23 for Firefox support.
69-
const userIdHash = crypto.createHash('md5').update(userId).digest('hex');
70-
return '\'data:image/svg+xml;utf8,' +
71-
jdenticon.toSvg(userIdHash, 100).replace(/#/g, '%23') + '\'';
72-
} else if (!newImageData) {
61+
export function getImageData(userId: string, oldImageData: string,
62+
newImageData: string): string {
63+
if (newImageData) {
64+
return newImageData;
65+
} else if (oldImageData) {
7366
// This case is hit when we've already generated a jdenticon for a user
7467
// who doesn't have any image in uProxy core.
7568
return oldImageData;
76-
} else {
77-
return newImageData;
7869
}
70+
71+
// Extra single-quotes are needed for CSS/Polymer parsing. This is safe
72+
// as long as jdenticon only uses '"' in the generated code...
73+
// The size is arbitrarily set to 100 pixels. SVG is scalable and our CSS
74+
// scales the image to fit the space, so this parameter has no effect.
75+
// We must also replace # with %23 for Firefox support.
76+
const userIdHash = crypto.createHash('md5').update(userId).digest('hex');
77+
return '\'data:image/svg+xml;utf8,' +
78+
jdenticon.toSvg(userIdHash, 100).replace(/#/g, '%23') + '\'';
7979
}
8080

8181
/**

src/generic_ui/scripts/user.ts

Lines changed: 93 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -77,134 +77,147 @@ export class User implements social.BaseUser {
7777
console.error('Unexpected userId: ' + profile.userId);
7878
}
7979

80-
// if we do not have stored state, no use in checking for changes
81-
if (this.consent_ &&
82-
// Don't show notifications for other instances of yourself
83-
this.userId !== this.network.userId) {
84-
// notifications for get mode
85-
if (!payload.consent.ignoringRemoteUserOffer) {
86-
if (this.offeringInstances.length === 0 && payload.offeringInstances.length > 0) {
87-
if (payload.consent.localRequestsAccessFromRemote) {
88-
this.ui_.showNotification(i18n_t('GRANTED_ACCESS_NOTIFICATION', {name: profile.name}),
89-
{ mode: 'get', network: this.network.name, user: this.userId });
90-
} else {
91-
this.ui_.showNotification(i18n_t('OFFERED_ACCESS_NOTIFICATION', {name: profile.name}),
92-
{ mode: 'get', network: this.network.name, user: this.userId });
93-
}
94-
}
95-
}
96-
97-
// notifications for share mode
98-
if (!payload.consent.ignoringRemoteUserRequest) {
99-
if (!this.consent_.remoteRequestsAccessFromLocal && payload.consent.remoteRequestsAccessFromLocal) {
100-
if (payload.consent.localGrantsAccessToRemote) {
101-
this.ui_.showNotification(i18n_t('ACCEPTED_OFFER_NOTIFICATION', {name: profile.name}),
102-
{ mode: 'share', network: this.network.name, user: this.userId });
103-
} else {
104-
this.ui_.showNotification(i18n_t('REQUESTING_ACCESS_NOTIFICATION', {name: profile.name}),
105-
{ mode: 'share', network: this.network.name, user: this.userId });
106-
}
107-
}
108-
}
109-
}
110-
111-
this.name = profile.name;
112-
this.url = profile.url;
113-
11480
// We want to make it obvious that cloud friends are sharable to others.
11581
// Normally, friends will only have share expanded if they are requesting
11682
// access, but that will never be the case for a cloud server.
11783
if (!this.status && profile.status === social.UserStatus.CLOUD_INSTANCE_CREATED_BY_LOCAL) {
11884
this.shareExpanded = true;
11985
}
12086

87+
this.name = profile.name;
88+
this.url = profile.url;
12189
this.status = profile.status;
12290

91+
// check whether to show notifications if we have previous state and it's
92+
// not another instance of us
93+
if (this.consent_ && this.userId !== this.network.userId) {
94+
this.showNotificationsFromUpdate_(payload);
95+
}
96+
12397
this.imageData = user_interface.getImageData(this.userId, this.imageData,
12498
profile.imageData);
12599

100+
this.mergeOfferingInstaces_(payload.offeringInstances);
101+
102+
this.allInstanceIds = payload.allInstanceIds;
103+
this.updateInstanceDescriptions_();
104+
this.consent_ = payload.consent;
105+
this.isOnline = payload.isOnline;
106+
107+
if (this.shouldGetBeExpanded_()) {
108+
this.getExpanded = true;
109+
}
110+
111+
if (this.shouldShareBeExpanded_()) {
112+
this.shareExpanded = true;
113+
}
114+
115+
this.gettingConsentState = this.calculateGettingConsentState_();
116+
this.sharingConsentState = this.calculateSharingConsentState_();
117+
}
118+
119+
private showNotificationsFromUpdate_ = (payload: social.UserData): void => {
120+
// notifications for get mode
121+
if (!payload.consent.ignoringRemoteUserOffer) {
122+
if (this.offeringInstances.length === 0 && payload.offeringInstances.length > 0) {
123+
if (payload.consent.localRequestsAccessFromRemote) {
124+
this.ui_.showNotification(i18n_t('GRANTED_ACCESS_NOTIFICATION', {name: payload.user.name}),
125+
{ mode: 'get', network: this.network.name, user: this.userId });
126+
} else {
127+
this.ui_.showNotification(i18n_t('OFFERED_ACCESS_NOTIFICATION', {name: payload.user.name}),
128+
{ mode: 'get', network: this.network.name, user: this.userId });
129+
}
130+
}
131+
}
132+
133+
// notifications for share mode
134+
if (!payload.consent.ignoringRemoteUserRequest) {
135+
if (!this.consent_.remoteRequestsAccessFromLocal && payload.consent.remoteRequestsAccessFromLocal) {
136+
if (payload.consent.localGrantsAccessToRemote) {
137+
this.ui_.showNotification(i18n_t('ACCEPTED_OFFER_NOTIFICATION', {name: payload.user.name}),
138+
{ mode: 'share', network: this.network.name, user: this.userId });
139+
} else {
140+
this.ui_.showNotification(i18n_t('REQUESTING_ACCESS_NOTIFICATION', {name: payload.user.name}),
141+
{ mode: 'share', network: this.network.name, user: this.userId });
142+
}
143+
}
144+
}
145+
}
146+
147+
private mergeOfferingInstaces_ = (newOfferingIrstances: social.InstanceData[]): void => {
126148
// iterate backwards to allow removing elements
127149
var i = this.offeringInstances.length;
128150
while (i--) {
129-
var found = _.findIndex(payload.offeringInstances, (obj) => {
151+
var found = _.findIndex(newOfferingIrstances, (obj) => {
130152
return obj.instanceId === this.offeringInstances[i].instanceId;
131153
});
132154

133155
if (found !== -1) {
134-
_.merge(this.offeringInstances[i], payload.offeringInstances[found]);
135-
payload.offeringInstances.splice(found, 1);
156+
_.merge(this.offeringInstances[i], newOfferingIrstances[found]);
157+
newOfferingIrstances.splice(found, 1);
136158
} else {
137159
this.offeringInstances.splice(i, 1);
138160
}
139161
}
140162

141-
for (var j in payload.offeringInstances) {
142-
this.offeringInstances.push(payload.offeringInstances[j]);
163+
for (var j in newOfferingIrstances) {
164+
this.offeringInstances.push(newOfferingIrstances[j]);
143165
}
166+
}
144167

145-
//this.offeringInstances = payload.offeringInstances;
146-
this.allInstanceIds = payload.allInstanceIds;
147-
this.updateInstanceDescriptions();
148-
this.consent_ = payload.consent;
149-
this.isOnline = payload.isOnline;
150-
151-
// Update gettingConsentState, used to display correct getting buttons.
152-
if (this.offeringInstances.length > 0) {
153-
// Expand the contact if there previously were no offers, we are not
154-
// ignoring offers, and the contact is online.
155-
if ((this.gettingConsentState ==
168+
private shouldGetBeExpanded_ = (): boolean => {
169+
return this.offeringInstances.length > 0 &&
170+
(this.gettingConsentState ==
156171
GettingConsentState.NO_OFFER_OR_REQUEST ||
157172
this.gettingConsentState ==
158173
GettingConsentState.LOCAL_REQUESTED_REMOTE_NO_ACTION) &&
159-
!this.consent_.ignoringRemoteUserOffer && this.isOnline) {
160-
this.getExpanded = true;
161-
}
174+
!this.consent_.ignoringRemoteUserOffer && this.isOnline;
175+
}
176+
177+
private calculateGettingConsentState_ = (): GettingConsentState => {
178+
if (this.offeringInstances.length > 0) {
179+
// there is an instance offering access to us
162180
if (this.consent_.localRequestsAccessFromRemote) {
163-
this.gettingConsentState =
164-
GettingConsentState.LOCAL_REQUESTED_REMOTE_GRANTED;
181+
return GettingConsentState.LOCAL_REQUESTED_REMOTE_GRANTED;
165182
} else if (this.consent_.ignoringRemoteUserOffer) {
166-
this.gettingConsentState =
167-
GettingConsentState.REMOTE_OFFERED_LOCAL_IGNORED;
183+
return GettingConsentState.REMOTE_OFFERED_LOCAL_IGNORED;
168184
} else {
169-
this.gettingConsentState =
170-
GettingConsentState.REMOTE_OFFERED_LOCAL_NO_ACTION;
185+
return GettingConsentState.REMOTE_OFFERED_LOCAL_NO_ACTION;
171186
}
172187
} else {
173188
if (this.consent_.localRequestsAccessFromRemote) {
174-
this.gettingConsentState =
175-
GettingConsentState.LOCAL_REQUESTED_REMOTE_NO_ACTION;
189+
return GettingConsentState.LOCAL_REQUESTED_REMOTE_NO_ACTION;
176190
} else {
177-
this.gettingConsentState = GettingConsentState.NO_OFFER_OR_REQUEST;
191+
return GettingConsentState.NO_OFFER_OR_REQUEST;
178192
}
179193
}
194+
}
180195

181-
// Update sharingConsentState, used to display correct sharing buttons.
182-
if (this.consent_.remoteRequestsAccessFromLocal) {
183-
// Expand the contact if there previously were no requests, we are not
184-
// ignoring requests, and the contact is online.
185-
if ((this.sharingConsentState ==
196+
private shouldShareBeExpanded_ = (): boolean => {
197+
// Expand the contact if there previously were no requests, we are not
198+
// ignoring requests, and the contact is online.
199+
return this.consent_.remoteRequestsAccessFromLocal &&
200+
(this.sharingConsentState ==
186201
SharingConsentState.NO_OFFER_OR_REQUEST ||
187202
this.sharingConsentState ==
188203
SharingConsentState.LOCAL_OFFERED_REMOTE_NO_ACTION) &&
189-
!this.consent_.ignoringRemoteUserRequest && this.isOnline) {
190-
this.shareExpanded = true;
191-
}
204+
!this.consent_.ignoringRemoteUserRequest && this.isOnline;
205+
}
206+
207+
private calculateSharingConsentState_ = (): SharingConsentState => {
208+
if (this.consent_.remoteRequestsAccessFromLocal) {
192209
if (this.consent_.localGrantsAccessToRemote) {
193-
this.sharingConsentState =
194-
SharingConsentState.LOCAL_OFFERED_REMOTE_ACCEPTED;
210+
return SharingConsentState.LOCAL_OFFERED_REMOTE_ACCEPTED;
195211
} else if (this.consent_.ignoringRemoteUserRequest) {
196-
this.sharingConsentState =
197-
SharingConsentState.REMOTE_REQUESTED_LOCAL_IGNORED;
212+
return SharingConsentState.REMOTE_REQUESTED_LOCAL_IGNORED;
198213
} else {
199-
this.sharingConsentState =
200-
SharingConsentState.REMOTE_REQUESTED_LOCAL_NO_ACTION;
214+
return SharingConsentState.REMOTE_REQUESTED_LOCAL_NO_ACTION;
201215
}
202216
} else {
203217
if (this.consent_.localGrantsAccessToRemote) {
204-
this.sharingConsentState =
205-
SharingConsentState.LOCAL_OFFERED_REMOTE_NO_ACTION;
218+
return SharingConsentState.LOCAL_OFFERED_REMOTE_NO_ACTION;
206219
} else {
207-
this.sharingConsentState = SharingConsentState.NO_OFFER_OR_REQUEST;
220+
return SharingConsentState.NO_OFFER_OR_REQUEST;
208221
}
209222
}
210223
}
@@ -261,7 +274,7 @@ export class User implements social.BaseUser {
261274
};
262275
}
263276

264-
public updateInstanceDescriptions = () => {
277+
private updateInstanceDescriptions_ = () => {
265278
if (this.offeringInstances.length <= 1) {
266279
// Leave descriptions unchanged if there are 0 or 1 instances.
267280
return;

0 commit comments

Comments
 (0)