Added TagContent to gomatrix, imported.

This commit is contained in:
SUMUKHA-PK 2019-03-27 07:07:14 +05:30
parent 92bc8a42e2
commit 9fc30d85b3
8 changed files with 126 additions and 53 deletions

View file

@ -20,15 +20,15 @@ import (
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/common" "github.com/matrix-org/gomatrix"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
// newMTag creates and returns a new MTag type // newTag creates and returns a new Tag type
func newMTag() common.MTag { func newTag() gomatrix.TagContent {
return common.MTag{ return gomatrix.TagContent{
Tags: make(map[string]common.TagProperties), Tags: make(map[string]gomatrix.TagProperties),
} }
} }
@ -39,7 +39,7 @@ func GetTag(
userID string, userID string,
roomID string, roomID string,
) util.JSONResponse { ) util.JSONResponse {
mtag := newMTag() Tag := newTag()
localpart, _, err := gomatrixserverlib.SplitID('@', userID) localpart, _, err := gomatrixserverlib.SplitID('@', userID)
if err != nil { if err != nil {
@ -47,7 +47,7 @@ func GetTag(
} }
data, err := accountDB.GetAccountDataByType( data, err := accountDB.GetAccountDataByType(
req.Context(), localpart, roomID, "m.tag", req.Context(), localpart, roomID, "tag",
) )
if err != nil { if err != nil {
@ -58,7 +58,7 @@ func GetTag(
if err != nil { if err != nil {
httputil.LogThenError(req, err) httputil.LogThenError(req, err)
} }
err = json.Unmarshal(dataByte, &mtag) err = json.Unmarshal(dataByte, &Tag)
if err != nil { if err != nil {
httputil.LogThenError(req, err) httputil.LogThenError(req, err)
@ -66,7 +66,7 @@ func GetTag(
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: mtag, JSON: Tag,
} }
} }
@ -83,8 +83,8 @@ func PutTag(
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
mtag := newMTag() Tag := newTag()
var properties common.TagProperties var properties gomatrix.TagProperties
if reqErr := httputil.UnmarshalJSONRequest(req, &properties); reqErr != nil { if reqErr := httputil.UnmarshalJSONRequest(req, &properties); reqErr != nil {
return *reqErr return *reqErr
@ -95,12 +95,12 @@ func PutTag(
if err != nil { if err != nil {
httputil.LogThenError(req, err) httputil.LogThenError(req, err)
} }
if err = json.Unmarshal(dataByte, &mtag); err != nil { if err = json.Unmarshal(dataByte, &Tag); err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
} }
mtag.Tags[tag] = properties Tag.Tags[tag] = properties
addDataToDB(req, localpart, roomID, accountDB, mtag) addDataToDB(req, localpart, roomID, accountDB, Tag)
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
@ -121,17 +121,18 @@ func DeleteTag(
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
mtag := newMTag() Tag := newTag()
if len(data) > 0 { if len(data) > 0 {
dataByte, err := json.Marshal(data) dataByte, err := json.Marshal(data)
if err != nil { if err != nil {
httputil.LogThenError(req, err) httputil.LogThenError(req, err)
} }
if err := json.Unmarshal(dataByte, &mtag); err != nil { if err := json.Unmarshal(dataByte, &Tag); err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
} else { } else {
//Synapse returns a 200 OK response on finding no Tags, same policy is followed here.
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: struct{}{}, JSON: struct{}{},
@ -139,15 +140,16 @@ func DeleteTag(
} }
// Check whether the Tag to be deleted exists // Check whether the Tag to be deleted exists
if _, ok := mtag.Tags[tag]; ok { if _, ok := Tag.Tags[tag]; ok {
delete(mtag.Tags, tag) delete(Tag.Tags, tag)
} else { } else {
//Synapse returns a 200 OK response on finding no Tags, same policy is followed here.
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: struct{}{}, JSON: struct{}{},
} }
} }
addDataToDB(req, localpart, roomID, accountDB, mtag) addDataToDB(req, localpart, roomID, accountDB, Tag)
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
@ -168,7 +170,7 @@ func obtainSavedTags(
} }
data, err := accountDB.GetAccountDataByType( data, err := accountDB.GetAccountDataByType(
req.Context(), localpart, roomID, "m.tag", req.Context(), localpart, roomID, "tag",
) )
if err != nil { if err != nil {
return "", []gomatrixserverlib.ClientEvent{}, err return "", []gomatrixserverlib.ClientEvent{}, err
@ -183,14 +185,14 @@ func addDataToDB(
localpart string, localpart string,
roomID string, roomID string,
accountDB *accounts.Database, accountDB *accounts.Database,
mtag common.MTag, Tag gomatrix.TagContent,
) { ) {
newTagData, err := json.Marshal(mtag) newTagData, err := json.Marshal(Tag)
if err != nil { if err != nil {
httputil.LogThenError(req, err) httputil.LogThenError(req, err)
} }
if err = accountDB.SaveAccountData( if err = accountDB.SaveAccountData(
req.Context(), localpart, roomID, "m.tag", string(newTagData), req.Context(), localpart, roomID, "tag", string(newTagData),
); err != nil { ); err != nil {
httputil.LogThenError(req, err) httputil.LogThenError(req, err)
} }

View file

@ -46,16 +46,6 @@ type DisplayName struct {
// recognized by strconv.ParseBool // recognized by strconv.ParseBool
type WeakBoolean bool type WeakBoolean bool
// MTag contains the data for a Tag which can be referenced by the Tag name
type MTag struct {
Tags map[string]TagProperties `json:"tags"`
}
// TagProperties contains the properties of an MTag
type TagProperties struct {
Order float32 `json:"order,omitempty"` // Empty values must be neglected
}
// UnmarshalJSON is overridden here to allow strings vaguely representing a true // UnmarshalJSON is overridden here to allow strings vaguely representing a true
// or false boolean to be set as their closest counterpart // or false boolean to be set as their closest counterpart
func (b *WeakBoolean) UnmarshalJSON(data []byte) error { func (b *WeakBoolean) UnmarshalJSON(data []byte) error {

2
vendor/manifest vendored
View file

@ -142,7 +142,7 @@
{ {
"importpath": "github.com/matrix-org/gomatrix", "importpath": "github.com/matrix-org/gomatrix",
"repository": "https://github.com/matrix-org/gomatrix", "repository": "https://github.com/matrix-org/gomatrix",
"revision": "a7fc80c8060c2544fe5d4dae465b584f8e9b4e27", "revision": "51f01ddc3d93b709449b011bfc67a72a611b1510",
"branch": "master" "branch": "master"
}, },
{ {

View file

@ -13,6 +13,7 @@ import (
"net/url" "net/url"
"path" "path"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
) )
@ -68,6 +69,10 @@ func (cli *Client) BuildBaseURL(urlPath ...string) string {
parts := []string{hsURL.Path} parts := []string{hsURL.Path}
parts = append(parts, urlPath...) parts = append(parts, urlPath...)
hsURL.Path = path.Join(parts...) hsURL.Path = path.Join(parts...)
// Manually add the trailing slash back to the end of the path if it's explicitly needed
if strings.HasSuffix(urlPath[len(urlPath)-1], "/") {
hsURL.Path = hsURL.Path + "/"
}
query := hsURL.Query() query := hsURL.Query()
if cli.AccessToken != "" { if cli.AccessToken != "" {
query.Set("access_token", cli.AccessToken) query.Set("access_token", cli.AccessToken)
@ -529,7 +534,7 @@ func (cli *Client) ForgetRoom(roomID string) (resp *RespForgetRoom, err error) {
// InviteUser invites a user to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite // InviteUser invites a user to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
func (cli *Client) InviteUser(roomID string, req *ReqInviteUser) (resp *RespInviteUser, err error) { func (cli *Client) InviteUser(roomID string, req *ReqInviteUser) (resp *RespInviteUser, err error) {
u := cli.BuildURL("rooms", roomID, "invite") u := cli.BuildURL("rooms", roomID, "invite")
_, err = cli.MakeRequest("POST", u, struct{}{}, &resp) _, err = cli.MakeRequest("POST", u, req, &resp)
return return
} }

View file

@ -14,6 +14,8 @@ type Event struct {
ID string `json:"event_id"` // The unique ID of this event ID string `json:"event_id"` // The unique ID of this event
RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence) RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence)
Content map[string]interface{} `json:"content"` // The JSON content of the event. Content map[string]interface{} `json:"content"` // The JSON content of the event.
Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event
Unsigned map[string]interface{} `json:"unsigned"` // The unsigned portions of the event, such as age and prev_content
} }
// Body returns the value of the "body" key in the event content if it is // Body returns the value of the "body" key in the event content if it is

View file

@ -14,6 +14,8 @@
package gomatrix package gomatrix
import "errors"
//Filter is used by clients to specify how the server should filter responses to e.g. sync requests //Filter is used by clients to specify how the server should filter responses to e.g. sync requests
//Specified by: https://matrix.org/docs/spec/client_server/r0.2.0.html#filtering //Specified by: https://matrix.org/docs/spec/client_server/r0.2.0.html#filtering
type Filter struct { type Filter struct {
@ -21,23 +23,68 @@ type Filter struct {
EventFields []string `json:"event_fields,omitempty"` EventFields []string `json:"event_fields,omitempty"`
EventFormat string `json:"event_format,omitempty"` EventFormat string `json:"event_format,omitempty"`
Presence FilterPart `json:"presence,omitempty"` Presence FilterPart `json:"presence,omitempty"`
Room struct { Room RoomFilter `json:"room,omitempty"`
AccountData FilterPart `json:"account_data,omitempty"`
Ephemeral FilterPart `json:"ephemeral,omitempty"`
IncludeLeave bool `json:"include_leave,omitempty"`
NotRooms []string `json:"not_rooms,omitempty"`
Rooms []string `json:"rooms,omitempty"`
State FilterPart `json:"state,omitempty"`
Timeline FilterPart `json:"timeline,omitempty"`
} `json:"room,omitempty"`
} }
type FilterPart struct { // RoomFilter is used to define filtering rules for room events
NotRooms []string `json:"not_rooms,omitempty"` type RoomFilter struct {
Rooms []string `json:"rooms,omitempty"` AccountData FilterPart `json:"account_data,omitempty"`
Limit *int `json:"limit,omitempty"` Ephemeral FilterPart `json:"ephemeral,omitempty"`
NotSenders []string `json:"not_senders,omitempty"` IncludeLeave bool `json:"include_leave,omitempty"`
NotTypes []string `json:"not_types,omitempty"` NotRooms []string `json:"not_rooms,omitempty"`
Senders []string `json:"senders,omitempty"` Rooms []string `json:"rooms,omitempty"`
Types []string `json:"types,omitempty"` State FilterPart `json:"state,omitempty"`
Timeline FilterPart `json:"timeline,omitempty"`
}
// FilterPart is used to define filtering rules for specific categories of events
type FilterPart struct {
NotRooms []string `json:"not_rooms,omitempty"`
Rooms []string `json:"rooms,omitempty"`
Limit int `json:"limit,omitempty"`
NotSenders []string `json:"not_senders,omitempty"`
NotTypes []string `json:"not_types,omitempty"`
Senders []string `json:"senders,omitempty"`
Types []string `json:"types,omitempty"`
ContainsURL *bool `json:"contains_url,omitempty"`
}
// Validate checks if the filter contains valid property values
func (filter *Filter) Validate() error {
if filter.EventFormat != "client" && filter.EventFormat != "federation" {
return errors.New("Bad event_format value. Must be one of [\"client\", \"federation\"]")
}
return nil
}
// DefaultFilter returns the default filter used by the Matrix server if no filter is provided in the request
func DefaultFilter() Filter {
return Filter{
AccountData: DefaultFilterPart(),
EventFields: nil,
EventFormat: "client",
Presence: DefaultFilterPart(),
Room: RoomFilter{
AccountData: DefaultFilterPart(),
Ephemeral: DefaultFilterPart(),
IncludeLeave: false,
NotRooms: nil,
Rooms: nil,
State: DefaultFilterPart(),
Timeline: DefaultFilterPart(),
},
}
}
// DefaultFilterPart returns the default filter part used by the Matrix server if no filter is provided in the request
func DefaultFilterPart() FilterPart {
return FilterPart{
NotRooms: nil,
Rooms: nil,
Limit: 20,
NotSenders: nil,
NotTypes: nil,
Senders: nil,
Types: nil,
}
} }

View file

@ -84,7 +84,7 @@ type RespUserInteractive struct {
Stages []string `json:"stages"` Stages []string `json:"stages"`
} `json:"flows"` } `json:"flows"`
Params map[string]interface{} `json:"params"` Params map[string]interface{} `json:"params"`
Session string `json:"string"` Session string `json:"session"`
Completed []string `json:"completed"` Completed []string `json:"completed"`
ErrCode string `json:"errcode"` ErrCode string `json:"errcode"`
Error string `json:"error"` Error string `json:"error"`
@ -168,6 +168,7 @@ type RespSync struct {
} `json:"rooms"` } `json:"rooms"`
} }
// RespTurnServer is the JSON response from a Turn Server
type RespTurnServer struct { type RespTurnServer struct {
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`

View file

@ -0,0 +1,26 @@
// Copyright 2019 Sumukha PK
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gomatrix
// TagContent contains the data for an m.tag message type
// https://matrix.org/docs/spec/client_server/r0.4.0.html#m-tag
type TagContent struct {
Tags map[string]TagProperties `json:"tags"`
}
// TagProperties contains the properties of a Tag
type TagProperties struct {
Order float32 `json:"order,omitempty"` // Empty values must be neglected
}