Esempio n. 1
0
File: user.go Progetto: zeuson/wego
func (this *UserRouter) getFollows(user *models.User, following bool) []map[string]interface{} {
	var follow models.Follow
	if following {
		follow.UserId = user.Id
	} else {
		follow.FollowUserId = user.Id
	}

	nums, _ := models.Count(&follow)

	limit := 20
	pager := this.SetPaginator(limit, nums)

	var follows []*models.Follow
	models.ORM().Limit(limit, pager.Offset()).Find(&follows, &follow)

	if len(follows) == 0 {
		return nil
	}

	ids := make([]int, 0, len(follows))
	for _, follow := range follows {
		if following {
			ids = append(ids, int(follow.FollowUserId))
		} else {
			ids = append(ids, int(follow.UserId))
		}
	}

	var fids = make(map[int]bool)
	models.ORM().In("follow_user_id", ids).Iterate(&models.Follow{UserId: this.User.Id},
		func(idx int, bean interface{}) error {
			tid, _ := utils.StrTo(utils.ToStr(bean.(*models.Follow).Id)).Int()
			if tid > 0 {
				fids[tid] = true
			}
			return nil
		})

	users := make([]map[string]interface{}, 0, len(follows))
	for _, follow := range follows {
		IsFollowed := false
		var u *models.User
		if following {
			u = follow.FollowUser()
		} else {
			u = follow.User()
		}
		if fids != nil {
			IsFollowed = fids[int(u.Id)]
		}
		users = append(users, map[string]interface{}{
			"User":       u,
			"IsFollowed": IsFollowed,
		})
	}

	return users
}
Esempio n. 2
0
File: we.go Progetto: trigrass2/wego
func main() {
	// init configs
	setting.LoadConfig()

	// init models
	models.Init(setting.IsProMode)

	// init social
	social.SetORM(models.ORM())
	setting.SocialAuth = social.NewSocial("/login/", auth.SocialAuther)
	setting.SocialAuth.ConnectSuccessURL = "/settings/profile"
	setting.SocialAuth.ConnectFailedURL = "/settings/profile"
	setting.SocialAuth.ConnectRegisterURL = "/register/connect"
	setting.SocialAuth.LoginURL = "/login"

	// init tango
	t := initTango(setting.IsProMode)

	// init routers
	routers.Init(t)

	// run
	setting.Log.Info("start WeGo", "v"+setting.APP_VER, setting.AppUrl)
	t.Run(setting.AppHost)
}
Esempio n. 3
0
func (this *ModelSelect) Post() {
	search := this.GetString("search")
	model := this.GetString("model")
	result := map[string]interface{}{
		"success": false,
	}

	var data = make([][]interface{}, 0)

	defer func() {
		if len(data) > 0 {
			result["success"] = true
			result["data"] = data
		}
		this.Data["json"] = result
		this.ServeJson(this.Data)
	}()

	if len(search) < 3 {
		return
	}

	if model == "User" {
		models.ORM().Limit(10).Where("user_name like ?", "%"+search+"%").
			Iterate(&models.User{}, func(idx int, bean interface{}) error {
				user := bean.(*models.User)
				data = append(data, []interface{}{user.Id, user.UserName})
				return nil
			})
	}
}
Esempio n. 4
0
func (this *ModelGet) Get() {
	id, _ := this.GetInt("id")
	model := this.GetString("model")
	result := map[string]interface{}{
		"success": false,
	}

	var data = make([][]interface{}, 0)

	defer func() {
		if len(data) > 0 {
			result["success"] = true
			result["data"] = data[0]
		}
		this.Data["json"] = result
		this.ServeJson(this.Data)
	}()

	if model == "User" {
		models.ORM().Iterate(&models.User{Id: id}, func(idx int, bean interface{}) error {
			user := bean.(*models.User)
			data = append(data, []interface{}{user.Id, user.UserName})
			return nil
		})
	}
}
Esempio n. 5
0
func (this *BulletinAdminList) Get() {
	var bulletins []models.Bulletin
	sess := models.ORM().Asc("type")
	if err := this.SetObjects(sess, &bulletins); err != nil {
		this.Data["Error"] = err
		log.Error(err)
	}
}
Esempio n. 6
0
func UserUnFollow(user *models.User, theUser *models.User) {
	follow := &models.Follow{UserId: user.Id, FollowUserId: theUser.Id}
	num, _ := models.ORM().Delete(follow)
	if num > 0 {
		models.ORM().UseBool().Update(&models.Follow{}, follow)

		if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil {
			user.Following = int(nums)
			models.UpdateById(user.Id, user, "following")
		}

		if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil {
			theUser.Followers = int(nums)
			models.UpdateById(theUser.Id, theUser, "followers")
		}
	}
}
Esempio n. 7
0
// view for list model data
func (this *TopicAdminList) Get() {
	var topics []models.Topic
	sess := models.ORM().Desc("category_id")
	if err := this.SetObjects(sess, &topics); err != nil {
		this.Data["Error"] = err
		log.Error(err)
	}
}
Esempio n. 8
0
// view for list model data
func (this *CommentAdminList) Get() {
	var comments []models.Comment
	sess := models.ORM().NewSession()
	defer sess.Close()
	if err := this.SetObjects(sess, &comments); err != nil {
		this.Data["Error"] = err
		log.Error(err)
	}
}
Esempio n. 9
0
// view for list model data
func (this *PageAdminList) Get() {
	var pages []models.Page
	sess := models.ORM().NewSession()
	defer sess.Close()
	if err := this.SetObjects(sess, &pages); err != nil {
		this.Data["Error"] = err
		log.Error(err)
	}
}
Esempio n. 10
0
File: user.go Progetto: zeuson/wego
func (this *Home) Get() error {
	this.Data["IsUserHomePage"] = true

	var user models.User
	if this.getUser(&user) {
		return nil
	}

	//recent posts and comments
	limit := 5

	posts, _ := models.RecentPosts("recent", limit, 0)
	comments, _ := models.RecentCommentsByUserId(user.Id, limit)

	this.Data["TheUserPosts"] = posts
	this.Data["TheUserComments"] = comments

	//follow topics
	var topics []*models.Topic
	ftopics, _ := models.FindFollowTopic(user.Id, 8)
	if len(ftopics) > 0 {
		topics = make([]*models.Topic, 0, len(ftopics))
		for _, ft := range ftopics {
			topics = append(topics, ft.Topic())
		}
	}
	this.Data["TheUserFollowTopics"] = topics
	this.Data["TheUserFollowTopicsMore"] = len(ftopics) >= 8

	//favorite posts
	var favPostIds = make([]int64, 0)
	var favPosts []models.Post
	models.ORM().Limit(8).Desc("created").Iterate(new(models.FavoritePost), func(idx int, bean interface{}) error {
		favPostIds = append(favPostIds, bean.(*models.FavoritePost).PostId)
		return nil
	})
	if len(favPostIds) > 0 {
		models.ORM().In("id", favPostIds).Desc("created").Find(&favPosts)
	}
	this.Data["TheUserFavoritePosts"] = favPosts
	this.Data["TheUserFavoritePostsMore"] = len(favPostIds) >= 8

	return this.Render("user/home.html", this.Data)
}
Esempio n. 11
0
// CanRegistered checks if the username or e-mail is available.
func CanRegistered(userName string, email string) (bool, bool, error) {
	var user models.User
	has, err := models.ORM().Where("user_name = ?", userName).Or("email = ?", email).Get(&user)
	if err != nil {
		return false, false, err
	}
	if has {
		return user.UserName != userName, user.Email != email, nil
	}

	return true, true, nil
}
Esempio n. 12
0
File: user.go Progetto: zeuson/wego
func (this *FavoritePosts) Get() {
	this.TplNames = "user/favorite-posts.html"

	var user models.User
	if this.getUser(&user) {
		return
	}

	var postIds = make([]int64, 0)
	var posts []models.Post
	models.ORM().Desc("created").Iterate(&models.FavoritePost{UserId: user.Id},
		func(idx int, bean interface{}) error {
			postIds = append(postIds, bean.(*models.FavoritePost).PostId)
			return nil
		})
	if len(postIds) > 0 {
		cnt, _ := models.ORM().In("id", postIds).Count(models.Post{})
		pager := this.SetPaginator(setting.PostCountPerPage, cnt)
		models.ORM().Desc("created").Limit(setting.PostCountPerPage, pager.Offset()).Find(&posts)
	}

	this.Data["TheUserFavoritePosts"] = posts
}
Esempio n. 13
0
// view for list model data
func (this *UserAdminList) Get() {
	var q = this.GetString("q")
	var users []models.User
	sess := models.ORM().NewSession()
	defer sess.Close()
	if q != "" {
		sess.Where("email = ?", q).Or("user_name = ?", q)
	}

	this.Data["q"] = q
	if err := this.SetObjects(sess, &users); err != nil {
		this.Data["Error"] = err
		log.Error(err)
	}
}
Esempio n. 14
0
func (this *Users) Post() {
	result := map[string]interface{}{
		"success": false,
	}

	defer func() {
		this.Data["json"] = result
		this.ServeJson(this.Data)
	}()

	if !this.IsAjax() {
		return
	}

	action := this.GetString("action")

	if this.IsLogin {

		switch action {
		case "get-follows":
			var data = make([][]interface{}, 0)
			models.ORM().Iterate(&models.Follow{UserId: this.User.Id},
				func(idx int, bean interface{}) error {
					followUser := bean.(*models.Follow).FollowUser()
					if followUser != nil {
						data = append(data, []interface{}{followUser.NickName, followUser.UserName})
					}
					return nil
				})
			result["success"] = true
			result["data"] = data

		case "follow", "unfollow":
			id, err := utils.StrTo(this.GetString("user")).Int()
			if err == nil && id != int(this.User.Id) {
				fuser := models.User{Id: int64(id)}
				if action == "follow" {
					auth.UserFollow(&this.User, &fuser)
				} else {
					auth.UserUnFollow(&this.User, &fuser)
				}
				result["success"] = true
			}
		}
	}
}