diff --git a/background/parser/facebook.go b/background/parser/facebook.go index a878069..7aa35e3 100644 --- a/background/parser/facebook.go +++ b/background/parser/facebook.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "time" "github.com/aws/aws-sdk-go/aws/session" "github.com/getsentry/sentry-go" @@ -186,18 +187,33 @@ func ParseFacebookArchive(sess *session.Session, db *gorm.DB, accountNumber, wor friendIDs[f.FriendName] = f.ID } - // FIXME: non-friends couldn't be tagged for _, tag := range postTags { friendID, ok := friendIDs[tag.FriendName] - if ok { - tag.FriendID = friendID - tag.PostID = p.ID - if err := db.Create(&tag).Error; err != nil { + if !ok { + friend := &facebook.FriendORM{ + FriendName: tag.FriendName, + DataOwnerID: dataOwner, + Timestamp: time.Now().UnixNano(), + } + if err := db.Where("data_owner_id = ? AND friend_name = ?", dataOwner, tag.FriendName). + FirstOrCreate(&friend).Error; err != nil { if err != sql.ErrNoRows { contextLogger.Error(err) sentry.CaptureException(err) + continue } } + + friendID = friend.ID + friendIDs[tag.FriendName] = friendID + } + tag.FriendID = friendID + tag.PostID = p.ID + if err := db.Create(&tag).Error; err != nil { + if err != sql.ErrNoRows { + contextLogger.Error(err) + sentry.CaptureException(err) + } } } } diff --git a/schema/facebook/friend.go b/schema/facebook/friend.go index a032bba..0ed2545 100644 --- a/schema/facebook/friend.go +++ b/schema/facebook/friend.go @@ -31,7 +31,6 @@ func FriendSchemaLoader() *gojsonschema.Schema { type FriendORM struct { ID uuid.UUID `gorm:"type:uuid;primary_key" sql:"default:uuid_generate_v4()"` - FriendID int64 FriendName string Timestamp int64 `gorm:"unique_index:facebook_friend_owner_timestamp_unique"` DataOwnerID string `gorm:"unique_index:facebook_friend_owner_timestamp_unique"`