Skip to content

Commit a1b024c

Browse files
author
Jim Yeh
committed
Improve friend parser
1 parent 39bd962 commit a1b024c

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

background/parser/facebook.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"time"
910

1011
"github.com/aws/aws-sdk-go/aws/session"
1112
"github.com/getsentry/sentry-go"
@@ -186,18 +187,33 @@ func ParseFacebookArchive(sess *session.Session, db *gorm.DB, accountNumber, wor
186187
friendIDs[f.FriendName] = f.ID
187188
}
188189

189-
// FIXME: non-friends couldn't be tagged
190190
for _, tag := range postTags {
191191
friendID, ok := friendIDs[tag.FriendName]
192-
if ok {
193-
tag.FriendID = friendID
194-
tag.PostID = p.ID
195-
if err := db.Create(&tag).Error; err != nil {
192+
if !ok {
193+
friend := &facebook.FriendORM{
194+
FriendName: tag.FriendName,
195+
DataOwnerID: dataOwner,
196+
Timestamp: time.Now().UnixNano(),
197+
}
198+
if err := db.Where("data_owner_id = ? AND friend_name = ?", dataOwner, tag.FriendName).
199+
FirstOrCreate(&friend).Error; err != nil {
196200
if err != sql.ErrNoRows {
197201
contextLogger.Error(err)
198202
sentry.CaptureException(err)
203+
continue
199204
}
200205
}
206+
207+
friendID = friend.ID
208+
friendIDs[tag.FriendName] = friendID
209+
}
210+
tag.FriendID = friendID
211+
tag.PostID = p.ID
212+
if err := db.Create(&tag).Error; err != nil {
213+
if err != sql.ErrNoRows {
214+
contextLogger.Error(err)
215+
sentry.CaptureException(err)
216+
}
201217
}
202218
}
203219
}

schema/facebook/friend.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func FriendSchemaLoader() *gojsonschema.Schema {
3131

3232
type FriendORM struct {
3333
ID uuid.UUID `gorm:"type:uuid;primary_key" sql:"default:uuid_generate_v4()"`
34-
FriendID int64
3534
FriendName string
3635
Timestamp int64 `gorm:"unique_index:facebook_friend_owner_timestamp_unique"`
3736
DataOwnerID string `gorm:"unique_index:facebook_friend_owner_timestamp_unique"`

0 commit comments

Comments
 (0)