|
| 1 | +package example |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + |
| 10 | + "github.com/aws/aws-lambda-go/events" |
| 11 | + "github.com/aws/aws-lambda-go/lambda" |
| 12 | + "github.com/sfomuseum/go-activitypub/database" |
| 13 | + "github.com/sfomuseum/go-activitypub/queue" |
| 14 | +) |
| 15 | + |
| 16 | +func Run(ctx context.Context) error { |
| 17 | + fs := DefaultFlagSet() |
| 18 | + return RunWithFlagSet(ctx, fs) |
| 19 | +} |
| 20 | + |
| 21 | +func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet) error { |
| 22 | + |
| 23 | + opts, err := OptionsFromFlagSet(ctx, fs) |
| 24 | + |
| 25 | + if err != nil { |
| 26 | + return fmt.Errorf("Failed to derive options from flagset, %w", err) |
| 27 | + } |
| 28 | + |
| 29 | + return RunWithOptions(ctx, opts) |
| 30 | +} |
| 31 | + |
| 32 | +func RunWithOptions(ctx context.Context, opts *RunOptions) error { |
| 33 | + |
| 34 | + messages_db, err := database.NewMessagesDatabase(ctx, opts.MessagesDatabaseURI) |
| 35 | + |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("Failed to create new messages database, %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + defer messages_db.Close(ctx) |
| 41 | + |
| 42 | + notes_db, err := database.NewNotesDatabase(ctx, opts.NotesDatabaseURI) |
| 43 | + |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("Failed to create new notes database, %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + defer notes_db.Close(ctx) |
| 49 | + |
| 50 | + accounts_db, err := database.NewAccountsDatabase(ctx, opts.AccountsDatabaseURI) |
| 51 | + |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("Failed to create new accounts database, %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + defer accounts_db.Close(ctx) |
| 57 | + |
| 58 | + properties_db, err := database.NewPropertiesDatabase(ctx, opts.PropertiesDatabaseURI) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("Failed to create new properties database, %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + defer properties_db.Close(ctx) |
| 65 | + |
| 66 | + activities_db, err := database.NewActivitiesDatabase(ctx, opts.ActivitiesDatabaseURI) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("Failed to create new activities database, %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + defer activities_db.Close(ctx) |
| 73 | + |
| 74 | + posts_db, err := database.NewPostsDatabase(ctx, opts.PostsDatabaseURI) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("Failed to create new posts database, %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + defer posts_db.Close(ctx) |
| 81 | + |
| 82 | + post_tags_db, err := database.NewPostTagsDatabase(ctx, opts.PostTagsDatabaseURI) |
| 83 | + |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("Failed to create new post tags database, %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + defer post_tags_db.Close(ctx) |
| 89 | + |
| 90 | + deliveries_db, err := database.NewDeliveriesDatabase(ctx, opts.DeliveriesDatabaseURI) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("Failed to create new deliveries database, %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + defer deliveries_db.Close(ctx) |
| 97 | + |
| 98 | + followers_db, err := database.NewFollowersDatabase(ctx, opts.FollowersDatabaseURI) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("Failed to create new followers database, %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + defer followers_db.Close(ctx) |
| 105 | + |
| 106 | + _, err = queue.NewDeliveryQueue(ctx, opts.DeliveryQueueURI) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("Failed to create new delivery queue, %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Note: Don't close delivery_q in a Lambda context. This will trigger errors like this: |
| 113 | + // "Failed to send message, pubsub: Topic has been Shutdown (code=FailedPrecondition)" |
| 114 | + |
| 115 | + process_follower := func(ctx context.Context, follower_id int64) error { |
| 116 | + |
| 117 | + logger := slog.Default() |
| 118 | + logger = logger.With("method", "process_follow") |
| 119 | + logger = logger.With("follower id", follower_id) |
| 120 | + |
| 121 | + logger.Info("Process follow") |
| 122 | + |
| 123 | + // Message is the thing which was dispatched by the process message queue (in www/inbox_post.go) |
| 124 | + |
| 125 | + f, err := followers_db.GetFollowerWithId(ctx, follower_id) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + logger.Error("Failed to retrieve message", "error", err) |
| 129 | + return fmt.Errorf("Failed to retrieve message, %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + // Account is the account that the note (message) was delivered to |
| 133 | + |
| 134 | + msg_acct, err := accounts_db.GetAccountWithId(ctx, f.AccountId) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + logger.Error("Failed to retrieve account for follow", "account id", f.AccountId, "error", err) |
| 138 | + return fmt.Errorf("Failed to retrieve account for message, %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + logger = logger.With("follow account id", msg_acct.Id) |
| 142 | + logger = logger.With("follow follower address", f.FollowerAddress) |
| 143 | + |
| 144 | + logger.Info("Your code goes here (parse note, etc.)") |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + // Process multiple follower IDs |
| 149 | + // Maybe try to do this concurrently? |
| 150 | + |
| 151 | + process_followers := func(ctx context.Context, follower_ids ...int64) error { |
| 152 | + |
| 153 | + for _, id := range follower_ids { |
| 154 | + |
| 155 | + err := process_follower(ctx, id) |
| 156 | + |
| 157 | + if err != nil { |
| 158 | + slog.Error("Failed to process follower", "id", id, "error", err) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | + } |
| 164 | + |
| 165 | + // Actually start the application |
| 166 | + |
| 167 | + switch opts.Mode { |
| 168 | + case "cli": |
| 169 | + |
| 170 | + return process_followers(ctx, opts.FollowerIds...) |
| 171 | + case "lambda": |
| 172 | + |
| 173 | + handler := func(ctx context.Context, sqsEvent events.SQSEvent) error { |
| 174 | + |
| 175 | + follower_ids := make([]int64, len(sqsEvent.Records)) |
| 176 | + |
| 177 | + for idx, message := range sqsEvent.Records { |
| 178 | + |
| 179 | + logger := slog.Default() |
| 180 | + logger = logger.With("message id", message.MessageId) |
| 181 | + |
| 182 | + // logger.Debug("SQS", "message", message.Body) |
| 183 | + |
| 184 | + var follower_id int64 |
| 185 | + |
| 186 | + err := json.Unmarshal([]byte(message.Body), &follower_id) |
| 187 | + |
| 188 | + if err != nil { |
| 189 | + logger.Error("Failed to unmarshal message", "error", err) |
| 190 | + return fmt.Errorf("Failed to unmarshal message ID, %w", err) |
| 191 | + } |
| 192 | + |
| 193 | + follower_ids[idx] = follower_id |
| 194 | + } |
| 195 | + |
| 196 | + return process_followers(ctx, follower_ids...) |
| 197 | + } |
| 198 | + |
| 199 | + lambda.Start(handler) |
| 200 | + |
| 201 | + default: |
| 202 | + return fmt.Errorf("Invalid or unsupported mode") |
| 203 | + } |
| 204 | + |
| 205 | + return nil |
| 206 | +} |
0 commit comments