-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: conversations API & migration tool
- Loading branch information
1 parent
f1cbf6f
commit 548b244
Showing
36 changed files
with
1,755 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// GoToSocial | ||
// Copyright (C) GoToSocial Authors [email protected] | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package conversations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" | ||
"github.com/superseriousbusiness/gotosocial/internal/db/bundb" | ||
"github.com/superseriousbusiness/gotosocial/internal/email" | ||
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility" | ||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | ||
"github.com/superseriousbusiness/gotosocial/internal/log" | ||
"github.com/superseriousbusiness/gotosocial/internal/oauth" | ||
"github.com/superseriousbusiness/gotosocial/internal/processing/stream" | ||
"github.com/superseriousbusiness/gotosocial/internal/processing/workers" | ||
"github.com/superseriousbusiness/gotosocial/internal/state" | ||
"github.com/superseriousbusiness/gotosocial/internal/typeutils" | ||
) | ||
|
||
func initState(ctx context.Context) (*state.State, error) { | ||
var state state.State | ||
state.Caches.Init() | ||
state.Caches.Start() | ||
|
||
// Set the state DB connection | ||
dbConn, err := bundb.NewBunDBService(ctx, &state) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating dbConn: %w", err) | ||
} | ||
state.DB = dbConn | ||
|
||
return &state, nil | ||
} | ||
|
||
func stopState(state *state.State) error { | ||
err := state.DB.Close() | ||
state.Caches.Stop() | ||
return err | ||
} | ||
|
||
// Migrate processes every DM to create conversations. | ||
var Migrate action.GTSAction = func(ctx context.Context) (err error) { | ||
state, err := initState(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
// Ensure state gets stopped on return. | ||
if err := stopState(state); err != nil { | ||
log.Error(ctx, err) | ||
} | ||
}() | ||
|
||
streamProcessor := stream.New(state, oauth.New(ctx, state.DB)) | ||
surface := workers.Surface{ | ||
State: state, | ||
Converter: typeutils.NewConverter(state), | ||
Stream: &streamProcessor, | ||
Filter: visibility.NewFilter(state), | ||
} | ||
if surface.EmailSender, err = email.NewNoopSender(func(toAddress string, message string) {}); err != nil { | ||
return nil | ||
} | ||
|
||
return state.DB.MigrateConversations(ctx, func(ctx context.Context, status *gtsmodel.Status) error { | ||
return surface.UpdateConversationsForStatus(ctx, status, false) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// GoToSocial | ||
// Copyright (C) GoToSocial Authors [email protected] | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package conversations | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" | ||
"github.com/superseriousbusiness/gotosocial/internal/gtserror" | ||
"github.com/superseriousbusiness/gotosocial/internal/oauth" | ||
) | ||
|
||
// ConversationDELETEHandler swagger:operation DELETE /api/v1/conversations/{id} conversationDelete | ||
// | ||
// Delete a single conversation with the given ID. | ||
// | ||
// --- | ||
// tags: | ||
// - conversations | ||
// | ||
// produces: | ||
// - application/json | ||
// | ||
// parameters: | ||
// - | ||
// name: id | ||
// type: string | ||
// description: ID of the conversation | ||
// in: path | ||
// required: true | ||
// | ||
// security: | ||
// - OAuth2 Bearer: | ||
// - write:conversations | ||
// | ||
// responses: | ||
// '200': | ||
// description: conversation deleted | ||
// '400': | ||
// description: bad request | ||
// '401': | ||
// description: unauthorized | ||
// '404': | ||
// description: not found | ||
// '406': | ||
// description: not acceptable | ||
// '500': | ||
// description: internal server error | ||
func (m *Module) ConversationDELETEHandler(c *gin.Context) { | ||
authed, err := oauth.Authed(c, true, true, true, true) | ||
if err != nil { | ||
apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { | ||
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
id, errWithCode := apiutil.ParseID(c.Param(apiutil.IDKey)) | ||
if errWithCode != nil { | ||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
errWithCode = m.processor.Conversations().Delete(c.Request.Context(), authed.Account, id) | ||
if errWithCode != nil { | ||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, apiutil.EmptyJSONObject) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// GoToSocial | ||
// Copyright (C) GoToSocial Authors [email protected] | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package conversations | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" | ||
"github.com/superseriousbusiness/gotosocial/internal/gtserror" | ||
"github.com/superseriousbusiness/gotosocial/internal/oauth" | ||
) | ||
|
||
// ConversationReadPOSTHandler swagger:operation POST /api/v1/conversation/{id}/read conversationRead | ||
// | ||
// Mark a conversation with the given ID as read. | ||
// | ||
// --- | ||
// tags: | ||
// - conversations | ||
// | ||
// produces: | ||
// - application/json | ||
// | ||
// parameters: | ||
// - | ||
// name: id | ||
// in: path | ||
// type: string | ||
// required: true | ||
// description: ID of the conversation. | ||
// | ||
// security: | ||
// - OAuth2 Bearer: | ||
// - write:conversations | ||
// | ||
// responses: | ||
// '200': | ||
// name: conversation | ||
// description: Updated conversation. | ||
// schema: | ||
// "$ref": "#/definitions/conversation" | ||
// '400': | ||
// description: bad request | ||
// '401': | ||
// description: unauthorized | ||
// '404': | ||
// description: not found | ||
// '406': | ||
// description: not acceptable | ||
// '422': | ||
// description: unprocessable content | ||
// '500': | ||
// description: internal server error | ||
func (m *Module) ConversationReadPOSTHandler(c *gin.Context) { | ||
authed, err := oauth.Authed(c, true, true, true, true) | ||
if err != nil { | ||
apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { | ||
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
id, errWithCode := apiutil.ParseID(c.Param(apiutil.IDKey)) | ||
if errWithCode != nil { | ||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
apiConversation, errWithCode := m.processor.Conversations().Read(c.Request.Context(), authed.Account, id) | ||
if errWithCode != nil { | ||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | ||
return | ||
} | ||
|
||
apiutil.JSON(c, http.StatusOK, apiConversation) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.