forked from markboard-io/markboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request markboard-io#31 from CulturalProfessor/feature-fav…
…ourites Feat :Favourite Boards
- Loading branch information
Showing
16 changed files
with
287 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { BaseCollection } from './BaseCollection' | ||
import { Meteor } from 'meteor/meteor' | ||
import { BoardRecord } from './BoardCollection' | ||
|
||
export interface IBoardFavoriteRecord { | ||
_id: string | ||
userid: string | ||
boardId: string | ||
last_updated: number | ||
} | ||
|
||
export class BoardFavoritesCollectionClass extends BaseCollection<IBoardFavoriteRecord> { | ||
constructor() { | ||
super('board_favorites') | ||
} | ||
|
||
public async addFavoriteBoard(board: BoardRecord | null, userid: string): Promise<boolean> { | ||
const { id, title, files, elements } = board ?? {} | ||
const values = { userid: userid, files, title, elements, last_updated: Date.now() } | ||
const result = await this.collection.upsertAsync({ _id: id }, { $set: values }) | ||
const numberAffected = result?.numberAffected ?? 0 | ||
return numberAffected > 0 | ||
} | ||
|
||
public async removeFavoriteBoard(userid: string, _id: string): Promise<boolean> { | ||
const query = { userid, _id } | ||
const result = await this.collection.removeAsync(query) | ||
return result > 0 | ||
} | ||
|
||
public async isFavoriteBoard(userid: string, _id: string): Promise<boolean> { | ||
const query = { userid, _id } | ||
const result = await this.collection.findOneAsync(query) | ||
return result != null | ||
} | ||
public async getBoardById(boardId: string): Promise<BoardRecord | null> { | ||
const document = await this.collection.findOneAsync({ _id: boardId }) | ||
if (document != null) Object.assign(document, { id: document._id }) | ||
return document as BoardRecord | ||
} | ||
|
||
public async getFavoriteBoards(userid: string): Promise<IBoardFavoriteRecord[]> { | ||
try { | ||
const query = { userid } | ||
const lastCreatedSortTop = { created_at: -1 } | ||
const favoriteBoards = this.collection | ||
.find(query, { sort: lastCreatedSortTop }) | ||
.fetchAsync() as Promise<IBoardFavoriteRecord[]> | ||
return favoriteBoards | ||
} catch (err) { | ||
console.error(`Error getting favorite boards for user ${userid}:`, err) | ||
throw new Meteor.Error(`Failed to get favorite boards for user ${userid}`) | ||
} | ||
} | ||
} | ||
|
||
export const BoardFavoritesCollection = new BoardFavoritesCollectionClass() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { IBoard } from '../excalidraw/types' | ||
import { BoardFavoritesCollection,BoardCollection } from '../models' | ||
import { Collections } from '../models/Collections' | ||
import { BaseService } from './BaseService' | ||
import { Meteor } from 'meteor/meteor' | ||
import { _makeBoard, _makeBoards } from '../utils/boards' | ||
|
||
export interface IBoardFilterOptions { | ||
keys?: (keyof IBoard)[] | ||
favorite?: boolean | ||
} | ||
|
||
export class BoardFavoriteService extends BaseService { | ||
constructor() { | ||
super('board_favorite') | ||
} | ||
|
||
public startup(): void { | ||
Meteor.publish(Collections.names.board_favorites, () => { | ||
const userid = Meteor.userId() | ||
if (userid != null) { | ||
return BoardFavoritesCollection.raw.find({ userid }) | ||
} | ||
}) | ||
} | ||
public async getBoardById(boardId: string) { | ||
const userid = Meteor.userId() | ||
if (userid != null) { | ||
const record = await BoardFavoritesCollection.getBoardById(boardId) | ||
return record != null ? _makeBoard(record) : null | ||
} | ||
throw new Meteor.Error('Unauthorized') | ||
} | ||
|
||
public async starBoard(boardId: string): Promise<boolean> { | ||
const userId = Meteor.userId() | ||
if (userId != null) { | ||
const board= await BoardCollection.getBoardById(boardId) | ||
const result=BoardFavoritesCollection.addFavoriteBoard(board,userId) | ||
return result | ||
} else { | ||
throw new Meteor.Error('Unauthorized') | ||
} | ||
} | ||
|
||
public cancelStarBoard(boardId: string): Promise<boolean> { | ||
const userId = Meteor.userId() | ||
if (userId != null) { | ||
return BoardFavoritesCollection.removeFavoriteBoard(userId, boardId) | ||
} else { | ||
throw new Meteor.Error('Unauthorized') | ||
} | ||
} | ||
|
||
public async isBoardFavorite(boardId: string): Promise<boolean> { | ||
const userId = Meteor.userId() | ||
if (userId != null) { | ||
return BoardFavoritesCollection.isFavoriteBoard(userId, boardId) | ||
} else { | ||
throw new Meteor.Error('Unauthorized') | ||
} | ||
} | ||
|
||
public async getMyFavoriteBoard(): Promise<IBoard[]> { | ||
const userid = Meteor.userId() | ||
if (userid != null) { | ||
const favoriteBoardRecords = (await BoardFavoritesCollection.getFavoriteBoards(userid)) ?? [] | ||
const favoriteBoards = await Promise.all( | ||
favoriteBoardRecords.map(async record => { | ||
const board = await this.getBoardById(record._id) | ||
return board | ||
}) | ||
) | ||
return favoriteBoards.filter(board => board != null) as IBoard[] | ||
} else { | ||
throw new Meteor.Error('Unauthorized') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './ServiceManagerServer' | ||
export * from './AccountService' | ||
export * from './ExcalidrawSyncService' | ||
export * from './BoardService' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.