Exemple #1
0
func TestUnmarshal(t *testing.T) {
	var es ExampleStruct
	mongo.Unmarshal(b, &es)
	assertTrue(es.First == 1, "unmarshal int", t)
	assertTrue(es.Second == 2, "unmarshal float64", t)
	assertTrue(es.Third == "three", "unmarshal string", t)
	assertTrue(es.Fifth.F == "i" && es.Fifth.V == "e", "unmarshal struct", t)
}
Exemple #2
0
func TestMarshal(t *testing.T) {
	var es1 ExampleStruct
	mongo.Unmarshal(b, &es1)
	bs1, _ := mongo.Marshal(&es1)
	bs2, _ := mongo.BytesToBSON(b)
	assertTrue(mongo.Equal(bs1, bs2), "unmarshal->marshal", t)

	m := map[string]string{"f": "i", "v": "e"}
	bs3, _ := mongo.Marshal(&m)
	assertTrue(mongo.Equal(bs3, bs2.Get("fifth")), "marshal map", t)

	arr, _ := mongo.Marshal([]int{1, 2, 3})
	assertTrue(arr.Elem(0).Long() == 1, "array marshal (0)", t)
	assertTrue(arr.Elem(1).Long() == 2, "array marshal (1)", t)
	assertTrue(arr.Elem(2).Long() == 3, "array marshal (2)", t)

	d := time.UTC()
	es2 := &ExampleStruct2{d}
	bs2, _ = mongo.Marshal(es2)
	assertTrue(bs2.Get("date").Date().Seconds() == d.Seconds(), "date marshal", t)
	es2 = new(ExampleStruct2)
	mongo.Unmarshal(bs2.Bytes(), es2)
	assertTrue(es2.Date.Seconds() == d.Seconds(), "date unmarshal", t)
}
Exemple #3
0
func GetPost(postId string) (user *UserPost, err os.Error) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	qfind, err := mongo.Marshal(OidSearch{"_id": mongo.ObjectId{postId}}, atreps)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUser: Cannot marshal. %s", err))
	}

	doc, err := ColPost.FindOne(qfind)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUser: Cannot find user by id `%s`. %s.", postId, err))
	}

	var post *UserPost = new(UserPost)

	mongo.Unmarshal(doc.Bytes(), post, atreps)

	return post, nil
}
Exemple #4
0
func GetUserSettings(userId string) (user_settings *UserSettings, err os.Error) {
	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}
	qfind, err := mongo.Marshal(map[string]string{"_user_id": userId}, atreps)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUserSettings: Cannot marshal. %s", err))
	}

	doc, err := ColUserSettings.FindOne(qfind)
	if err != nil {
		return nil, os.NewError(fmt.Sprintf("getUserSettings: Cannot find user settings by user id `%s`. %s.", userId, err))
	}

	user_settings = new(UserSettings)

	mongo.Unmarshal(doc.Bytes(), user_settings, atreps)

	return user_settings, nil
}
Exemple #5
0
func ProcessPost(post_id string) {

	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}

	// get post

	qfind, err := mongo.Marshal(oidSearch{"_id": mongo.ObjectId{post_id}}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	doc, err := ColPost.FindOne(qfind)
	if err != nil {
		Warn("Cannot find post by id `%s`. %s.\n", post_id, err)
		return
	}

	var post UserPost

	mongo.Unmarshal(doc.Bytes(), &post, atreps)

	Info2("Got post id: %v, writer: %s, origin: %s\n", strid(post.Id_), post.WriterId, post.Origin_id_)

	// get writer
	writer, err := GetUser(post.WriterId)
	if err != nil {
		Warn("Cannot get writer with id `%s` for post id `%s`. err: %v\n", post.WriterId, strid(post.Id_), err)
		return
	}

	//fmt.Printf("writer: %v\n", writer.Name)
	InsertPostStream(strid(writer.Id_), post_id)

	var writer_id string = strid(writer.Id_)
	//fmt.Printf("writer_id: %s\n", writer_id)

	// get all followers
	qfind, err = mongo.Marshal(map[string]string{"_followed_user_ids": writer_id}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	// broadcast to all followers
	cursor, err := ColUser.FindAll(qfind)
	if err == nil {
		for cursor.HasMore() {
			doc, err = cursor.GetNext()
			if err != nil {
				Error("Cannot get next. e: %v\n", err)
				break
			}

			var follower User
			mongo.Unmarshal(doc.Bytes(), &follower, atreps)

			if strid(follower.Id_) == post.Origin_id_ {
				continue
			}

			Info2("broadcast to follower: id: %v, name: %v\n", strid(follower.Id_), follower.Name)

			// insert to follower streams
			InsertPostStream(strid(follower.Id_), post_id)
		}
	} else {
		Warn("Cannot find post by id `%s`. %s.\n", post_id, err)
	}

	// get origin
	if len(strings.Trim(post.Origin_id_, " \n\t\r")) == 0 {
		Warn("post with id `%s` has no origin id\n", strid(post.Id_))
		return
	}

	doc, err = GetOrigin(post.Origin_id_)
	if err == nil {

		var spdoc SuperDoc

		mongo.Unmarshal(doc.Bytes(), &spdoc, atreps)

		switch spdoc.Metaname_ {
		case "Channel":
			var ch Channel
			mongo.Unmarshal(doc.Bytes(), &ch, atreps)

			//var ch_id string = strid(ch.Id_)
			// fmt.Printf("ch_id: %s\n", ch_id)

			// get all members
			qfind, err := mongo.Marshal(map[string]string{"_channel_ids": strid(ch.Id_)}, atreps)
			if err != nil {
				Error("Cannot marshal. %s\n", err)
				return
			}

			// broadcast to all members
			cursor, err := ColUser.FindAll(qfind)
			if err == nil {
				for cursor.HasMore() {
					doc, err = cursor.GetNext()
					if err != nil {
						Error("Cannot get next. e: %v\n", err)
						break
					}

					var follower User
					mongo.Unmarshal(doc.Bytes(), &follower, atreps)
					Info2("broadcast to member: id: %v, name: %v\n", strid(follower.Id_), follower.Name)

					// insert to follower streams
					InsertPostStream(strid(follower.Id_), post_id)
				}
			} else {
				Warn("Cannot find post by id `%s`. %s.\n", post_id, err)
			}
		case "User":
			Info2("Inserting into origin=User. origin_id: %v\n", post.Origin_id_)
			InsertPostStream(post.Origin_id_, post_id)
		}

	} else {
		Warn("Cannot get origin id `%s`\n", post.Origin_id_)
	}

}
Exemple #6
0
func BroadcastAll(postId string) {

	if dbcon == nil {
		Error("Database not connected.\n")
		return
	}

	// get post

	qfind, err := mongo.Marshal(oidSearch{"_id": mongo.ObjectId{postId}}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	doc, err := ColPost.FindOne(qfind)
	if err != nil {
		Warn("Cannot find post by id `%s`. %s.\n", postId, err)
		return
	}

	var post UserPost

	mongo.Unmarshal(doc.Bytes(), &post, atreps)

	Info2("Got post id: %v, writer: %s, origin: %s\n", strid(post.Id_), post.WriterId, post.Origin_id_)

	// rm old refs
	RmPostStream(postId)

	// broadcast to all users
	qfind, err = mongo.Marshal(map[string]string{}, atreps)
	if err != nil {
		Error("Cannot marshal. %s\n", err)
		return
	}

	cursor, err := ColUser.FindAll(qfind)
	if err == nil {
		for cursor.HasMore() {
			doc, err = cursor.GetNext()
			if err != nil {
				Error("Cannot get next. e: %v\n", err)
				break
			}

			var user User
			mongo.Unmarshal(doc.Bytes(), &user, atreps)
			user_id := strid(user.Id_)
			Info2("broadcast to all: id: %v, name: %v\n", user_id, user.Name)

			// get user settings, is accept feed pop article
			st, err := GetUserSettings(user_id)
			if err != nil {
				Error("BroadcastAll: Cannot get user settings for user id `%v`\n", user_id)
				continue
			}
			if st.Feed_pop_article == false {
				Info2("BroadcastAll: Ignore feed article for user id `%v`\n", user_id)
				continue
			}

			// insert to follower streams
			InsertPostStream(user_id, postId)
		}
	} else {
		Warn("Cannot find post by id `%s`. %s.\n", postId, err)
	}

}