aboutsummaryrefslogtreecommitdiff
path: root/web/revolt/user.go
blob: 6fd004e6a7691007e0870f1fda12c867a91ed1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package revolt

import (
	"context"
	"encoding/json"
	"time"

	"github.com/oklog/ulid/v2"
)

// User struct.
type User struct {
	Client    *Client
	CreatedAt time.Time

	Id             string           `json:"_id"`
	Username       string           `json:"username"`
	Avatar         *Attachment      `json:"avatar"`
	Relations      []*UserRelations `json:"relations"`
	Badges         int              `json:"badges"`
	Status         *UserStatus      `json:"status"`
	Relationship   string           `json:"relationship"`
	IsOnline       bool             `json:"online"`
	Flags          int              `json:"flags"`
	BotInformation *BotInformation  `json:"bot"`
}

// User relations struct.
type UserRelations struct {
	Id     string `json:"_id"`
	Status string `json:"status"`
}

// User status struct.
type UserStatus struct {
	Text     string `json:"text"`
	Presence string `json:"presence"`
}

// Bot information struct.
type BotInformation struct {
	Owner string `json:"owner"`
}

// Calculate creation date and edit the struct.
func (u *User) CalculateCreationDate() error {
	ulid, err := ulid.Parse(u.Id)

	if err != nil {
		return err
	}

	u.CreatedAt = time.UnixMilli(int64(ulid.Time()))
	return nil
}

// Create a mention format.
func (u User) FormatMention() string {
	return "<@" + u.Id + ">"
}

// Open a DM with the user.
func (c *Client) UserOpenDirectMessage(ctx context.Context, uid string) (*Channel, error) {
	dmChannel := &Channel{}

	resp, err := c.Request(ctx, "GET", "/users/"+uid+"/dm", []byte{})

	if err != nil {
		return dmChannel, err
	}

	err = json.Unmarshal(resp, dmChannel)
	return dmChannel, err
}

// Fetch default user avatar.
func (c *Client) UserFetchDefaultAvatar(ctx context.Context, uid string) (*Binary, error) {
	avatarData := &Binary{}

	resp, err := c.Request(ctx, "GET", "/users/"+uid+"/default_avatar", []byte{})

	if err != nil {
		return avatarData, err
	}

	avatarData.Data = resp
	return avatarData, nil
}

// Fetch user relationship.
func (c *Client) UserFetchRelationship(ctx context.Context, uid string) (*UserRelations, error) {
	relationshipData := &UserRelations{}
	relationshipData.Id = uid

	resp, err := c.Request(ctx, "GET", "/users/"+uid+"/relationship", []byte{})

	if err != nil {
		return relationshipData, err
	}

	err = json.Unmarshal(resp, relationshipData)
	return relationshipData, err
}

// Block user.
func (c *Client) UserBlock(ctx context.Context, uid string) (*UserRelations, error) {
	relationshipData := &UserRelations{}
	relationshipData.Id = uid

	resp, err := c.Request(ctx, "PUT", "/users/"+uid+"/block", []byte{})

	if err != nil {
		return relationshipData, err
	}

	err = json.Unmarshal(resp, relationshipData)
	return relationshipData, err
}

// Un-block user.
func (c *Client) UserUnblock(ctx context.Context, uid string) (*UserRelations, error) {
	relationshipData := &UserRelations{}
	relationshipData.Id = uid

	resp, err := c.Request(ctx, "DELETE", "/users/"+uid+"/block", []byte{})

	if err != nil {
		return relationshipData, err
	}

	err = json.Unmarshal(resp, relationshipData)
	return relationshipData, err
}