Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview/consts: init consts package

things like TangledDid are now defined in the consts package

Signed-off-by: oppiliappan <me@oppi.li>

+114 -27
+108 -13
appview/db/label.go
··· 13 13 14 14 "github.com/bluesky-social/indigo/atproto/syntax" 15 15 "tangled.sh/tangled.sh/core/api/tangled" 16 + "tangled.sh/tangled.sh/core/consts" 16 17 ) 17 18 18 19 type ConcreteType string ··· 78 77 return vt.Type == ConcreteTypeBool 79 78 } 80 79 81 - func (vt ValueType) IsEnumType() bool { 80 + func (vt ValueType) IsEnum() bool { 82 81 return len(vt.Enum) > 0 83 82 } 84 83 ··· 97 96 98 97 Name string 99 98 ValueType ValueType 100 - Scope syntax.NSID 99 + Scope []string 101 100 Color *string 102 101 Multiple bool 103 102 Created time.Time ··· 114 113 Color: l.Color, 115 114 CreatedAt: l.Created.Format(time.RFC3339), 116 115 Multiple: &l.Multiple, 117 - Scope: l.Scope.String(), 116 + Scope: l.Scope, 118 117 ValueType: &vt, 119 118 } 120 119 } ··· 140 139 return *ld.Color 141 140 } 142 141 143 - func LabelDefinitionFromRecord(did, rkey string, record tangled.LabelDefinition) LabelDefinition { 142 + func LabelDefinitionFromRecord(did, rkey string, record tangled.LabelDefinition) (*LabelDefinition, error) { 144 143 created, err := time.Parse(time.RFC3339, record.CreatedAt) 145 144 if err != nil { 146 145 created = time.Now() ··· 156 155 vt = ValueTypeFromRecord(*record.ValueType) 157 156 } 158 157 159 - return LabelDefinition{ 158 + return &LabelDefinition{ 160 159 Did: did, 161 160 Rkey: rkey, 162 161 163 162 Name: record.Name, 164 163 ValueType: vt, 165 - Scope: syntax.NSID(record.Scope), 164 + Scope: record.Scope, 166 165 Color: record.Color, 167 166 Multiple: multiple, 168 167 Created: created, 169 - } 168 + }, nil 170 169 } 171 170 172 171 func DeleteLabelDefinition(e Execer, filters ...filter) error { ··· 185 184 return err 186 185 } 187 186 187 + // no updating type for now 188 188 func AddLabelDefinition(e Execer, l *LabelDefinition) (int64, error) { 189 189 result, err := e.Exec( 190 190 `insert into label_definitions ( ··· 212 210 l.ValueType.Type, 213 211 l.ValueType.Format, 214 212 strings.Join(l.ValueType.Enum, ","), 215 - l.Scope.String(), 213 + strings.Join(l.Scope, ","), 216 214 l.Color, 217 215 l.Multiple, 218 216 l.Created.Format(time.RFC3339), ··· 276 274 277 275 for rows.Next() { 278 276 var labelDefinition LabelDefinition 279 - var createdAt, enumVariants string 277 + var createdAt, enumVariants, scopes string 280 278 var color sql.Null[string] 281 279 var multiple int 282 280 ··· 288 286 &labelDefinition.ValueType.Type, 289 287 &labelDefinition.ValueType.Format, 290 288 &enumVariants, 291 - &labelDefinition.Scope, 289 + &scopes, 292 290 &color, 293 291 &multiple, 294 292 &createdAt, ··· 311 309 312 310 if enumVariants != "" { 313 311 labelDefinition.ValueType.Enum = strings.Split(enumVariants, ",") 312 + } 313 + 314 + for s := range strings.SplitSeq(scopes, ",") { 315 + labelDefinition.Scope = append(labelDefinition.Scope, s) 314 316 } 315 317 316 318 labelDefinitions = append(labelDefinitions, labelDefinition) ··· 637 631 return false 638 632 } 639 633 640 - func (s *LabelState) GetValSet(l string) set { 641 - return s.inner[l] 634 + // go maps behavior in templates make this necessary, 635 + // indexing a map and getting `set` in return is apparently truthy 636 + func (s LabelState) ContainsLabelAndVal(l, v string) bool { 637 + if valset, exists := s.inner[l]; exists { 638 + if _, exists := valset[v]; exists { 639 + return true 640 + } 641 + } 642 + 643 + return false 644 + } 645 + 646 + func (s LabelState) GetValSet(l string) set { 647 + if valset, exists := s.inner[l]; exists { 648 + return valset 649 + } else { 650 + return make(set) 651 + } 642 652 } 643 653 644 654 type LabelApplicationCtx struct { ··· 680 658 } 681 659 682 660 func (c *LabelApplicationCtx) ApplyLabelOp(state LabelState, op LabelOp) error { 683 - def := c.Defs[op.OperandKey] 661 + def, ok := c.Defs[op.OperandKey] 662 + if !ok { 663 + // this def was deleted, but an op exists, so we just skip over the op 664 + return nil 665 + } 684 666 685 667 switch op.Operation { 686 668 case LabelOperationAdd: ··· 744 718 for _, o := range ops { 745 719 _ = c.ApplyLabelOp(state, o) 746 720 } 721 + } 722 + 723 + // IsInverse checks if one label operation is the inverse of another 724 + // returns true if one is an add and the other is a delete with the same key and value 725 + func (op1 LabelOp) IsInverse(op2 LabelOp) bool { 726 + if op1.OperandKey != op2.OperandKey || op1.OperandValue != op2.OperandValue { 727 + return false 728 + } 729 + 730 + return (op1.Operation == LabelOperationAdd && op2.Operation == LabelOperationDel) || 731 + (op1.Operation == LabelOperationDel && op2.Operation == LabelOperationAdd) 732 + } 733 + 734 + // removes pairs of label operations that are inverses of each other 735 + // from the given slice. the function preserves the order of remaining operations. 736 + func ReduceLabelOps(ops []LabelOp) []LabelOp { 737 + if len(ops) <= 1 { 738 + return ops 739 + } 740 + 741 + keep := make([]bool, len(ops)) 742 + for i := range keep { 743 + keep[i] = true 744 + } 745 + 746 + for i := range ops { 747 + if !keep[i] { 748 + continue 749 + } 750 + 751 + for j := i + 1; j < len(ops); j++ { 752 + if !keep[j] { 753 + continue 754 + } 755 + 756 + if ops[i].IsInverse(ops[j]) { 757 + keep[i] = false 758 + keep[j] = false 759 + break // move to next i since this one is now eliminated 760 + } 761 + } 762 + } 763 + 764 + // build result slice with only kept operations 765 + var result []LabelOp 766 + for i, op := range ops { 767 + if keep[i] { 768 + result = append(result, op) 769 + } 770 + } 771 + 772 + return result 773 + } 774 + 775 + func DefaultLabelDefs() []string { 776 + rkeys := []string{ 777 + "wontfix", 778 + "duplicate", 779 + "assignee", 780 + "good-first-issue", 781 + "documentation", 782 + } 783 + 784 + defs := make([]string, len(rkeys)) 785 + for i, r := range rkeys { 786 + defs[i] = fmt.Sprintf("at://%s/%s/%s", consts.TangledDid, tangled.LabelDefinitionNSID, r) 787 + } 788 + 789 + return defs 747 790 }
+6 -14
appview/oauth/handler/handler.go
··· 353 353 return pubKey, nil 354 354 } 355 355 356 - var ( 357 - tangledDid = "did:plc:wshs7t2adsemcrrd4snkeqli" 358 - icyDid = "did:plc:hwevmowznbiukdf6uk5dwrrq" 359 - 360 - defaultSpindle = "spindle.tangled.sh" 361 - defaultKnot = "knot1.tangled.sh" 362 - ) 363 - 364 356 func (o *OAuthHandler) addToDefaultSpindle(did string) { 365 357 // use the tangled.sh app password to get an accessJwt 366 358 // and create an sh.tangled.spindle.member record with that ··· 372 380 } 373 381 374 382 log.Printf("adding %s to default spindle", did) 375 - session, err := o.createAppPasswordSession(o.config.Core.AppPassword, tangledDid) 383 + session, err := o.createAppPasswordSession(o.config.Core.AppPassword, consts.TangledDid) 376 384 if err != nil { 377 385 log.Printf("failed to create session: %s", err) 378 386 return ··· 381 389 record := tangled.SpindleMember{ 382 390 LexiconTypeID: "sh.tangled.spindle.member", 383 391 Subject: did, 384 - Instance: defaultSpindle, 392 + Instance: consts.DefaultSpindle, 385 393 CreatedAt: time.Now().Format(time.RFC3339), 386 394 } 387 395 ··· 403 411 return 404 412 } 405 413 406 - if slices.Contains(allKnots, defaultKnot) { 414 + if slices.Contains(allKnots, consts.DefaultKnot) { 407 415 log.Printf("did %s is already a member of the default knot", did) 408 416 return 409 417 } 410 418 411 419 log.Printf("adding %s to default knot", did) 412 - session, err := o.createAppPasswordSession(o.config.Core.TmpAltAppPassword, icyDid) 420 + session, err := o.createAppPasswordSession(o.config.Core.TmpAltAppPassword, consts.IcyDid) 413 421 if err != nil { 414 422 log.Printf("failed to create session: %s", err) 415 423 return ··· 418 426 record := tangled.KnotMember{ 419 427 LexiconTypeID: "sh.tangled.knot.member", 420 428 Subject: did, 421 - Domain: defaultKnot, 429 + Domain: consts.DefaultKnot, 422 430 CreatedAt: time.Now().Format(time.RFC3339), 423 431 } 424 432 ··· 427 435 return 428 436 } 429 437 430 - if err := o.enforcer.AddKnotMember(defaultKnot, did); err != nil { 438 + if err := o.enforcer.AddKnotMember(consts.DefaultKnot, did); err != nil { 431 439 log.Printf("failed to set up enforcer rules: %s", err) 432 440 return 433 441 }