Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | 84x 84x 84x 90x 90x 90x 90x 90x 90x 90x | import { Injectable } from '@angular/core';
import { delay, isArray, uniq, without } from 'lodash-es';
import { DateTime } from 'luxon';
import { runInAction } from 'mobx';
import { catchError, forkJoin, map, Observable, of, shareReplay, throwError } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { Ext } from '../model/ext';
import { User } from '../model/user';
import { UserConfig } from '../mods/user';
import { Store } from '../store/store';
import { hasPrefix } from '../util/tag';
import { AdminService } from './admin.service';
import { ExtService } from './api/ext.service';
import { RefService } from './api/ref.service';
import { UserService } from './api/user.service';
import { ConfigService } from './config.service';
export const CACHE_MS = 15 * 1000;
@Injectable({
providedIn: 'root',
})
export class AccountService {
private _user$?: Observable<User | undefined>;
private _userExt$?: Observable<Ext>;
constructor(
private store: Store,
private config: ConfigService,
private admin: AdminService,
private users: UserService,
private exts: ExtService,
private refs: RefService,
) { }
get whoAmI$() {
return this.users.whoAmI().pipe(
catchError(err => {
if ([0, 200, 401, 403].includes(err.status)) {
// Requires auth to access at all
this.config.logIn();
}
return throwError(() => err);
}),
tap(roles => this.store.account.setRoles(roles)),
);
}
get initExt$() {
return this.userExt$.pipe(catchError(() => of(null)));
}
get init$() {
runInAction(() => this.store.account.defaultConfig = this.admin.defaultConfig('user'));
if (!this.store.account.signedIn) return this.subscriptions$.pipe(
switchMap(() => this.bookmarks$),
switchMap(() => this.theme$),
);
return this.loadUserExt$.pipe(
switchMap(() => this.user$),
switchMap(() => this.subscriptions$),
switchMap(() => this.bookmarks$),
switchMap(() => this.theme$),
catchError(err => {
console.error('Can not create user data');
console.error(err);
return of(null);
}),
);
}
private get loadUserExt$() {
if (!this.store.account.signedIn) return of(undefined);
if (!this.admin.getTemplate('user')) return of(undefined);
return this.userExt$.pipe(
catchError(() => of(undefined)),
switchMap(ext => ext ? of(ext) : this.exts.create({ tag: this.store.account.localTag, origin: this.store.account.origin })),
map(() => {}),
);
}
clearCache() {
this._userExt$ = undefined;
this._user$ = undefined;
}
private get user$(): Observable<User | undefined> {
if (!this.store.account.signedIn) return throwError(() => 'Not signed in');
if (!this._user$) {
this._user$ = this.users.get(this.store.account.tag).pipe(
tap(user => runInAction(() => this.store.account.access = user)),
shareReplay(1),
catchError(() => of(undefined)),
);
delay(() => this._user$ = undefined, CACHE_MS);
}
return this._user$;
}
private get userExt$(): Observable<Ext> {
if (!this.store.account.signedIn) return throwError(() => 'Not signed in');
if (!this._userExt$) {
this._userExt$ = this.exts.get(this.store.account.tag).pipe(
tap(ext => runInAction(() => this.store.account.ext = ext)),
shareReplay(1),
);
delay(() => this._userExt$ = undefined, CACHE_MS);
}
return this._userExt$;
}
get forYouQuery$(): Observable<string> {
const followers = this.store.account.userSubs
.map(u => this.exts.getCachedExt(u));
return (followers.length ? forkJoin(followers) : of([])).pipe(
map(es => [
...this.store.account.tagSubs,
...es
.flatMap(e => e?.config?.subscriptions)
.filter(s => !!s)
.filter(s => !hasPrefix(s, 'user'))
]),
map(uniq),
map(es => es.length === 0 ? 'none' : '!internal:(' + es.join('|') + ')'),
);
}
get subscriptions$(): Observable<string[]> {
if (!this.admin.getTemplate('user')) return of(this.store.account.subs);
return this.userExt$.pipe(
catchError(() => of(null)),
map(() => this.store.account.subs),
);
}
get bookmarks$(): Observable<string[]> {
if (!this.admin.getTemplate('user')) return of(this.store.account.bookmarks);
return this.userExt$.pipe(
catchError(() => of(null)),
map(() => this.store.account.bookmarks),
);
}
get alarms$(): Observable<string[]> {
if (!this.admin.getTemplate('user')) return of(this.store.account.alarms);
return this.userExt$.pipe(
catchError(() => of(null)),
map(() => this.store.account.alarms),
);
}
get theme$(): Observable<string | undefined> {
if (!this.admin.getTemplate('user')) return of(this.store.account.config.theme);
return this.userExt$.pipe(
catchError(() => of(null)),
map(() => this.store.account.config.theme),
);
}
addSub$(tag: string): Observable<any> {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
return this.addConfigArray$('subscriptions', tag).pipe(
tap(() => this.clearCache()),
switchMap(() => this.subscriptions$),
);
}
removeSub$(tag: string): Observable<any> {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
return this.subscriptions$.pipe(
switchMap(() => this.removeConfigArray$('subscriptions', tag)),
tap(() => this.clearCache()),
switchMap(() => this.subscriptions$),
);
}
addBookmark$(tag: string): Observable<any> {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
return this.addConfigArray$('bookmarks', tag).pipe(
tap(() => this.clearCache()),
switchMap(() => this.bookmarks$),
);
}
removeBookmark$(tag: string): Observable<any> {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
return this.bookmarks$.pipe(
switchMap(() => this.removeConfigArray$('bookmarks', tag)),
tap(() => this.clearCache()),
switchMap(() => this.bookmarks$),
);
}
addAlarm$(tag: string): Observable<any> {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
return this.addConfigArray$('alarms', tag).pipe(
tap(() => this.clearCache()),
switchMap(() => this.alarms$),
);
}
removeAlarm$(tag: string): Observable<any> {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
return this.alarms$.pipe(
switchMap(() => this.removeConfigArray$('alarms', tag)),
tap(() => this.clearCache()),
switchMap(() => this.alarms$),
);
}
checkNotifications() {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
this.userExt$.pipe(
switchMap(() => this.refs.count({
query: this.store.account.notificationsQuery,
modifiedAfter: this.store.account.config.lastNotified || DateTime.now().minus({ year: 1 }),
})),
).subscribe(count => runInAction(() => this.store.account.notifications = count));
this.checkAlarms();
}
clearNotificationsIfNone(readDate?: DateTime) {
if (!readDate || this.store.account.config.lastNotified && readDate < DateTime.fromISO(this.store.account.config.lastNotified)) return;
if (!this.store.account.signedIn) return;
if (!this.admin.getTemplate('user')) return;
this.userExt$.pipe(
switchMap(() => this.refs.count({
query: this.store.account.notificationsQuery,
modifiedAfter: this.store.account.config.lastNotified || DateTime.now().minus({year: 1}),
modifiedBefore: readDate,
})),
).subscribe(count => {
if (count === 0) {
this.clearNotifications(readDate);
} else {
runInAction(() => this.store.account.ignoreNotifications.push(readDate.valueOf()));
}
});
}
clearNotifications(readDate?: DateTime) {
if (readDate) {
if (this.store.account.config.lastNotified && readDate < DateTime.fromISO(this.store.account.config.lastNotified)) return;
} else {
readDate = DateTime.now();
}
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
const lastNotified = readDate.plus({ millisecond: 1 }).toISO();
this.updateConfig$('lastNotified', lastNotified).subscribe(() => {
this.clearCache();
this.checkNotifications();
});
}
checkAlarms() {
if (!this.store.account.signedIn) throw 'Not signed in';
if (!this.admin.getTemplate('user')) throw 'User template not installed';
if (!this.store.account.alarms.length) return;
this.userExt$.pipe(
switchMap(() => this.refs.count({
query: this.store.account.alarmsQuery,
modifiedAfter: this.store.account.config.lastNotified || DateTime.now().minus({ year: 1 }),
})),
).subscribe(count => runInAction(() => this.store.account.alarmCount = count));
}
checkConsent(consent?: [string, string][]) {
if (!consent?.length) return;
let status = this.store.account.ext?.config?.consent || {};
let result = null;
for (const [key, disclosure] of consent) {
if (!status?.[key] && confirm(disclosure)) {
result ||= { ...status };
result[key] = true;
}
}
if (result) {
this.userExt$.pipe(
switchMap(() => this.updateConfig$('consent', result)),
).subscribe();
}
}
// TODO: move to ext, plugin, template service as a mixin
updateConfig$(name: keyof UserConfig, value: any) {
return this.exts.patch(this.store.account.tag, this.store.account.ext!.modifiedString!, [{
op: 'add',
path: '/config/' + name,
value: value,
}]).pipe(tap(cursor => runInAction(() => {
this.store.account.ext = <Ext> {
...this.store.account.ext,
config: {
...this.store.account.config,
[name]: value,
},
modified: DateTime.fromISO(cursor),
modifiedString: cursor,
};
})));
}
addConfigArray$(name: keyof UserConfig, value: any) {
let path = name;
let patchValue = value;
if (!this.store.account.config[name]) {
patchValue = [value];
} else {
if ((this.store.account.config[name] as any[]).includes(value)) return of();
path += '/-';
}
return this.exts.patch(this.store.account.tag, this.store.account.ext!.modifiedString!, [{
op: 'add',
path: '/config/' + path,
value: patchValue,
}]).pipe(tap(cursor => runInAction(() => {
this.store.account.ext = <Ext> {
...this.store.account.ext,
config: {
...this.store.account.config,
[name]: [
...(this.store.account.config[name] as any[] || []),
value,
],
},
modified: DateTime.fromISO(cursor),
modifiedString: cursor,
};
})));
}
removeConfigArray$(name: keyof UserConfig, value: any) {
if (!isArray(this.store.account.config[name])) return of();
const index = (this.store.account.config[name] as any[]).indexOf(value);
if (index === -1) return of();
return this.exts.patch(this.store.account.tag, this.store.account.ext!.modifiedString!, [{
op: 'remove',
path: '/config/' + name + '/' + index,
}]).pipe(tap(cursor => runInAction(() => {
this.store.account.ext = <Ext> {
...this.store.account.ext,
config: {
...this.store.account.config,
[name]: without(this.store.account.config[name] as any[], value)
},
modified: DateTime.fromISO(cursor),
modifiedString: cursor,
};
})));
}
}
|