Exemplo n.º 1
0
func (us BoltUserStore) Delete(userId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		if len(userId) <= 0 {
			result.Err = model.NewAppError("BoltUserStore.Delete", "You must get userId in delete", "user_id = "+userId)
			storeChannel <- result
			close(storeChannel)
		}

		err := us.usersBucket.Delete([]byte(userId))
		if err != nil {
			result.Err = model.NewAppError("BoltUserStore.Delete", err.Error(), "")
			storeChannel <- result
			close(storeChannel)
		}

		result.Data = "ok"

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}
Exemplo n.º 2
0
func (cs BoltChannelStore) GetMember(channelId string, userId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		var member *model.ChannelMember
		var itemString string
		notFound := true

		items, err := cs.channelMembersBucket.Items()
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.GetMember", "Error while get members", "")
		} else {
			for _, item := range items {
				itemString = string(item.Value)
				member = model.ChannelMemberFromJson(strings.NewReader(itemString))
				if member.ChannelId == channelId && member.UserId == userId {
					result.Data = member
					notFound = false
				}
			}
			if notFound {
				result.Err = model.NewAppError("BoltChannelStore.GetMember", "Not found", "")
			}
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 3
0
func (cs BoltChannelStore) DeleteMember(member *model.ChannelMember) StoreChannel {
	storeChannel := make(StoreChannel)
	go func() {
		var result StoreResult
		var key string
		var memberJson string
		var m *model.ChannelMember
		items, err := cs.channelMembersBucket.Items()
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.SaveMember", "Error while save members", "")
		} else {
			for _, item := range items {
				memberJson = string(item.Value)
				m = model.ChannelMemberFromJson(strings.NewReader(memberJson))
				if member.UserId == m.UserId && member.ChannelId == m.ChannelId {
					key = string(item.Key)
					break
				}
				err := cs.channelMembersBucket.Delete([]byte(key))
				if err != nil {
					result.Err = model.NewAppError("BoltChannelStore.SaveMember", "Error while save members", "")
				} else {
					result.Data = "ok"
				}

			}

		}

		storeChannel <- result
		close(storeChannel)
		return
	}()
	return storeChannel
}
Exemplo n.º 4
0
func (ps BoltPostStore) GetPosts(channelID string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		posts := make(map[string]*model.Post)
		channelPostsJson, err := ps.postsByChannel.Get([]byte(channelID))
		if err != nil {
			result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
		} else {
			channelPosts := model.BoolMapFromJson(strings.NewReader(string(channelPostsJson)))
			postItems, err := ps.postStore.Items()
			if err != nil {
				result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
				storeChannel <- result
				close(storeChannel)
				return
			}

			for _, postItem := range postItems {
				if channelPosts[string(postItem.Key)] {
					postString := string(postItem.Value)
					posts[string(postItem.Key)] = model.PostFromJson(strings.NewReader(postString))
				}
			}
			result.Data = posts
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 5
0
func (cs BoltChannelStore) SaveMember(member *model.ChannelMember) StoreChannel {
	storeChannel := make(StoreChannel)
	go func() {
		var result StoreResult
		items, err := cs.channelMembersBucket.Items()
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.SaveMember", "Error while save members", "")
		} else {
			id := len(items) + 1
			if len(items) <= 0 {
				member.Role = model.CHANNEL_ROLE_ADMIN
			}
			err := cs.channelMembersBucket.Put([]byte(strconv.Itoa(id)), []byte(member.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveMember", "Error while save members", "")
			} else {
				result.Data = member
			}
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()
	return storeChannel
}
Exemplo n.º 6
0
func (us BoltUserStore) Update(user *model.User) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		if len(user.Password) > 0 {
			if user.PasswordConfirmation != user.Password {
				result.Err = model.NewAppError("BoltUserStore.Update", "When update password must be equal to passwordConfirmation", "user_id"+user.Id)
				storeChannel <- result
				close(storeChannel)
			}
		}
		if us.isLoginTaken(user.Login) {
			result.Err = model.NewAppError("BoltUserStore.Save", "User Login already taken", "user_login="+user.Login)
			storeChannel <- result
			close(storeChannel)
			return
		}

		user.PreSave()
		userJson := user.ToJson()
		us.usersBucket.Put([]byte(user.Id), []byte(userJson))
		us.usersByLoginBucket.Put([]byte(user.Login), []byte(user.Id))

		result.Data = user

		storeChannel <- result
		return
	}()

	return storeChannel
}
Exemplo n.º 7
0
func SetNewChannelAdmin(channelId string, userId string, oldAdminId string) error {
	if len(userId) <= 0 || userId == oldAdminId {
		return model.NewAppError("api.SetNewChannelAdmin", "Wrong User id", "")
	}

	om := <-Srv.Store.Channel().GetMember(channelId, oldAdminId)
	if om.Err != nil {
		return om.Err
	}
	oldAdmin := om.Data.(*model.ChannelMember)
	oldAdmin.Role = model.CHANNEL_ROLE_USER

	nm := <-Srv.Store.Channel().GetMember(channelId, userId)
	if nm.Err != nil {
		return nm.Err
	}

	newAdmin := nm.Data.(*model.ChannelMember)
	newAdmin.Role = model.CHANNEL_ROLE_ADMIN

	result1 := <-Srv.Store.Channel().SaveMember(oldAdmin)
	if result1.Err != nil {
		return result1.Err
	}

	result2 := <-Srv.Store.Channel().SaveMember(newAdmin)
	if result2.Err != nil {
		return result2.Err
	}

	return nil
}
Exemplo n.º 8
0
func (us BoltUserStore) GetUsers() StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var userString string
		var user *model.User
		users := []string{}

		result := StoreResult{}

		items, err := us.usersBucket.Items()

		if err != nil {
			result.Err = model.NewAppError("BoltUserStore.GetUsers", err.Error(), "")
			storeChannel <- result
			close(storeChannel)
		}

		for _, item := range items {
			userString = string(item.Value)
			user = model.UserFromJson(strings.NewReader(userString))
			user.Sanitize()
			users = append(users, string(user.ToJson()))
		}

		result.Data = users
		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}
Exemplo n.º 9
0
func (ps BoltPostStore) Update(post *model.Post) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		postErr := post.IsValid()
		if postErr != nil {
			err := ps.postStore.Put([]byte(post.Id), []byte(post.ToJson()))

			if err != nil {
				result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
			} else {
				result.Data = post
			}
		} else {
			result.Err = postErr
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 10
0
func (cs BoltChannelStore) GetMembers(channel *model.Channel) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult

		items, err := cs.channelMembersBucket.Items()
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.GetMembers", "Error while get members", "")
		} else {
			resultData := make(map[*model.ChannelMember]bool)
			for _, item := range items {
				memberJson := string(item.Value)
				member := model.ChannelMemberFromJson(strings.NewReader(memberJson))
				if member.ChannelId == channel.Id {
					resultData[member] = true
				}
			}

			result.Data = resultData
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 11
0
func (cs BoltChannelStore) GetChannels(userId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		var channel *model.Channel

		items, err := cs.channelsBucket.Items()
		if err != nil || len(userId) <= 0 {
			result.Err = model.NewAppError("BoltChannelStore.GetChannels", "Error while get items", "")
		} else {
			data := make(map[*model.Channel]bool)

			for _, item := range items {
				channel = model.ChannelFromJson(strings.NewReader(string(item.Value)))
				if channel != nil {
					data[channel] = true
				}
			}
			result.Data = data
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 12
0
func (ps BoltPostStore) Save(post *model.Post) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		postErr := post.IsValid()
		if postErr != nil {
			post.PreSave()
			err := ps.postStore.Put([]byte(post.Id), []byte(post.ToJson()))

			postsByChannelsJson, err := ps.postsByChannel.Get([]byte(post.ChannelId))
			postsByChannelsString := string(postsByChannelsJson)
			postsByChannels := model.BoolMapFromJson(strings.NewReader(postsByChannelsString))
			postsByChannels[post.Id] = true

			postsByChannelsString = model.BoolMapToJson(postsByChannels)

			err = ps.postStore.Put([]byte(post.ChannelId), []byte(postsByChannelsString))

			if err != nil {
				result.Err = model.NewAppError("BoltPostStore.", "Post is valid", "")
			} else {
				result.Data = post
			}
		} else {
			result.Err = postErr
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 13
0
func (cs BoltChannelStore) GetChannelMembers(channelId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		var channelMember *model.ChannelMember
		items, err := cs.channelsBucket.Items()

		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.GetChannelMembers", "Error while get members", "")
		} else {
			data := make(map[*model.ChannelMember]bool)
			for _, item := range items {
				itemString := string(item.Value)
				channelMember = model.ChannelMemberFromJson(strings.NewReader(itemString))
				if channelMember != nil && channelId == channelMember.ChannelId {
					data[channelMember] = true
				}
			}
			result.Data = data
		}
		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 14
0
func (cs BoltChannelStore) GetByName(name string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult

		items, err := cs.channelsBucket.Items()
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.GetByName", "Error while get by name", "")
			storeChannel <- result
			close(storeChannel)
			return
		}

		for _, item := range items {
			channel := model.ChannelFromJson(strings.NewReader(string(item.Value)))
			if channel.Name == name {
				result.Data = channel
				storeChannel <- result
				close(storeChannel)
				return
			}
		}
	}()

	return storeChannel
}
Exemplo n.º 15
0
func GetPost(postId string) (*model.Post, *model.AppError) {
	result := <-Srv.Store.Post().Get(postId)
	if result.Err != nil {
		return nil, result.Err
	}

	if result.Data.(*model.Post) == nil {
		return nil, model.NewAppError("Get Post", "Post not found", "")
	}

	return result.Data.(*model.Post), nil
}
Exemplo n.º 16
0
func (us BoltUserStore) Save(user *model.User) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		if len(user.Id) > 0 {
			result.Err = model.NewAppError("BoltUserStore.Save", "Must call update for exisiting user", "user_id="+user.Id)
			storeChannel <- result
			close(storeChannel)
			return
		}

		if us.isLoginTaken(user.Login) {
			result.Err = model.NewAppError("BoltUserStore.Save", "User Login already taken", "user_login="******"BoltUserStore.Save", "User Login already taken", "PasswordConformition="+user.PasswordConfirmation+" Password="+user.Password)
			storeChannel <- result
			close(storeChannel)
			return
		}

		user.PreSave()
		userJSON := user.ToJson()
		us.usersBucket.Put([]byte(user.Id), []byte(userJSON))
		us.usersByLoginBucket.Put([]byte(user.Login), []byte(user.Id))

		result.Data = user

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}
Exemplo n.º 17
0
func (cs BoltChannelStore) Delete(channelId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult

		if len(channelId) <= 0 {
			result.Err = model.NewAppError("BoltChanelStore.Delete", "You must get channelId in delete", "")
			storeChannel <- result
			close(storeChannel)
			return
		}
		err := cs.channelsBucket.Delete([]byte(channelId))
		if err != nil {
			result.Err = model.NewAppError("BoltUserStore.Delete", err.Error(), "")
			storeChannel <- result
			close(storeChannel)
		}
	}()

	return storeChannel
}
Exemplo n.º 18
0
func (cs BoltChannelStore) SaveDirectChannel(channel *model.Channel, mb1 *model.ChannelMember, mb2 *model.ChannelMember) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		channel.PreSave()
		mb1.ChannelId = channel.Id
		mb2.ChannelId = channel.Id
		err := cs.channelsBucket.Put([]byte(channel.Id), []byte(channel.ToJson()))
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel", "")
		} else {

			items, err := cs.channelMembersBucket.Items()
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel", "")
			}

			count := len(items)
			err = cs.channelMembersBucket.Put([]byte(strconv.Itoa(count+1)), []byte(mb1.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel member", "")
			}
			err = cs.channelMembersBucket.Put([]byte(strconv.Itoa(count+2)), []byte(mb2.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.SaveDirectChannel", "Error while save channel member", "")
			}
		}

		result.Data = channel

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 19
0
func (cs BoltChannelStore) Save(channel *model.Channel) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		if channel.Type == model.CHANNEL_DIRECT {
			result.Err = model.NewAppError("BoltChannelStore.Save", "Use Direct channel save to save direct channel", "")
		} else {
			channel.PreSave()
			err := cs.channelsBucket.Put([]byte(channel.Id), []byte(channel.ToJson()))
			if err != nil {
				result.Err = model.NewAppError("BoltChannelStore.Save", "Error while save", "")
			} else {
				result.Data = channel
			}
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 20
0
func (cs BoltChannelStore) GetCount() StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		items, err := cs.channelsBucket.Items()
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.GetCount", "Error while get items", "")
		} else {
			result.Data = len(items)
		}
		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 21
0
func RequireAuth(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	queryParams, err := url.ParseQuery(req.URL.RawQuery)
	token := queryParams["token"][0]
	sessionContext := Context{}
	if err != nil || token == "" || !tokenAuth(token) {
		sessionContext.Err = model.NewAppError("ServeHttp", "Invalid auth data", "")
		rw.WriteHeader(http.StatusUnauthorized)
	} else {
		user := getUserFromJWT(token)
		if user != nil {
			sessionContext.User = (*user)
			context.Set(req, "context", sessionContext)
			next(rw, req)
		} else {
			rw.WriteHeader(http.StatusBadRequest)
		}

	}
}
Exemplo n.º 22
0
func (ps BoltPostStore) Get(postId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		post, err := ps.postStore.Get([]byte(postId))
		if err != nil {
			result.Err = model.NewAppError("BoltPostStore.", "Post is not valid", "")
		} else {
			result.Data = model.PostFromJson(strings.NewReader(string(post)))
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 23
0
func (cs BoltChannelStore) Update(channel *model.Channel) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		channelJson := channel.ToJson()
		err := cs.channelsBucket.Put([]byte(channel.Id), []byte(channelJson))
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.Update", "Error while update", "")
		} else {
			result.Data = channelJson
		}
		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 24
0
func (cs BoltChannelStore) Get(channelId string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		var result StoreResult
		channelJson, err := cs.channelsBucket.Get([]byte(channelId))
		if err != nil {
			result.Err = model.NewAppError("BoltChannelStore.Get", "Error while get", "")
		} else {
			result.Data = model.ChannelFromJson(strings.NewReader(string(channelJson)))
		}

		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 25
0
func (us BoltUserStore) GetByLogin(login string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}
		data := us.getByLogin(login)
		if data == nil {
			result.Err = model.NewAppError("BoltUserStore.GetByLogin", "User not found", "userLogin="+login)
			storeChannel <- result
			close(storeChannel)
			return
		}

		result.Data = data
		storeChannel <- result
		close(storeChannel)
		return
	}()

	return storeChannel
}
Exemplo n.º 26
0
func (us BoltUserStore) GetCount() StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		items, err := us.usersBucket.Items()

		if err != nil {
			result.Err = model.NewAppError("BoltUserStore.GetUsers", err.Error(), "")
			storeChannel <- result
			close(storeChannel)
		}

		result.Data = len(items)
		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}
Exemplo n.º 27
0
func RequireAuthAndUser(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	queryParams, err := url.ParseQuery(req.URL.RawQuery)
	token := queryParams["token"][0]
	sessionContext := Context{}
	if err != nil || token == "" {
		sessionContext.Err = model.NewAppError("ServeHttp", "Invalid auth data", "")
		rw.WriteHeader(http.StatusUnauthorized)
	} else {
		vars := mux.Vars(req)
		uuid := vars["uuid"]
		if tokenAuthWithUser(string(uuid), string(token)) {
			result := <-Srv.Store.User().Get(uuid)
			if result.Err != nil {
				rw.WriteHeader(http.StatusUnauthorized)
			} else {
				sessionContext.User = (*result.Data.(*model.User))
				context.Set(req, "context", sessionContext)
				next(rw, req)
			}
		}
	}
}
Exemplo n.º 28
0
func (c *Context) SetInvalidParam(where string, name string) {
	c.Err = model.NewAppError(where, "Invalid "+name+" parameter", "")
	fmt.Errorf(c.Err.Error())
	c.Err.StatusCode = http.StatusBadRequest
}