github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/plugin/rpcplugin/api.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package rpcplugin 5 6 import ( 7 "encoding/gob" 8 "encoding/json" 9 "io" 10 "net/http" 11 "net/rpc" 12 13 "github.com/mattermost/mattermost-server/model" 14 "github.com/mattermost/mattermost-server/plugin" 15 ) 16 17 type LocalAPI struct { 18 api plugin.API 19 muxer *Muxer 20 } 21 22 func (api *LocalAPI) LoadPluginConfiguration(args struct{}, reply *[]byte) error { 23 var config interface{} 24 if err := api.api.LoadPluginConfiguration(&config); err != nil { 25 return err 26 } 27 b, err := json.Marshal(config) 28 if err != nil { 29 return err 30 } 31 *reply = b 32 return nil 33 } 34 35 func (api *LocalAPI) RegisterCommand(args *model.Command, reply *APITeamReply) error { 36 return api.api.RegisterCommand(args) 37 } 38 39 func (api *LocalAPI) UnregisterCommand(args *APIUnregisterCommandArgs, reply *APITeamReply) error { 40 return api.api.UnregisterCommand(args.TeamId, args.Trigger) 41 } 42 43 type APIErrorReply struct { 44 Error *model.AppError 45 } 46 47 type APITeamReply struct { 48 Team *model.Team 49 Error *model.AppError 50 } 51 52 func (api *LocalAPI) CreateTeam(args *model.Team, reply *APITeamReply) error { 53 team, err := api.api.CreateTeam(args) 54 *reply = APITeamReply{ 55 Team: team, 56 Error: err, 57 } 58 return nil 59 } 60 61 func (api *LocalAPI) DeleteTeam(args string, reply *APIErrorReply) error { 62 *reply = APIErrorReply{ 63 Error: api.api.DeleteTeam(args), 64 } 65 return nil 66 } 67 68 func (api *LocalAPI) GetTeam(args string, reply *APITeamReply) error { 69 team, err := api.api.GetTeam(args) 70 *reply = APITeamReply{ 71 Team: team, 72 Error: err, 73 } 74 return nil 75 } 76 77 func (api *LocalAPI) GetTeamByName(args string, reply *APITeamReply) error { 78 team, err := api.api.GetTeamByName(args) 79 *reply = APITeamReply{ 80 Team: team, 81 Error: err, 82 } 83 return nil 84 } 85 86 func (api *LocalAPI) UpdateTeam(args *model.Team, reply *APITeamReply) error { 87 team, err := api.api.UpdateTeam(args) 88 *reply = APITeamReply{ 89 Team: team, 90 Error: err, 91 } 92 return nil 93 } 94 95 type APIUserReply struct { 96 User *model.User 97 Error *model.AppError 98 } 99 100 func (api *LocalAPI) CreateUser(args *model.User, reply *APIUserReply) error { 101 user, err := api.api.CreateUser(args) 102 *reply = APIUserReply{ 103 User: user, 104 Error: err, 105 } 106 return nil 107 } 108 109 func (api *LocalAPI) DeleteUser(args string, reply *APIErrorReply) error { 110 *reply = APIErrorReply{ 111 Error: api.api.DeleteUser(args), 112 } 113 return nil 114 } 115 116 func (api *LocalAPI) GetUser(args string, reply *APIUserReply) error { 117 user, err := api.api.GetUser(args) 118 *reply = APIUserReply{ 119 User: user, 120 Error: err, 121 } 122 return nil 123 } 124 125 func (api *LocalAPI) GetUserByEmail(args string, reply *APIUserReply) error { 126 user, err := api.api.GetUserByEmail(args) 127 *reply = APIUserReply{ 128 User: user, 129 Error: err, 130 } 131 return nil 132 } 133 134 func (api *LocalAPI) GetUserByUsername(args string, reply *APIUserReply) error { 135 user, err := api.api.GetUserByUsername(args) 136 *reply = APIUserReply{ 137 User: user, 138 Error: err, 139 } 140 return nil 141 } 142 143 func (api *LocalAPI) UpdateUser(args *model.User, reply *APIUserReply) error { 144 user, err := api.api.UpdateUser(args) 145 *reply = APIUserReply{ 146 User: user, 147 Error: err, 148 } 149 return nil 150 } 151 152 type APIGetChannelByNameArgs struct { 153 Name string 154 TeamId string 155 } 156 157 type APIGetDirectChannelArgs struct { 158 UserId1 string 159 UserId2 string 160 } 161 162 type APIGetGroupChannelArgs struct { 163 UserIds []string 164 } 165 166 type APIAddChannelMemberArgs struct { 167 ChannelId string 168 UserId string 169 } 170 171 type APIGetChannelMemberArgs struct { 172 ChannelId string 173 UserId string 174 } 175 176 type APIUpdateChannelMemberRolesArgs struct { 177 ChannelId string 178 UserId string 179 NewRoles string 180 } 181 182 type APIUpdateChannelMemberNotificationsArgs struct { 183 ChannelId string 184 UserId string 185 Notifications map[string]string 186 } 187 188 type APIDeleteChannelMemberArgs struct { 189 ChannelId string 190 UserId string 191 } 192 193 type APIChannelReply struct { 194 Channel *model.Channel 195 Error *model.AppError 196 } 197 198 type APIChannelMemberReply struct { 199 ChannelMember *model.ChannelMember 200 Error *model.AppError 201 } 202 203 func (api *LocalAPI) CreateChannel(args *model.Channel, reply *APIChannelReply) error { 204 channel, err := api.api.CreateChannel(args) 205 *reply = APIChannelReply{ 206 Channel: channel, 207 Error: err, 208 } 209 return nil 210 } 211 212 func (api *LocalAPI) DeleteChannel(args string, reply *APIErrorReply) error { 213 *reply = APIErrorReply{ 214 Error: api.api.DeleteChannel(args), 215 } 216 return nil 217 } 218 219 func (api *LocalAPI) GetChannel(args string, reply *APIChannelReply) error { 220 channel, err := api.api.GetChannel(args) 221 *reply = APIChannelReply{ 222 Channel: channel, 223 Error: err, 224 } 225 return nil 226 } 227 228 func (api *LocalAPI) GetChannelByName(args *APIGetChannelByNameArgs, reply *APIChannelReply) error { 229 channel, err := api.api.GetChannelByName(args.Name, args.TeamId) 230 *reply = APIChannelReply{ 231 Channel: channel, 232 Error: err, 233 } 234 return nil 235 } 236 237 func (api *LocalAPI) GetDirectChannel(args *APIGetDirectChannelArgs, reply *APIChannelReply) error { 238 channel, err := api.api.GetDirectChannel(args.UserId1, args.UserId2) 239 *reply = APIChannelReply{ 240 Channel: channel, 241 Error: err, 242 } 243 return nil 244 } 245 246 func (api *LocalAPI) GetGroupChannel(args *APIGetGroupChannelArgs, reply *APIChannelReply) error { 247 channel, err := api.api.GetGroupChannel(args.UserIds) 248 *reply = APIChannelReply{ 249 Channel: channel, 250 Error: err, 251 } 252 return nil 253 } 254 255 func (api *LocalAPI) UpdateChannel(args *model.Channel, reply *APIChannelReply) error { 256 channel, err := api.api.UpdateChannel(args) 257 *reply = APIChannelReply{ 258 Channel: channel, 259 Error: err, 260 } 261 return nil 262 } 263 264 func (api *LocalAPI) AddChannelMember(args *APIAddChannelMemberArgs, reply *APIChannelMemberReply) error { 265 member, err := api.api.AddChannelMember(args.ChannelId, args.UserId) 266 *reply = APIChannelMemberReply{ 267 ChannelMember: member, 268 Error: err, 269 } 270 return nil 271 } 272 273 func (api *LocalAPI) GetChannelMember(args *APIGetChannelMemberArgs, reply *APIChannelMemberReply) error { 274 member, err := api.api.GetChannelMember(args.ChannelId, args.UserId) 275 *reply = APIChannelMemberReply{ 276 ChannelMember: member, 277 Error: err, 278 } 279 return nil 280 } 281 282 func (api *LocalAPI) UpdateChannelMemberRoles(args *APIUpdateChannelMemberRolesArgs, reply *APIChannelMemberReply) error { 283 member, err := api.api.UpdateChannelMemberRoles(args.ChannelId, args.UserId, args.NewRoles) 284 *reply = APIChannelMemberReply{ 285 ChannelMember: member, 286 Error: err, 287 } 288 return nil 289 } 290 291 func (api *LocalAPI) UpdateChannelMemberNotifications(args *APIUpdateChannelMemberNotificationsArgs, reply *APIChannelMemberReply) error { 292 member, err := api.api.UpdateChannelMemberNotifications(args.ChannelId, args.UserId, args.Notifications) 293 *reply = APIChannelMemberReply{ 294 ChannelMember: member, 295 Error: err, 296 } 297 return nil 298 } 299 300 func (api *LocalAPI) DeleteChannelMember(args *APIDeleteChannelMemberArgs, reply *APIErrorReply) error { 301 err := api.api.DeleteChannelMember(args.ChannelId, args.UserId) 302 *reply = APIErrorReply{ 303 Error: err, 304 } 305 return nil 306 } 307 308 type APIPostReply struct { 309 Post *model.Post 310 Error *model.AppError 311 } 312 313 func (api *LocalAPI) CreatePost(args *model.Post, reply *APIPostReply) error { 314 post, err := api.api.CreatePost(args) 315 *reply = APIPostReply{ 316 Post: post, 317 Error: err, 318 } 319 return nil 320 } 321 322 func (api *LocalAPI) DeletePost(args string, reply *APIErrorReply) error { 323 *reply = APIErrorReply{ 324 Error: api.api.DeletePost(args), 325 } 326 return nil 327 } 328 329 func (api *LocalAPI) GetPost(args string, reply *APIPostReply) error { 330 post, err := api.api.GetPost(args) 331 *reply = APIPostReply{ 332 Post: post, 333 Error: err, 334 } 335 return nil 336 } 337 338 func (api *LocalAPI) UpdatePost(args *model.Post, reply *APIPostReply) error { 339 post, err := api.api.UpdatePost(args) 340 *reply = APIPostReply{ 341 Post: post, 342 Error: err, 343 } 344 return nil 345 } 346 347 type APIKeyValueStoreReply struct { 348 Value []byte 349 Error *model.AppError 350 } 351 352 type APIKeyValueStoreSetArgs struct { 353 Key string 354 Value []byte 355 } 356 357 func (api *LocalAPI) KeyValueStoreSet(args *APIKeyValueStoreSetArgs, reply *APIErrorReply) error { 358 err := api.api.KeyValueStore().Set(args.Key, args.Value) 359 *reply = APIErrorReply{ 360 Error: err, 361 } 362 return nil 363 } 364 365 func (api *LocalAPI) KeyValueStoreGet(args string, reply *APIKeyValueStoreReply) error { 366 v, err := api.api.KeyValueStore().Get(args) 367 *reply = APIKeyValueStoreReply{ 368 Value: v, 369 Error: err, 370 } 371 return nil 372 } 373 374 func (api *LocalAPI) KeyValueStoreDelete(args string, reply *APIErrorReply) error { 375 err := api.api.KeyValueStore().Delete(args) 376 *reply = APIErrorReply{ 377 Error: err, 378 } 379 return nil 380 } 381 382 func ServeAPI(api plugin.API, conn io.ReadWriteCloser, muxer *Muxer) { 383 server := rpc.NewServer() 384 server.Register(&LocalAPI{ 385 api: api, 386 muxer: muxer, 387 }) 388 server.ServeConn(conn) 389 } 390 391 type RemoteAPI struct { 392 client *rpc.Client 393 muxer *Muxer 394 keyValueStore *RemoteKeyValueStore 395 } 396 397 type RemoteKeyValueStore struct { 398 api *RemoteAPI 399 } 400 401 var _ plugin.API = (*RemoteAPI)(nil) 402 var _ plugin.KeyValueStore = (*RemoteKeyValueStore)(nil) 403 404 func (api *RemoteAPI) LoadPluginConfiguration(dest interface{}) error { 405 var config []byte 406 if err := api.client.Call("LocalAPI.LoadPluginConfiguration", struct{}{}, &config); err != nil { 407 return err 408 } 409 return json.Unmarshal(config, dest) 410 } 411 412 func (api *RemoteAPI) RegisterCommand(command *model.Command) error { 413 return api.client.Call("LocalAPI.RegisterCommand", command, nil) 414 } 415 416 type APIUnregisterCommandArgs struct { 417 TeamId string 418 Trigger string 419 } 420 421 func (api *RemoteAPI) UnregisterCommand(teamId, trigger string) error { 422 return api.client.Call("LocalAPI.UnregisterCommand", &APIUnregisterCommandArgs{ 423 TeamId: teamId, 424 Trigger: trigger, 425 }, nil) 426 } 427 428 func (api *RemoteAPI) CreateUser(user *model.User) (*model.User, *model.AppError) { 429 var reply APIUserReply 430 if err := api.client.Call("LocalAPI.CreateUser", user, &reply); err != nil { 431 return nil, model.NewAppError("RemoteAPI.CreateUser", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 432 } 433 return reply.User, reply.Error 434 } 435 436 func (api *RemoteAPI) DeleteUser(userId string) *model.AppError { 437 var reply APIErrorReply 438 if err := api.client.Call("LocalAPI.DeleteUser", userId, &reply); err != nil { 439 return model.NewAppError("RemoteAPI.DeleteUser", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 440 } 441 return reply.Error 442 } 443 444 func (api *RemoteAPI) GetUser(userId string) (*model.User, *model.AppError) { 445 var reply APIUserReply 446 if err := api.client.Call("LocalAPI.GetUser", userId, &reply); err != nil { 447 return nil, model.NewAppError("RemoteAPI.GetUser", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 448 } 449 return reply.User, reply.Error 450 } 451 452 func (api *RemoteAPI) GetUserByEmail(email string) (*model.User, *model.AppError) { 453 var reply APIUserReply 454 if err := api.client.Call("LocalAPI.GetUserByEmail", email, &reply); err != nil { 455 return nil, model.NewAppError("RemoteAPI.GetUserByEmail", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 456 } 457 return reply.User, reply.Error 458 } 459 460 func (api *RemoteAPI) GetUserByUsername(name string) (*model.User, *model.AppError) { 461 var reply APIUserReply 462 if err := api.client.Call("LocalAPI.GetUserByUsername", name, &reply); err != nil { 463 return nil, model.NewAppError("RemoteAPI.GetUserByUsername", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 464 } 465 return reply.User, reply.Error 466 } 467 468 func (api *RemoteAPI) UpdateUser(user *model.User) (*model.User, *model.AppError) { 469 var reply APIUserReply 470 if err := api.client.Call("LocalAPI.UpdateUser", user, &reply); err != nil { 471 return nil, model.NewAppError("RemoteAPI.UpdateUser", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 472 } 473 return reply.User, reply.Error 474 } 475 476 func (api *RemoteAPI) CreateTeam(team *model.Team) (*model.Team, *model.AppError) { 477 var reply APITeamReply 478 if err := api.client.Call("LocalAPI.CreateTeam", team, &reply); err != nil { 479 return nil, model.NewAppError("RemoteAPI.CreateTeam", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 480 } 481 return reply.Team, reply.Error 482 } 483 484 func (api *RemoteAPI) DeleteTeam(teamId string) *model.AppError { 485 var reply APIErrorReply 486 if err := api.client.Call("LocalAPI.DeleteTeam", teamId, &reply); err != nil { 487 return model.NewAppError("RemoteAPI.DeleteTeam", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 488 } 489 return reply.Error 490 } 491 492 func (api *RemoteAPI) GetTeam(teamId string) (*model.Team, *model.AppError) { 493 var reply APITeamReply 494 if err := api.client.Call("LocalAPI.GetTeam", teamId, &reply); err != nil { 495 return nil, model.NewAppError("RemoteAPI.GetTeam", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 496 } 497 return reply.Team, reply.Error 498 } 499 500 func (api *RemoteAPI) GetTeamByName(name string) (*model.Team, *model.AppError) { 501 var reply APITeamReply 502 if err := api.client.Call("LocalAPI.GetTeamByName", name, &reply); err != nil { 503 return nil, model.NewAppError("RemoteAPI.GetTeamByName", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 504 } 505 return reply.Team, reply.Error 506 } 507 508 func (api *RemoteAPI) UpdateTeam(team *model.Team) (*model.Team, *model.AppError) { 509 var reply APITeamReply 510 if err := api.client.Call("LocalAPI.UpdateTeam", team, &reply); err != nil { 511 return nil, model.NewAppError("RemoteAPI.UpdateTeam", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 512 } 513 return reply.Team, reply.Error 514 } 515 516 func (api *RemoteAPI) CreateChannel(channel *model.Channel) (*model.Channel, *model.AppError) { 517 var reply APIChannelReply 518 if err := api.client.Call("LocalAPI.CreateChannel", channel, &reply); err != nil { 519 return nil, model.NewAppError("RemoteAPI.CreateChannel", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 520 } 521 return reply.Channel, reply.Error 522 } 523 524 func (api *RemoteAPI) DeleteChannel(channelId string) *model.AppError { 525 var reply APIErrorReply 526 if err := api.client.Call("LocalAPI.DeleteChannel", channelId, &reply); err != nil { 527 return model.NewAppError("RemoteAPI.DeleteChannel", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 528 } 529 return reply.Error 530 } 531 532 func (api *RemoteAPI) GetChannel(channelId string) (*model.Channel, *model.AppError) { 533 var reply APIChannelReply 534 if err := api.client.Call("LocalAPI.GetChannel", channelId, &reply); err != nil { 535 return nil, model.NewAppError("RemoteAPI.GetChannel", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 536 } 537 return reply.Channel, reply.Error 538 } 539 540 func (api *RemoteAPI) GetChannelByName(name, teamId string) (*model.Channel, *model.AppError) { 541 var reply APIChannelReply 542 if err := api.client.Call("LocalAPI.GetChannelByName", &APIGetChannelByNameArgs{ 543 Name: name, 544 TeamId: teamId, 545 }, &reply); err != nil { 546 return nil, model.NewAppError("RemoteAPI.GetChannelByName", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 547 } 548 return reply.Channel, reply.Error 549 } 550 551 func (api *RemoteAPI) GetDirectChannel(userId1, userId2 string) (*model.Channel, *model.AppError) { 552 var reply APIChannelReply 553 if err := api.client.Call("LocalAPI.GetDirectChannel", &APIGetDirectChannelArgs{ 554 UserId1: userId1, 555 UserId2: userId2, 556 }, &reply); err != nil { 557 return nil, model.NewAppError("RemoteAPI.GetDirectChannel", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 558 } 559 return reply.Channel, reply.Error 560 } 561 562 func (api *RemoteAPI) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError) { 563 var reply APIChannelReply 564 if err := api.client.Call("LocalAPI.GetGroupChannel", &APIGetGroupChannelArgs{ 565 UserIds: userIds, 566 }, &reply); err != nil { 567 return nil, model.NewAppError("RemoteAPI.GetGroupChannel", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 568 } 569 return reply.Channel, reply.Error 570 } 571 572 func (api *RemoteAPI) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) { 573 var reply APIChannelReply 574 if err := api.client.Call("LocalAPI.UpdateChannel", channel, &reply); err != nil { 575 return nil, model.NewAppError("RemoteAPI.UpdateChannel", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 576 } 577 return reply.Channel, reply.Error 578 } 579 580 func (api *RemoteAPI) AddChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) { 581 var reply APIChannelMemberReply 582 if err := api.client.Call("LocalAPI.AddChannelMember", &APIAddChannelMemberArgs{ 583 ChannelId: channelId, 584 UserId: userId, 585 }, &reply); err != nil { 586 return nil, model.NewAppError("RemoteAPI.AddChannelMember", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 587 } 588 return reply.ChannelMember, reply.Error 589 } 590 591 func (api *RemoteAPI) GetChannelMember(channelId, userId string) (*model.ChannelMember, *model.AppError) { 592 var reply APIChannelMemberReply 593 if err := api.client.Call("LocalAPI.GetChannelMember", &APIGetChannelMemberArgs{ 594 ChannelId: channelId, 595 UserId: userId, 596 }, &reply); err != nil { 597 return nil, model.NewAppError("RemoteAPI.GetChannelMember", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 598 } 599 return reply.ChannelMember, reply.Error 600 } 601 602 func (api *RemoteAPI) UpdateChannelMemberRoles(channelId, userId, newRoles string) (*model.ChannelMember, *model.AppError) { 603 var reply APIChannelMemberReply 604 if err := api.client.Call("LocalAPI.UpdateChannelMemberRoles", &APIUpdateChannelMemberRolesArgs{ 605 ChannelId: channelId, 606 UserId: userId, 607 NewRoles: newRoles, 608 }, &reply); err != nil { 609 return nil, model.NewAppError("RemoteAPI.UpdateChannelMemberRoles", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 610 } 611 return reply.ChannelMember, reply.Error 612 } 613 614 func (api *RemoteAPI) UpdateChannelMemberNotifications(channelId, userId string, notifications map[string]string) (*model.ChannelMember, *model.AppError) { 615 var reply APIChannelMemberReply 616 if err := api.client.Call("LocalAPI.UpdateChannelMemberNotifications", &APIUpdateChannelMemberNotificationsArgs{ 617 ChannelId: channelId, 618 UserId: userId, 619 Notifications: notifications, 620 }, &reply); err != nil { 621 return nil, model.NewAppError("RemoteAPI.UpdateChannelMemberNotifications", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 622 } 623 return reply.ChannelMember, reply.Error 624 } 625 626 func (api *RemoteAPI) DeleteChannelMember(channelId, userId string) *model.AppError { 627 var reply APIErrorReply 628 if err := api.client.Call("LocalAPI.DeleteChannelMember", &APIDeleteChannelMemberArgs{ 629 ChannelId: channelId, 630 UserId: userId, 631 }, &reply); err != nil { 632 return model.NewAppError("RemoteAPI.DeleteChannelMember", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 633 } 634 return reply.Error 635 } 636 637 func (api *RemoteAPI) CreatePost(post *model.Post) (*model.Post, *model.AppError) { 638 var reply APIPostReply 639 if err := api.client.Call("LocalAPI.CreatePost", post, &reply); err != nil { 640 return nil, model.NewAppError("RemoteAPI.CreatePost", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 641 } 642 return reply.Post, reply.Error 643 } 644 645 func (api *RemoteAPI) DeletePost(postId string) *model.AppError { 646 var reply APIErrorReply 647 if err := api.client.Call("LocalAPI.DeletePost", postId, &reply); err != nil { 648 return model.NewAppError("RemoteAPI.DeletePost", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 649 } 650 return reply.Error 651 } 652 653 func (api *RemoteAPI) GetPost(postId string) (*model.Post, *model.AppError) { 654 var reply APIPostReply 655 if err := api.client.Call("LocalAPI.GetPost", postId, &reply); err != nil { 656 return nil, model.NewAppError("RemoteAPI.GetPost", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 657 } 658 return reply.Post, reply.Error 659 } 660 661 func (api *RemoteAPI) UpdatePost(post *model.Post) (*model.Post, *model.AppError) { 662 var reply APIPostReply 663 if err := api.client.Call("LocalAPI.UpdatePost", post, &reply); err != nil { 664 return nil, model.NewAppError("RemoteAPI.UpdatePost", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 665 } 666 return reply.Post, reply.Error 667 } 668 669 func (api *RemoteAPI) KeyValueStore() plugin.KeyValueStore { 670 return api.keyValueStore 671 } 672 673 func (s *RemoteKeyValueStore) Set(key string, value []byte) *model.AppError { 674 var reply APIErrorReply 675 if err := s.api.client.Call("LocalAPI.KeyValueStoreSet", &APIKeyValueStoreSetArgs{Key: key, Value: value}, &reply); err != nil { 676 return model.NewAppError("RemoteAPI.KeyValueStoreSet", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 677 } 678 return reply.Error 679 } 680 681 func (s *RemoteKeyValueStore) Get(key string) ([]byte, *model.AppError) { 682 var reply APIKeyValueStoreReply 683 if err := s.api.client.Call("LocalAPI.KeyValueStoreGet", key, &reply); err != nil { 684 return nil, model.NewAppError("RemoteAPI.KeyValueStoreGet", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 685 } 686 return reply.Value, reply.Error 687 } 688 689 func (s *RemoteKeyValueStore) Delete(key string) *model.AppError { 690 var reply APIErrorReply 691 if err := s.api.client.Call("LocalAPI.KeyValueStoreDelete", key, &reply); err != nil { 692 return model.NewAppError("RemoteAPI.KeyValueStoreDelete", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError) 693 } 694 return reply.Error 695 } 696 697 func (h *RemoteAPI) Close() error { 698 return h.client.Close() 699 } 700 701 func ConnectAPI(conn io.ReadWriteCloser, muxer *Muxer) *RemoteAPI { 702 remoteKeyValueStore := &RemoteKeyValueStore{} 703 remoteApi := &RemoteAPI{ 704 client: rpc.NewClient(conn), 705 muxer: muxer, 706 keyValueStore: remoteKeyValueStore, 707 } 708 709 remoteKeyValueStore.api = remoteApi 710 711 return remoteApi 712 } 713 714 func init() { 715 gob.Register([]*model.SlackAttachment{}) 716 gob.Register([]interface{}{}) 717 gob.Register(map[string]interface{}{}) 718 }