Subscribed class

This commit is contained in:
Patrick Haßel 2024-10-29 15:11:00 +01:00
parent a4c0969ce6
commit 4104c46f3d
16 changed files with 173 additions and 61 deletions

View File

@ -0,0 +1,46 @@
import {Subscription} from "rxjs";
import {Next} from "./common/types";
export class Subscribed<T> {
private subscription: Subscription | null = null;
private _value: T | null = null;
constructor(
private readonly same: (a: T, b: T) => boolean,
private readonly subscribe: (value: T, next: Next<T>) => Subscription,
) {
// -
}
get value(): T | null {
return this._value;
}
set value(value: T | null) {
if (!this.isSame(value)) {
if (this.subscription) {
this.subscription.unsubscribe();
this.subscription = null;
}
if (value) {
this.subscription = this.subscribe(value, next => this.value = next);
}
}
this._value = value;
}
private isSame(value: T | null) {
if (this._value === null) {
return value === null;
} else {
if (value === null) {
return false
} else {
return this.same(this._value, value);
}
}
}
}

View File

@ -30,4 +30,8 @@ export class UserPrivate {
return UserPrivate.fromJson(json); return UserPrivate.fromJson(json);
} }
static samePrivateUuid(a: UserPrivate, b: UserPrivate): boolean {
return a.privateUuid === b.privateUuid;
}
} }

View File

@ -4,10 +4,11 @@ import {UserPrivate} from "./UserPrivate";
import {Next} from "../common/types"; import {Next} from "../common/types";
import {UserCommon} from "./UserCommon"; import {UserCommon} from "./UserCommon";
import {UserPublic} from "./UserPublic"; import {UserPublic} from "./UserPublic";
import {Router} from "@angular/router"; import {EventType, Router} from "@angular/router";
import {BehaviorSubject, Subscription} from "rxjs"; import {BehaviorSubject, Subscription} from "rxjs";
import {Group} from "../group/Group"; import {Group} from "../group/Group";
import {StompService} from "@stomp/ng2-stompjs"; import {StompService} from "@stomp/ng2-stompjs";
import {Subscribed} from "../Subscribed";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -16,6 +17,8 @@ export class UserService {
private readonly subject: BehaviorSubject<UserPrivate | null> = new BehaviorSubject<UserPrivate | null>(null); private readonly subject: BehaviorSubject<UserPrivate | null> = new BehaviorSubject<UserPrivate | null>(null);
private subscription?: Subscription;
get user(): UserPrivate | null { get user(): UserPrivate | null {
return this.subject.value; return this.subject.value;
} }
@ -25,9 +28,13 @@ export class UserService {
protected readonly api: ApiService, protected readonly api: ApiService,
protected readonly stompService: StompService, protected readonly stompService: StompService,
) { ) {
this.stompService.connected$.subscribe(() => { this.router.events.subscribe(e => {
this.refresh(); if (e.type === EventType.NavigationEnd || e.type === EventType.NavigationSkipped) {
console.log(e);
this.refresh();
}
}); });
this.stompService.connected$.subscribe(() => this.refresh());
} }
getCommonByUuid(uuid: string, next: Next<UserCommon>): void { getCommonByUuid(uuid: string, next: Next<UserCommon>): void {
@ -35,17 +42,11 @@ export class UserService {
} }
delete(next?: Next<void>) { delete(next?: Next<void>) {
this.api.getNone(['User', 'delete'], _ => { this.api.getNone(['User', 'delete'], next);
this.refresh();
next && next();
});
} }
changeName(name: string, next?: Next<UserPrivate>) { changeName(name: string, next?: Next<UserPrivate>) {
this.api.postSingle(['User', 'changeName'], name, UserPrivate.fromJson, user => { this.api.postSingle(['User', 'changeName'], name, UserPrivate.fromJson, next);
this.refresh();
next && next(user);
});
} }
goto(user: UserPublic) { goto(user: UserPublic) {
@ -53,7 +54,7 @@ export class UserService {
} }
refresh() { refresh() {
this.api.getSingle(['User', 'whoAmI'], UserPrivate.fromJsonOrNull, user => this.subject.next(user)); this.api.getSingle(['User', 'whoAmI'], UserPrivate.fromJsonOrNull, user => this.setUser(user));
} }
subscribe(next: Next<UserPrivate | null>): Subscription { subscribe(next: Next<UserPrivate | null>): Subscription {
@ -64,4 +65,21 @@ export class UserService {
return this.user?.publicUuid === group.owner.publicUuid; return this.user?.publicUuid === group.owner.publicUuid;
} }
private setUser(user: UserPrivate | null) {
if (this.subject.value?.privateUuid !== user?.privateUuid) {
if (this.subscription) {
this.subscription.unsubscribe();
this.subscription = undefined;
}
if (user) {
this.subscription = this.api.subscribe(["User", user.privateUuid], UserPrivate.fromJson, user => this.setUser(user));
}
}
this.subject.next(user);
}
newSubscriber(): Subscribed<UserPrivate> {
return new Subscribed<UserPrivate>(UserPrivate.samePrivateUuid, (user, next) => this.api.subscribe(['UserPrivate', user.privateUuid], UserPrivate.fromJson, next));
}
} }

View File

@ -31,10 +31,6 @@ export class Group {
); );
} }
static compareCreated(a: Group, b: Group): number {
return a.created.getTime() - b.created.getTime();
}
usersByNameOwnerFirst() { usersByNameOwnerFirst() {
return this.users.sort((a, b) => this.compareOwnerFirstThenName(a, b)); return this.users.sort((a, b) => this.compareOwnerFirstThenName(a, b));
} }
@ -48,5 +44,13 @@ export class Group {
return UserPublic.compareName(a, b); return UserPublic.compareName(a, b);
} }
static sameUuid(a: Group, b: Group): boolean {
return a.uuid === b.uuid;
}
static compareCreated(a: Group, b: Group): number {
return a.created.getTime() - b.created.getTime();
}
} }

View File

@ -5,6 +5,7 @@ import {Router} from "@angular/router";
import {UserService} from "../User/user.service"; import {UserService} from "../User/user.service";
import {validateBoolean} from "../common/validators"; import {validateBoolean} from "../common/validators";
import {Injectable} from "@angular/core"; import {Injectable} from "@angular/core";
import {Subscribed} from "../Subscribed";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -28,10 +29,7 @@ export class GroupService {
} }
create(next: Next<Group>): void { create(next: Next<Group>): void {
this.api.getSingle(['Group', 'create'], Group.fromJson, group => { this.api.getSingle(['Group', 'create'], Group.fromJson, next);
next(group);
this.userService.refresh();
});
} }
changeTitle(group: Group, title: string, next?: Next<Group>) { changeTitle(group: Group, title: string, next?: Next<Group>) {
@ -66,5 +64,9 @@ export class GroupService {
this.router.navigate(['Group', group.uuid]); this.router.navigate(['Group', group.uuid]);
} }
newSubscriber(): Subscribed<Group> {
return new Subscribed<Group>(Group.sameUuid, (group, next) => this.api.subscribe(['Group', group.uuid], Group.fromJson, next));
}
} }

View File

@ -1,34 +1,34 @@
<ng-container *ngIf="group !== null"> <ng-container *ngIf="group.value">
<h1>Nummern</h1> <h1>Nummern</h1>
<table> <table>
<tr> <tr>
<th>Erstellt</th> <th>Erstellt</th>
<td>{{ group.created | date:'yyyy-MM-dd HH:mm' }}</td> <td>{{ group.value.created | date:'yyyy-MM-dd HH:mm' }}</td>
</tr> </tr>
<tr> <tr>
<th>Titel</th> <th>Titel</th>
<td> <td>
<app-text [initial]="group.title" [editable]="userService.iOwn(group)" (onChange)="changeTitle(group, $event)"></app-text> <app-text [initial]="group.value.title" [editable]="userService.iOwn(group.value)" (onChange)="changeTitle(group.value, $event)"></app-text>
<ng-container *ngIf="!userService.iOwn(group)"></ng-container> <ng-container *ngIf="!userService.iOwn(group.value)"></ng-container>
</td> </td>
</tr> </tr>
<tr> <tr>
<th>Passwort</th> <th>Passwort</th>
<td> <td>
<app-text [initial]="group.password" [editable]="userService.iOwn(group)" (onChange)="changePassword(group, $event)"></app-text> <app-text [initial]="group.value.password" [editable]="userService.iOwn(group.value)" (onChange)="changePassword(group.value, $event)"></app-text>
<ng-container *ngIf="!userService.iOwn(group)"></ng-container> <ng-container *ngIf="!userService.iOwn(group.value)"></ng-container>
</td> </td>
</tr> </tr>
<tr> <tr>
<th>Teilnehmer</th> <th>Teilnehmer</th>
<td> <td>
<div class="user" [class.user_owner]="group.isOwner(user)" *ngFor="let user of group.usersByNameOwnerFirst()" (click)="userService.goto(user)">{{ user.name }}</div> <div class="user" [class.user_owner]="group.value.isOwner(user)" *ngFor="let user of group.value.usersByNameOwnerFirst()" (click)="userService.goto(user)">{{ user.name }}</div>
</td> </td>
</tr> </tr>
</table> </table>
</ng-container> </ng-container>
<ng-container *ngIf="uuid && !group && accessDenied"> <ng-container *ngIf="uuid && !group.value && accessDenied">
<h1>Passwort</h1> <h1>Passwort</h1>
<input type="text" [(ngModel)]="password" (keydown.enter)="join()"> <input type="text" [(ngModel)]="password" (keydown.enter)="join()">
<button (click)="join()">Beitreten</button> <button (click)="join()">Beitreten</button>

View File

@ -3,9 +3,10 @@ import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {FormsModule, ReactiveFormsModule} from "@angular/forms"; import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {TextComponent} from "../../../shared/text/text.component"; import {TextComponent} from "../../../shared/text/text.component";
import {ActivatedRoute, Router} from "@angular/router"; import {ActivatedRoute, Router} from "@angular/router";
import {GroupService} from "../../../api/group/GroupService"; import {GroupService} from "../../../api/group/group.service";
import {UserService} from "../../../api/User/user.service"; import {UserService} from "../../../api/User/user.service";
import {Group} from "../../../api/group/Group"; import {Group} from "../../../api/group/Group";
import {Subscribed} from "../../../api/Subscribed";
@Component({ @Component({
selector: 'app-group', selector: 'app-group',
@ -23,7 +24,7 @@ import {Group} from "../../../api/group/Group";
}) })
export class GroupComponent implements OnInit { export class GroupComponent implements OnInit {
protected group: Group | null = null; protected readonly group: Subscribed<Group>;
protected uuid: string | null = null; protected uuid: string | null = null;
@ -37,7 +38,7 @@ export class GroupComponent implements OnInit {
protected readonly groupService: GroupService, protected readonly groupService: GroupService,
protected readonly userService: UserService, protected readonly userService: UserService,
) { ) {
// - this.group = this.groupService.newSubscriber();
} }
ngOnInit(): void { ngOnInit(): void {
@ -49,29 +50,25 @@ export class GroupComponent implements OnInit {
this.groupService.canAccess(this.uuid, canAccess => { this.groupService.canAccess(this.uuid, canAccess => {
this.accessDenied = !canAccess; this.accessDenied = !canAccess;
if (canAccess && this.uuid) { if (canAccess && this.uuid) {
this.groupService.get(this.uuid, group => this.setGroup(group)); this.groupService.get(this.uuid, group => this.group.value = group);
} }
}); });
} }
}); });
} }
private setGroup(group: Group): void {
this.group = group;
}
protected join() { protected join() {
if (this.uuid) { if (this.uuid) {
this.groupService.join(this.uuid, this.password, group => this.setGroup(group)); this.groupService.join(this.uuid, this.password, group => this.group.value = group);
} }
} }
protected changeTitle(group: Group, title: string) { protected changeTitle(group: Group, title: string) {
this.groupService.changeTitle(group, title, group => this.setGroup(group)); this.groupService.changeTitle(group, title, group => this.group.value = group);
} }
protected changePassword(group: Group, password: string) { protected changePassword(group: Group, password: string) {
this.groupService.changePassword(group, password, group => this.setGroup(group)); this.groupService.changePassword(group, password, group => this.group.value = group);
} }
} }

View File

@ -1,8 +1,8 @@
import {Component, OnInit} from '@angular/core'; import {Component} from '@angular/core';
import {GroupListComponent} from "../shared/group-list/group-list.component"; import {GroupListComponent} from "../shared/group-list/group-list.component";
import {UserService} from "../../../api/User/user.service"; import {UserService} from "../../../api/User/user.service";
import {NgIf} from "@angular/common"; import {NgIf} from "@angular/common";
import {GroupService} from "../../../api/group/GroupService"; import {GroupService} from "../../../api/group/group.service";
@Component({ @Component({
selector: 'app-groups', selector: 'app-groups',
@ -14,7 +14,7 @@ import {GroupService} from "../../../api/group/GroupService";
templateUrl: './groups.component.html', templateUrl: './groups.component.html',
styleUrl: './groups.component.less' styleUrl: './groups.component.less'
}) })
export class GroupsComponent implements OnInit { export class GroupsComponent {
constructor( constructor(
protected readonly userService: UserService, protected readonly userService: UserService,
@ -23,10 +23,6 @@ export class GroupsComponent implements OnInit {
// - // -
} }
ngOnInit(): void {
this.userService.refresh();
}
create(): void { create(): void {
this.groupService.create(group => this.groupService.goto(group)); this.groupService.create(group => this.groupService.goto(group));
} }

View File

@ -2,7 +2,7 @@ import {Component, Input} from '@angular/core';
import {UserService} from "../../../../api/User/user.service"; import {UserService} from "../../../../api/User/user.service";
import {Group} from "../../../../api/group/Group"; import {Group} from "../../../../api/group/Group";
import {DatePipe, NgForOf} from "@angular/common"; import {DatePipe, NgForOf} from "@angular/common";
import {GroupService} from "../../../../api/group/GroupService"; import {GroupService} from "../../../../api/group/group.service";
@Component({ @Component({
selector: 'app-group-list', selector: 'app-group-list',

View File

@ -1,5 +1,5 @@
<ng-container *ngIf="userService.user !== null"> <ng-container *ngIf="userService.user !== null">
<h1>Profil</h1> <h1>Profil</h1>
<app-text [initial]="userService.user.name" (onChange)="userService.changeName($event)"></app-text> <app-text [initial]="userService.user.name" [editable]="true" (onChange)="userService.changeName($event)"></app-text>
<app-group-list [groups]="userService.user.groups"></app-group-list> <app-group-list [groups]="userService.user.groups"></app-group-list>
</ng-container> </ng-container>

View File

@ -1,4 +1,4 @@
import {Component, OnInit} from '@angular/core'; import {Component} from '@angular/core';
import {NgForOf, NgIf} from "@angular/common"; import {NgForOf, NgIf} from "@angular/common";
import {UserService} from "../../api/User/user.service"; import {UserService} from "../../api/User/user.service";
import {FormsModule} from "@angular/forms"; import {FormsModule} from "@angular/forms";
@ -18,7 +18,7 @@ import {GroupListComponent} from "../group/shared/group-list/group-list.componen
templateUrl: './profile.component.html', templateUrl: './profile.component.html',
styleUrl: './profile.component.less' styleUrl: './profile.component.less'
}) })
export class ProfileComponent implements OnInit { export class ProfileComponent {
constructor( constructor(
protected readonly userService: UserService, protected readonly userService: UserService,
@ -26,8 +26,4 @@ export class ProfileComponent implements OnInit {
// - // -
} }
ngOnInit(): void {
this.userService.refresh();
}
} }

View File

@ -1,17 +1,19 @@
package de.ph87.tools.group; package de.ph87.tools.group;
import de.ph87.tools.user.UserPublicDto; import de.ph87.tools.user.UserPublicDto;
import de.ph87.tools.web.IWebSocketMessage;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.ToString; import lombok.ToString;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Getter @Getter
@ToString @ToString
public class GroupDto { public class GroupDto implements IWebSocketMessage {
@NonNull @NonNull
public final String uuid; public final String uuid;
@ -43,4 +45,9 @@ public class GroupDto {
this.initial = group.isInitial(); this.initial = group.isInitial();
} }
@Override
public List<Object> getWebsocketTopic() {
return List.of("Group", uuid);
}
} }

View File

@ -1,16 +1,18 @@
package de.ph87.tools.user; package de.ph87.tools.user;
import de.ph87.tools.group.GroupDto; import de.ph87.tools.group.GroupDto;
import de.ph87.tools.web.IWebSocketMessage;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.ToString; import lombok.ToString;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.List;
import java.util.Set; import java.util.Set;
@Getter @Getter
@ToString @ToString
public class UserPrivateDto { public class UserPrivateDto implements IWebSocketMessage {
@NonNull @NonNull
public final String publicUuid; public final String publicUuid;
@ -35,4 +37,9 @@ public class UserPrivateDto {
this.groups = groups; this.groups = groups;
} }
@Override
public List<Object> getWebsocketTopic() {
return List.of("User", privateUuid);
}
} }

View File

@ -6,6 +6,7 @@ import jakarta.annotation.Nullable;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -21,6 +22,8 @@ public class UserPrivateMapper {
private final GroupMapper groupMapper; private final GroupMapper groupMapper;
private final ApplicationEventPublisher applicationEventPublisher;
@NonNull @NonNull
public UserPrivateDto toPrivateDto(@NonNull final User user) { public UserPrivateDto toPrivateDto(@NonNull final User user) {
final Set<GroupDto> groups = groupMapper.findAllByUser(user); final Set<GroupDto> groups = groupMapper.findAllByUser(user);
@ -35,4 +38,9 @@ public class UserPrivateMapper {
return toPrivateDto(user); return toPrivateDto(user);
} }
public void publish(@NonNull final User user) {
final UserPrivateDto dto = toPrivateDto(user);
applicationEventPublisher.publishEvent(dto);
}
} }

View File

@ -57,7 +57,7 @@ public class UserService {
return modify(privateUuid, user -> { return modify(privateUuid, user -> {
user.setName(name); user.setName(name);
log.info("User name changed: user={}", user); log.info("User name changed: user={}", user);
}, true); });
} }
@NonNull @NonNull
@ -83,12 +83,11 @@ public class UserService {
} }
@NonNull @NonNull
private UserPrivateDto modify(@NonNull final String privateUuid, @NonNull Consumer<User> modifier, final boolean doPublish) { private UserPrivateDto modify(@NonNull final String privateUuid, @NonNull Consumer<User> modifier) {
final User user = getByPrivateUuidOrThrow(privateUuid); final User user = getByPrivateUuidOrThrow(privateUuid);
modifier.accept(user); modifier.accept(user);
if (doPublish) { userPublicMapper.publish(user);
userPublicMapper.publish(user); userPrivateMapper.publish(user);
}
return userPrivateMapper.toPrivateDto(user); return userPrivateMapper.toPrivateDto(user);
} }

View File

@ -0,0 +1,28 @@
package de.ph87.tools.web;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SubscriptionInterceptor implements ChannelInterceptor {
@Override
public Message<?> preSend(@NonNull final Message<?> message, @NonNull final MessageChannel channel) {
final StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
if (accessor != null && StompCommand.SUBSCRIBE.equals(accessor.getCommand())) {
final String sessionId = accessor.getSessionId();
final String destination = accessor.getDestination();
log.info("User subscribed with sessionId: {} to destination: {}", sessionId, destination);
}
return message;
}
}