Skip to content

Commit

Permalink
显示地理位置
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Mar 16, 2024
1 parent b0fb67f commit cc9cc83
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 84 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect

require (
github.com/RoaringBitmap/roaring v0.9.1 // indirect
github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0 h1:t7uX3JBHdVwAi3G7sSSdbsk8NfgA+LnUS88V/2EKaA0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0/go.mod h1:4OGVnY4qf2+gw+ssiHbW+pq4mo2yko94YxxMmXZ7jCA=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
150 changes: 80 additions & 70 deletions protocols/comment.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protocols/comment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ message Comment {
repeated Comment children = 13;
bool is_admin = 14;
string date_fuzzy = 15;
string geo_location = 16;
}

message GetCommentRequest {
Expand Down
20 changes: 15 additions & 5 deletions service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *Service) GetComment2(name int64) *models.Comment {
// TODO remove email & user
func (s *Service) GetComment(ctx context.Context, req *protocols.GetCommentRequest) (*protocols.Comment, error) {
user := s.auth.AuthGRPC(ctx)
return s.GetComment2(req.Id).ToProtocols(s.isAdminEmail, user), nil
return s.GetComment2(req.Id).ToProtocols(s.isAdminEmail, user, s.geoLocation), nil
}

// UpdateComment ...
Expand Down Expand Up @@ -87,7 +87,7 @@ func (s *Service) UpdateComment(ctx context.Context, req *protocols.UpdateCommen
s.tdb.Where(`id=?`, req.Comment.Id).MustFind(&comment)
}

return comment.ToProtocols(s.isAdminEmail, user), nil
return comment.ToProtocols(s.isAdminEmail, user, s.geoLocation), nil
}

// DeleteComment ...
Expand Down Expand Up @@ -125,7 +125,7 @@ func (s *Service) ListComments(ctx context.Context, in *protocols.ListCommentsRe
stmt.InnerJoin("posts", "comments.post_id = posts.id AND posts.status = 'public'")
}
stmt.MustFind(&parents)
parentProtocolComments = parents.ToProtocols(s.isAdminEmail, user)
parentProtocolComments = parents.ToProtocols(s.isAdminEmail, user, s.geoLocation)
}

needChildren := in.Mode == protocols.ListCommentsMode_ListCommentsModeTree && len(parentProtocolComments) > 0
Expand All @@ -144,7 +144,7 @@ func (s *Service) ListComments(ctx context.Context, in *protocols.ListCommentsRe
stmt.InnerJoin("posts", "comments.post_id = posts.id AND posts.status = 'public'")
}
stmt.MustFind(&children)
childrenProtocolComments = children.ToProtocols(s.isAdminEmail, user)
childrenProtocolComments = children.ToProtocols(s.isAdminEmail, user, s.geoLocation)
}
{
childrenMap := make(map[int64][]*protocols.Comment, len(parentProtocolComments))
Expand All @@ -168,6 +168,11 @@ func (s *Service) GetAllCommentsCount() int64 {
return count
}

func (s *Service) geoLocation(ip string) string {
s.cmtgeo.Queue(ip, nil)
return s.cmtgeo.GetTimeout(ip, time.Millisecond*500)
}

// CreateComment ...
func (s *Service) CreateComment(ctx context.Context, in *protocols.Comment) (*protocols.Comment, error) {
user := s.auth.User(ctx)
Expand Down Expand Up @@ -195,6 +200,11 @@ func (s *Service) CreateComment(ctx context.Context, in *protocols.Comment) (*pr
forward = forward[:p]
}

// 尽早查询地理信息
if err := s.cmtgeo.Queue(forward, nil); err != nil {
log.Println(err)
}

comment := models.Comment{
PostID: in.PostId,
Parent: in.Parent,
Expand Down Expand Up @@ -277,7 +287,7 @@ func (s *Service) CreateComment(ctx context.Context, in *protocols.Comment) (*pr

s.doCommentNotification(&comment)

return comment.ToProtocols(s.isAdminEmail, user), nil
return comment.ToProtocols(s.isAdminEmail, user, s.geoLocation), nil
}

func (s *Service) updateCommentsCount() {
Expand Down
Loading

0 comments on commit cc9cc83

Please sign in to comment.