Skip to content

Commit b3a6ee1

Browse files
committed
added new endpoint to return a group's avatar
1 parent 5920754 commit b3a6ee1

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

src/api/api.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,40 @@ func (a *Api) GetGroup(c *gin.Context) {
967967
}
968968
}
969969

970+
// @Summary Returns the avatar of a Signal Group.
971+
// @Tags Groups
972+
// @Description Returns the avatar of a Signal Group.
973+
// @Accept json
974+
// @Produce json
975+
// @Success 200 {string} string "Image"
976+
// @Failure 400 {object} Error
977+
// @Param number path string true "Registered Phone Number"
978+
// @Param groupid path string true "Group ID"
979+
// @Router /v1/groups/{number}/{groupid}/avatar [get]
980+
func (a *Api) GetGroupAvatar(c *gin.Context) {
981+
number, err := url.PathUnescape(c.Param("number"))
982+
if err != nil {
983+
c.JSON(400, Error{Msg: "Couldn't process request - malformed number"})
984+
return
985+
}
986+
groupId := c.Param("groupid")
987+
988+
groupAvatar, err := a.signalClient.GetGroupAvatar(number, groupId)
989+
if err != nil {
990+
switch err.(type) {
991+
case *client.NotFoundError:
992+
c.JSON(404, Error{Msg: err.Error()})
993+
return
994+
default:
995+
c.JSON(400, Error{Msg: err.Error()})
996+
return
997+
}
998+
}
999+
1000+
mimeType := mimetype.Detect(groupAvatar)
1001+
c.Data(200, mimeType.String(), groupAvatar)
1002+
}
1003+
9701004
// @Summary Delete a Signal Group.
9711005
// @Tags Groups
9721006
// @Description Delete the specified Signal Group.

src/client/client.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,57 @@ func (s *SignalClient) GetGroup(number string, groupId string) (*GroupEntry, err
12881288
return nil, nil
12891289
}
12901290

1291+
func (s *SignalClient) GetGroupAvatar(number string, groupId string) ([]byte, error) {
1292+
var err error
1293+
var rawData string
1294+
1295+
internalGroupId, err := ConvertGroupIdToInternalGroupId(groupId)
1296+
if err != nil {
1297+
return []byte{}, errors.New("Invalid group id")
1298+
}
1299+
1300+
if s.signalCliMode == JsonRpc {
1301+
type Request struct {
1302+
GroupId string `json:"groupId"`
1303+
}
1304+
1305+
request := Request{GroupId: internalGroupId}
1306+
1307+
jsonRpc2Client, err := s.getJsonRpc2Client()
1308+
if err != nil {
1309+
return []byte{}, err
1310+
}
1311+
rawData, err = jsonRpc2Client.getRaw("getAvatar", &number, request)
1312+
if err != nil {
1313+
if err.Error() == "Could not find avatar" {
1314+
return []byte{},&NotFoundError{Description: "No avatar found."}
1315+
}
1316+
return []byte{}, err
1317+
}
1318+
} else {
1319+
rawData, err = s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "-o", "json", "-a", number, "getAvatar", "-g", internalGroupId}, "")
1320+
if err != nil {
1321+
return []byte{}, err
1322+
}
1323+
}
1324+
1325+
type SignalCliResponse struct {
1326+
Data string `json:"data"`
1327+
}
1328+
var signalCliResponse SignalCliResponse
1329+
err = json.Unmarshal([]byte(rawData), &signalCliResponse)
1330+
if err != nil {
1331+
return []byte{}, errors.New("Couldn't unmarshal data: " + err.Error())
1332+
}
1333+
1334+
groupAvatarBytes, err := base64.StdEncoding.DecodeString(signalCliResponse.Data)
1335+
if err != nil {
1336+
return []byte{}, errors.New("Couldn't decode base64 encoded group avatar: " + err.Error())
1337+
}
1338+
1339+
return groupAvatarBytes, nil
1340+
}
1341+
12911342
func (s *SignalClient) DeleteGroup(number string, groupId string) error {
12921343
if s.signalCliMode == JsonRpc {
12931344
type Request struct {

src/docs/docs.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,51 @@ const docTemplate = `{
10801080
}
10811081
}
10821082
},
1083+
"/v1/groups/{number}/{groupid}/avatar": {
1084+
"get": {
1085+
"description": "Returns the avatar of a Signal Group.",
1086+
"consumes": [
1087+
"application/json"
1088+
],
1089+
"produces": [
1090+
"application/json"
1091+
],
1092+
"tags": [
1093+
"Groups"
1094+
],
1095+
"summary": "Returns the avatar of a Signal Group.",
1096+
"parameters": [
1097+
{
1098+
"type": "string",
1099+
"description": "Registered Phone Number",
1100+
"name": "number",
1101+
"in": "path",
1102+
"required": true
1103+
},
1104+
{
1105+
"type": "string",
1106+
"description": "Group ID",
1107+
"name": "groupid",
1108+
"in": "path",
1109+
"required": true
1110+
}
1111+
],
1112+
"responses": {
1113+
"200": {
1114+
"description": "Image",
1115+
"schema": {
1116+
"type": "string"
1117+
}
1118+
},
1119+
"400": {
1120+
"description": "Bad Request",
1121+
"schema": {
1122+
"$ref": "#/definitions/api.Error"
1123+
}
1124+
}
1125+
}
1126+
}
1127+
},
10831128
"/v1/groups/{number}/{groupid}/block": {
10841129
"post": {
10851130
"description": "Block the specified Signal Group.",
@@ -2720,6 +2765,9 @@ const docTemplate = `{
27202765
},
27212766
"status": {
27222767
"type": "string"
2768+
},
2769+
"uuid": {
2770+
"type": "string"
27232771
}
27242772
}
27252773
},

src/docs/swagger.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,51 @@
10771077
}
10781078
}
10791079
},
1080+
"/v1/groups/{number}/{groupid}/avatar": {
1081+
"get": {
1082+
"description": "Returns the avatar of a Signal Group.",
1083+
"consumes": [
1084+
"application/json"
1085+
],
1086+
"produces": [
1087+
"application/json"
1088+
],
1089+
"tags": [
1090+
"Groups"
1091+
],
1092+
"summary": "Returns the avatar of a Signal Group.",
1093+
"parameters": [
1094+
{
1095+
"type": "string",
1096+
"description": "Registered Phone Number",
1097+
"name": "number",
1098+
"in": "path",
1099+
"required": true
1100+
},
1101+
{
1102+
"type": "string",
1103+
"description": "Group ID",
1104+
"name": "groupid",
1105+
"in": "path",
1106+
"required": true
1107+
}
1108+
],
1109+
"responses": {
1110+
"200": {
1111+
"description": "Image",
1112+
"schema": {
1113+
"type": "string"
1114+
}
1115+
},
1116+
"400": {
1117+
"description": "Bad Request",
1118+
"schema": {
1119+
"$ref": "#/definitions/api.Error"
1120+
}
1121+
}
1122+
}
1123+
}
1124+
},
10801125
"/v1/groups/{number}/{groupid}/block": {
10811126
"post": {
10821127
"description": "Block the specified Signal Group.",
@@ -2717,6 +2762,9 @@
27172762
},
27182763
"status": {
27192764
"type": "string"
2765+
},
2766+
"uuid": {
2767+
"type": "string"
27202768
}
27212769
}
27222770
},

src/docs/swagger.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ definitions:
367367
type: string
368368
status:
369369
type: string
370+
uuid:
371+
type: string
370372
type: object
371373
client.ListContactsResponse:
372374
properties:
@@ -1178,6 +1180,36 @@ paths:
11781180
summary: Add one or more admins to an existing Signal Group.
11791181
tags:
11801182
- Groups
1183+
/v1/groups/{number}/{groupid}/avatar:
1184+
get:
1185+
consumes:
1186+
- application/json
1187+
description: Returns the avatar of a Signal Group.
1188+
parameters:
1189+
- description: Registered Phone Number
1190+
in: path
1191+
name: number
1192+
required: true
1193+
type: string
1194+
- description: Group ID
1195+
in: path
1196+
name: groupid
1197+
required: true
1198+
type: string
1199+
produces:
1200+
- application/json
1201+
responses:
1202+
"200":
1203+
description: Image
1204+
schema:
1205+
type: string
1206+
"400":
1207+
description: Bad Request
1208+
schema:
1209+
$ref: '#/definitions/api.Error'
1210+
summary: Returns the avatar of a Signal Group.
1211+
tags:
1212+
- Groups
11811213
/v1/groups/{number}/{groupid}/block:
11821214
post:
11831215
consumes:

src/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func main() {
210210
groups.POST(":number", api.CreateGroup)
211211
groups.GET(":number", api.GetGroups)
212212
groups.GET(":number/:groupid", api.GetGroup)
213+
groups.GET(":number/:groupid/avatar", api.GetGroupAvatar)
213214
groups.DELETE(":number/:groupid", api.DeleteGroup)
214215
groups.POST(":number/:groupid/block", api.BlockGroup)
215216
groups.POST(":number/:groupid/join", api.JoinGroup)

0 commit comments

Comments
 (0)