示例#1
0
// removeAction removes an action (flag, like, etc) from an Item.
func removeAction(c *web.Context, userID string, action string, targetID string) error {

	// Get the Target by targetID.
	target, err := service.GetItemByID(c, targetID)
	if err != nil {
		return err
	}

	// Get the actions that the target already has.
	var actions []interface{}
	actions, ok := target.Data[action].([]interface{})
	if !ok {
		return ErrActionNotFound
	}

	// Remove the action in the target's action array.
	for i, usrID := range actions {
		u, ok := usrID.(string)
		if !ok {
			continue
		}
		if u == userID {
			target.Data[action] = append(actions[:i], actions[i+1:]...)
			break
		}
	}

	// Update the target with the new actions's slice.
	if err = service.UpsertItem(c, target); err != nil {
		return err
	}

	return nil
}
示例#2
0
// addAction ads an action (flag, like, etc) perform by an user UserID on a Target.
func addAction(c *web.Context, userID string, action string, targetID string) error {

	// Get the Target's data by targetID.
	target, err := service.GetItemByID(c, targetID)
	if err != nil {
		return err
	}

	// Get the actions that the target already has.
	// If it has no action 'action' then create the field to store the new one.
	var actions []interface{}
	actions, ok := target.Data[action].([]interface{})
	if !ok {
		target.Data[action] = make([]interface{}, 0)
	}

	// Store the action in the target's action array.
	var found bool
	for _, usrID := range actions {
		u, ok := usrID.(string)
		if !ok {
			continue
		}
		if u == userID {
			found = true
			break
		}
	}

	// If the user did not add that action before, then add the action for the user to the target.
	if !found {
		target.Data[action] = append(actions, userID)
	}

	// Update the target with the new actions's slice.
	if err = service.UpsertItem(c, target); err != nil {
		return err
	}

	return nil
}