Problem: no source in micropub item
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

Solution: add source to micropub new item
This commit is contained in:
Peter Stuifzand 2022-04-20 13:44:19 +02:00
parent 1505943783
commit 7a4f0ce8e1
Signed by: peter
GPG Key ID: 374322D56E5209E8
2 changed files with 17 additions and 11 deletions

View File

@ -86,13 +86,13 @@ func (d *databaseSuite) TestGetChannelFromAuthorization() {
// source_id found // source_id found
r := httptest.NewRequest("POST", "/micropub?source_id=1234", nil) 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.NoError(d.T(), err, "channel from source_id")
assert.Equal(d.T(), "abcdef", c, "channel uid found") assert.Equal(d.T(), "abcdef", c, "channel uid found")
// source_id not found // source_id not found
r = httptest.NewRequest("POST", "/micropub?source_id=1111", nil) 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.Error(d.T(), err, "channel from authorization header")
assert.Equal(d.T(), "", c, "channel uid found") assert.Equal(d.T(), "", c, "channel uid found")
} }

View File

@ -66,7 +66,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
var channel string var channel string
channel, err = getChannelFromAuthorization(r, conn, h.Backend.database) sourceID, channel, err := getChannelFromAuthorization(r, conn, h.Backend.database)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, "unauthorized", http.StatusUnauthorized) http.Error(w, "unauthorized", http.StatusUnauthorized)
@ -100,6 +100,11 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
item.ID = newID item.ID = newID
item.Source = &microsub.Source{
ID: fmt.Sprintf("micropub:%d", sourceID),
Name: fmt.Sprintf("Source %d", sourceID),
}
_, err = h.Backend.channelAddItemWithMatcher(channel, *item) _, err = h.Backend.channelAddItemWithMatcher(channel, *item)
if err != nil { if err != nil {
log.Printf("could not add item to channel %s: %v", channel, err) 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) 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 // backward compatible
sourceID := r.URL.Query().Get("source_id") sourceID := r.URL.Query().Get("source_id")
if sourceID != "" { if sourceID != "" {
row := database.QueryRow(` row := database.QueryRow(`
SELECT c.uid SELECT s.id as source_id, c.uid
FROM "sources" AS "s" FROM "sources" AS "s"
INNER JOIN "channels" AS "c" ON s.channel_id = c.id INNER JOIN "channels" AS "c" ON s.channel_id = c.id
WHERE "auth_code" = $1 WHERE "auth_code" = $1
`, sourceID) `, sourceID)
var channel string var channel string
if err := row.Scan(&channel); err == sql.ErrNoRows { var sourceID int
return "", errors.Wrapf(err, "could not get channel for sourceID: %s", sourceID) 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 // full micropub with indieauth
@ -190,11 +196,11 @@ WHERE "auth_code" = $1
token := authHeader[7:] token := authHeader[7:]
channel, err := redis.String(conn.Do("HGET", "token:"+token, "channel")) channel, err := redis.String(conn.Do("HGET", "token:"+token, "channel"))
if err != nil { 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")
} }