@@ -23,19 +23,21 @@ type Object struct {
2323 URL types.String `db:"url"`
2424 MuteReason types.String `db:"mute_reason"`
2525
26- Tags map [string ]string `db:"-"`
26+ Tags map [string ]string `db:"-"`
27+ ExtraTags map [string ]string `db:"-"`
2728
2829 db * database.DB
2930}
3031
3132// New creates a new object from the given event.
3233func New (db * database.DB , ev * event.Event ) * Object {
3334 obj := & Object {
34- SourceID : ev .SourceId ,
35- Name : ev .Name ,
36- db : db ,
37- URL : types .MakeString (ev .URL , types .TransformEmptyStringToNull ),
38- Tags : ev .Tags ,
35+ SourceID : ev .SourceId ,
36+ Name : ev .Name ,
37+ db : db ,
38+ URL : types .MakeString (ev .URL , types .TransformEmptyStringToNull ),
39+ Tags : ev .Tags ,
40+ ExtraTags : ev .ExtraTags ,
3941 }
4042 if ev .Mute .Valid && ev .Mute .Bool {
4143 obj .MuteReason = types.String {NullString : sql.NullString {String : ev .MuteReason , Valid : true }}
@@ -79,6 +81,7 @@ func FromEvent(ctx context.Context, db *database.DB, ev *event.Event) (*Object,
7981 } else {
8082 * newObject = * object
8183
84+ newObject .ExtraTags = ev .ExtraTags
8285 newObject .Name = ev .Name
8386 newObject .URL = types .MakeString (ev .URL , types .TransformEmptyStringToNull )
8487 if ev .Mute .Valid {
@@ -104,11 +107,25 @@ func FromEvent(ctx context.Context, db *database.DB, ev *event.Event) (*Object,
104107 }
105108
106109 stmt , _ = db .BuildUpsertStmt (& IdTagRow {})
107- _ , err = tx .NamedExecContext (ctx , stmt , mapToIdTagRows (newObject .ID , ev .Tags ))
110+ _ , err = tx .NamedExecContext (ctx , stmt , mapToTagRows (newObject .ID , ev .Tags ))
108111 if err != nil {
109112 return nil , fmt .Errorf ("failed to upsert object id tags: %w" , err )
110113 }
111114
115+ extraTag := & ExtraTagRow {ObjectId : newObject .ID }
116+ _ , err = tx .NamedExecContext (ctx , `DELETE FROM "object_extra_tag" WHERE "object_id" = :object_id` , extraTag )
117+ if err != nil {
118+ return nil , fmt .Errorf ("failed to delete object extra tags: %w" , err )
119+ }
120+
121+ if len (ev .ExtraTags ) > 0 {
122+ stmt , _ := db .BuildInsertStmt (extraTag )
123+ _ , err = tx .NamedExecContext (ctx , stmt , mapToTagRows (newObject .ID , ev .ExtraTags ))
124+ if err != nil {
125+ return nil , fmt .Errorf ("failed to insert object extra tags: %w" , err )
126+ }
127+ }
128+
112129 if err = tx .Commit (); err != nil {
113130 return nil , fmt .Errorf ("cannot commit object database transaction: %w" , err )
114131 }
@@ -155,6 +172,15 @@ func (o *Object) String() string {
155172 _ , _ = fmt .Fprintf (& b , " Source %d:\n " , o .SourceID )
156173 _ , _ = fmt .Fprintf (& b , " Name: %q\n " , o .Name )
157174 _ , _ = fmt .Fprintf (& b , " URL: %q\n " , o .URL .String )
175+ _ , _ = fmt .Fprintf (& b , " Extra Tags:\n " )
176+
177+ for tag , value := range o .ExtraTags {
178+ _ , _ = fmt .Fprintf (& b , " %q" , tag )
179+ if value != "" {
180+ _ , _ = fmt .Fprintf (& b , " = %q" , value )
181+ }
182+ _ , _ = fmt .Fprintf (& b , "\n " )
183+ }
158184
159185 return b .String ()
160186}
@@ -193,11 +219,11 @@ func ID(source int64, tags map[string]string) types.Binary {
193219 return h .Sum (nil )
194220}
195221
196- // mapToIdTagRows transforms the object tags map to a slice of TagRow struct.
197- func mapToIdTagRows (objectId types.Binary , tags map [string ]string ) []* IdTagRow {
198- var tagRows []* IdTagRow
199- for key , val := range tags {
200- tagRows = append (tagRows , & IdTagRow {
222+ // mapToTagRows transforms the object (extra) tags map to a slice of TagRow struct.
223+ func mapToTagRows (objectId types.Binary , extraTags map [string ]string ) []* TagRow {
224+ var tagRows []* TagRow
225+ for key , val := range extraTags {
226+ tagRows = append (tagRows , & TagRow {
201227 ObjectId : objectId ,
202228 Tag : key ,
203229 Value : val ,
0 commit comments