Example #1
0
// Move a member record to the queue for getting their user account removed
// (e.g. when they leave us). Set the retention to 2 years instead of just
// 6 months, since they have been a member.
func (m *MembershipDB) MoveMemberToTrash(id, initiator, reason string) error {
	var now time.Time = time.Now()
	var now_long uint64 = uint64(now.Unix())
	var uuid cassandra.UUID
	var mmap map[string]map[string][]*cassandra.Mutation
	var member *MembershipAgreement

	var cp *cassandra.ColumnPath = cassandra.NewColumnPath()
	var cos *cassandra.ColumnOrSuperColumn
	var del *cassandra.Deletion = cassandra.NewDeletion()
	var mu *cassandra.Mutation
	var ts int64

	var err error

	cp.ColumnFamily = "members"
	cp.Column = []byte("pb_data")

	uuid, err = cassandra.GenTimeUUID(&now)
	if err != nil {
		return err
	}

	cos, err = m.conn.Get(
		[]byte(memberPrefix+id), cp, cassandra.ConsistencyLevel_QUORUM)
	if err != nil {
		return err
	}

	member = new(MembershipAgreement)
	err = proto.Unmarshal(cos.Column.Value, member)
	if err != nil {
		return err
	}

	del.Predicate = cassandra.NewSlicePredicate()
	del.Predicate.ColumnNames = allColumns
	del.Timestamp = cos.Column.Timestamp

	mu = cassandra.NewMutation()
	mu.Deletion = del

	mmap = make(map[string]map[string][]*cassandra.Mutation)
	mmap[memberPrefix+id] = make(map[string][]*cassandra.Mutation)
	mmap[memberPrefix+id]["members"] = []*cassandra.Mutation{mu}

	member.Metadata.GoodbyeInitiator = &initiator
	member.Metadata.GoodbyeTimestamp = &now_long
	member.Metadata.GoodbyeReason = &reason

	ts = now.UnixNano()
	cos.Column = cassandra.NewColumn()
	cos.Column.Name = []byte("pb_data")
	cos.Column.Timestamp = &ts
	cos.Column.Value, err = proto.Marshal(member)
	if err != nil {
		return err
	}

	mu = cassandra.NewMutation()
	mu.ColumnOrSupercolumn = cos

	mmap[dequeuePrefix+id] = make(map[string][]*cassandra.Mutation)
	mmap[dequeuePrefix+string([]byte(uuid))] =
		make(map[string][]*cassandra.Mutation)
	mmap[dequeuePrefix+string([]byte(uuid))]["membership_dequeue"] =
		[]*cassandra.Mutation{mu}

	err = m.conn.AtomicBatchMutate(
		mmap, cassandra.ConsistencyLevel_QUORUM)
	return err
}
Example #2
0
// Store the given membership request in the database.
func (m *MembershipDB) StoreMembershipRequest(req *FormInputData) (key string, err error) {
	var bmods map[string]map[string][]*cassandra.Mutation
	var pb *MembershipAgreement = new(MembershipAgreement)
	var bdata []byte
	var now = time.Now()
	var uuid cassandra.UUID
	var c_key string

	// First, let's generate an UUID for the new record.
	uuid, err = cassandra.GenTimeUUID(&now)
	if err != nil {
		return "", err
	}

	c_key = applicationPrefix + string(uuid)
	key = hex.EncodeToString(uuid)

	bmods = make(map[string]map[string][]*cassandra.Mutation)
	bmods[c_key] = make(map[string][]*cassandra.Mutation)
	bmods[c_key]["application"] = make([]*cassandra.Mutation, 0)

	addMembershipRequestInfoString(bmods[c_key], "name", req.MemberData.Name, &now)
	addMembershipRequestInfoString(bmods[c_key], "street", req.MemberData.Street, &now)
	addMembershipRequestInfoString(bmods[c_key], "city", req.MemberData.City, &now)
	addMembershipRequestInfoString(bmods[c_key], "zipcode", req.MemberData.Zipcode, &now)
	addMembershipRequestInfoString(bmods[c_key], "country", req.MemberData.Country, &now)
	addMembershipRequestInfoString(bmods[c_key], "email", req.MemberData.Email, &now)
	addMembershipRequestInfoBytes(bmods[c_key], "email_verified", []byte{0}, &now)
	addMembershipRequestInfoString(bmods[c_key], "phone", req.MemberData.Phone, &now)
	bdata = make([]byte, 8)
	binary.BigEndian.PutUint64(bdata, req.MemberData.GetFee())
	addMembershipRequestInfoBytes(bmods[c_key], "fee", bdata, &now)
	addMembershipRequestInfoString(bmods[c_key], "username", req.MemberData.Username, &now)
	addMembershipRequestInfoString(bmods[c_key], "pwhash", req.MemberData.Pwhash, &now)
	if req.MemberData.GetFeeYearly() {
		addMembershipRequestInfoBytes(bmods[c_key], "fee_yearly", []byte{1}, &now)
	} else {
		addMembershipRequestInfoBytes(bmods[c_key], "fee_yearly", []byte{0}, &now)
	}

	// Set the IP explicitly.
	addMembershipRequestInfoString(bmods[c_key], "sourceip",
		req.Metadata.RequestSourceIp, &now)
	addMembershipRequestInfoString(bmods[c_key], "useragent",
		req.Metadata.UserAgent, &now)

	// Add the membership metadata.
	if req.Metadata.RequestTimestamp == nil {
		req.Metadata.RequestTimestamp = new(uint64)
		*req.Metadata.RequestTimestamp = uint64(now.Unix())
	}
	pb.MemberData = req.MemberData
	pb.Metadata = req.Metadata

	bdata, err = proto.Marshal(pb)
	if err != nil {
		return
	}
	addMembershipRequestInfoBytes(bmods[c_key], "pb_data", bdata, &now)

	// Now execute the batch mutation.
	err = m.conn.BatchMutate(bmods, cassandra.ConsistencyLevel_QUORUM)
	if err != nil {
		return
	}
	return
}