Skip to content

Commit 5d91211

Browse files
authored
Add persistence of metrics and sort by popular on home (#134)
- Closes #101
1 parent 17be823 commit 5d91211

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/app/home/home.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { DataService } from '../services/data';
1717
import { StorageService } from '../services/storage';
1818
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
1919
import { UIService } from '../services/ui';
20+
import { MetricService } from '../services/metric-service';
2021

2122
interface DefaultProfile {
2223
pubkey: string;
@@ -118,7 +119,8 @@ export class HomeComponent {
118119
private router: Router,
119120
private breakpointObserver: BreakpointObserver,
120121
private ngZone: NgZone,
121-
private formBuilder: UntypedFormBuilder
122+
private formBuilder: UntypedFormBuilder,
123+
private metricService: MetricService
122124
) {
123125
console.log('HOME constructor!!'); // Hm.. called twice, why?
124126
}
@@ -257,7 +259,11 @@ export class HomeComponent {
257259
this.profileService.following$.subscribe((profiles) => {
258260
// this.profileCount = Math.floor(window.innerWidth / this.profileThumbnailWidth);
259261
this.profileCount = 75;
260-
this.profiles = profiles.slice(0, this.profileCount);
262+
let slized = profiles.slice(0, this.profileCount);
263+
let sorted = slized.sort((a, b) => {
264+
return this.metricService.get(a.pubkey) < this.metricService.get(b.pubkey) ? 1 : -1;
265+
});
266+
this.profiles = sorted;
261267
})
262268
);
263269

src/app/services/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export interface StateDocument {
9090
since: number;
9191
modified?: number;
9292
mediaQueue: MediaItem [];
93+
metrics: { users: any }
9394
}
9495

9596
export interface NostrRelayDocument {

src/app/services/metric-service.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { Injectable } from '@angular/core';
2+
import { StorageService } from './storage';
23

34
@Injectable({
45
providedIn: 'root',
56
})
67
export class MetricService {
7-
users: {
8-
[pubKey: string]: number;
9-
} = {};
8+
constructor(private storage: StorageService) {}
9+
10+
get users(): { [pubKey: string]: number } {
11+
if (!this.storage.state?.metrics?.users) {
12+
this.storage.state.metrics = { users: {} };
13+
}
14+
15+
return this.storage.state.metrics.users;
16+
}
1017

1118
increase(value: number, pubKey: string) {
1219
let existingMetric = this.users[pubKey];

src/app/services/storage.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export class StorageService {
2727
state = {
2828
id: 1,
2929
since: timeAgo,
30-
mediaQueue: []
30+
mediaQueue: [],
31+
metrics: { users: {} },
3132
};
3233
}
3334

0 commit comments

Comments
 (0)