diff --git a/cmd/eksterd/database_test.go b/cmd/eksterd/database_test.go index 38a9657..74f1ca0 100644 --- a/cmd/eksterd/database_test.go +++ b/cmd/eksterd/database_test.go @@ -86,13 +86,13 @@ func (d *databaseSuite) TestGetChannelFromAuthorization() { // source_id found r := httptest.NewRequest("POST", "/micropub?source_id=1234", nil) - c, err := getChannelFromAuthorization(r, d.Redis, d.Database) + _, c, err := getChannelFromAuthorization(r, d.Redis, d.Database) assert.NoError(d.T(), err, "channel from source_id") assert.Equal(d.T(), "abcdef", c, "channel uid found") // source_id not found r = httptest.NewRequest("POST", "/micropub?source_id=1111", nil) - c, err = getChannelFromAuthorization(r, d.Redis, d.Database) + _, c, err = getChannelFromAuthorization(r, d.Redis, d.Database) assert.Error(d.T(), err, "channel from authorization header") assert.Equal(d.T(), "", c, "channel uid found") } diff --git a/cmd/eksterd/micropub.go b/cmd/eksterd/micropub.go index 7f87eac..57f379b 100644 --- a/cmd/eksterd/micropub.go +++ b/cmd/eksterd/micropub.go @@ -66,7 +66,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var channel string - channel, err = getChannelFromAuthorization(r, conn, h.Backend.database) + sourceID, channel, err := getChannelFromAuthorization(r, conn, h.Backend.database) if err != nil { log.Println(err) http.Error(w, "unauthorized", http.StatusUnauthorized) @@ -100,6 +100,11 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } item.ID = newID + item.Source = µsub.Source{ + ID: fmt.Sprintf("micropub:%d", sourceID), + Name: fmt.Sprintf("Source %d", sourceID), + } + _, err = h.Backend.channelAddItemWithMatcher(channel, *item) if err != nil { log.Printf("could not add item to channel %s: %v", channel, err) @@ -166,22 +171,23 @@ func parseIncomingItem(r *http.Request) (*microsub.Item, error) { return nil, fmt.Errorf("content-type %q is not supported", contentType) } -func getChannelFromAuthorization(r *http.Request, conn redis.Conn, database *sql.DB) (string, error) { +func getChannelFromAuthorization(r *http.Request, conn redis.Conn, database *sql.DB) (int, string, error) { // backward compatible sourceID := r.URL.Query().Get("source_id") if sourceID != "" { row := database.QueryRow(` -SELECT c.uid +SELECT s.id as source_id, c.uid FROM "sources" AS "s" INNER JOIN "channels" AS "c" ON s.channel_id = c.id WHERE "auth_code" = $1 `, sourceID) var channel string - if err := row.Scan(&channel); err == sql.ErrNoRows { - return "", errors.Wrapf(err, "could not get channel for sourceID: %s", sourceID) + var sourceID int + if err := row.Scan(&sourceID, &channel); err == sql.ErrNoRows { + return 0, "", errors.New("channel not found") } - return channel, nil + return sourceID, channel, nil } // full micropub with indieauth @@ -190,11 +196,11 @@ WHERE "auth_code" = $1 token := authHeader[7:] channel, err := redis.String(conn.Do("HGET", "token:"+token, "channel")) if err != nil { - return "", errors.Wrap(err, "could not get channel for token") + return 0, "", errors.Wrap(err, "could not get channel for token") } - return channel, nil + return 0, channel, nil } - return "", fmt.Errorf("could not get channel from authorization") + return 0, "", fmt.Errorf("could not get channel from authorization") }