Move all firbase dependencies from actions layer to firebaseService layer (#13)

This commit is contained in:
Qolzam
2017-10-12 17:47:26 +07:00
parent c82826bdd4
commit 0785c38d42
48 changed files with 1305 additions and 796 deletions

View File

@@ -30,10 +30,10 @@ export class AuthorizeService implements IAuthorizeService {
resolve(new LoginUser(result.uid))
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
reject(new SocialError(error.code, error.message))
})
})
}
})
}
/**
* Logs out the user
@@ -41,19 +41,19 @@ export class AuthorizeService implements IAuthorizeService {
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public logout: () => Promise<void> = () => {
return new Promise<void>((resolve, reject) => {
firebaseAuth()
public logout: () => Promise<void> = () => {
return new Promise<void>((resolve, reject) => {
firebaseAuth()
.signOut()
.then((result) => {
resolve()
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
reject(new SocialError(error.code, error.message))
})
})
}
})
}
/**
* Register a user
@@ -61,24 +61,24 @@ export class AuthorizeService implements IAuthorizeService {
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public registerUser: (user: User) => Promise<RegisterUserResult> = (user) => {
return new Promise<RegisterUserResult>((resolve, reject) => {
firebaseAuth()
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`)
firebaseRef.child(`users/${signupResult.uid}/info`)
.set({
...user,
...user,
avatar: 'noImage'
})
.then((result) => {
resolve(new RegisterUserResult(signupResult.uid))
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
@@ -87,25 +87,20 @@ export class AuthorizeService implements IAuthorizeService {
* @memberof IAuthorizeService
*/
public updatePassword: (newPassword: string) => Promise<void> = (newPassword) => {
console.log('====================================')
console.log('update password')
console.log('====================================')
return new Promise<void>((resolve, reject) => {
let user = firebaseAuth().currentUser
console.log('====================================')
console.log(user)
console.log('====================================')
if (user) {
user.updatePassword(newPassword).then(() => {
// Update successful.
resolve()
}).catch((error: any) => {
// An error happened.
reject(new SocialError(error.code, error.message))
})
}
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))
})
}
})
}
}

View File

@@ -1,7 +1,6 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { ICircleService } from 'services/circles'
import { Circle, UserFollower } from 'domain/circles'
@@ -9,19 +8,113 @@ import { User } from 'domain/users'
/**
* Firbase circle service
*
*
* @export
* @class CircleService
* @implements {ICircleService}
*/
export class CircleService implements ICircleService {
addCircle: (userId: string, circle: Circle) => Promise<string>
addFollowingUser: (userId: string, circleId: string, userCircle: User, userFollower: UserFollower, userFollowingId: string) => Promise<void>
deleteFollowingUser: (userId: string, circleId: string, userFollowingId: string) => Promise<void>
updateCircle: (userId: string, circle: Circle, circleId: string) => Promise<void>
deleteCircle: (circleId: string, userId: string) => Promise<void>
getCircles: () => Promise<{ [circleId: string]: Circle }>
getCirclesByUserId: (userId: string) => Promise<{ [circleId: string]: Circle }>
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))
})
})
}
}

View File

@@ -1,5 +1,5 @@
import { AuthorizeService } from './CircleService'
import { CircleService } from './CircleService'
export {
AuthorizeService
}
CircleService
}

View File

@@ -3,15 +3,69 @@ import { firebaseRef, firebaseAuth } from 'app/firebase/'
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))
})
})
}
}

View File

@@ -1,17 +1,110 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { firebaseRef, firebaseAuth, storageRef } from 'app/firebase/'
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))
})
})
}
}

View File

@@ -2,16 +2,71 @@
import { firebaseRef, firebaseAuth } from 'app/firebase/'
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))
})
})
}
}

View File

@@ -2,16 +2,100 @@
import { firebaseRef, firebaseAuth } from 'app/firebase/'
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))
})
})
}
}

View File

@@ -2,16 +2,72 @@
import { firebaseRef, firebaseAuth } from 'app/firebase/'
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((userId) => {
if (userId !== userId) {
let userInfo = usersProfile[userId].info
parsedusersProfile[userId] = {
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))
})
})
}
}

View File

@@ -2,16 +2,56 @@
import { firebaseRef, firebaseAuth } from 'app/firebase/'
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))
})
})
}
}

View File

@@ -2,4 +2,4 @@ import { VoteService } from './VoteService'
export {
VoteService
}
}