Rename firebase folder to solve conflict
This commit is contained in:
30
app/firebaseClient/index.ts
Normal file
30
app/firebaseClient/index.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
declare const process: any
|
||||||
|
|
||||||
|
import firebase from 'firebase'
|
||||||
|
|
||||||
|
try {
|
||||||
|
let config = {
|
||||||
|
apiKey: process.env.API_KEY,
|
||||||
|
authDomain: process.env.AUTH_DOMAIN,
|
||||||
|
databaseURL: process.env.DATABASE_URL,
|
||||||
|
projectId: process.env.PROJECT_ID,
|
||||||
|
storageBucket: process.env.STORAGE_BUCKET,
|
||||||
|
messagingSenderId: process.env.MESSAGING_SENDER_ID
|
||||||
|
}
|
||||||
|
|
||||||
|
firebase.initializeApp(config)
|
||||||
|
} catch (error) {
|
||||||
|
console.log('=========Firebase initializer==============')
|
||||||
|
console.log(error)
|
||||||
|
console.log('====================================')
|
||||||
|
}
|
||||||
|
|
||||||
|
// - Storage reference
|
||||||
|
export let storageRef = firebase.storage().ref()
|
||||||
|
|
||||||
|
// - Database authorize
|
||||||
|
export let firebaseAuth = firebase.auth
|
||||||
|
export let firebaseRef = firebase.database().ref()
|
||||||
|
|
||||||
|
// - Firebase default
|
||||||
|
export default firebase
|
||||||
106
app/firebaseClient/services/authorize/AuthorizeService.ts
Normal file
106
app/firebaseClient/services/authorize/AuthorizeService.ts
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { IAuthorizeService } from 'services/authorize'
|
||||||
|
import { User } from 'Domain/users'
|
||||||
|
import { LoginUser, RegisterUserResult } from 'domain/authorize'
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase authorize service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class AuthorizeService
|
||||||
|
* @implements {IAuthorizeService}
|
||||||
|
*/
|
||||||
|
export class AuthorizeService implements IAuthorizeService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login the user
|
||||||
|
*
|
||||||
|
* @returns {Promise<LoginUser>}
|
||||||
|
* @memberof IAuthorizeService
|
||||||
|
*/
|
||||||
|
public login: (email: string, password: string) => Promise<LoginUser> = (email, password) => {
|
||||||
|
|
||||||
|
return new Promise<LoginUser>((resolve, reject) => {
|
||||||
|
firebaseAuth()
|
||||||
|
.signInWithEmailAndPassword(email, password)
|
||||||
|
.then((result) => {
|
||||||
|
resolve(new LoginUser(result.uid))
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs out the user
|
||||||
|
*
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @memberof IAuthorizeService
|
||||||
|
*/
|
||||||
|
public logout: () => Promise<void> = () => {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
firebaseAuth()
|
||||||
|
.signOut()
|
||||||
|
.then((result) => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a user
|
||||||
|
*
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @memberof IAuthorizeService
|
||||||
|
*/
|
||||||
|
public registerUser: (user: User) => Promise<RegisterUserResult> = (user) => {
|
||||||
|
return new Promise<RegisterUserResult>((resolve, reject) => {
|
||||||
|
firebaseAuth()
|
||||||
|
.createUserWithEmailAndPassword(user.email as string, user.password as string)
|
||||||
|
.then((signupResult) => {
|
||||||
|
firebaseRef.child(`users/${signupResult.uid}/info`)
|
||||||
|
.set({
|
||||||
|
...user,
|
||||||
|
avatar: 'noImage'
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
resolve(new RegisterUserResult(signupResult.uid))
|
||||||
|
})
|
||||||
|
.catch((error: any) => reject(new SocialError(error.name, error.message)))
|
||||||
|
})
|
||||||
|
.catch((error: any) => reject(new SocialError(error.code, error.message)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update user password
|
||||||
|
*
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @memberof IAuthorizeService
|
||||||
|
*/
|
||||||
|
public updatePassword: (newPassword: string) => Promise<void> = (newPassword) => {
|
||||||
|
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
let user = firebaseAuth().currentUser
|
||||||
|
if (user) {
|
||||||
|
user.updatePassword(newPassword).then(() => {
|
||||||
|
// Update successful.
|
||||||
|
resolve()
|
||||||
|
}).catch((error: any) => {
|
||||||
|
// An error happened.
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/authorize/index.ts
Normal file
5
app/firebaseClient/services/authorize/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { AuthorizeService } from './AuthorizeService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
AuthorizeService
|
||||||
|
}
|
||||||
120
app/firebaseClient/services/circles/CircleService.ts
Normal file
120
app/firebaseClient/services/circles/CircleService.ts
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { ICircleService } from 'services/circles'
|
||||||
|
import { Circle, UserFollower } from 'domain/circles'
|
||||||
|
import { User } from 'domain/users'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase circle service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class CircleService
|
||||||
|
* @implements {ICircleService}
|
||||||
|
*/
|
||||||
|
export class CircleService implements ICircleService {
|
||||||
|
|
||||||
|
public addCircle: (userId: string, circle: Circle)
|
||||||
|
=> Promise<string> = (userId, circle) => {
|
||||||
|
return new Promise<string>((resolve,reject) => {
|
||||||
|
let circleRef = firebaseRef.child(`userCircles/${userId}/circles`).push(circle)
|
||||||
|
circleRef.then(() => {
|
||||||
|
resolve(circleRef.key as string)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public addFollowingUser: (userId: string, circleId: string, userCircle: User, userFollower: UserFollower, userFollowingId: string)
|
||||||
|
=> Promise<void> = (userId, circleId, userCircle, userFollower, userFollowingId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userCircles/${userId}/circles/${circleId}/users/${userFollowingId}`] = userCircle
|
||||||
|
updates[`userCircles/${userFollowingId}/circles/-Followers/users/${userId}`] = userFollower
|
||||||
|
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
public deleteFollowingUser: (userId: string, circleId: string, userFollowingId: string)
|
||||||
|
=> Promise<void> = (userId, circleId, userFollowingId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userCircles/${userId}/circles/${circleId}/users/${userFollowingId}`] = null
|
||||||
|
updates[`userCircles/${userFollowingId}/circles/-Followers/users/${userId}`] = null
|
||||||
|
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
public updateCircle: (userId: string, circleId: string, circle: Circle)
|
||||||
|
=> Promise<void> = (userId, circleId, circle) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userCircles/${userId}/circles/${circleId}`] = circle
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteCircle: (userId: string, circleId: string)
|
||||||
|
=> Promise<void> = (userId, circleId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userCircles/${userId}/circles/${circleId}`] = null
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
public getCircles: (userId: string) => Promise<{ [circleId: string]: Circle }> = (userId) => {
|
||||||
|
return new Promise<{ [circleId: string]: Circle }>((resolve,reject) => {
|
||||||
|
let circlesRef: any = firebaseRef.child(`userCircles/${userId}/circles`)
|
||||||
|
|
||||||
|
circlesRef.once('value').then((snapshot: any) => {
|
||||||
|
let circles: any = snapshot.val() || {}
|
||||||
|
let parsedCircles: { [circleId: string]: Circle } = {}
|
||||||
|
Object.keys(circles).forEach((circleId) => {
|
||||||
|
|
||||||
|
parsedCircles[circleId] = {
|
||||||
|
id: circleId,
|
||||||
|
...circles[circleId]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
resolve(parsedCircles)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/circles/index.ts
Normal file
5
app/firebaseClient/services/circles/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { CircleService } from './CircleService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
CircleService
|
||||||
|
}
|
||||||
71
app/firebaseClient/services/comments/CommentService.ts
Normal file
71
app/firebaseClient/services/comments/CommentService.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { ICommentService } from 'services/comments'
|
||||||
|
import { Comment } from 'domain/comments'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase comment service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class CommentService
|
||||||
|
* @implements {ICommentService}
|
||||||
|
*/
|
||||||
|
export class CommentService implements ICommentService {
|
||||||
|
public addComment: (postId: string, comment: Comment)
|
||||||
|
=> Promise<string> = (postId, comment) => {
|
||||||
|
return new Promise<string>((resolve,reject) => {
|
||||||
|
let commentRef: any = firebaseRef.child(`postComments/${postId}`).push(comment)
|
||||||
|
commentRef.then(() => {
|
||||||
|
resolve(commentRef.key)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public getComments: ()
|
||||||
|
=> Promise<{ [postId: string]: { [commentId: string]: Comment } }> = () => {
|
||||||
|
return new Promise<{ [postId: string]: { [commentId: string]: Comment }}>((resolve,reject) => {
|
||||||
|
let commentsRef: any = firebaseRef.child(`postComments`)
|
||||||
|
commentsRef.on('value', (snapshot: any) => {
|
||||||
|
let comments: {[postId: string]: {[commentId: string]: Comment}} = snapshot!.val() || {}
|
||||||
|
resolve(comments)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateComment: (userId: string, postId: string, comment: Comment)
|
||||||
|
=> Promise<void> = (userId, postId, comment) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`postComments/${postId}/${userId}`] = comment
|
||||||
|
firebaseRef.update(updates)
|
||||||
|
.then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteComment: (commentId: string, postId: string)
|
||||||
|
=> Promise<void> = (commentId, postId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`postComments/${postId}/${commentId}`] = null
|
||||||
|
firebaseRef.update(updates)
|
||||||
|
.then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/comments/index.ts
Normal file
5
app/firebaseClient/services/comments/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { CommentService } from './CommentService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
CommentService
|
||||||
|
}
|
||||||
17
app/firebaseClient/services/common/CommonService.ts
Normal file
17
app/firebaseClient/services/common/CommonService.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { ICommonService } from 'services/common'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase common service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class CommonService
|
||||||
|
* @implements {ICommonService}
|
||||||
|
*/
|
||||||
|
export class CommonService implements ICommonService {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/common/index.ts
Normal file
5
app/firebaseClient/services/common/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { CommonService } from './CommonService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
CommonService
|
||||||
|
}
|
||||||
110
app/firebaseClient/services/imageGallery/ImageGalleryService.ts
Normal file
110
app/firebaseClient/services/imageGallery/ImageGalleryService.ts
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth, storageRef } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { IImageGalleryService } from 'services/imageGallery'
|
||||||
|
import { Image } from 'domain/imageGallery'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase image gallery service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class ImageGalleryService
|
||||||
|
* @implements {IImageGalleryService}
|
||||||
|
*/
|
||||||
|
export class ImageGalleryService implements IImageGalleryService {
|
||||||
|
|
||||||
|
public getImageGallery: (userId: string)
|
||||||
|
=> Promise<Image[]> = (userId) => {
|
||||||
|
return new Promise<Image[]>((resolve,reject) => {
|
||||||
|
let imagesRef: any = firebaseRef.child(`userFiles/${userId}/files/images`)
|
||||||
|
|
||||||
|
imagesRef.once('value').then((snapshot: any) => {
|
||||||
|
let images = snapshot.val() || {}
|
||||||
|
let parsedImages: Image[] = []
|
||||||
|
Object.keys(images).forEach((imageId) => {
|
||||||
|
parsedImages.push({
|
||||||
|
id: imageId,
|
||||||
|
...images[imageId]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
resolve(parsedImages)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public saveImage: (userId: string, image: Image)
|
||||||
|
=> Promise<string> = (userId, image) => {
|
||||||
|
return new Promise<string>((resolve,reject) => {
|
||||||
|
|
||||||
|
let imageRef = firebaseRef.child(`userFiles/${userId}/files/images`).push(image)
|
||||||
|
imageRef.then(() => {
|
||||||
|
resolve(imageRef.key!)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteImage: (userId: string, imageId: string)
|
||||||
|
=> Promise<void> = (userId, imageId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userFiles/${userId}/files/images/${imageId}`] = null
|
||||||
|
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public uploadImage: (file: any, fileName: string, progressCallback: Function)
|
||||||
|
=> Promise<void> = (file, fileName, progressCallback) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
|
||||||
|
let storegeFile: any = storageRef.child(`images/${fileName}`)
|
||||||
|
|
||||||
|
// Upload file
|
||||||
|
let task: any = storegeFile.put(file)
|
||||||
|
|
||||||
|
// Upload storage bar
|
||||||
|
task.on('state_changed', (snapshot: any) => {
|
||||||
|
let percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
|
||||||
|
progressCallback(percentage)
|
||||||
|
|
||||||
|
}, (error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
}, () => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public downloadImage: (fileName: string)
|
||||||
|
=> Promise<string> = (fileName) => {
|
||||||
|
return new Promise<string>((resolve,reject) => {
|
||||||
|
|
||||||
|
// Create a reference to the file we want to download
|
||||||
|
let starsRef: any = storageRef.child(`images/${fileName}`)
|
||||||
|
|
||||||
|
// Get the download URL
|
||||||
|
starsRef.getDownloadURL().then((url: string) => {
|
||||||
|
resolve(url)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/imageGallery/index.ts
Normal file
5
app/firebaseClient/services/imageGallery/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { ImageGalleryService } from './ImageGalleryService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
ImageGalleryService
|
||||||
|
}
|
||||||
21
app/firebaseClient/services/index.ts
Normal file
21
app/firebaseClient/services/index.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { AuthorizeService } from './authorize'
|
||||||
|
import { CircleService } from './circles'
|
||||||
|
import { CommentService } from './comments'
|
||||||
|
import { CommonService } from './common'
|
||||||
|
import { ImageGalleryService } from './imageGallery'
|
||||||
|
import { NotificationService } from './notifications'
|
||||||
|
import { PostService } from './posts'
|
||||||
|
import { UserService } from './users'
|
||||||
|
import { VoteService } from './votes'
|
||||||
|
|
||||||
|
export {
|
||||||
|
AuthorizeService,
|
||||||
|
CircleService,
|
||||||
|
CommentService,
|
||||||
|
CommonService,
|
||||||
|
ImageGalleryService,
|
||||||
|
NotificationService,
|
||||||
|
PostService,
|
||||||
|
UserService,
|
||||||
|
VoteService
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/notifications/index.ts
Normal file
5
app/firebaseClient/services/notifications/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { NotificationService } from './NotificationService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
NotificationService
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { Notification } from 'domain/notifications'
|
||||||
|
import { INotificationService } from 'services/notifications'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase notification service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class NotificationService
|
||||||
|
* @implements {INotificationService}
|
||||||
|
*/
|
||||||
|
export class NotificationService implements INotificationService {
|
||||||
|
|
||||||
|
public addNotification: (notification: Notification)
|
||||||
|
=> Promise<void> = (notification: Notification) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
firebaseRef.child(`userNotifies/${notification.notifyRecieverUserId}`)
|
||||||
|
.push(notification)
|
||||||
|
.then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public getNotifications: (userId: string)
|
||||||
|
=> Promise<{ [notifyId: string]: Notification }> = (userId) => {
|
||||||
|
return new Promise<{ [notifyId: string]: Notification }>((resolve,reject) => {
|
||||||
|
let notifiesRef: any = firebaseRef.child(`userNotifies/${userId}`)
|
||||||
|
notifiesRef.on('value', (snapshot: any) => {
|
||||||
|
let notifies: {[notifyId: string]: Notification} = snapshot.val() || {}
|
||||||
|
resolve(notifies)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteNotification: (notificationId: string, userId: string)
|
||||||
|
=> Promise <void> = (notificationId, userId) => {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userNotifies/${userId}/${notificationId}`] = null
|
||||||
|
firebaseRef.update(updates)
|
||||||
|
.then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public setSeenNotification: (notificationId: string, userId: string, notification: Notification)
|
||||||
|
=> Promise <void> = (notificationId, userId, notification) => {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userNotifies/${userId}/${notificationId}`] = notification
|
||||||
|
firebaseRef.update(updates)
|
||||||
|
.then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code, error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
101
app/firebaseClient/services/posts/PostService.ts
Normal file
101
app/firebaseClient/services/posts/PostService.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { Post } from 'domain/posts'
|
||||||
|
import { IPostService } from 'services/posts'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase post service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class PostService
|
||||||
|
* @implements {IPostService}
|
||||||
|
*/
|
||||||
|
export class PostService implements IPostService {
|
||||||
|
|
||||||
|
public addPost: (userId: string, post: Post)
|
||||||
|
=> Promise<string> = (userId, post) => {
|
||||||
|
return new Promise<string>((resolve,reject) => {
|
||||||
|
let postRef: any = firebaseRef.child(`userPosts/${userId}/posts`).push(post)
|
||||||
|
postRef.then(() => {
|
||||||
|
resolve(postRef.key)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public updatePost: (userId: string, postId: string, post: Post)
|
||||||
|
=> Promise<void> = (userId, postId, post) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userPosts/${userId}/posts/${postId}`] = post
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public deletePost: (userId: string, postId: string)
|
||||||
|
=> Promise<void> = (userId, postId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`userPosts/${userId}/posts/${postId}`] = null
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public getPosts: (userId: string)
|
||||||
|
=> Promise<{ [postId: string]: Post }> = (userId) => {
|
||||||
|
return new Promise<{ [postId: string]: Post }>((resolve,reject) => {
|
||||||
|
|
||||||
|
let postsRef: any = firebaseRef.child(`userPosts/${userId}/posts`)
|
||||||
|
postsRef.once('value').then((snapshot: any) => {
|
||||||
|
let posts: any = snapshot.val() || {}
|
||||||
|
let parsedPosts: { [postId: string]: Post } = {}
|
||||||
|
|
||||||
|
Object.keys(posts).forEach((postId) => {
|
||||||
|
parsedPosts[postId] = {
|
||||||
|
id: postId,
|
||||||
|
...posts[postId]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
resolve(parsedPosts)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public getPostById: (userId: string, postId: string)
|
||||||
|
=> Promise<Post> = (userId, postId) => {
|
||||||
|
return new Promise<Post>((resolve,reject) => {
|
||||||
|
|
||||||
|
let postsRef: any = firebaseRef.child(`userPosts/${userId}/posts/${postId}`)
|
||||||
|
|
||||||
|
postsRef.once('value').then((snapshot: any) => {
|
||||||
|
let newPost = snapshot.val() || {}
|
||||||
|
let post: Post = {
|
||||||
|
id: postId,
|
||||||
|
...newPost
|
||||||
|
}
|
||||||
|
resolve(post)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/posts/index.ts
Normal file
5
app/firebaseClient/services/posts/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { PostService } from './PostService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
PostService
|
||||||
|
}
|
||||||
73
app/firebaseClient/services/users/UserService.ts
Normal file
73
app/firebaseClient/services/users/UserService.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { Profile } from 'domain/users'
|
||||||
|
import { IUserService } from 'services/users'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase user service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class UserService
|
||||||
|
* @implements {IUserService}
|
||||||
|
*/
|
||||||
|
export class UserService implements IUserService {
|
||||||
|
public getUserProfile: (userId: string)
|
||||||
|
=> Promise<Profile> = (userId) => {
|
||||||
|
return new Promise<Profile>((resolve,reject) => {
|
||||||
|
let userProfileRef: any = firebaseRef.child(`users/${userId}/info`)
|
||||||
|
|
||||||
|
userProfileRef.once('value').then((snapshot: any) => {
|
||||||
|
let userProfile: Profile = snapshot.val() || {}
|
||||||
|
resolve(userProfile)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateUserProfile: (userId: string, profile: Profile)
|
||||||
|
=> Promise<void> = (userId, profile) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
let updates: any = {}
|
||||||
|
|
||||||
|
updates[`users/${userId}/info`] = profile
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
public getUsersProfile: (userId: string)
|
||||||
|
=> Promise<{ [userId: string]: Profile }> = (userId) => {
|
||||||
|
return new Promise<{ [userId: string]: Profile }>((resolve,reject) => {
|
||||||
|
let usersProfileRef: any = firebaseRef.child(`users`)
|
||||||
|
|
||||||
|
usersProfileRef.once('value').then((snapshot: any) => {
|
||||||
|
let usersProfile: any = snapshot.val() || {}
|
||||||
|
let parsedusersProfile: {[userId: string]: Profile} = {}
|
||||||
|
Object.keys(usersProfile).forEach((userKey) => {
|
||||||
|
if (userId !== userKey) {
|
||||||
|
let userInfo = usersProfile[userKey].info
|
||||||
|
parsedusersProfile[userKey] = {
|
||||||
|
avatar: userInfo.avatar,
|
||||||
|
email: userInfo.email,
|
||||||
|
fullName: userInfo.fullName,
|
||||||
|
banner: userInfo.banner,
|
||||||
|
tagLine: userInfo.tagLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
resolve(parsedusersProfile)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/users/index.ts
Normal file
5
app/firebaseClient/services/users/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { UserService } from './UserService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
UserService
|
||||||
|
}
|
||||||
57
app/firebaseClient/services/votes/VoteService.ts
Normal file
57
app/firebaseClient/services/votes/VoteService.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
// - Import react components
|
||||||
|
import { firebaseRef, firebaseAuth } from 'app/firebaseClient/'
|
||||||
|
|
||||||
|
import { SocialError } from 'domain/common'
|
||||||
|
import { Vote } from 'domain/votes'
|
||||||
|
import { IVoteService } from 'services/votes'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Firbase vote service
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @class VoteService
|
||||||
|
* @implements {IVoteService}
|
||||||
|
*/
|
||||||
|
export class VoteService implements IVoteService {
|
||||||
|
|
||||||
|
public addVote: (vote: Vote)
|
||||||
|
=> Promise<string> = (vote) => {
|
||||||
|
return new Promise<string>((resolve,reject) => {
|
||||||
|
let voteRef = firebaseRef.child(`postVotes/${vote.postId}`)
|
||||||
|
.push(vote)
|
||||||
|
voteRef.then(() => {
|
||||||
|
resolve(voteRef.key!)
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
public getVotes: ()
|
||||||
|
=> Promise<{ [postId: string]: { [voteId: string]: Vote } }> = () => {
|
||||||
|
return new Promise<{ [postId: string]: { [voteId: string]: Vote } }>((resolve,reject) => {
|
||||||
|
let votesRef: any = firebaseRef.child(`postVotes`)
|
||||||
|
|
||||||
|
votesRef.on('value',(snapshot: any) => {
|
||||||
|
let postVotes: {[postId: string]: {[voteId: string]: Vote}} = snapshot.val() || {}
|
||||||
|
resolve(postVotes)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteVote: (voteId: string, postId: string)
|
||||||
|
=> Promise<void> = (voteId, postId) => {
|
||||||
|
return new Promise<void>((resolve,reject) => {
|
||||||
|
let updates: any = {}
|
||||||
|
updates[`postVotes/${postId}/${voteId}`] = null
|
||||||
|
firebaseRef.update(updates).then(() => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
reject(new SocialError(error.code,error.message))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
5
app/firebaseClient/services/votes/index.ts
Normal file
5
app/firebaseClient/services/votes/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { VoteService } from './VoteService'
|
||||||
|
|
||||||
|
export {
|
||||||
|
VoteService
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user