class BlockService {
constructor(storageService) {
this.storage = storageService;
this.blockedUsers = new Set();
this.onBlockChange = null;
}
init() {
const blocked = this.storage.getBlockedUsers();
this.blockedUsers = new Set(blocked.map(u => u.toLowerCase()));
}
isBlocked(username) {
if (!username) return false;
return this.blockedUsers.has(username.toLowerCase());
}
block(username) {
if (!username) return false;
const normalized = username.toLowerCase();
if (this.blockedUsers.has(normalized)) return false;
this.blockedUsers.add(normalized);
this.save();
this.notifyChange();
return true;
}
unblock(username) {
if (!username) return false;
const normalized = username.toLowerCase();
if (!this.blockedUsers.has(normalized)) return false;
this.blockedUsers.delete(normalized);
this.save();
this.notifyChange();
return true;
}
toggle(username) {
if (this.isBlocked(username)) {
return this.unblock(username);
}
return this.block(username);
}
getBlockedUsers() {
return Array.from(this.blockedUsers).sort();
}
getBlockedCount() {
return this.blockedUsers.size;
}
clear() {
this.blockedUsers.clear();
this.save();
this.notifyChange();
}
save() {
this.storage.setBlockedUsers(Array.from(this.blockedUsers));
}
notifyChange() {
if (this.onBlockChange) {
this.onBlockChange(this.getBlockedUsers());
}
window.dispatchEvent(new CustomEvent('rantii:block-change', {
detail: { blockedUsers: this.getBlockedUsers() }
}));
}
setBlockChangeCallback(callback) {
this.onBlockChange = callback;
}
filterRants(rants) {
if (!rants || !Array.isArray(rants)) return [];
return rants.filter(rant => !this.isBlocked(rant.user_username));
}
filterComments(comments) {
if (!comments || !Array.isArray(comments)) return [];
return comments.filter(comment => !this.isBlocked(comment.user_username));
}
filterNotifications(notifications) {
if (!notifications || !Array.isArray(notifications)) return [];
return notifications.filter(notif => {
const username = notif.username?.name || notif.user_username?.name || notif.name;
return !this.isBlocked(username);
});
}
}
export { BlockService };