Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db14f04

Browse files
author
Michael Demetriou
committed
Redirects from the intermediate page work and if there's an old mention
there it updates the table to include the handle. migrations WIP
1 parent 99bb771 commit db14f04

7 files changed

Lines changed: 71 additions & 36 deletions

File tree

collections.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,14 @@ func handleViewMention(app *App, w http.ResponseWriter, r *http.Request) error {
824824
vars := mux.Vars(r)
825825
handle := vars["handle"]
826826

827-
remoteUser, err := getRemoteUserFromHandle(app, handle)
827+
remoteUser, err := app.db.getProfilePageFromHandle(app, handle)
828828
if err != nil {
829-
log.Error("Couldn't find this user in our database "+handle, err)
830-
return err
829+
log.Error("Couldn't find this user "+handle, err)
830+
return nil
831831
}
832832

833-
w.Write([]byte("go to " + remoteUser.ActorID))
833+
http.Redirect(w, r, remoteUser, http.StatusSeeOther)
834+
w.Write([]byte("go to " + remoteUser))
834835

835836
return nil
836837
}

database.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/guregu/null"
2121
"github.com/guregu/null/zero"
2222
uuid "github.com/nu7hatch/gouuid"
23+
"github.com/writeas/activityserve"
2324
"github.com/writeas/impart"
2425
"github.com/writeas/nerds/store"
2526
"github.com/writeas/web-core/activitypub"
@@ -2449,3 +2450,38 @@ func handleFailedPostInsert(err error) error {
24492450
log.Error("Couldn't insert into posts: %v", err)
24502451
return err
24512452
}
2453+
2454+
func (db *datastore) getProfilePageFromHandle(app *App, handle string) (actorIRI string, err error) {
2455+
remoteuser, errRemoteUser := getRemoteUserFromHandle(app, handle)
2456+
if errRemoteUser != nil {
2457+
// can't find using handle in the table but the table may already have this user without
2458+
// handle from a previous version
2459+
actorIRI = RemoteLookup(handle)
2460+
_, errRemoteUser := getRemoteUser(app, actorIRI)
2461+
// if it exists then we need to update the handle
2462+
if errRemoteUser == nil {
2463+
// query := "UPDATE remoteusers SET handle='" + handle + "' WHERE actor_id='" + iri + "';"
2464+
// log.Info(query)
2465+
_, err := app.db.Exec("UPDATE remoteusers SET handle=? WHERE actor_id=?;", handle, actorIRI)
2466+
if err != nil {
2467+
log.Error("Can't update handle (" + handle + ") in database for user " + actorIRI)
2468+
}
2469+
} else {
2470+
// this probably means we don't have the user in the table so let's try to insert it
2471+
// here we need to ask the server for the inboxes
2472+
remoteActor, err := activityserve.NewRemoteActor(actorIRI)
2473+
if err != nil {
2474+
log.Error("Couldn't fetch remote actor", err)
2475+
}
2476+
fmt.Println(actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
2477+
_, err = app.db.Exec("INSERT INTO remoteusers (actor_id, inbox, shared_inbox, handle) VALUES( ?, ?, ?, ?)", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
2478+
if err != nil {
2479+
log.Error("Can't insert remote user in database", err)
2480+
return "", err
2481+
}
2482+
}
2483+
} else {
2484+
actorIRI = remoteuser.ActorID
2485+
}
2486+
return actorIRI, nil
2487+
}

migrations/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (m *migration) Migrate(db *datastore) error {
5757
var migrations = []Migration{
5858
New("support user invites", supportUserInvites), // -> V1 (v0.8.0)
5959
New("support dynamic instance pages", supportInstancePages), // V1 -> V2 (v0.9.0)
60+
New("support activityPub mentions", supportActivityPubMentions), // V2 -> V3 (v0.1x.0)
6061
}
6162

6263
// CurrentVer returns the current migration version the application is on

migrations/v3.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2019 A Bunch Tell LLC.
3+
*
4+
* This file is part of WriteFreely.
5+
*
6+
* WriteFreely is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License, included
8+
* in the LICENSE file in this source code package.
9+
*/
10+
11+
package migrations
12+
13+
func supportActivityPubMentions(db *datastore) error {
14+
t, err := db.Begin()
15+
16+
_, err = t.Exec(`ALTER TABLE remoteusers ADD COLUMN handle ` + db.typeVarChar(255) + ` DEFAULT '' NOT NULL`)
17+
if err != nil {
18+
t.Rollback()
19+
return err
20+
}
21+
22+
return nil
23+
}

posts.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/guregu/null/zero"
2626
"github.com/kylemcc/twitter-text-go/extract"
2727
"github.com/microcosm-cc/bluemonday"
28-
"github.com/writeas/activityserve"
2928
stripmd "github.com/writeas/go-strip-markdown"
3029
"github.com/writeas/impart"
3130
"github.com/writeas/monday"
@@ -1118,37 +1117,10 @@ func (p *PublicPost) ActivityObject(app *App) *activitystreams.Object {
11181117
mentions := mentionRegex.FindAllString(content, -1)
11191118

11201119
for _, handle := range mentions {
1121-
var actorIRI string
1122-
remoteuser, errRemoteUser := getRemoteUserFromHandle(app, handle)
1123-
if errRemoteUser != nil {
1124-
// can't find using handle in the table but the table may already have this user without
1125-
// handle from a previous version
1126-
actorIRI = RemoteLookup(handle)
1127-
_, errRemoteUser := getRemoteUser(app, actorIRI)
1128-
// if it exists then we need to update the handle
1129-
if errRemoteUser == nil {
1130-
// query := "UPDATE remoteusers SET handle='" + handle + "' WHERE actor_id='" + iri + "';"
1131-
// log.Info(query)
1132-
_, err := app.db.Exec("UPDATE remoteusers SET handle=? WHERE actor_id=?;", handle, actorIRI)
1133-
if err != nil {
1134-
log.Error("Can't update handle (" + handle + ") in database for user " + actorIRI)
1135-
}
1136-
} else {
1137-
// this probably means we don't have the user in the table so let's try to insert it
1138-
// here we need to ask the server for the inboxes
1139-
remoteActor, err := activityserve.NewRemoteActor(actorIRI)
1140-
if err != nil {
1141-
log.Error("Couldn't fetch remote actor", err)
1142-
}
1143-
fmt.Println(actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
1144-
_, err = app.db.Exec("INSERT INTO remoteusers (actor_id, inbox, shared_inbox, handle) VALUES( ?, ?, ?, ?)", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
1145-
if err != nil {
1146-
log.Error("Can't insert remote user in database", err)
1147-
return nil
1148-
}
1149-
}
1150-
} else {
1151-
actorIRI = remoteuser.ActorID
1120+
actorIRI, err := app.db.getProfilePageFromHandle(app, handle)
1121+
if err != nil {
1122+
log.Info("Can't find this user either in the database nor in the remote instance")
1123+
return nil
11521124
}
11531125
mentionedUsers[handle] = actorIRI
11541126
}

schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ CREATE TABLE IF NOT EXISTS `remoteusers` (
181181
`actor_id` varchar(255) NOT NULL,
182182
`inbox` varchar(255) NOT NULL,
183183
`shared_inbox` varchar(255) NOT NULL,
184+
`handle` varchar(255) DEFAULT '' NOT NULL,
184185
PRIMARY KEY (`id`),
185186
UNIQUE KEY `collection_id` (`actor_id`)
186187
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

sqlite.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ CREATE TABLE IF NOT EXISTS `remoteusers` (
172172
actor_id TEXT NOT NULL,
173173
inbox TEXT NOT NULL,
174174
shared_inbox TEXT NOT NULL,
175+
handle TEXT DEFAULT '' NOT NULL,
175176
CONSTRAINT collection_id UNIQUE (actor_id)
176177
);
177178

0 commit comments

Comments
 (0)