-
Notifications
You must be signed in to change notification settings - Fork 1
/
phone.acceptCall_handler.go
executable file
·99 lines (90 loc) · 4.21 KB
/
phone.acceptCall_handler.go
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
/*
* Copyright (c) 2018, https://github.com/airwide-code/airwide.datacenter
* All rights reserved.
*
*
*
*/
package rpc
import (
"github.com/golang/glog"
"github.com/airwide-code/airwide.datacenter/baselib/logger"
"github.com/airwide-code/airwide.datacenter/baselib/grpc_util"
"github.com/airwide-code/airwide.datacenter/mtproto"
"golang.org/x/net/context"
"time"
"github.com/airwide-code/airwide.datacenter/biz/core/user"
update2 "github.com/airwide-code/airwide.datacenter/biz/core/update"
"github.com/airwide-code/airwide.datacenter/biz/core/phone_call"
"fmt"
"github.com/airwide-code/airwide.datacenter/biz_server/sync_client"
)
// https://core.telegram.org/api/end-to-end/voice-calls
//
// B accepts the call on one of their devices,
// stores the received value of g_a_hash for this instance of the voice call creation protocol,
// chooses a random value of b, 1 < b < p-1, computes g_b:=power(g,b) mod p,
// performs all the required security checks, and invokes the phone.acceptCall method,
// which has a g_b:bytes field (among others), to be filled with the value of g_b itself (not its hash).
//
// The Server S sends an updatePhoneCall with the phoneCallDiscarded constructor to all other devices B has authorized,
// to prevent accepting the same call on any of the other devices. From this point on,
// the server S works only with that of B's devices which has invoked phone.acceptCall first.
//
// phone.acceptCall#3bd2b4a0 peer:InputPhoneCall g_b:bytes protocol:PhoneCallProtocol = phone.PhoneCall;
func (s *PhoneServiceImpl) PhoneAcceptCall(ctx context.Context, request *mtproto.TLPhoneAcceptCall) (*mtproto.Phone_PhoneCall, error) {
md := grpc_util.RpcMetadataFromIncoming(ctx)
glog.Infof("phone.acceptCall#3bd2b4a0 - metadata: %s, request: %s", logger.JsonDebugData(md), logger.JsonDebugData(request))
//// TODO(@benqi): check peer
peer := request.GetPeer().To_InputPhoneCall()
callSession, err := phone_call.MakePhoneCallLogcByLoad(peer.GetId())
if err != nil {
glog.Errorf("invalid peer: {%v}, err: %v", peer, err)
return nil, err
}
if peer.GetAccessHash() != callSession.ParticipantAccessHash {
err = fmt.Errorf("invalid peer: {%v}", peer)
glog.Errorf("invalid peer: {%v}", peer)
return nil, err
}
// cache g_b
callSession.SetGB(request.GetGB())
/////////////////////////////////////////////////////////////////////////////////
updatesData := update2.NewUpdatesLogic(callSession.ParticipantId)
// 1. add updateUserStatus
//var status *mtproto.UserStatus
statusOnline := &mtproto.TLUserStatusOnline{Data2: &mtproto.UserStatus_Data{
Expires: int32(time.Now().Unix() + 5*30),
}}
// status = statusOnline.To_UserStatus()
updateUserStatus := &mtproto.TLUpdateUserStatus{Data2: &mtproto.Update_Data{
UserId: callSession.ParticipantId,
Status: statusOnline.To_UserStatus(),
}}
updatesData.AddUpdate(updateUserStatus.To_Update())
// 2. add phoneCallRequested
phoneCallAccepted := mtproto.NewTLPhoneCallAccepted()
phoneCallAccepted.SetId(callSession.Id)
phoneCallAccepted.SetAccessHash(callSession.ParticipantAccessHash)
phoneCallAccepted.SetDate(int32(callSession.Date))
phoneCallAccepted.SetAdminId(callSession.AdminId)
phoneCallAccepted.SetParticipantId(callSession.ParticipantId)
phoneCallAccepted.SetGB(callSession.GB)
phoneCallAccepted.SetProtocol(callSession.ToPhoneCallProtocol())
updatePhoneCall := &mtproto.TLUpdatePhoneCall{Data2: &mtproto.Update_Data{
//PhoneCall: callSession.ToPhoneCallRequested().To_PhoneCall(),
PhoneCall: phoneCallAccepted.To_PhoneCall(),
}}
updatesData.AddUpdate(updatePhoneCall.To_Update())
// 3. add users
updatesData.AddUsers(user.GetUsersBySelfAndIDList(callSession.AdminId, []int32{md.UserId, callSession.AdminId}))
sync_client.GetSyncClient().PushToUserUpdatesData(callSession.AdminId, updatesData.ToUpdates())
/////////////////////////////////////////////////////////////////////////////////
// 2. reply
phoneCall := &mtproto.TLPhonePhoneCall{Data2: &mtproto.Phone_PhoneCall_Data{
PhoneCall: callSession.ToPhoneCallWaiting(md.UserId, 0).To_PhoneCall(),
Users: user.GetUsersBySelfAndIDList(md.UserId, []int32{md.UserId, callSession.AdminId}),
}}
glog.Infof("phone.acceptCall#3bd2b4a0 - reply: {%v}", phoneCall)
return phoneCall.To_Phone_PhoneCall(), nil
}