Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for posting on timeline (mastodon) #503

Merged
merged 1 commit into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions bridge/mastodon/mastodon.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ func (m *Mastodon) MsgUser(username, text string) (string, error) {
}

func (m *Mastodon) MsgChannel(channelID, text string) (string, error) {
return "", nil
s, err := m.mc.PostStatus(context.Background(), &mastodon.Toot{
Status: text,
})
if err != nil {
return "", err
}

return string(s.ID), nil
}

func (m *Mastodon) StatusUser(name string) (string, error) {
Expand All @@ -99,7 +106,7 @@ func (m *Mastodon) StatusUsers() (map[string]string, error) {
}

func (m *Mastodon) Protocol() string {
return "mastodon"
return "mastodon" //nolint:goconst
}

func (m *Mastodon) Kick(channelID, username string) error {
Expand All @@ -115,24 +122,32 @@ func (m *Mastodon) Nick(name string) error {
}

func (m *Mastodon) GetChannelName(channelID string) string {
return "#mastodon"
if channelID == "mastodon" {
return "#mastodon"
}

return channelID
}

func (m *Mastodon) GetChannelUsers(channelID string) ([]*bridge.UserInfo, error) {
return nil, nil
}

func (m *Mastodon) GetUsers() []*bridge.UserInfo {
return nil
return []*bridge.UserInfo{}
}

func (m *Mastodon) GetChannels() []*bridge.ChannelInfo {
return nil
}

func (m *Mastodon) GetChannel(channelID string) (*bridge.ChannelInfo, error) {
if channelID != "mastodon" {
return nil, fmt.Errorf("channel not found")
}

return &bridge.ChannelInfo{
ID: channelID,
ID: "mastodon",
Name: "#mastodon",
TeamID: "mastodon",
DM: false,
Expand Down Expand Up @@ -210,18 +225,16 @@ func (m *Mastodon) loginToMastodon() (*mastodon.Client, error) {
return mc, nil
}

//nolint:gocritic
func (m *Mastodon) handleMastodon() {
for event := range m.eventChanIn {
logger.Tracef("handleMastodon %s", spew.Sdump(event))
switch event := event.(type) {
case *mastodon.UpdateEvent:
m.handleMastodonUpdate(event)
/*
case *mastodon.NotificationEvent:
m.handleMastodonNotification(event)
case *mastodon.DeleteEvent:
m.handleMastodonDelete(event)
case *mastodon.NotificationEvent:
m.handleMastodonNotification(event)
/* case *mastodon.DeleteEvent:
m.handleMastodonDelete(event)
*/
}
}
Expand All @@ -242,12 +255,20 @@ func (m *Mastodon) sendPublicMessage(ghost *bridge.UserInfo, msg, channelID stri
m.eventChan <- event
}

func (m *Mastodon) handleMastodonNotification(event *mastodon.NotificationEvent) {
if event.Notification == nil {
return
}

logger.Tracef("handleMastodonNotification %s", spew.Sdump(event))
}

func (m *Mastodon) handleMastodonUpdate(event *mastodon.UpdateEvent) {
if event.Status == nil {
return
}

logger.Debugf("handleMastodonUpdate %s", spew.Sdump(event))
logger.Tracef("handleMastodonUpdate %s", spew.Sdump(event))

s := event.Status

Expand Down Expand Up @@ -294,7 +315,7 @@ func (m *Mastodon) createUser(muser *mastodon.Account) *bridge.UserInfo {
}

info := &bridge.UserInfo{
Nick: username,
Nick: strings.ReplaceAll(muser.Acct, "@", "|"),
User: username,
Real: host,
Host: host,
Expand Down
21 changes: 15 additions & 6 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ func (u *User) getMessageChannel(channelID string, sender *bridge.UserInfo) Chan

// if it's another user, let them join
if !ghost.Me && !ch.HasUser(ghost) {
logger.Debugf("User %s is not in channel %s. Joining now", ghost.Nick, ch.String())
ch.Join(ghost)
if u.br.Protocol() != "mastodon" {
logger.Debugf("User %s is not in channel %s. Joining now", ghost.Nick, ch.String())
ch.Join(ghost) //nolint:errcheck
}
}

// check if we mayjoin this channel
Expand Down Expand Up @@ -541,11 +543,18 @@ func (u *User) addUsersToChannels() {

// create and join the users
users := u.CreateUsersFromInfo(u.br.GetUsers())
srv.BatchAdd(users)
u.addUsersToChannel(users, "&users", "&users")
if len(users) > 0 {
srv.BatchAdd(users)
u.addUsersToChannel(users, "&users", "&users")

// join ourself
ch.Join(u)
// join ourself
ch.Join(u) //nolint:errcheck
}

if u.br.Protocol() == "mastodon" {
ch = srv.Channel("mastodon")
ch.Join(u) //nolint:errcheck
}

// channel that receives messages from channels not joined on irc
ch = srv.Channel("&messages")
Expand Down