コード例 #1
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
}
コード例 #2
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
}
コード例 #3
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
}
コード例 #4
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
}
コード例 #5
0
ファイル: channel.go プロジェクト: sichacvah/portable_chat
func addMember(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	sessionContext := context.Get(r, "context").(Context)
	newMember := model.ChannelMemberFromJson(r.Body)
	vars := mux.Vars(r)
	channelId := string(vars["id"])

	uc := <-Srv.Store.Channel().GetMember(channelId, sessionContext.User.Id)
	if uc.Err != nil {
		sessionContext.SetInvalidParam("add User to Channel", "Channel ID = "+channelId)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	channelMember := uc.Data.(*model.ChannelMember)
	if channelMember.Role != model.CHANNEL_ROLE_ADMIN {
		sessionContext.SetInvalidParam("add User to Channel", "Channel ID = "+channelId)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	mc := <-Srv.Store.Channel().SaveMember(newMember)
	if mc.Err != nil {
		sessionContext.SetInvalidParam("add User to Channel", "Channel ID = "+channelId)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte(newMember.ToJson()))
}
コード例 #6
0
ファイル: channel.go プロジェクト: sichacvah/portable_chat
func deleteMember(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	sessionContext := context.Get(r, "context").(Context)
	vars := mux.Vars(r)
	channelId := string(vars["id"])
	m := model.ChannelMemberFromJson(r.Body)

	if m.UserId == sessionContext.User.Id {
		sessionContext.SetInvalidParam("delete User from Channel", "cant delete themself")
		w.WriteHeader(http.StatusBadRequest)
		return

	}

	cm := <-Srv.Store.Channel().GetMember(channelId, sessionContext.User.Id)
	if cm.Err != nil {
		sessionContext.SetInvalidParam("delete User from Channel", "")
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	currenMember := cm.Data.(*model.ChannelMember)
	if currenMember.Role != model.CHANNEL_ROLE_ADMIN {
		sessionContext.SetInvalidParam("delete User from Channel", "")
		w.WriteHeader(http.StatusForbidden)
		return
	}

	result := <-Srv.Store.Channel().DeleteMember(m)
	if result.Err != nil {
		sessionContext.SetInvalidParam("delete User from Channel", "")
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusOK)
}