feat: Add pm
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import type { DirectMessageConversation } from '../domain/models/direct-message.model';
|
||||
|
||||
const STORAGE_PREFIX = 'metoyou_direct_message_conversations';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DirectMessageRepository {
|
||||
async loadConversations(ownerId: string): Promise<DirectMessageConversation[]> {
|
||||
return this.read(ownerId);
|
||||
}
|
||||
|
||||
async saveConversation(ownerId: string, conversation: DirectMessageConversation): Promise<void> {
|
||||
const conversations = this.read(ownerId).filter((entry) => entry.id !== conversation.id);
|
||||
|
||||
conversations.push(conversation);
|
||||
this.write(ownerId, conversations);
|
||||
}
|
||||
|
||||
async getConversation(ownerId: string, conversationId: string): Promise<DirectMessageConversation | null> {
|
||||
return this.read(ownerId).find((conversation) => conversation.id === conversationId) ?? null;
|
||||
}
|
||||
|
||||
async deleteConversation(ownerId: string, conversationId: string): Promise<void> {
|
||||
this.write(ownerId, this.read(ownerId).filter((conversation) => conversation.id !== conversationId));
|
||||
}
|
||||
|
||||
async markRead(ownerId: string, conversationId: string): Promise<void> {
|
||||
const conversations = this.read(ownerId).map((conversation) =>
|
||||
conversation.id === conversationId ? { ...conversation, unreadCount: 0 } : conversation
|
||||
);
|
||||
|
||||
this.write(ownerId, conversations);
|
||||
}
|
||||
|
||||
private read(ownerId: string): DirectMessageConversation[] {
|
||||
const rawValue = localStorage.getItem(this.key(ownerId));
|
||||
|
||||
if (!rawValue) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(rawValue) as DirectMessageConversation[];
|
||||
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private write(ownerId: string, conversations: DirectMessageConversation[]): void {
|
||||
localStorage.setItem(this.key(ownerId), JSON.stringify(conversations));
|
||||
}
|
||||
|
||||
private key(ownerId: string): string {
|
||||
return `${STORAGE_PREFIX}:${ownerId}`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import type { Friend } from '../domain/models/direct-message.model';
|
||||
|
||||
const STORAGE_PREFIX = 'metoyou_friends';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FriendRepository {
|
||||
async loadFriends(ownerId: string): Promise<Friend[]> {
|
||||
return this.read(ownerId);
|
||||
}
|
||||
|
||||
async addFriend(ownerId: string, friend: Friend): Promise<void> {
|
||||
const friends = this.read(ownerId).filter((entry) => entry.userId !== friend.userId);
|
||||
|
||||
friends.push(friend);
|
||||
this.write(ownerId, friends);
|
||||
}
|
||||
|
||||
async removeFriend(ownerId: string, userId: string): Promise<void> {
|
||||
this.write(
|
||||
ownerId,
|
||||
this.read(ownerId).filter((entry) => entry.userId !== userId)
|
||||
);
|
||||
}
|
||||
|
||||
private read(ownerId: string): Friend[] {
|
||||
const rawValue = localStorage.getItem(this.key(ownerId));
|
||||
|
||||
if (!rawValue) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(rawValue) as Friend[];
|
||||
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private write(ownerId: string, friends: Friend[]): void {
|
||||
localStorage.setItem(this.key(ownerId), JSON.stringify(friends));
|
||||
}
|
||||
|
||||
private key(ownerId: string): string {
|
||||
return `${STORAGE_PREFIX}:${ownerId}`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
const STORAGE_PREFIX = 'metoyou_direct_message_queue';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class OfflineQueueRepository {
|
||||
async load(ownerId: string): Promise<string[]> {
|
||||
return this.read(ownerId);
|
||||
}
|
||||
|
||||
async enqueue(ownerId: string, messageId: string): Promise<void> {
|
||||
this.write(ownerId, Array.from(new Set([...this.read(ownerId), messageId])));
|
||||
}
|
||||
|
||||
async remove(ownerId: string, messageId: string): Promise<void> {
|
||||
this.write(
|
||||
ownerId,
|
||||
this.read(ownerId).filter((entry) => entry !== messageId)
|
||||
);
|
||||
}
|
||||
|
||||
async clear(ownerId: string): Promise<void> {
|
||||
this.write(ownerId, []);
|
||||
}
|
||||
|
||||
private read(ownerId: string): string[] {
|
||||
const rawValue = localStorage.getItem(this.key(ownerId));
|
||||
|
||||
if (!rawValue) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(rawValue) as string[];
|
||||
|
||||
return Array.isArray(parsed) ? parsed.filter((entry) => typeof entry === 'string') : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private write(ownerId: string, messageIds: string[]): void {
|
||||
localStorage.setItem(this.key(ownerId), JSON.stringify(messageIds));
|
||||
}
|
||||
|
||||
private key(ownerId: string): string {
|
||||
return `${STORAGE_PREFIX}:${ownerId}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user