accord

package module
v0.0.0-...-cae5519 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2020 License: AGPL-3.0 Imports: 20 Imported by: 0

README

Accord

[WIP] Real time chat application, with features similar to discord/telegram using gRPC.

Features

  • discord-like server hierarchy
  • federated self-hosted servers
  • file sharing via webtorrent?
  • voice chat via webrtc?

Documentation

Overview

File for the generation of random objects, suitable for testing needs

Index

Constants

This section is empty.

Variables

AccordToPBPermissions is a mapping from objects of "Permission" type of this package to the objects from "pb" package.

Functions

func GetRandBool

func GetRandBool() bool

func GetRandChannelName

func GetRandChannelName() string

func GetRandPassword

func GetRandPassword() string

func GetRandUsername

func GetRandUsername() string

func RandString

func RandString(length int) string

func RandStringWithCharset

func RandStringWithCharset(length int, charset string) string

Types

type AccordClient

type AccordClient struct {
	pb.ChatClient
	Username string
	ServerID uint64
	Channels map[uint64]*ClientChannel
	// contains filtered or unexported fields
}

func NewAccordClient

func NewAccordClient(serverID uint64) *AccordClient

func (*AccordClient) AuthClient

func (c *AccordClient) AuthClient() *AuthClient

func (*AccordClient) Connect

func (c *AccordClient) Connect(addr string) error

func (*AccordClient) CreateChannel

func (c *AccordClient) CreateChannel(name string, isPublic bool) (uint64, error)

CreateChannel sends the request to create new channel.

func (*AccordClient) CreateUser

func (c *AccordClient) CreateUser(username string, password string) error

func (*AccordClient) GetChannel

func (c *AccordClient) GetChannel(channelID uint64) error

func (*AccordClient) GetChannels

func (c *AccordClient) GetChannels() error

func (*AccordClient) Login

func (c *AccordClient) Login(username string, password string) error

func (*AccordClient) RemoveChannel

func (c *AccordClient) RemoveChannel(channelID uint64) error

RemoveChannel permanently deletes the channel and all of its data.

func (*AccordClient) Send

func (c *AccordClient) Send(msg *ChannelStreamRequest) error

func (*AccordClient) Subscribe

func (c *AccordClient) Subscribe(channelID uint64) (*StreamResponseCommunication, error)

Subscribe returns the channel, which will send all the updates about the channel.

type AccordServer

type AccordServer struct {
	// contains filtered or unexported fields
}

func NewAccordServer

func NewAccordServer() *AccordServer

func (*AccordServer) AddChannel

AddChannel creates a new channel with given parameters. The user who created the channel automatically becomes the channel's superadmin.

func (*AccordServer) ChannelStream

func (s *AccordServer) ChannelStream(srv pb.Chat_ChannelStreamServer) error

ChannelStream is the implementation of bidirectional streaming of client with one channel on the server.

func (*AccordServer) GetChannel

func (*AccordServer) GetChannels

func (*AccordServer) Listen

func (s *AccordServer) Listen(serv_addr string) (string, error)

func (*AccordServer) LoadChannels

func (s *AccordServer) LoadChannels() error

LoadChannels loads channels from the persistent storage

func (*AccordServer) LoadUsers

func (s *AccordServer) LoadUsers() error

LoadUsers loads channels from the persistent storage

func (*AccordServer) RemoveChannel

func (*AccordServer) Start

func (s *AccordServer) Start()

type AuthClient

type AuthClient struct {
	pb.AuthServiceClient
}

AuthClient is a client to call authentication RPC

func NewAuthClient

func NewAuthClient(cc *grpc.ClientConn) *AuthClient

NewAuthClient returns a new auth client

func (*AuthClient) CreateUser

func (c *AuthClient) CreateUser(username string, password string) error

func (*AuthClient) Login

func (c *AuthClient) Login(username string, password string) (string, error)

Login login user and returns the access token

type AuthServer

type AuthServer struct {
	// contains filtered or unexported fields
}

AuthServer is the server for authentication

func NewAuthServer

func NewAuthServer() *AuthServer

NewAuthServer returns a new auth server

func (*AuthServer) CreateUser

func (*AuthServer) GetUser

func (s *AuthServer) GetUser(username string) *User

func (*AuthServer) JWTManager

func (s *AuthServer) JWTManager() *JWTManager

func (*AuthServer) Login

func (*AuthServer) Logout

type ChannelConfigMessage

type ChannelConfigMessage struct {
	Msg isChannelConfigMessageMsg
}

ChannelConfigMessage is used in ChannelStreamRequest- and Response to initiate and broadcast channel-related changes.

type ChannelStreamRequest

type ChannelStreamRequest struct {
	ChannelID uint64
	Msg       isChannelStreamRequestMsg
}

ChannelStreamRequest represents a stream request for a single channel.

func (*ChannelStreamRequest) GetChannelID

func (m *ChannelStreamRequest) GetChannelID() uint64

func (*ChannelStreamRequest) GetConfMsg

func (x *ChannelStreamRequest) GetConfMsg() *ChannelConfigMessage

func (*ChannelStreamRequest) GetMsg

func (m *ChannelStreamRequest) GetMsg() isChannelStreamRequestMsg

func (*ChannelStreamRequest) GetUserMsg

type ChannelStreamRequestType

type ChannelStreamRequestType int

ChannelStreamRequestType is a type of channel stream request message.

const (
	// FromUserChannelStreamRequestType is for sending new messages, and updating or
	// deletion of messages.
	FromUserChannelStreamRequestType ChannelStreamRequestType = iota
	// ChannelConfigChannelStreamRequestType carries channel configuration changes,
	// including name change, updating user roles, and specifying pinned message.
	ChannelConfigChannelStreamRequestType
)

type ChannelStreamResponse

type ChannelStreamResponse struct {
	Msg isChannelStreamResponseMsg
}

type ChannelStreamResponseType

type ChannelStreamResponseType int

ChannelStreamResponseType is a type of channel stream response message.

const (
	// FromUserChannelStreamResponseType is for sending new messages, and updating or
	// deletion of messages.
	FromUserChannelStreamResponseType ChannelStreamResponseType = iota
	// ChannelConfigChannelStreamResponseType carries channel configuration changes,
	// including name change, updating user roles, and specifying pinned message.
	ChannelConfigChannelStreamResponseType
)

type ClientAuthInterceptor

type ClientAuthInterceptor struct {
	// contains filtered or unexported fields
}

ClientAuthInterceptor is a client interceptor for authentication

func NewClientAuthInterceptor

func NewClientAuthInterceptor(
	authClient *AuthClient,
	username string,
	password string,
	refreshDuration time.Duration,
) (*ClientAuthInterceptor, error)

NewClientAuthInterceptor returns a new auth interceptor

func (*ClientAuthInterceptor) Stream

Stream returns a client interceptor to authenticate stream RPC

func (*ClientAuthInterceptor) Unary

Unary returns a client interceptor to authenticate unary RPC

type ClientChannel

type ClientChannel struct {
	ChannelId uint64
	Name      string
	// Set if the **fixed** data for this channel has been fetched on the client side.
	// Data that is mutable (and is frequently updated) such as pinned message, channel name,
	// and messages, is not polled through "IsFetched".
	IsFetched           bool
	IsPublic            bool
	PinnedMsgId         uint64
	Users               map[string]Role
	RolesWithPermission map[Permission][]Role
	Stream              pb.Chat_ChannelStreamClient
	Messages            []Message
}

ClientChannel represents a single private or public messaging channel.

func NewClientChannel

func NewClientChannel(uid uint64, name string, isPublic bool) *ClientChannel

NewClientChannel creates a new client channel with provided parameters.

type DeleteMessageUserChannelStreamRequest

type DeleteMessageUserChannelStreamRequest struct {
	MessageID uint64
}

type DeleteMessageUserChannelStreamResponse

type DeleteMessageUserChannelStreamResponse struct{}

type EditMessageUserChannelStreamRequest

type EditMessageUserChannelStreamRequest struct {
	MessageID uint64
	Content   string
}

type JWTManager

type JWTManager struct {
	// contains filtered or unexported fields
}

JWTManager is a JSON web token manager

func NewJWTManager

func NewJWTManager(secretKey string, tokenDuration time.Duration) *JWTManager

NewJWTManager returns a new JWT manager

func (*JWTManager) Generate

func (manager *JWTManager) Generate(username string) (string, error)

Generate generates and signs a new token for a user

func (*JWTManager) Verify

func (manager *JWTManager) Verify(accessToken string) (*UserClaims, error)

Verify verifies the access token string and return a user claim if the token is valid

type Message

type Message struct {
	MessageID uint64
	Timestamp time.Time
	// TODO: add "Sender" field, denoting the user, who sent the message.
	Content string
}

Message is a single message in the chat

type NameChannelConfigMessage

type NameChannelConfigMessage struct {
	NewChannelName string
}

type NewAndUpdateMessageUserChannelStreamResponse

type NewAndUpdateMessageUserChannelStreamResponse struct {
	Timestamp time.Time
	Content   string
}

type NewMessageUserChannelStreamRequest

type NewMessageUserChannelStreamRequest struct {
	Content string
}

type Permission

type Permission int

Permission represents actions allowed to the role within a channel.

const (
	// UnknownPermission is needed as a part of mapping to unknown message
	// from "pb" package.
	UnknownPermission Permission = iota
	// ReadPermission is for subscribed users or any member of the channel
	ReadPermission
	// WritePermission includes writing, modifying, and deletion of messages
	WritePermission
	// DeletePermission allows deleting othes users' messages
	DeletePermission
	// ModifyPermission is for modifying channel configurations
	ModifyPermission
	// KickPermission is for kicking users out of the channel
	KickPermission
	// BanPermission is for banning users
	BanPermission
	// AssignRolePermission is for assignment of roles to all channel's users
	AssignRolePermission
	// RemoveChannelPermission is a permission to permanently remove the channel
	// and all of its data.
	RemoveChannelPermission
)

type PinChannelConfigMessage

type PinChannelConfigMessage struct {
	MessageID uint64
}

type Role

type Role int

Role is a role of a user within a channel.

const (
	UnknownRole Role = iota
	SubscriberRole
	MemberRole
	AdminRole
	SuperadminRole
)

type RoleChannelConfigMessage

type RoleChannelConfigMessage struct {
	Username string
	Role     Role
}

type ServerAuthInterceptor

type ServerAuthInterceptor struct {
	// contains filtered or unexported fields
}

ServerAuthInterceptor is a server interceptor for authentication and authorization

func NewServerAuthInterceptor

func NewServerAuthInterceptor(jwtManager *JWTManager) *ServerAuthInterceptor

NewServerAuthInterceptor returns a new auth interceptor

func (*ServerAuthInterceptor) Authorize

func (interceptor *ServerAuthInterceptor) Authorize(ctx context.Context, method string) (*UserClaims, error)

func (*ServerAuthInterceptor) Stream

func (interceptor *ServerAuthInterceptor) Stream() grpc.StreamServerInterceptor

Stream returns a server interceptor function to authenticate and authorize stream RPC

func (*ServerAuthInterceptor) Unary

func (interceptor *ServerAuthInterceptor) Unary() grpc.UnaryServerInterceptor

Unary returns a server interceptor function to authenticate and authorize unary RPC

type ServerChannel

type ServerChannel struct {
	// contains filtered or unexported fields
}

ServerChannel represents a single private or public messaging channel.

func NewServerChannel

func NewServerChannel(uid uint64, name string, isPublic bool) *ServerChannel

NewServerChannel creates a new server channel with provided parameters.

type StreamRequestCommunication

type StreamRequestCommunication struct {
	Reqc   chan<- *ChannelStreamRequest
	Closec <-chan struct{}
}

StreamRequestCommunication is used as a communication interface for users

of this package who use "Stream" function.

type StreamResponseCommunication

type StreamResponseCommunication struct {
	Resc   <-chan *ChannelStreamResponse
	Closec chan<- struct{}
}

StreamResponseCommunication is used as a communication interface for users

of this package who use "Stream" function.

type User

type User struct {
	// contains filtered or unexported fields
}

User contains user's information

func NewUser

func NewUser(username string, password string) (*User, error)

NewUser returns a new user

func (*User) Clone

func (user *User) Clone() *User

Clone returns a clone of this user

func (*User) IsCorrectPassword

func (user *User) IsCorrectPassword(password string) bool

IsCorrectPassword checks if the provided password is correct or not

type UserChannelStreamRequest

type UserChannelStreamRequest struct {
	UserMsg isUserChannelStreamRequestUserMsg
}

UserChannelStreamRequest is a stream message sent by one of the users to the channel.

type UserChannelStreamResponse

type UserChannelStreamResponse struct {
	MessageID uint64
	UserMsg   isUserChannelStreamResponseUserMsg
}

UserChannelStreamResponse is a stream message broadcasted to all users in the channel.

func (*UserChannelStreamResponse) GetDeleteUserMsg

GetDeleteUserMsg gets the deleted message in user response message.

func (*UserChannelStreamResponse) GetMessageID

func (m *UserChannelStreamResponse) GetMessageID() uint64

GetMessageID returns message ID or the user stream response. It returns 0 if the user stream response is nil.

func (*UserChannelStreamResponse) GetNewAndUpdateUserMsg

GetNewAndUpdateUserMsg gets the new and update response messages from some user.

func (*UserChannelStreamResponse) GetUserMsg

func (m *UserChannelStreamResponse) GetUserMsg() isUserChannelStreamResponseUserMsg

GetUserMsg returns the user message contained in the user channel stream response.

type UserClaims

type UserClaims struct {
	jwt.StandardClaims
	Username string `json:"username"`
}

UserClaims is a custom JWT claims that contains some user's information

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL