This repository was archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
backend interfaces #3
Open
DUHsoALEX
wants to merge
7
commits into
main
Choose a base branch
from
backend-interfaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc76d52
backend interfaces
DUHsoALEX d80eef1
convert local backend interface to singleton class
DUHsoALEX f1e8e63
using Records, returning promises, error throwing
DUHsoALEX 702aeed
filters page implements local mode
DUHsoALEX 720285c
filters shows wikis, enable/disable filter stubs
DUHsoALEX 64bbe56
enabled + hit properties
DUHsoALEX 77490ce
controllers
DUHsoALEX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
31 changes: 31 additions & 0 deletions
31
src/frontend/discussions-abusefilter-app/src/controllers/FilterController.ts
This file contains hidden or 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,31 @@ | ||
| import type { Filter, FilterMetadata } from '@shared/filters'; | ||
| import type { BackendInterface } from '../interfaces/BackendInterface'; | ||
|
|
||
| export class FilterController { | ||
| constructor(private readonly backendInterface: BackendInterface) {} | ||
|
|
||
| getFilter(filterId: string) { | ||
| return this.backendInterface.getFilter(filterId); | ||
| } | ||
|
|
||
| createFilter(filter: Filter, filterMetadata: FilterMetadata) { | ||
| this.backendInterface.createFilter(filter, filterMetadata); | ||
| } | ||
|
|
||
| updateFilter(filter: Filter, filterMetadata: FilterMetadata) { | ||
| this.backendInterface.updateFilter(filter, filterMetadata); | ||
| } | ||
|
|
||
| archiveFilter(filterId: string) { | ||
| this.backendInterface.archiveFilter(filterId); | ||
| } | ||
|
|
||
| enableFilter(filterId: string) { | ||
| return this.backendInterface.enableFilter(filterId); | ||
| } | ||
|
|
||
| disableFilter(filterId: string) { | ||
| return this.backendInterface.disableFilter(filterId); | ||
| } | ||
| } | ||
|
|
10 changes: 10 additions & 0 deletions
10
src/frontend/discussions-abusefilter-app/src/controllers/FiltersController.ts
This file contains hidden or 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,10 @@ | ||
| import type { BackendInterface } from '../interfaces/BackendInterface'; | ||
|
|
||
| export class FiltersController { | ||
| constructor(private readonly backendInterface: BackendInterface) {} | ||
|
|
||
| getFilters() { | ||
| return this.backendInterface.getFilters(); | ||
| } | ||
| } | ||
|
|
19 changes: 19 additions & 0 deletions
19
src/frontend/discussions-abusefilter-app/src/interfaces/BackendInterface.ts
This file contains hidden or 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,19 @@ | ||
| import type { Filter, FilterMetadata } from '@shared/filters'; | ||
|
|
||
| export type Error = { | ||
| error: { | ||
| message: string | ||
| } | ||
| }; | ||
|
|
||
| export const interfaceMode = 'local'; | ||
|
|
||
| export interface BackendInterface { | ||
| getFilters: () => Promise<FilterMetadata[]>, | ||
| getFilter: (filterId: string) => Promise<{ filterDetails: Filter, filterMetadata: FilterMetadata }> | Error, | ||
| createFilter: (filter: Filter, filterMetadata: FilterMetadata) => void, | ||
| updateFilter: (filter: Filter, filterMetadata: FilterMetadata) => void, | ||
| archiveFilter: (filterId: string) => void, | ||
| enableFilter: (filterId: string) => void, | ||
| disableFilter: (filterId: string) => void, | ||
|
Comment on lines
+14
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should all ideally return promises ( |
||
| } | ||
kcnotes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
64 changes: 64 additions & 0 deletions
64
src/frontend/discussions-abusefilter-app/src/interfaces/LocalBackendInterface.ts
This file contains hidden or 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,64 @@ | ||
| import type { Filter, FilterMetadata } from '@shared/filters'; | ||
| import type { BackendInterface, Error } from './BackendInterface'; | ||
|
|
||
| export class LocalBackendInterface implements BackendInterface { | ||
| filters: Record<string, Filter>; | ||
| filtersMetadata: Record<string, FilterMetadata>; | ||
| archivedFilters: string[]; | ||
| private static instance: LocalBackendInterface; | ||
|
|
||
| private constructor() { | ||
| this.filters = {}; | ||
| this.filtersMetadata = {}; | ||
| this.archivedFilters = []; | ||
| } | ||
|
|
||
| getFilters(): Promise<FilterMetadata[]> { | ||
| return Promise.resolve(Object.values(this.filtersMetadata)); | ||
| } | ||
|
|
||
| getFilter(filterId: string): Promise<{ filterDetails: Filter, filterMetadata: FilterMetadata }> | Error { | ||
| return new Promise<{ filterDetails: Filter, filterMetadata: FilterMetadata}>((resolve, reject) => { | ||
| if (this.filters[filterId] != null && this.filtersMetadata[filterId] != null) { | ||
| resolve({ filterDetails: this.filters[filterId], filterMetadata: this.filtersMetadata[filterId] }); | ||
| } else { | ||
| reject({ | ||
| error: { | ||
| message: 'Filter ID does not exist', | ||
| }, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| createFilter(filter: Filter, filterMetadata: FilterMetadata) { | ||
| this.filters[filter.id] = filter; | ||
| this.filtersMetadata[filterMetadata.id] = filterMetadata; | ||
| } | ||
|
|
||
| updateFilter(filter: Filter, filterMetadata: FilterMetadata) { | ||
| this.filters[filter.id] = filter; | ||
| this.filtersMetadata[filter.id] = filterMetadata; | ||
| } | ||
|
|
||
| archiveFilter(filterId: string) { | ||
| this.archivedFilters.push(filterId); | ||
| delete this.filters[filterId]; | ||
| delete this.filtersMetadata[filterId]; | ||
| } | ||
|
|
||
| enableFilter(filterId: string) { | ||
| this.filtersMetadata[filterId].enabled = true; | ||
| } | ||
|
|
||
| disableFilter(filterId: string) { | ||
| this.filtersMetadata[filterId].enabled = false; | ||
| } | ||
|
|
||
| static getInstance(): LocalBackendInterface { | ||
| if (!LocalBackendInterface.instance) { | ||
| LocalBackendInterface.instance = new LocalBackendInterface(); | ||
| } | ||
| return LocalBackendInterface.instance; | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/frontend/discussions-abusefilter-app/src/interfaces/ProductionBackendInterface.ts
This file contains hidden or 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,70 @@ | ||
| import type { Filter, FilterMetadata } from '@shared/filters'; | ||
| import type { BackendInterface } from './BackendInterface'; | ||
|
|
||
| const baseApiUrl = ''; | ||
|
|
||
| export class ProductionBackendInterface implements BackendInterface { | ||
| userToken: string; | ||
|
|
||
| constructor( userToken: string) { | ||
| this.userToken = userToken; | ||
| } | ||
|
|
||
| getFilters<T = any>(): Promise<T> { | ||
| return fetch(`${baseApiUrl}/filters`, { | ||
| method: 'GET', | ||
| headers: { | ||
| token: this.userToken, | ||
| }, | ||
| }) | ||
| .then(res => res.json()) | ||
| .catch(e => console.log(e)); | ||
| } | ||
|
|
||
| getFilter<T = any>(filterId: string): Promise<T> { | ||
| return fetch(`${baseApiUrl}/filter/${filterId}`, { | ||
| method: 'GET', | ||
| headers: { | ||
| token: this.userToken, | ||
| }, | ||
| }) | ||
| .then(res => res.json()) | ||
| .catch(e => console.log(e)); | ||
| } | ||
|
|
||
| createFilter(filter: Filter, filterMetadata: FilterMetadata) { | ||
| console.log(filterMetadata); | ||
| return fetch(`${baseApiUrl}/filter/new`, { | ||
| method: 'POST', | ||
| headers: { | ||
| token: this.userToken, | ||
| }, | ||
| body: JSON.stringify(filter), | ||
| }) | ||
| .catch(e => console.log(e)); | ||
| } | ||
|
|
||
| updateFilter(filter: Filter, filterMetadata: FilterMetadata) { | ||
| console.log(filterMetadata); | ||
| return fetch(`${baseApiUrl}/filter/${filter.id}`, { | ||
| method: 'POST', | ||
| headers: { | ||
| token: this.userToken, | ||
| }, | ||
| body: JSON.stringify(filter), | ||
| }) | ||
| .catch(e => console.log(e)); | ||
| } | ||
|
|
||
| archiveFilter(filterId: string) { | ||
| return filterId; | ||
| } | ||
|
|
||
| enableFilter(filterId: string) { | ||
| console.log(filterId); | ||
| } | ||
|
|
||
| disableFilter(filterId: string) { | ||
| console.log(filterId); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Error should be wrapped in
Promise<>, as it's returned in the promise