4
4
"bytes"
5
5
"context"
6
6
"database/sql"
7
+ "encoding/base64"
7
8
"fmt"
8
9
"log"
9
10
"net"
@@ -12,7 +13,6 @@ import (
12
13
"strings"
13
14
"time"
14
15
// required for postgres
15
- "encoding/base64"
16
16
_ "github.com/lib/pq"
17
17
"github.com/segmentio/kafka-go"
18
18
@@ -293,7 +293,6 @@ func (s *GrpcServer) getURLForPost(object string) (string, error) {
293
293
log .Println (err )
294
294
return "" , err
295
295
}
296
- fmt .Println ("Successfully generated presigned URL" , presignedURL )
297
296
return presignedURL .String (), nil
298
297
299
298
}
@@ -379,3 +378,49 @@ func (s *GrpcServer) SearchSimilarFaces(ctx context.Context, request *proto.Face
379
378
380
379
return & proto.FaceSearchResponse {Faces : responseFaces }, nil
381
380
}
381
+
382
+ // SearchUsersWithWeightedPosts searches for users by their occurence in posts, taking the weights into account
383
+ func (s * GrpcServer ) SearchUsersWithWeightedPosts (ctx context.Context , weightedPosts * proto.WeightedPosts ) (* proto.WeightedUsers , error ) {
384
+ userIDToFaces := map [string ]* proto.UserWithFaces {}
385
+ for _ , post := range weightedPosts .Posts {
386
+ rows , err := s .db .Query (`SELECT u.id, COALESCE(user_name, '') as user_name,
387
+ COALESCE(u.real_name, '') as real_name,
388
+ COALESCE(u.bio, '') as bio,
389
+ COALESCE(u.avatar_url, '') as avatar_url
390
+ FROM users u
391
+ INNER JOIN posts p on p.user_id = u.id
392
+ WHERE p.id = $1` , post .PostId )
393
+
394
+ if err != nil {
395
+ return nil , err
396
+ }
397
+
398
+ defer rows .Close ()
399
+
400
+ for rows .Next () {
401
+ user := & proto.User {}
402
+
403
+ err := rows .Scan (& user .Id , & user .UserName , & user .RealName , & user .Bio , & user .AvatarUrl )
404
+ if err != nil {
405
+ return nil , err
406
+ }
407
+
408
+ if userIDToFaces [user .Id ] == nil {
409
+ userIDToFaces [user .Id ] = & proto.UserWithFaces {User : user }
410
+ }
411
+
412
+ wrappedUser := userIDToFaces [user .Id ]
413
+
414
+ wrappedUser .Faces = append (wrappedUser .Faces , post .Faces ... )
415
+ wrappedUser .Weight += post .Weight
416
+ }
417
+ }
418
+
419
+ weightedUsers := & proto.WeightedUsers {}
420
+
421
+ for _ , user := range userIDToFaces {
422
+ weightedUsers .UsersWithFaces = append (weightedUsers .UsersWithFaces , user )
423
+ }
424
+
425
+ return weightedUsers , nil
426
+ }
0 commit comments